all files / montage/ui/base/ abstract-alert.js

100% Statements 45/45
81.25% Branches 13/16
100% Functions 11/11
100% Lines 45/45
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                        25×                                                                         11×   11×       11× 11× 11×   11×                                 28× 26× 26× 26× 26× 26× 26×       28×                                                                   23×     23× 17× 17× 17× 17×       23×           17×   17×     17× 17× 17×   17×           12×     12× 10×     12×   12×                
/**
 * @module ui/alert.reel
 */
var Component = require("../component").Component,
    ModalOverlay = require("../modal-overlay.reel").ModalOverlay,
    Promise = require("../../core/promise").Promise;
 
/**
 * @class Alert
 * @extends Component
 */
exports.AbstractAlert = Component.specialize(/** @lends AbstractAlert# */ {
    constructor: {
        value: function AbstractAlert() {
            if (this.constructor === AbstractAlert) {
                throw new Error("AbstractAlert cannot be instantiated.");
            }
        }
    },
 
    /**
     * This property should point to the overlay component.
     */
    _overlay: {
        value: null
    },
 
    /**
     * This property should point to the "OK" "action" component.
     */
    _okButton: {
        value: null
    },
 
    _userActionPromise: {
        value: null
    },
 
    title: {
        value: "Alert"
    },
 
    okLabel: {
        value: "OK"
    },
 
    message: {
        value: null
    },
 
    enterDocument: {
        value: function (firstTime) {
            var constructor;
 
            Eif (firstTime) {
                // If the instance entering the document is the singleton of
                // its constructor then we need to resolve the promise to start
                // the chain of messages that might be pending.
                constructor = Object.getPrototypeOf(this).constructor;
                Eif (this === constructor._instance) {
                    constructor._nextMessagePromiseHandlerResolve();
                }
                this._okButton.addEventListener("action", this, false);
            }
        }
    },
    
    resolveUserAction: {
        value: null
    },
    rejectUserAction: {
        value: null
    },
 
    /**
     * Returns a promise for the close of the alert
     */
    show: {
        value: function() {
            if (!this._userActionPromise) {
                this._overlay.hasModalMask = false;
                this._overlay.show();
                var self = this;
                this._userActionPromise = new Promise(function(resolve, reject) {
                    self.resolveUserAction = resolve;
                    self.rejectUserAction = reject;
                });
            }
 
            return this._userActionPromise;
        }
    },
 
    handleAction: {
        value: function (event) {
            Eif (event.target === this._okButton) {
                this.resolveUserAction();
                this._userActionPromise = null;
                this._overlay.hide();
            }
        }
    }
}, {
    _instance: {
        value: null
    },
 
    /**
     * This promise is initially the promise at _nextMessagePromise and is
     * resolved at enterDocument time, this is when the overlay is available.
     
     * Implemented with a getter to ensure that this property is not initialized
     * on the AbstractAlert but on the type that extends it.
     
     */
     _nextMessagePromiseHandlerResolve: {
         value: null
     },
     _nextMessagePromiseHandlerReject: {
         value: null
     },
    __nextMessagePromise: {
        value: null
    },
 
    _nextMessagePromise: {
        set: function (value) {
            this.__nextMessagePromise = value;
        },
        get: function () {
            if (!this.hasOwnProperty("__nextMessagePromise")) {
                var self = this;
                this.__nextMessagePromise = new Promise(function(resolve, reject) {
                     self._nextMessagePromiseHandlerResolve = resolve;
                     self._nextMessagePromiseHandlerReject = reject;
                 });
            }
 
            return this.__nextMessagePromise;
        }
    },
 
    _setupInstance: {
        value: function () {
            var instance;
 
            instance = this._instance = new this();
            // HACK: outside of the draw cycle, no other way to do it at the
            // moment we need the DrawManager to solve this problem.
            instance.element = instance.rootComponent.element.createElement("div");
            instance.element.ownerDocument.body.appendChild(instance.element);
            instance.attachToParentComponent();
 
            instance.needsDraw = true;
        }
    },
 
    show: {
        value: function (message, title) {
            var instance,
                self = this;
 
            if (!this.hasOwnProperty("_instance")) {
                this._setupInstance();
            }
 
            instance = this._instance;
 
            return this._nextMessagePromise = this._nextMessagePromise.then(function () {
                if (title) {
                    instance.title = title;
                } else {
                    instance.title = self.prototype.title;
                }
                instance.message = message;
 
                return instance.show();
            });
        }
    }
});