(function(define){'use strict';define(function(require,exports,module){ /*globals define*//*jshint node:true*/ /** * Dependencies */ var loadGaiaIcons = require('gaia-icons'); var fontFit = require('./lib/font-fit'); /** * Locals */ var baseComponents = window.COMPONENTS_BASE_URL || 'bower_components/'; var base = window.GAIA_HEADER_BASE_URL || baseComponents + 'gaia-header/'; /** * Element prototype, extends from HTMLElement * * @type {Object} */ var proto = Object.create(HTMLElement.prototype); /** * Supported action types * * @type {Object} */ var actionTypes = { menu: true, back: true, close: true, }; /** * Called when the element is first created. * * Here we create the shadow-root and * inject our template into it. * * @private */ proto.createdCallback = function() { var shadow = this.createShadowRoot(); var tmpl = template.content.cloneNode(true); // Get els this.els = { actionButton: tmpl.querySelector('.action-button'), headings: this.querySelectorAll('h1,h2,h3,h4'), inner: tmpl.querySelector('.inner') }; this.els.actionButton.addEventListener('click', proto.onActionButtonClick.bind(this)); this.configureActionButton(); this.setupInteractionListeners(); shadow.appendChild(tmpl); this.styleHack(); // Font fit must be run only once the element is styled this.addEventListener('styled', this.runFontFit.bind(this)); }; /** * Load in the the component's styles. * * We're working around a few platform bugs * here related to @import in the shadow-dom * stylesheet. When HTML-Imports are ready * we won't have to use @import anymore. * * The `-content` class is added to the element * as a simple 'polyfill' for `::content` selector. * We can use `.-content` in our CSS to indicate * we're styling 'distributed' nodes. This will * make the transition to `::content` a lot simpler. * * @private */ proto.styleHack = function() { var style = document.createElement('style'); var self = this; this.style.visibility = 'hidden'; style.innerHTML = '@import url(' + base + 'style.css);'; style.setAttribute('scoped', ''); this.classList.add('-content'); this.appendChild(style); // There are platform issues around using // @import inside shadow root. Ensuring the // stylesheet has loaded before putting it in // the shadow root seems to work around this. style.addEventListener('load', function() { self.shadowRoot.appendChild(style.cloneNode(true)); self.style.visibility = ''; self.styled = true; self.dispatchEvent(new CustomEvent('styled')); }); }; proto.runFontFit = function() { for (var i = 0; i < this.els.headings.length; i++) { fontFit.reformatHeading(this.els.headings[i]); fontFit.observeHeadingChanges(this.els.headings[i]); } }; /** * Called when one of the attributes * on the element changes. * * @private */ proto.attributeChangedCallback = function(attr, oldVal, newVal) { if (attr === 'action') { this.configureActionButton(); fontFit.reformatHeading(this._heading); } }; /** * Triggers the 'action' button * (used in testing). * * @public */ proto.triggerAction = function() { if (this.isSupportedAction(this.getAttribute('action'))) { this.els.actionButton.click(); } }; /** * Configure the action button based * on the value of the `data-action` * attribute. * * @private */ proto.configureActionButton = function() { var old = this.els.actionButton.getAttribute('icon'); var type = this.getAttribute('action'); var supported = this.isSupportedAction(type); this.els.actionButton.classList.remove('icon-' + old); this.els.actionButton.setAttribute('icon', type); this.els.inner.classList.toggle('supported-action', supported); if (supported) { this.els.actionButton.classList.add('icon-' + type); } }; /** * Validate action against supported list. * * @private */ proto.isSupportedAction = function(action) { return action && actionTypes[action]; }; /** * Handle clicks on the action button. * * Fired async to allow the 'click' event * to finish its event path before * dispatching the 'action' event. * * @param {Event} e * @private */ proto.onActionButtonClick = function(e) { var config = { detail: { type: this.getAttribute('action') } }; var actionEvent = new CustomEvent('action', config); setTimeout(this.dispatchEvent.bind(this, actionEvent)); }; /** * Adds helper classes to allow us to style * specifically when a touch interaction is * taking place. * * We use this specifically to apply a * transition-delay when the user releases * their finger from a button so that they * can momentarily see the :active state, * reinforcing the UI has responded to * their touch. * * We bind to mouse events to facilitate * desktop usage. * * @private */ proto.setupInteractionListeners = function() { stickyActive(this.els.inner); }; // HACK: Create a