API Docs for: 3.13.0
Show:

File: button/js/button.js

  1. /**
  2. * A Button Widget
  3. *
  4. * @module button
  5. * @since 3.5.0
  6. */
  7.  
  8. var ButtonCore = Y.ButtonCore,
  9. CLASS_NAMES = ButtonCore.CLASS_NAMES,
  10. ARIA_STATES = ButtonCore.ARIA_STATES,
  11. ARIA_ROLES = ButtonCore.ARIA_ROLES;
  12.  
  13. /**
  14. * Creates a Button
  15. *
  16. * @class Button
  17. * @extends Widget
  18. * @uses ButtonCore
  19. * @param config {Object} Configuration object
  20. * @constructor
  21. */
  22. function Button() {
  23. Button.superclass.constructor.apply(this, arguments);
  24. }
  25.  
  26. /* Button extends Widget */
  27. Y.extend(Button, Y.Widget, {
  28.  
  29. // Y.Button prototype properties
  30.  
  31. /**
  32. * Bounding box template that will contain the Button's DOM subtree.
  33. *
  34. * @property BOUNDING_TEMPLATE
  35. * @type {String}
  36. * @default <button/>
  37. */
  38. BOUNDING_TEMPLATE : ButtonCore.prototype.TEMPLATE,
  39.  
  40. /**
  41. * Content box template
  42. *
  43. * @property CONTENT_TEMPLATE
  44. * @type {String}
  45. * @default null
  46. */
  47. CONTENT_TEMPLATE : null
  48.  
  49. }, {
  50.  
  51. // Y.Button static properties
  52.  
  53. /**
  54. * The identity of the widget.
  55. *
  56. * @property NAME
  57. * @type String
  58. * @default 'button'
  59. * @readOnly
  60. * @protected
  61. * @static
  62. */
  63. NAME: ButtonCore.NAME,
  64.  
  65. /**
  66. * Static property used to define the default attribute configuration of
  67. * the Widget.
  68. *
  69. * @property ATTRS
  70. * @type {Object}
  71. * @protected
  72. * @static
  73. */
  74. ATTRS: ButtonCore.ATTRS,
  75.  
  76. /**
  77. * The text of the button's label
  78. *
  79. * @attribute label
  80. * @type String
  81. */
  82.  
  83. /**
  84. * The HTML of the button's label
  85. *
  86. * This attribute accepts HTML and inserts it into the DOM **without**
  87. * sanitization. This attribute should only be used with HTML that has
  88. * either been escaped (using `Y.Escape.html`), or sanitized according to
  89. * the requirements of your application.
  90. *
  91. * If all you need is support for text labels, please use the `label`
  92. * attribute instead.
  93. *
  94. * @attribute labelHTML
  95. * @type HTML
  96. */
  97.  
  98. /**
  99. * @property HTML_PARSER
  100. * @type {Object}
  101. * @protected
  102. * @static
  103. */
  104. HTML_PARSER: {
  105. labelHTML: ButtonCore._getHTMLFromNode,
  106. disabled: ButtonCore._getDisabledFromNode
  107. },
  108.  
  109. /**
  110. * List of class names used in the Button's DOM
  111. *
  112. * @property CLASS_NAMES
  113. * @type Object
  114. * @static
  115. */
  116. CLASS_NAMES: CLASS_NAMES
  117. });
  118.  
  119. Y.mix(Button.prototype, ButtonCore.prototype);
  120.  
  121. /**
  122. * Creates a ToggleButton
  123. *
  124. * @class ToggleButton
  125. * @extends Button
  126. * @param config {Object} Configuration object
  127. * @constructor
  128. */
  129. function ToggleButton() {
  130. Button.superclass.constructor.apply(this, arguments);
  131. }
  132.  
  133. // TODO: move to ButtonCore subclass to enable toggle plugin, widget, etc.
  134. /* ToggleButton extends Button */
  135. Y.extend(ToggleButton, Button, {
  136.  
  137. /**
  138. *
  139. *
  140. * @property trigger
  141. * @type {String}
  142. * @default
  143. */
  144. trigger: 'click',
  145.  
  146. /**
  147. *
  148. *
  149. * @property selectedAttrName
  150. * @type {String}
  151. * @default
  152. */
  153. selectedAttrName: '',
  154.  
  155. /**
  156. *
  157. * @method initializer
  158. */
  159. initializer: function (config) {
  160. var button = this,
  161. type = button.get('type'),
  162. selectedAttrName = (type === "checkbox" ? 'checked' : 'pressed'),
  163. selectedState = config[selectedAttrName] || false;
  164.  
  165. // Create the checked/pressed attribute
  166. button.addAttr(selectedAttrName, {
  167. value: selectedState
  168. });
  169.  
  170. button.selectedAttrName = selectedAttrName;
  171. },
  172.  
  173. /**
  174. *
  175. * @method destructor
  176. */
  177. destructor: function () {
  178. delete this.selectedAttrName;
  179. },
  180.  
  181. /**
  182. * @method bindUI
  183. * @description Hooks up events for the widget
  184. */
  185. bindUI: function() {
  186. var button = this,
  187. cb = button.get('contentBox');
  188.  
  189. ToggleButton.superclass.bindUI.call(button);
  190.  
  191. cb.on(button.trigger, button.toggle, button);
  192. button.after(button.selectedAttrName + 'Change', button._afterSelectedChange);
  193. },
  194.  
  195. /**
  196. * @method syncUI
  197. * @description Syncs the UI for the widget
  198. */
  199. syncUI: function() {
  200. var button = this,
  201. cb = button.get('contentBox'),
  202. type = button.get('type'),
  203. ROLES = ToggleButton.ARIA_ROLES,
  204. role = (type === 'checkbox' ? ROLES.CHECKBOX : ROLES.TOGGLE),
  205. selectedAttrName = button.selectedAttrName;
  206.  
  207. ToggleButton.superclass.syncUI.call(button);
  208.  
  209. cb.set('role', role);
  210. button._uiSetSelected(button.get(selectedAttrName));
  211. },
  212.  
  213. /**
  214. * @method _afterSelectedChange
  215. * @private
  216. */
  217. _afterSelectedChange: function(e){
  218. this._uiSetSelected(e.newVal);
  219. },
  220.  
  221. /**
  222. * @method _uiSetSelected
  223. * @private
  224. */
  225. _uiSetSelected: function(value) {
  226. var button = this,
  227. cb = button.get('contentBox'),
  228. STATES = ToggleButton.ARIA_STATES,
  229. type = button.get('type'),
  230. ariaState = (type === 'checkbox' ? STATES.CHECKED : STATES.PRESSED);
  231.  
  232. cb.toggleClass(Button.CLASS_NAMES.SELECTED, value);
  233. cb.set(ariaState, value);
  234. },
  235.  
  236. /**
  237. * @method toggle
  238. * @description Toggles the selected/pressed/checked state of a ToggleButton
  239. * @public
  240. */
  241. toggle: function() {
  242. var button = this;
  243. button._set(button.selectedAttrName, !button.get(button.selectedAttrName));
  244. }
  245.  
  246. }, {
  247.  
  248. /**
  249. * The identity of the widget.
  250. *
  251. * @property NAME
  252. * @type {String}
  253. * @default 'buttongroup'
  254. * @readOnly
  255. * @protected
  256. * @static
  257. */
  258. NAME: 'toggleButton',
  259.  
  260. /**
  261. * Static property used to define the default attribute configuration of
  262. * the Widget.
  263. *
  264. * @property ATTRS
  265. * @type {Object}
  266. * @protected
  267. * @static
  268. */
  269. ATTRS: {
  270.  
  271. /**
  272. *
  273. *
  274. * @attribute type
  275. * @type String
  276. */
  277. type: {
  278. value: 'toggle',
  279. writeOnce: 'initOnly'
  280. }
  281. },
  282.  
  283. /**
  284. * @property HTML_PARSER
  285. * @type {Object}
  286. * @protected
  287. * @static
  288. */
  289. HTML_PARSER: {
  290. checked: function(node) {
  291. return node.hasClass(CLASS_NAMES.SELECTED);
  292. },
  293. pressed: function(node) {
  294. return node.hasClass(CLASS_NAMES.SELECTED);
  295. }
  296. },
  297.  
  298. /**
  299. * @property ARIA_STATES
  300. * @type {Object}
  301. * @protected
  302. * @static
  303. */
  304. ARIA_STATES: ARIA_STATES,
  305.  
  306. /**
  307. * @property ARIA_ROLES
  308. * @type {Object}
  309. * @protected
  310. * @static
  311. */
  312. ARIA_ROLES: ARIA_ROLES,
  313.  
  314. /**
  315. * Array of static constants used to identify the classnames applied to DOM nodes
  316. *
  317. * @property CLASS_NAMES
  318. * @type Object
  319. * @static
  320. */
  321. CLASS_NAMES: CLASS_NAMES
  322.  
  323. });
  324.  
  325. // Export
  326. Y.Button = Button;
  327. Y.ToggleButton = ToggleButton;
  328.