/* global define */ (function(define){'use strict';define(['require','exports','module','gaia-icons','gaia-component'],function(require,exports,module){ /** * Dependencies */ require('gaia-icons'); var component = require('gaia-component'); /** * Exports */ module.exports = component.register('gaia-checkbox', { extends: HTMLInputElement.prototype, created: function() { this.setupShadowRoot(); this.els = { inner: this.shadowRoot.querySelector('.inner') }; this.addEventListener('click', this.onClick.bind(this)); // Setup initial attributes this.checked = this.getAttribute('checked'); this.danger = this.getAttribute('danger'); this.name = this.getAttribute('name'); // process everything that doesn't affect user interaction // after the component is created setTimeout(() => this.makeAccessible()); }, makeAccessible: function() { this.setAttribute('role', 'checkbox'); // Make tabable this.tabIndex = 0; this.setAttr('aria-checked', this.checked); }, onClick: function(e) { e.stopPropagation(); this.checked = !this.checked; }, toggle: function(value) { value = arguments.length ? value : !this.checked; if (value || value === '') { this.check(); } else { this.uncheck(); } }, check: function() { if (this.checked) { return; } this.setAttr('checked', ''); this.setAttribute('aria-checked', true); this._checked = true; }, uncheck: function() { if (!this.checked) { return; } this.removeAttr('checked'); this.setAttribute('aria-checked', false); this._checked = false; }, attrs: { checked: { get: function() { return !!this._checked; }, set: function() { this.toggle(); } }, danger: { get: function() { return this._danger; }, set: function(value) { if (value || value === '') { this.setAttr('danger', value); } else { this.removeAttr('danger'); } this._danger = value; } }, name: { get: function() { return this._name; }, set: function(value) { if (value === null) { this.removeAttr('name'); } else { this.setAttr('name', value); } this._name = value; } } }, template: `
`, globalCss: `@keyframes gaia-checkbox-animation { 0% { transform: scale(0); opacity: 1; } 65% { transform: scale(1); opacity: 1; } 100% { opacity: 0; transform: scale(1); } }` }); // Makes checkboxs togglable via keboard addEventListener('keypress', function(e) { if (e.which !== 32) { return; } var el = document.activeElement; var isCheckbox = el.tagName === 'GAIA-CHECKBOX'; if (isCheckbox) { el.click(); } }); // Bind a 'click' delegate to the // window to listen for all clicks // and toggle checkboxes when required. addEventListener('click', function(e) { var label = getLabel(e.target); var gaiaCheckbox = getLinkedCheckbox(label); if (gaiaCheckbox) { gaiaCheckbox.toggle(); } }, true); /** * Find a checkbox when given a