Reusable Accessible Mapping Platform

API Docs for: 3.0.0
Show:

File: src\js\RAMP\Utils\checkbox.js

  1. /* global define */
  2.  
  3. /**
  4. * @module Utils
  5. */
  6.  
  7. /**
  8. * Wraps the specified checkbox input nodes to provide an alternative rendering of checkbox without compromising
  9. * its functionality. Handles synchronization of the checkbox's state with its new rendering.
  10. * Also adds highlight/unhighlight on focus/unfocus, update label when checked/unchecked
  11. *
  12. *
  13. * @class Checkbox
  14. * @constructor
  15. * @uses dojo/Evented
  16. * @uses dojo/_base/declare
  17. * @uses dojo/lang
  18. *
  19. * @param {jObject} node a jQuery object representing the input checkbox node to be wrapped
  20. * @param {Object} [options] Additional options
  21. * @param {String} [options.nodeIdAttr] Name of the "data-*" attribute set on the checkbox node to be treated as the checkbox id. If no appropriate "data-*" attribute found,
  22. * `nodeIdAttr` is used directly, failing that, regular `id` is used.
  23. * @param {Object} [options.cssClass] `active`, `focus`, and `check` CSS class to be applied to the Checkbox correspondingly.
  24. * @param {Object} [options.cssClass.active] CSS class to be set when the Checkbox is `active`.
  25. * @param {Object} [options.cssClass.focus] CSS class to be set when the Checkbox is `focused`.
  26. * @param {Object} [options.cssClass.check] CSS class to be set when the Checkbox is `checked`.
  27. * @param {Object} [options.label] `check` and `uncheck` label texts to be applied to the Checkbox labels.
  28. * @param {Object} [options.label.check] A text to be set as a label when the Checkbox is `checked`
  29. * @param {Object} [options.label.uncheck] A text to be set as a label when the Checkbox is `unchecked`
  30. * @param {Function} [options.onChnage] A function to be called when the state of the Checkbox changes.
  31. *
  32. * @return {Checkbox} A control objects allowing to toggle checkbox.
  33. */
  34.  
  35. define(["dojo/Evented", "dojo/_base/declare", "dojo/_base/lang"],
  36. function (Evented, declare, lang) {
  37. "use strict";
  38.  
  39. return declare([Evented], {
  40. constructor: function (node, options) {
  41. var that = this;
  42.  
  43. // declare individual properties inside the constructor: http://dojotoolkit.org/reference-guide/1.9/dojo/_base/declare.html#id6
  44. lang.mixin(this,
  45. {
  46. /**
  47. * Node of the input checkbox originally supplied to the Checkbox.
  48. *
  49. * @property node
  50. * @type JObject
  51. * @default null
  52. */
  53. node: null,
  54.  
  55. /**
  56. * Node of the input checkbox label.
  57. *
  58. * @property labelNode
  59. * @type JObject
  60. * @default null
  61. * @private
  62. */
  63. labelNode: null,
  64.  
  65. /**
  66. * Name of the "data-*" attribute set on the checkbox node to be treated as the checkbox id.
  67. *
  68. * @property nodeIdAttr
  69. * @type String
  70. * @default "id"
  71. */
  72. nodeIdAttr: "id",
  73.  
  74. /**
  75. * `active`, `focus`, and `check` CSS class to be applied to the Checkbox correspondingly.
  76. *
  77. * @property cssClass
  78. * @type {Object}
  79. * @default
  80. * @example
  81. * cssClass: {
  82. * active: "active",
  83. * focus: "focused",
  84. * check: "checked"
  85. * }
  86. */
  87. cssClass: {
  88. active: "active",
  89. focus: "focused",
  90. check: "checked"
  91. },
  92.  
  93. /**
  94. * `check` and `uncheck` label texts to be applied to the Checkbox labels.
  95. *
  96. * @property label
  97. * @type {Object}
  98. * @default
  99. * @example
  100. * label: {
  101. * check: "check",
  102. * uncheck: "unchecked"
  103. * }
  104. */
  105. label: {
  106. check: "checked",
  107. uncheck: "unchecked"
  108. },
  109.  
  110. /**
  111. * A function to be called when the state of the Checkbox changes.
  112. *
  113. * @property onChnage
  114. * @type Function
  115. * @default
  116. * @example function () { }
  117. */
  118. onChange: function () { },
  119.  
  120. /**
  121. * State of the Checkbox: true | false
  122. *
  123. * @property state
  124. * @type Boolean
  125. * @default null
  126. */
  127. state: null,
  128.  
  129. /**
  130. * Id of the Checkbox as specified by `nodeIdAttr`.
  131. *
  132. * @property id
  133. * @type String
  134. * @default null
  135. */
  136. id: null,
  137.  
  138. /**
  139. * An object specifying possible agencies that can affect the Checkbox.
  140. *
  141. * @property agency
  142. * @type Object
  143. * @private
  144. * @default
  145. * @example
  146. * agency: {
  147. * USER: "USER",
  148. * CODE: "CODE"
  149. * }
  150. */
  151. agency: {
  152. USER: "USER",
  153. CODE: "CODE"
  154. },
  155.  
  156. /**
  157. * Event names published by the Checkbox
  158. *
  159. * @private
  160. * @property event
  161. * @type Object
  162. * @default null
  163. * @example
  164. * {
  165. * TOGGLE: "checkbox/toggle"
  166. * }
  167. */
  168. event: {
  169. /**
  170. * Published whenever a Checkbox get toggled.
  171. *
  172. * @event TOGGLE
  173. * @param event {Object}
  174. * @param event.checkbox {Checkbox} Checkbox object that has been toggled
  175. * @param event.agency {String} Agency that toggled the Checkbox
  176. */
  177. TOGGLE: "checkbox/toggle"
  178. }
  179. },
  180. options,
  181. {
  182. node: node
  183. }
  184. );
  185.  
  186. this.node
  187. .on("change", function () {
  188. that._toggleLabel();
  189.  
  190. that._emit(that.agency.USER);
  191. })
  192. .on("focus", function () {
  193. that.node.findInputLabel().addClass(that.cssClass.focus);
  194. })
  195. .on("focusout", function () {
  196. that.node.findInputLabel().removeClass(that.cssClass.focus);
  197. });
  198.  
  199. this.id = this.node.data(this.nodeIdAttr) || this.node.attr(this.nodeIdAttr) || this.node.id;
  200. this.labelNode = this.node.findInputLabel();
  201.  
  202. this._toggleLabel();
  203. },
  204.  
  205. /*
  206. * Adds the "checked", "focused" or "active" CSS class to the label so it displays visually matches the changed state.
  207. * Updates the title attribute and text inside an invisible span housed inside the label.
  208. *
  209. * @method _toggleLabel
  210. * @private
  211. */
  212. _toggleLabel: function () {
  213. var newText;
  214.  
  215. this.state = this.node.is(':checked');
  216.  
  217. if (this.state) {
  218. newText = String.format(this.label.check,
  219. this.labelNode.data("label-name"));
  220.  
  221. this.labelNode
  222. .addClass(this.cssClass.check)
  223. .prop('title', newText)
  224. .find("> span").text(newText);
  225. } else {
  226. newText = String.format(this.label.uncheck,
  227. this.labelNode.data("label-name"));
  228.  
  229. this.labelNode
  230. .removeClass(this.cssClass.check)
  231. .prop('title', newText)
  232. .find("> span").text(newText);
  233. }
  234.  
  235. this.onChange.call(this);
  236. },
  237.  
  238. /**
  239. * Emits a `TOGGLE` event when the checkbox's state is changed.
  240. *
  241. * @method _emit
  242. * @private
  243. * @param {String} agency Specified the agency that toggled the Checkbox.
  244. *
  245. */
  246. _emit: function (agency) {
  247. //console.log("Checkbox ->", this.id, "set by", agency, "to", this.state);
  248.  
  249. this.emit(this.event.TOGGLE, {
  250. agency: agency,
  251. checkbox: this
  252. });
  253. },
  254.  
  255. /**
  256. * Toggle the state of Checkbox.
  257. *
  258. * @method setState
  259. * @param {Boolean} state Specifies the state of the checkbox: true, false
  260. * @return {Checkbox} Control object for chaining
  261. * @chainable
  262. */
  263. setState: function (state) {
  264. // change state only if it's different from the current one
  265. if (this.state !== state) {
  266. this.node.prop('checked', state);
  267. this._toggleLabel();
  268.  
  269. this._emit(this.agency.CODE);
  270. }
  271.  
  272. return this;
  273. }
  274. });
  275. });