all files / montage/ui/modal-overlay.reel/ modal-overlay.js

97.73% Statements 43/44
80% Branches 16/20
100% Functions 6/6
97.73% Lines 43/44
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                          26×   26×   26× 26× 26×                                                                 19×               19× 17× 10× 10×         17×       17×                     19×                                                
/**
 * @module "ui/modal-overlay.reel"
 */
var Overlay = require("../overlay.reel").Overlay,
    Promise = require("../../core/promise").Promise;
 
var CLASS_PREFIX = "montage-ModalOverlay";
 
/**
 * @class ModalOverlay
 * @extends Overlay
 */
exports.ModalOverlay = Overlay.specialize(/** @lends ModalOverlay.prototype # */ {
 
    enterDocument: {
        value: function (firstTime) {
            var body;
 
            this.super(firstTime);
 
            Eif (firstTime) {
                body = this.element.ownerDocument.body;
                body.appendChild(this.modalMaskElement);
            }
        }
    },
 
    _queue: {
        value: []
    },
 
    _showPromise: {
        value: null
    },
 
    _dismissOnExternalInteraction: {
        value: false
    },
 
    hasModalMask: {
        value: true
    },
 
    _slotComponent: {
        value: null
    },
 
    /**
     * Returns a promise for the show of the overlay. A modal overlay might not
     * be immediately shown if another modal overlay is being shown. When this
     * is the case then only after the previous overlay is hidden will the
     * overlay be shown.
     */
    show: {
        value: function (component) {
            var queue = this._queue,
                ix = queue.indexOf(this),
                promise;
 
            // The overlay is not scheduled to draw so we add it to the queue
            // and return a promise that will be solved when shown.
            // If no overlay is being shown (empty queue) then we show it
            // immediately and return a solved promise.
            if (ix === -1) {
                if (queue.length === 0) {
                    this.super();
                    promise = Promise.resolve();
                } else {
                    promise = this._showPromise = {};
                     this._showPromise.promise = new Promise(function(resolve, reject) {
                         promise.resolve = resolve;
                         promise.reject = reject;
                     });
                    promise = this._showPromise.promise;
                }
 
                Iif (component) {
                    this._slotComponent.content = component;
                }
 
                queue.push(this);
 
                // The overlay is scheduled to draw so we just return the
                // previously created promise. If the overlay is currently
                // being shown (head of the queue) then we add it again to the
                // queue.
            } else {
                if (ix === 0) {
                    promise = this._showPromise = {};
                     this._showPromise.promise = new Promise(function(resolve, reject) {
                         promise.resolve = resolve;
                         promise.reject = reject;
                     });
                    queue.push(this);
                }
                promise = this._showPromise.promise;
            }
 
            return promise;
        }
    },
 
    hide: {
        value: function () {
            var queue = this._queue,
                ix = queue.indexOf(this),
                nextOverlay;
 
            if (ix === 0) {
                queue.shift();
                this.super();
                Eif (queue.length > 0) {
                    nextOverlay = queue[0];
                    nextOverlay._showPromise.resolve();
                    Overlay.prototype.show.call(nextOverlay);
                }
            } else Eif (ix > 0) {
                queue.splice(ix, 1);
                this._showPromise.reject(new Error("Modal Overlay position in the queue is not 0"));
            }
        }
    },
 
    draw: {
        value: function () {
            this.super();
 
            if (this._isShown && this.hasModalMask) {
                this.modalMaskElement.classList.add(CLASS_PREFIX + "-modalMask--visible");
            } else {
                this.modalMaskElement.classList.remove(CLASS_PREFIX + "-modalMask--visible");
            }
        }
    }
 
});