-
Notifications
You must be signed in to change notification settings - Fork 36
/
Dialog.js
226 lines (196 loc) · 5.71 KB
/
Dialog.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
define([
"dcl/advise",
"dojo/dnd/Moveable",
"delite/popup",
"delite/register",
"delite/Container",
"delite/Dialog",
"delite/Viewport",
"./ResizeHandle",
"delite/handlebars!./Dialog/Dialog.html",
"requirejs-dplugins/i18n!./Dialog/nls/Dialog",
"requirejs-dplugins/css!./Dialog/Dialog.css",
"delite/a11yclick", // Dialog.html uses d-keyboard-click
"delite/uacss" // Dialog.css references "d-ie" class
], function (
advise,
Moveable,
popup,
register,
Container,
Dialog,
Viewport,
ResizeHandle,
template,
messages
) {
"use strict";
/**
* A dialog widget, to be used as a popup.
* Meant to contain forms or interactive controls (ex: links, buttons).
*
* @class module:deliteful/Dialog
* @augments module:delite/Dialog
* @augments module:delite/Container
*/
return register("d-dialog", [HTMLElement, Dialog, Container], /** @lends module:deliteful/Dialog# */ {
baseClass: "d-dialog",
nls: messages,
template: template,
/**
* Whether or not dialog is draggable.
*/
draggable: true,
/**
* If set, makes dialog resizable. One of: x|y|xy limit resizing to a single axis.
* @member {string}
*/
resizeAxis: "",
/**
* The title of the Dialog, displayed at the top, above the content.
* @member {string}
*/
label: "",
/**
* Class to display the close button icon.
* @member {string}
* @default "d-dialog-close-icon"
*/
closeButtonIconClass: "d-dialog-close-icon",
refreshRendering: function (props) {
// Add/remove ResizeHandle depending on whether or not Dialog is resizable.
if ("resizeAxis" in props) {
if (this._resizeHandle) {
this._resizeHandle.destroy();
}
if (this.resizeAxis) {
this._resizeHandle = new ResizeHandle({
target: this,
resizeAxis: this.resizeAxis
});
// When resize starts, set min/max size restrictions.
advise.before(this._resizeHandle, "_beginSizing", function () {
// Don't let the user shrink the dialog below the space needed for the boilerplate
// and 50px for the content.
var minHeight = this.getBoundingClientRect().height
- this.containerNode.getBoundingClientRect().height + 50,
minWidth = 200;
// Adjust min height/width if Dialog has custom getMinSize() method.
if (this.getMinSize) {
var minSize = this.getMinSize();
minHeight = Math.max(minHeight, minSize.h);
minWidth = Math.max(minWidth, minSize.w);
}
this._resizeHandle.minHeight = minHeight;
this._resizeHandle.minWidth = minWidth;
// If dialog has getMaxSize() method, set max size restrictions.
if (this.getMaxSize) {
var maxSize = this.getMaxSize();
this._resizeHandle.maxHeight = maxSize.h;
this._resizeHandle.maxWidth = maxSize.w;
}
}.bind(this));
// Add it to root node, not this.containerNode.
HTMLElement.prototype.appendChild.call(this, this._resizeHandle);
}
}
if ("draggable" in props) {
if (this.draggable) {
this.enableDrag();
} else {
this.disableDrag();
}
}
},
/**
* Display the Dialog.
*/
open: function () {
var previouslyFocusedNode = this.ownerDocument.activeElement;
popup.open({
parent: previouslyFocusedNode,
popup: this,
orient: ["center"],
onExecute: this.close.bind(this),
onCancel: this.close.bind(this),
onClose: function () {
// Focus previously focused node unless it's hidden or destroyed,
// in which case caller must handle the focus.
if (previouslyFocusedNode && previouslyFocusedNode.focus &&
previouslyFocusedNode.offsetParent !== null) {
previouslyFocusedNode.focus();
}
}.bind(this)
});
this.focus();
},
/**
* Closes the dialog.
* Called automatically when dialog emits an "execute" or "cancel" event.
*/
close: function () {
popup.close(this);
},
/**
* Make the dialog draggable.
*/
enableDrag: function () {
if (!this.moveable) {
this.moveable = new Moveable(this, {
handle: this.headerNode,
area: "padding",
within: true,
skip: true
});
advise.after(this.moveable, "onMoveStart", function () {
// Lock in width/height to prevent squashing when Dialog dragged past right edge of screen.
var cs = getComputedStyle(this);
this.style.width = cs.width;
this.style.height = cs.height;
this.emit("delite-dragged");
}.bind(this));
advise.after(this.moveable, "onMoveStop", function () {
// Snap back so drag handle on screen.
this.moveIntoView();
}.bind(this));
}
},
/**
* Move the dialog back into view, at least enough so that the user can drag it again.
*/
moveIntoView: function () {
// Note: all positions relative to document (not to viewport).
var viewport = Viewport.getEffectiveBox(),
bcr = this.headerNode.getBoundingClientRect(),
curTop = parseFloat(this.style.top),
curLeft = parseFloat(this.style.left),
minTop = viewport.t,
minLeft = viewport.l + 50 - bcr.width,
maxTop = Math.max(viewport.t + viewport.h - bcr.height, 0),
maxLeft = Math.max(viewport.l + viewport.w - 50, 0);
if (curTop < 0 || curTop > maxTop || curLeft < minLeft || curLeft > maxLeft) {
var top = Math.min(Math.max(curTop, minTop), maxTop),
left = Math.min(Math.max(curLeft, minLeft), maxLeft);
this.style.top = top + "px";
this.style.left = left + "px";
this.emit("popup-after-position");
}
},
/**
* Remove ability to drag the dialog.
*/
disableDrag: function () {
if (this.moveable) {
this.moveable.destroy();
delete this.moveable;
}
},
/**
* Called when clicking the dialog's close button.
* @protected
*/
closeButtonClickHandler: function () {
this.emit("cancel");
}
});
});