Create our own ModalDialog
[armadillo.git] / web_frontend / modal_dialog.js
1 //
2 // Armadillo File Manager
3 // Copyright (c) 2010, Robert Sesek <http://www.bluestatic.org>
4 //
5 // This program is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free Software
7 // Foundation, either version 3 of the License, or any later version.
8 //
9
10 goog.provide('armadillo.ModalDialog');
11
12 goog.require('goog.events.EventHandler');
13 goog.require('goog.ui.Container');
14 goog.require('goog.ui.Control');
15
16 /**
17 * A modal dialog that does not use iframe masks, but instead installs a global
18 * event listener to prevent events.
19 * @param {goog.ui.DomHelper} opt_domHelper
20 * @constructor
21 */
22 armadillo.ModalDialog = function(opt_domHelper) {
23 goog.ui.Container.call(this, null, null, opt_domHelper);
24
25 /**
26 * Master EventHandler
27 */
28 this.eh_ = new goog.events.EventHandler();
29 };
30 goog.inherits(armadillo.ModalDialog, goog.ui.Container);
31
32 /**
33 * Disposer
34 * @private
35 */
36 armadillo.ModalDialog.prototype.disposeInternal = function() {
37 armadillo.ModalDialog.superClass_.disposeInternal.call(this);
38 this.eh_.dispose();
39 this.eh_ = null;
40 };
41
42 /**
43 * @inheritsDoc
44 */
45 armadillo.ModalDialog.prototype.createDom = function() {
46 this.decorateInternal(this.dom_.createElement('div'));
47 };
48
49 /**
50 * @inheritsDoc
51 */
52 armadillo.ModalDialog.prototype.decorateInternal = function(element) {
53 this.element_ = element;
54
55 var close = new goog.ui.Control('Close Modal Dialog');
56 var dialog = this;
57 close.handleMouseDown = function(e) {
58 e.stopPropagation();
59 dialog.dispose();
60 };
61 this.addChild(close, true);
62 };
63
64 /**
65 * @inheritsDoc
66 */
67 armadillo.ModalDialog.prototype.enterDocument = function() {
68 armadillo.ModalDialog.superClass_.enterDocument.call(this);
69 // Create an event sink that captures all browser event types.
70 var types = new Array();
71 for (type in goog.events.EventType) {
72 types.push(goog.events.EventType[type]);
73 }
74 this.eventSinkKey_ = this.eh_.listen(this.dom_.getWindow(),
75 types, this.eventSink_, true, this);
76 };
77
78 /**
79 * @inheritsDoc
80 */
81 armadillo.ModalDialog.prototype.exitDocument = function() {
82 goog.events.unlistenByKey(this.eventSinkKey_);
83 this.eventSinkKey_ = null;
84 };
85
86 /**
87 * Every event that gets dispatched to the Window will first be evaluated by
88 * this memeber. If the event's target is not a child of the dialog, the event
89 * is cancelled.
90 * @param {Event} event
91 */
92 armadillo.ModalDialog.prototype.eventSink_ = function(event) {
93 var target = event.target;
94 while (target) {
95 if (target == this.element_) {
96 return;
97 }
98 target = target.parentNode;
99 }
100 event.stopPropagation();
101 };