1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | 1× 1× 36× 1× 35× 35× 35× 35× 35× 2× 2× 11× 10× 2× 2× 2× 10× 10× 49× 7× 7× 4× 4× 4× 5× 5× 2× 3× 3× 3× 3× 2× 2× 2× 1× 1× 1× 1× 1× 1× 3× 3× 3× 3× 3× 3× 3× 7× 4× 3× 7× 3× 1× 2× 7× 4× 7× 7× | /*global require, exports*/ /** * @module montage/ui/base/abstract-button * @requires montage/ui/base/abstract-control * @requires montage/composer/press-composer */ var AbstractControl = require("./abstract-control").AbstractControl, PressComposer = require("../../composer/press-composer").PressComposer; /** * @class AbstractButton * @extends AbstractControl */ var AbstractButton = exports.AbstractButton = AbstractControl.specialize( /** @lends AbstractButton.prototype # */ { /** * Dispatched when the button is activated through a mouse click, finger * tap, or when focused and the spacebar is pressed. * * @event AbstractButton#action * @property {Map} detail - The detail object as defined in {@link AbstractControl#detail} */ /** * Dispatched when the button is pressed for a period of time, set by * {@link AbstractButton#holdThreshold}. * * @event AbstractButton#longAction * @property {Map detail - The detail object as defined in {@link AbstractControl#detail} */ /** * @constructs */ constructor: { value: function AbstractButton() { if(this.constructor === AbstractButton) { throw new Error("AbstractButton cannot be instantiated."); } this._pressComposer = new PressComposer(); this.addComposer(this._pressComposer); this._pressComposer.defineBinding("longPressThreshold ", {"<-": "holdThreshold", source: this}); //classList management this.defineBinding("classList.has('montage--disabled')", {"<-": "!enabled"}); this.defineBinding("classList.has('montage--active')", {"<-": "active"}); } }, /** * Enables or disables the Button from user input. When this property is * set to `false`, the "montage--disabled" CSS style is applied to the * button's DOM element during the next draw cycle. When set to `true` the * "montage--disabled" CSS class is removed from the element's class * list. * @property {boolean} value */ enabled: { value: true }, /** * @private */ _preventFocus: { value: false }, /** * Specifies whether the button should receive focus or not. * * @property {boolean} * @default false */ preventFocus: { get: function () { return this._preventFocus; }, set: function (value) { this._preventFocus = !!value; this.needsDraw = true; } }, acceptsActiveTarget: { value: function () { return ! this._preventFocus; } }, willBecomeActiveTarget: { value: function (previousActiveTarget) { } }, /** * Stores the node that contains this button's value. Only used for * non-`<input>` elements. * @private */ _labelNode: {value:undefined, enumerable: false}, _label: {value: undefined, enumerable: false}, /** * The displayed text on the button. In an `input` element this is taken from the element's `value` attribute. * On any other element (including `button`) this is the first child node which is a text node. * If one isn't found then it will be created. If the button has a non-null `converter` property, * the converter object's `convert()` method is called on the value before being assigned to the button instance. * * @property {string} * @default undefined */ label: { get: function () { return this._label; }, set: function (value) { if (typeof value !== "undefined" && this.converter) { try { value = this.converter.convert(value); Iif (this.error) { this.error = null; } } catch(e) { // unable to convert - maybe error this.error = e; } } this._label = "" + value; this.needsDraw = true; } }, /** * The amount of time in milliseconds the user must press and hold the * button a `longAction` event is dispatched. The default is 1 second. * @property {number} value * @default 1000 */ holdThreshold: { value: 1000 }, /** * @property {PressComposer} value * @default null * @private */ _pressComposer: { value: null }, /** * @private */ _active: { value: false }, /** * This property is true when the button is being interacted with, either * through mouse click or touch event, otherwise false. * * @property {boolean} * @default false */ active: { get: function () { return this._active; }, set: function (value) { this._active = value; this.needsDraw = true; } }, prepareForActivationEvents: { value: function () { this._pressComposer.addEventListener("pressStart", this, false); this._pressComposer.addEventListener("press", this, false); this._pressComposer.addEventListener("pressCancel", this, false); } }, // Optimisation addEventListener: { value: function (type, listener, useCapture) { AbstractControl.prototype.addEventListener.call(this, type, listener, useCapture); if (type === "longAction") { this._pressComposer.addEventListener("longPress", this, false); } } }, // Handlers /** * Called when the user starts interacting with the component. * * @private */ handlePressStart: { value: function (event) { this.active = true; Iif (event.touch) { // Prevent default on touchmove so that if we are inside a scroller, // it scrolls and not the webpage document.addEventListener("touchmove", this, false); } Eif (!this._preventFocus) { this._element.focus(); } } }, /** * Called when the user has interacted with the button. * * @private */ handlePress: { value: function (event) { this.active = false; this.dispatchActionEvent(); document.removeEventListener("touchmove", this, false); } }, handleKeyup: { value: function (event) { // action event on spacebar if (event.keyCode === 32) { this.active = false; this.dispatchActionEvent(); } } }, handleLongPress: { value: function (event) { // When we fire the "longAction" event we don't want to fire the // "action" event as well. this._pressComposer.cancelPress(); var longActionEvent = document.createEvent("CustomEvent"); longActionEvent.initCustomEvent("longAction", true, true, null); this.dispatchEvent(longActionEvent); } }, /** * Called when all interaction is over. * @private */ handlePressCancel: { value: function (event) { this.active = false; document.removeEventListener("touchmove", this, false); } }, /** * @private */ handleTouchmove: { value: function (event) { event.preventDefault(); } }, /** * If this is an input element then the label is handled differently. * @private */ isInputElement: { value: false }, enterDocument: { value: function (firstDraw) { Eif(firstDraw) { this.isInputElement = (this.originalElement.tagName === "INPUT"); // Only take the value from the element if it hasn't been set // elsewhere (i.e. in the serialization) Eif (this.isInputElement) { Eif (this._label === undefined) { this.label = this.originalElement.value; } } else { if (!this.originalElement.firstChild) { this.originalElement.appendChild(document.createTextNode("")); } this._labelNode = this.originalElement.firstChild; if (this._label === undefined) { this.label = this._labelNode.data; } } //this.classList.add("montage-Button"); this.element.setAttribute("role", "button"); this.element.addEventListener("keyup", this, false); } } }, /** * Draws the label to the DOM. * @function * @private */ _drawLabel: { enumerable: false, value: function (value) { if (this.isInputElement) { this._element.value = value; } else Iif (this._labelNode) { this._labelNode.data = value; } } }, draw: { value: function () { if (this._elementNeedsTabIndex()) { if (this._preventFocus) { this.element.removeAttribute("tabindex"); } else { this.element.setAttribute("tabindex", "-1"); } } if (this.isInputElement) { this.element.disabled = !this.enabled; } this._drawLabel(this.label); } }, _elementNeedsTabIndexRegex: { value: /INPUT|TEXTAREA|A|SELECT|BUTTON|LABEL/ }, _elementNeedsTabIndex: { value: function () { return this.element.tagName.match(this._elementNeedsTabIndexRegex) === null; } } }); |