-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopup.js
289 lines (253 loc) · 12.6 KB
/
Popup.js
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
// This file is part of DQX - (C) Copyright 2014, Paul Vauterin, Ben Jeffery, Alistair Miles <[email protected]>
// This program is free software licensed under the GNU Affero General Public License.
// You can find a copy of this license in LICENSE in the top directory of the source code or at <http://opensource.org/licenses/AGPL-3.0>
/************************************************************************************************************************************
*************************************************************************************************************************************
Defines a popup window
Use Popup.create to create a new popup, and DQX.ClosePopup to close it.
*************************************************************************************************************************************
*************************************************************************************************************************************/
define(["jquery", "DQX/Utils", "DQX/DocEl", "DQX/Msg", "DQX/Controls"],
function ($, DQX, DocEl, Msg, Controls) {
var Popup = {};
Popup.activePopupList = [];
Popup._checkBackgroundBlockNeeded = function () {
var blockingPopupsPresent = false;
$('#DQXBackBlocker').find(".DQXFloatBox").each(function (index, Element) { blockingPopupsPresent = true; });
if (!blockingPopupsPresent)
$('#DQXBackBlocker').remove();
}
//Closes a popup, providing the unique identifier that was returned by Popup.create
DQX.ClosePopup = function (index) {
var posit = Popup.activePopupList.indexOf(index);
if ((posit < 0) && (_debug_)) DQX.reportError('Unable to find popup');
Popup.activePopupList.splice(posit, 1);
$("#" + index).remove();
DQX.unRegisterGlobalKeyDownReceiver(index);
Popup._checkBackgroundBlockNeeded();
}
DQX._popupIndex = 0;
//Internal
DQX.SwitchPinned = function (ID) {
var elem = $("#" + ID);
var newStatus = !Popup.isPinned(ID);
if (newStatus) {
$("#" + ID).appendTo("#DQXUtilContainer");
Popup._checkBackgroundBlockNeeded();
elem.find('.DQXPinBoxUnpinned').remove();
elem.find('.DQXPinBoxPinned').remove();
elem.append(Popup._createPinBox(ID, newStatus));
DQX.unRegisterGlobalKeyDownReceiver(ID); //should not receive global keyboard events anymore
}
}
Popup._floatBoxMaxIndex = 99;
//Converts a div element into a draggable box, providing the div id
Popup.makeDraggable = function (id) {
var dragElem = $('#' + id);
if (dragElem.length == 0)
DQX.reportError('Draggable container not found');
var dragHeaderElem = dragElem.find('.DQXDragHeader');
if (dragHeaderElem.length == 0)
DQX.reportError('Draggable container has no header element');
var headerID = dragHeaderElem.attr('id');
var dragOffsetX = 0;
var dragOffsetY = 0;
var boxW = 0;
var boxH = 0;
var dragOnMouseMove = function (ev) {
var newPosX = ev.pageX + dragOffsetX;
var newPosY = ev.pageY + dragOffsetY;
newPosX = Math.min(newPosX, DQX.getWindowClientW() - boxW - 10);
newPosY = Math.min(newPosY, DQX.getWindowClientH() - 40);
var newPosX = Math.max(0, newPosX);
var newPosY = Math.max(10, newPosY);
dragElem.css({ left: newPosX, top: newPosY });
return false;
}
var dragOnMouseUp = function (ev) {
$(document).unbind('mousemove.drag', dragOnMouseMove)
$(document).unbind('mouseup.drag', dragOnMouseUp);
dragElem.css('opacity', 1);
return false;
}
dragHeaderElem.bind('mousedown.drag', function (ev) {
var posX = dragElem.position().left;
var posY = dragElem.position().top;
var mouseStartX = ev.pageX;
var mouseStartY = ev.pageY;
dragOffsetX = posX - mouseStartX;
dragOffsetY = posY - mouseStartY;
boxW = dragElem.outerWidth();
boxH = dragElem.outerHeight();
$(document).bind('mousemove.drag', dragOnMouseMove);
$(document).bind('mouseup.drag', dragOnMouseUp);
dragElem.css('opacity', 0.7);
Popup._floatBoxMaxIndex++;
dragElem.css('z-index', Popup._floatBoxMaxIndex);
return false;
});
var touchHandler = {
handleTouchStart: function (info, ev) {
var posX = dragElem.position().left;
var posY = dragElem.position().top;
var mouseStartX = info.pageX;
var mouseStartY = info.pageY;
boxW = dragElem.outerWidth();
boxH = dragElem.outerHeight();
dragOffsetX = posX - mouseStartX;
dragOffsetY = posY - mouseStartY;
dragElem.css('opacity', 0.7);
Popup._floatBoxMaxIndex++;
dragElem.css('z-index', Popup._floatBoxMaxIndex);
},
handleTouchMove: function (info, ev) {
dragOnMouseMove(info);
},
handleTouchEnd: function () {
dragElem.css('opacity', 1);
}
}
DQX.augmentTouchEvents(touchHandler, headerID, true, false);
}
Popup.isPinned = function (ID) {
var elem = $("#" + ID);
return elem.find('.DQXPinBoxPinned').length > 0;
}
//Call this function to close a popup box if it is not pinned, providing the popup unique identifier
Popup.closeIfNeeded = function (ID) {
if (!Popup.isPinned(ID))
DQX.ClosePopup(ID);
}
//Automatically closes all unpinned (=blocking) popups
Popup.closeUnPinnedPopups = function () {
$.each(Popup.activePopupList, function (idx, popupID) {
Popup.closeIfNeeded(popupID);
});
}
//Automatically pins all unpinned (=blocking) popups
Popup.pinUnPinnedPopups = function () {
$.each(Popup.activePopupList, function (idx, popupID) {
if (!Popup.isPinned(popupID))
DQX.SwitchPinned(popupID);
});
}
Popup._createPinBox = function (ID, isPinned) {
var bmp = isPinned ? DQX.BMP('pin3.png') : DQX.BMP('pin4.png');
var thepinner = DocEl.JavaScriptBitmaplink(bmp, "Keep this info box visible", "DQX.SwitchPinned('" + ID + "')");
thepinner.setCssClass(isPinned ? "DQXPinBoxPinned" : "DQXPinBoxUnpinned");
thepinner.addStyle('position', 'absolute');
thepinner.addStyle('left', isPinned ? '0px' : '4px');
thepinner.addStyle('top', isPinned ? '-11px' : '-8px');
return thepinner.toString();
}
Popup._createBackBlocker = function () {
if ($('#DQXBackBlocker').length > 0)
return;
var background = DocEl.Div({ id: 'DQXBackBlocker' });
background.addStyle("position", "absolute");
background.addStyle("left", '0px');
background.addStyle("top", '0px');
background.addStyle('width', '100%');
background.addStyle('height', '100%');
var wizbackcol = 'rgba(100,100,100,0.4)';
background.addStyle('background-color', wizbackcol);
background.addStyle('z-index', '2000');
$('#DQXUtilContainer').append(background.toString());
$('#DQXBackBlocker').mousedown(function (ev) {
if (ev.target.id == 'DQXBackBlocker') {
$('#DQXBackBlocker').css('background-color', 'rgba(50,50,50,0.6)');
setTimeout(function () {
$('#DQXBackBlocker').css('background-color', wizbackcol);
setTimeout(function () {
$('#DQXBackBlocker').css('background-color', 'rgba(50,50,50,0.6)');
setTimeout(function () {
$('#DQXBackBlocker').css('background-color', wizbackcol);
}, 150);
}, 150);
}, 150);
}
});
}
//Creates a new popup box, providing a title and html content
//The function returns a unique identifier for this popup
// settings can contain:
// - canClose: True or false
Popup.create = function (title, content, helpID, settings) {
var canClose = true;
if (settings) {
if ('canClose' in settings)
canClose=settings.canClose;
}
var wasSet = false;
var popupID = '';
$(".DQXFloatBox").each(function (index, Element) {
if (!wasSet) {
if ($(this).find(".DQXPinBoxUnpinned").length > 0) {
$(this).find(".DQXFloatBoxHeader").html(title);
$(this).find(".DQXFloatBoxContent").html(content);
DQX.ExecPostCreateHtml();
wasSet = true;
popupID = $(this).attr('id');
}
}
});
if (wasSet) {
return popupID;
}
else {
Popup._createBackBlocker();
var posx = DQX.mousePosX + 10;
var posy = DQX.mousePosY + 10;
DQX._popupIndex++;
var ID = 'DXPopup' + DQX._popupIndex;
var thebox = DocEl.Div({ id: ID });
thebox.setCssClass("DQXFloatBox");
thebox.addStyle("position", "absolute");
thebox.addStyle("left", '0px');
thebox.addStyle("top", '0px');
var theheader = DocEl.Div({ id: ID + 'Handler', parent: thebox });
theheader.setCssClass("DQXFloatBoxHeader DQXDragHeader");
theheader.addElem(DQX.interpolate(title));
var thebody = DocEl.Div({ parent: thebox });
thebody.setCssClass("DQXFloatBoxContent");
thebody.addStyle("max-width", (DQX.getWindowClientW() - 100) + 'px');
thebody.addStyle("max-height", (DQX.getWindowClientH() - 100) + 'px');
thebody.addStyle("overflow-x", "hidden");
thebody.makeAutoVerticalScroller();
thebody.addElem(DQX.interpolate(content));
if (canClose) {
var thecloser = DocEl.JavaScriptBitmaplink(DQX.BMP("close2.png"), "Close", "DQX.ClosePopup('" + ID + "')");
thebox.addElem(thecloser);
thecloser.addStyle('position', 'absolute');
thecloser.addStyle('right', '-13px');
thecloser.addStyle('top', '-13px');
}
if (canClose) {
thebox.addElem(Popup._createPinBox(ID, false));
}
if (helpID) {//Help button
thebox.addElem('<IMG SRC="{bmp}" border=0 class="DQXBitmapLink Helpbutton" ALT="Help" TITLE="Help" style="opacity:0.70;position:absolute;right:35px;top:0px;">'.DQXformat({ bmp: DQX.BMP("info2.png") }));
}
var content = thebox.toString();
$('#DQXBackBlocker').append(content);
Popup.makeDraggable(ID);
var w = $('#' + ID).width();
var h = $('#' + ID).height();
var pageSizeX = $(window).width();
var pageSizeY = $(window).height();
$('#' + ID).offset({ left: (pageSizeX - w) / 2, top: (pageSizeY - h) / 2 });
DQX.ExecPostCreateHtml();
DQX.registerGlobalKeyDownReceiver(function (ev) {
if (ev.isEscape) DQX.ClosePopup(ID);
}, ID);
if (helpID) {
$('#' + ID).find('.Helpbutton').click(function () {
Msg.send({ type: 'ShowHelp' }, helpID);
});
}
}
Popup.activePopupList.push(ID);
return ID;
}
return Popup;
});