/* global AppModalDialog, AirplaneMode, SimpleKeyNavigation, SmartModalDialog, SmartInputDialog, focusManager */ 'use strict'; (function(exports) { var _ = navigator.mozL10n.get; var _id = 0; /** * The ModalDialog of the AppWindow. * * Including **alert**, **prompt**, **confirm**, and * **single select** dialogs. * * @class AppModalDialog * @param {AppWindow} app The app window instance * where this dialog should popup. * @extends BaseUI */ exports.AppModalDialog = function AppModalDialog(app) { this.app = app; this.containerElement = app.element; this.smartBubble = document.createElement('smart-bubbles'); this.smartBubble.addEventListener('all-items-bubbled', function() { focusManager.focus(); }); this.events = []; // One to one mapping. this.instanceID = _id++; this._injected = false; this.simpleKeyNavigation = new SimpleKeyNavigation(); app.element.addEventListener('mozbrowsershowmodalprompt', this); return this; }; AppModalDialog.prototype = Object.create(exports.BaseUI.prototype); AppModalDialog.prototype.CLASS_NAME = 'AppModalDialog'; AppModalDialog.prototype.ELEMENT_PREFIX = 'modal-dialog-'; AppModalDialog.prototype.customID = function amd_customID() { if (this.app) { return '[' + this.app.origin + ']'; } else { return ''; } }; AppModalDialog.prototype.handleEvent = function amd_handleEvent(evt) { this.app.debug('handling ' + evt.type); switch (evt.type) { case 'mozbrowsershowmodalprompt': evt.preventDefault(); evt.stopPropagation(); this.events.push(evt); if (!this._injected) { focusManager.addUI(this); this.render(); } this.show(); this._injected = true; break; case 'modal-dialog-will-open': case 'modal-dialog-closed': if (this.isSmartModalDialog) { focusManager.focus(); } break; } }; AppModalDialog.prototype._fetchElements = function amd__fetchElements() { // XXX: Place another container outside of the original element to prevent // smart-dialog being opened twice, we should have this removed when // prompt and value selector moving into smart-modal-dialog after // bug 1168252 landed. this.container = document.getElementById( this.CLASS_NAME + 'Container' + this.instanceID); this.smartModalDialog = new SmartModalDialog(this.container); this.smartInputDialog = new SmartInputDialog(this.container); this.container.addEventListener('modal-dialog-will-open', this); this.container.addEventListener('modal-dialog-closed', this); this.isSmartModalDialog = false; this.isSmartInputDialog = false; this.element = document.getElementById(this.CLASS_NAME + this.instanceID); this.element.setAttribute('tabIndex', -1); this.element.addEventListener('opened', function() { this._makeBubbleAnimation(); }.bind(this)); this.element.addEventListener('closed', function() { focusManager.focus(); }.bind(this)); this.elements = {}; var toCamelCase = function toCamelCase(str) { return str.replace(/\-(.)/g, function replacer(str, p1) { return p1.toUpperCase(); }); }; this.elementClasses = [ 'select-one', 'select-one-cancel', 'select-one-menu', 'select-one-title', 'custom-prompt', 'custom-prompt-message', 'custom-prompt-buttons', 'custom-prompt-checkbox' ]; // Loop and add element with camel style name to Modal Dialog attribute. this.elementClasses.forEach(function createElementRef(name) { this.elements[toCamelCase(name)] = this.element.querySelector('.' + this.ELEMENT_PREFIX + name); }, this); }; AppModalDialog.prototype._registerEvents = function amd__registerEvents() { var elements = this.elements; for (var id in elements) { var tagName = elements[id].tagName.toLowerCase(); if (tagName == 'smart-button' || tagName == 'ul') { if (elements[id].classList.contains('confirm')) { elements[id].addEventListener('click', this.confirmHandler.bind(this)); } else if (elements[id].classList.contains('cancel')) { elements[id].addEventListener('click', this.cancelHandler.bind(this)); } } } // XXX: Since we are trying to keep enter key actioning on keyup; submit // event should be prevented since it works on keydown. We can remove this // workaround after changing containers from