summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.3-sdk/frontend/framework/source/class/qx/ui/popup/Popup.js
blob: 171016d6582c182980e5d052a93eb055d8676eb7 (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
/* ************************************************************************

   qooxdoo - the new era of web development

   http://qooxdoo.org

   Copyright:
     2004-2006 by 1&1 Internet AG, Germany, http://www.1and1.org

   License:
     LGPL 2.1: http://www.gnu.org/licenses/lgpl.html

   Authors:
     * Sebastian Werner (wpbasti)
     * Andreas Ecker (ecker)

************************************************************************ */

/* ************************************************************************

#module(ui_popup)
#optional(qx.manager.object.MenuManager)

************************************************************************ */

qx.OO.defineClass("qx.ui.popup.Popup", qx.ui.layout.CanvasLayout,
function()
{
  qx.ui.layout.CanvasLayout.call(this);

  this.setZIndex(this._minZIndex);
});

qx.OO.changeProperty({ name : "appearance", type : "string", defaultValue : "popup" });

/*!
  Whether to let the system decide when to hide the popup. Setting
  this to false gives you better control but it also requires you
  to handle the closing of the popup.
*/
qx.OO.addProperty({ name : "autoHide", type : "boolean", defaultValue : true });

/*!
  Make element displayed (if switched to true the widget will be created, if needed, too).
  Instead of qx.ui.core.Widget, the default is false here.
*/
qx.OO.changeProperty({ name : "display", type : "boolean", defaultValue : false });

/*!
  Center the popup on open
*/
qx.OO.addProperty({ name : "centered", type : "boolean", defaultValue : false });

/**
 * Whether the popup should be restricted to the visible area of the page when opened.
 */
qx.OO.addProperty({ name : "restrictToPageOnOpen", type : "boolean", defaultValue : true });


qx.Proto._showTimeStamp = (new Date(0)).valueOf();
qx.Proto._hideTimeStamp = (new Date(0)).valueOf();


/**
 * The minimum offset to the left of the page too keep when
 * {@link #restrictToPageOnOpen} is true (in pixels).
 */
qx.Settings.setDefault("restrictToPageLeft", "5");

/**
 * The minimum offset to the right of the page too keep when
 * {@link #restrictToPageOnOpen} is true (in pixels).
 */
qx.Settings.setDefault("restrictToPageRight", "5");

/**
 * The minimum offset to the top of the page too keep when
 * {@link #restrictToPageOnOpen} is true (in pixels).
 */
qx.Settings.setDefault("restrictToPageTop", "5");

/**
 * The minimum offset to the bottom of the page too keep when
 * {@link #restrictToPageOnOpen} is true (in pixels).
 */
qx.Settings.setDefault("restrictToPageBottom", "5");





/*
---------------------------------------------------------------------------
  APPEAR/DISAPPEAR
---------------------------------------------------------------------------
*/

qx.Proto._beforeAppear = function()
{
  qx.ui.layout.CanvasLayout.prototype._beforeAppear.call(this);

  if (this.getRestrictToPageOnOpen()) {
    this._wantedLeft = this.getLeft();

    if (this._wantedLeft != null) {
      // Move the popup out of the view so its size could be calculated before
      // it is positioned.
      this.setLeft(10000);
      if (this.getElement() != null) {
        // The popup was already visible once before
        // -> Move it immediately before it gets visible again
        this.getElement().style.left = 10000;
      }
    }
  }

  qx.manager.object.PopupManager.getInstance().add(this);
  qx.manager.object.PopupManager.getInstance().update(this);

  this._showTimeStamp = (new Date).valueOf();
  this.bringToFront();
}

qx.Proto._beforeDisappear = function()
{
  qx.ui.layout.CanvasLayout.prototype._beforeDisappear.call(this);

  qx.manager.object.PopupManager.getInstance().remove(this);

  this._hideTimeStamp = (new Date).valueOf();
}

qx.Proto._afterAppear = function() {
  qx.ui.layout.CanvasLayout.prototype._afterAppear.call(this);

  if (this.getRestrictToPageOnOpen()) {
    var doc = qx.ui.core.ClientDocument.getInstance();
    var docWidth = doc.getClientWidth();
    var docHeight = doc.getClientHeight();
    var restrictToPageLeft   = parseInt(qx.Settings.getValueOfClass("qx.ui.popup.Popup", "restrictToPageLeft"));
    var restrictToPageRight  = parseInt(qx.Settings.getValueOfClass("qx.ui.popup.Popup", "restrictToPageRight"));
    var restrictToPageTop    = parseInt(qx.Settings.getValueOfClass("qx.ui.popup.Popup", "restrictToPageTop"));
    var restrictToPageBottom = parseInt(qx.Settings.getValueOfClass("qx.ui.popup.Popup", "restrictToPageBottom"));
    var left   = (this._wantedLeft == null) ? this.getLeft() : this._wantedLeft;
    var top    = this.getTop();
    var width  = this.getBoxWidth();
    var height = this.getBoxHeight();

    var oldLeft = this.getLeft();
    var oldTop = top;

    // NOTE: We check right and bottom first, because top and left should have
    //       priority, when both sides are violated.
    if (left + width > docWidth - restrictToPageRight) {
      left = docWidth - restrictToPageRight - width;
    }
    if (top + height > docHeight - restrictToPageBottom) {
      top = docHeight - restrictToPageBottom - height;
    }
    if (left < restrictToPageLeft) {
      left = restrictToPageLeft;
    }
    if (top < restrictToPageTop) {
      top = restrictToPageTop;
    }

    if (left != oldLeft || top != oldTop) {
      var self = this;
      window.setTimeout(function() {
        self.setLeft(left);
        self.setTop(top);
        qx.ui.core.Widget.flushGlobalQueues();
      }, 0);
    }
  }
};





/*
---------------------------------------------------------------------------
  ACTIVE/INACTIVE
---------------------------------------------------------------------------
*/

qx.Proto._makeActive = function() {
  this.getFocusRoot().setActiveChild(this);
}

qx.Proto._makeInactive = function()
{
  var vRoot = this.getFocusRoot();
  var vCurrent = vRoot.getActiveChild();

  if (vCurrent == this) {
    vRoot.setActiveChild(vRoot);
  }
}





/*
---------------------------------------------------------------------------
  FOCUS
---------------------------------------------------------------------------
*/

qx.Proto.isFocusable = function() {
  return false;
}





/*
---------------------------------------------------------------------------
  ZIndex Positioning
---------------------------------------------------------------------------
*/

qx.Proto._minZIndex = 1e6;

qx.Proto.bringToFront = function()
{
  this.forceZIndex(Infinity);
  this._sendTo();
}

qx.Proto.sendToBack = function()
{
  this.forceZIndex(-Infinity);
  this._sendTo();
}

qx.Proto._sendTo = function()
{
  var vPopups = qx.lang.Object.getValues(qx.manager.object.PopupManager.getInstance().getAll());
  var vMenus = qx.lang.Object.getValues(qx.manager.object.MenuManager.getInstance().getAll());

  var vAll = vPopups.concat(vMenus).sort(qx.util.Compare.byZIndex);
  var vLength = vAll.length;
  var vIndex = this._minZIndex;

  for (var i=0; i<vLength; i++) {
    vAll[i].setZIndex(vIndex++);
  }
}






/*
---------------------------------------------------------------------------
  TIMESTAMP HANDLING
---------------------------------------------------------------------------
*/

qx.Proto.getShowTimeStamp = function() {
  return this._showTimeStamp;
}

qx.Proto.getHideTimeStamp = function() {
  return this._hideTimeStamp;
}

/*
---------------------------------------------------------------------------
  UTILITIES
---------------------------------------------------------------------------
*/

/**
 * Positions the popup relative to some reference element.
 * @param el    {var} Reference DOM element/widget.
 * @param offsetX   {int} Offset in pixels in X direction (optional).
 * @param offsetY   {int} Offset in pixels in Y direction (optional).
 */
qx.Proto.positionRelativeTo = function(el, offsetX, offsetY)
{
  if (el instanceof qx.ui.core.Widget) {
    el = el.getElement();
  }
  if (el) {
    var gecko = qx.sys.Client.getInstance().isGecko();
    var loc = qx.dom.Location;
    this.setLocation(loc.getClientAreaLeft(el) - (gecko ? qx.dom.Style.getBorderLeft(el):0) + (offsetX || 0),
      loc.getClientAreaTop(el) - (gecko ? qx.dom.Style.getBorderTop(el):0) + (offsetY || 0));
  } else {
    this.warn('Missing reference element');
  }
}

qx.Proto.centerToBrowser = function()
{
  var d = qx.ui.core.ClientDocument.getInstance();

  var left = (d.getClientWidth() - this.getBoxWidth()) / 2;
  var top = (d.getClientHeight() - this.getBoxHeight()) / 2;

  this.setLeft(left < 0 ? 0 : left);
  this.setTop(top < 0 ? 0 : top);
}



/*
---------------------------------------------------------------------------
  DISPOSER
---------------------------------------------------------------------------
*/

qx.Proto.dispose = function()
{
  if (this.getDisposed()) {
    return;
  }

  this._showTimeStamp = null;
  this._hideTimeStamp = null;

  return qx.ui.layout.CanvasLayout.prototype.dispose.call(this);
}