aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/ui/popup/popup.reel/popup.js
blob: 2a61e75e635011dd744342a5a1abae174ac8a084 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/* <copyright>
 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
 No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
 (c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
 </copyright> */

/**
    @module "montage/ui/popup/popup.reel"
    @requires montage/core/core
    @requires montage/ui/component
*/
var Montage = require("montage").Montage,
    Component = require("ui/component").Component;

/**
    @class module:"montage/ui/popup.reel".Popup
    @extends module:montage/ui/component.Component
*/

var Popup = exports.Popup = Montage.create(Component, { /** @lends module:"module/ui/popup/popup.reel".Popup */

    hasTemplate: {value: true},

    // anchor element to which this popup must be anchored to
    anchor: {value: null},

    // A Delegate to control positioning (and other features, in future) of the popup in a custom manner
    delegate: {value: null},

    /**
        Internal property.
        @private
    */
    contentEl: {
        value: null
    },
/**
        Description TODO
        @type {Property}
        @default {Container} null
        @private
    */
    containerEl: {
        value: null
    },


/**
  Description TODO
  @private
*/
    _slot: {value: null},
/**

        Description TODO
        @type {Function}
        @default null
    @private
    */
    slot: {
        get: function() {
            return this._slot;
        },
        set: function(val) {
            this._slot = val;
            if (this.content) {
                this._slot.content = this.content;
            }
        }
    },
/**
  Description TODO
  @private
*/
    _content: {value: null},
/**
        The Montage component that will be shown in this popup.

        @type {Function}
        @default null
    */
    content: {
        serializable: true,
        get: function() {
            return this._content;
        },
        set: function(value) {
            if (this._content !== value && this.slot) {
                this.slot.content = value;
            }
            this._content = value;
            // set the popup property of the content.
            this._content.popup = this;
            this.needsDraw = true;
        }
    },
/**
  Description TODO
  @private
*/
    _modal: { value: false },
/**
        If true, the Popup will be rendered as a Modal.

        @type {Function}
        @default {Boolean} false
    */
    modal: {
        get: function() {
            return this._modal;
        },
        set: function(value) {
            // force this to be a boolean
            value = !!value;

            if (this._modal !== value) {
                this._modal = value;
                this.needsDraw = true;
            }
        }
    },

    /**
        An Object wtih values {top, left}. Set it only if the popup should display at a
        given location always.
    */
    _position: {value: null},
    position: {
        get: function() {
            return this._position;
        },
        set: function(pos) {
            this._position = pos;
            //this.needsDraw = true;
        }
    },

    /**
    * Number of milliseconds after which the Popup must be dismissed. Default is 0.
    */
    autoDismiss: { value: 0 },

    /** @private */
    _displayed: { value: false },
    displayed: {
        get: function() {
            return this._displayed;
        },
        set: function(value) {
            if (this._displayed !== value) {
                this.needsDraw = true;
            }
            this._displayed = value;

        }
    },
/**
    Description TODO
    @function
    */
    prepareForDraw: {
        value: function() {
            this.type = this.type || 'custom';
        }
    },

    _popupSlot: {
        value: null
    },

    _modalDialogMask: {
        value: null
    },


    /**
     Internal method.
     @private
     */
    _getElementPosition: {
        value: function(obj) {
            var curleft = 0, curtop = 0, curHt = 0, curWd = 0;
            if (obj.offsetParent) {
                do {
                    curleft += obj.offsetLeft;
                    curtop += obj.offsetTop;
                    curHt += obj.offsetHeight;
                    curWd += obj.offsetWidth;
                } while ((obj = obj.offsetParent));
            }
            return [curleft,curtop, curHt, curWd];

        }
    },

    _positionPopup: {
        value: function() {
            var position, delegate = this.delegate, anchor = this.anchor, type = this.type;

            // if a delegate is provided, use that to get the position
            if(delegate && (typeof delegate.willPositionPopup === 'function')) {
                var anchorPosition;
                if(anchor) {
                    anchorPosition = this._getElementPosition(anchor);
                }
                position = delegate.willPositionPopup(this, anchor, anchorPosition);
            } else if(this.position !== null) {
                // If a position has been specified but no delegate has been provided
                // we assume that the position is static and hence use that
                position = this.position;
            } else {
                // @todo - advanced positioning support
                var $el = this.contentEl || this.content.element;
                var elHeight = parseFloat($el.style.height || 0) || $el.offsetHeight || 0;
                var elWidth = parseFloat($el.style.width || 0) || $el.offsetWidth || 0;

                // @todo - to get the window from application
                var viewportHeight = window.innerHeight;
                var viewportWidth = window.innerWidth;

                if (anchor) {
                    // if an anchor is provided, we position the popup relative to the anchor
                    //
                    if (anchor.nodeName) {
                        // if anchor is an element
                        var elPosition = this._getElementPosition(anchor);
                        var tgtHeight = parseFloat(anchor.style.height || 0) || anchor.offsetHeight || 0;
                        var tgtWidth = parseFloat(anchor.style.width || 0) || anchor.offsetWidth || 0;

                        position = {
                            top: elPosition[1] + tgtHeight + 20 /* pointer */,
                            left: elPosition[0] + (tgtWidth / 2) - (elWidth / 2)
                        };

                        if (position.left < 0) {
                            position.left = elPosition[0];
                            this._showHidePointer(false);
                            // dont show the pointer - @todo - support pointer arrow at different parts of the popup
                        }
                    } else {
                        // anchor is absolute position {top, left}
                        position = anchor;
                    }
                } else {
                    // No positioning hints provided. POsition it at the center of the viewport by default
                    position = {
                        top: (viewportHeight / 2 - (elHeight / 2)),
                        left: (viewportWidth / 2 - (elWidth / 2))
                    };
                }
            }
            //this.position = position;
            var popupSlot = this._popupSlot;

            if(position) {
                popupSlot.element.style.top = (position.top ? position.top + 'px' : '');
                popupSlot.element.style.left = (position.left ? position.left + 'px' : '');
                popupSlot.element.style.right = (position.right ? position.right + 'px' : '');
                popupSlot.element.style.bottom = (position.bottom ? position.bottom + 'px' : '');
            }

        }
    },


    _createModalMask: {
        value: function() {
            var el = document.createElement('div');
            el.classList.add('montage-popup-modal-mask');
            el.style.zIndex = 6999;
            el.classList.add('montage-invisible');

            document.body.appendChild(el);
            return el;
        }
    },

    _showHidePointer: {
        value: function(showTip) {
        }
    },

    _addEventListeners: {
        value: function() {
            if (window.Touch) {
                this.element.ownerDocument.addEventListener('touchstart', this, false);
            } else {
                this.element.ownerDocument.addEventListener('mousedown', this, false);
                this.element.ownerDocument.addEventListener('keyup', this, false);
            }
            window.addEventListener('resize', this);
        }
    },

    _removeEventListeners: {
        value: function() {
            if (window.Touch) {
                this.element.ownerDocument.removeEventListener('touchstart', this, false);
            } else {
                this.element.ownerDocument.removeEventListener('mousedown', this, false);
                this.element.ownerDocument.removeEventListener('keyup', this, false);
            }
            window.removeEventListener('resize', this);
        }
    },

    /**
    * Show the Popup. The Popup is displayed at a position determined by the following conditions:
    * 1) If a delegate is provided and the willPositionPopup function is implemented, the position is always determined by the delegate
    * 2) If Popup.position has been set, the Popup is always displayed at this location
    * 3) If an anchor has been set, the popup is displayed below the anchor
    * 4) If no positional hints are provided, the Popup is displayed at the center of the screen
    */
    show: {
        value: function() {
            var type = this.type,
                self = this;
            this.application.getPopupSlot(type, this, function(slot) {
                self._popupSlot = slot;
                self.displayed = true;
                self._addEventListeners();
            });
        }
    },

    /**
    * Hide the popup
    */
    hide: {
        value: function() {
            this._removeEventListeners();

            var type = this.type,
                self = this;

            this.application.returnPopupSlot(type);
            this.displayed = false;
        }
    },

    _showModalMask: {
        value: function() {
            this._modalDialogMask = document.querySelector('.montage-popup-modal-mask');
            this._modalDialogMask = this._modalDialogMask || this._createModalMask();
            this._modalDialogMask.classList.remove('montage-invisible');
        }
    },

    _hideModalMask: {
        value: function() {
            // check to see if there is at least one modal dialog in the DOM
            // See https://github.com/Motorola-Mobility/montage/issues/32
            var activePopups = this.application._getActivePopupSlots();
            var count = 0;
            if(activePopups && activePopups.length > 0) {
                // look to see if any content is a modal
                var i, len = activePopups.length;
                for(i=0; i< len; i++) {
                    if(activePopups[i].content && activePopups[i].content.modal === true) {
                        count++;
                    }
                }
            }
            if(count <= 0) {
                this._modalDialogMask.classList.add('montage-invisible');
            }
        }
    },


    draw: {
        value: function() {
            if (this.displayed) {

                if(this.modal === true) {
                    this.element.classList.add('montage-modal');
                } else {
                    this.element.classList.remove('montage-modal');
                }

                // @todo - positioning should happen inside the draw. Looks like this is only possible
                // with a double draw where we calculate the position on didDraw and position it in draw().
                // For the first release, we position inside the didDraw
                //this._positionPopup();

                this.element.classList.remove('montage-invisible');
                this.content.element.style.display = 'block';
                this.content.element.classList.remove('montage-invisible');
                // TODO do we want the panel to be focusable?
                this.content.element.setAttribute("tabindex", "0"); // Make the popup content focusable

                if (this.autoDismiss) {
                    var self = this;
                    setTimeout(function() {
                        self.hide();
                    }, this.autoDismiss);
                }
            } else {
                if (!this.element.classList.contains('montage-invisible')) {
                    this.element.classList.add('montage-invisible');
                }
                this.content.element.classList.add('montage-invisible');
                if(this._popupSlot) {
                    this._popupSlot.content = null;
                }
            }
        }
    },
/**
    Description TODO
    @function
    */
    didDraw: {
        value: function() {
            if (this._displayed) {

                if(this.modal === true) {
                    this._showModalMask();
                }

                this._positionPopup();
                // focus the content to enable key events such as ENTER/ESC
                this.content.element.focus();

            } else {
                if(this.modal === true) {
                    this._hideModalMask();
                }
            }
            // kishore: invoking this event in didDraw as we need the dimensions of the content.
            // Inside the draw(), the display is set to none at the top level and hence
            // offsetWidth and Height are always 0
            var evt = document.createEvent("CustomEvent");
            evt.initCustomEvent((this._displayed === true ? 'show' : 'hide'), true, true, this);
            this.dispatchEvent(evt);

        }
    },

    getZIndex: {
        value: function(elem) {

            var position, value, zIndex;
            while (elem && elem !== document) {
                position = elem.style.position;
                if (position === "absolute" || position === "relative" || position === "fixed") {
                    // webkit returns a string for zindex value and "" if zindex is not available
                    zIndex = elem.style['z-index'];
                    value = parseInt(zIndex, 10);
                    if (!isNaN(value) && value !== 0) {
                        return value;
                    }
                }
                elem = elem.parentNode;
            }
            return 0;
        }
    },
/**
  Description TODO
  @private
*/
    _handleTouchMouseup: {
        value: function(event) {
            var targetzIndex = this.getZIndex(event.target),
                zIndex = this.getZIndex(this.element);

            if (this.displayed === true && targetzIndex < zIndex) {
                if (this.modal === true) {

                } else {
                    // hide the dialog when user clicks outside it
                    this.displayed = false;
                }
            }
        }
    },

    _timeoutId: {value: null},
    handleResize: {
        value: function(e) {
            //console.log('window resize');
            var self = this;
            if(this.displayed === true) {
                // an optimization to call positionPopup fewer times
                window.clearTimeout(this._timeoutId);
                this._timeoutId = setTimeout(function() {
                    self.needsDraw = true;
                }, 100);
            }
         }
     },
     handleMousedown: {
         value: function(event) {
            this._handleTouchMouseup(event);
        }
    },
/**
    Description TODO
    @function
    @param {Event} event The event.
    */
    handleTouchstart: {
        value: function(event) {
            this._handleTouchMouseup(event);
        }
    },
    handleKeyup: {
        value: function(e) {
            // default handling of the keyup event. Content inside the popup could
            // handle the event optionally for custom behavior
            if(this.displayed === true && !this.modal && e.keyCode === 27 /* ESC key */) {
               this.hide();
            }
        }
    }
});