-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathDialog.js
274 lines (236 loc) · 6.28 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
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
import { isPhone, isDesktop } from "@ui5/webcomponents-base/dist/Device.js";
import Popup from "./Popup.js";
// Template
import DialogTemplate from "./generated/templates/DialogTemplate.lit.js";
// Styles
import PopupsCommonCss from "./generated/themes/PopupsCommon.css.js";
import dialogCSS from "./generated/themes/Dialog.css.js";
/**
* @public
*/
const metadata = {
tag: "ui5-dialog",
slots: /** @lends sap.ui.webcomponents.main.Popup.prototype */ {
/**
* Defines the header HTML Element.
*
* @type {HTMLElement[]}
* @slot
* @public
*/
header: {
type: HTMLElement,
},
/**
* Defines the footer HTML Element.
*
* @type {HTMLElement[]}
* @slot
* @public
*/
footer: {
type: HTMLElement,
},
},
properties: /** @lends sap.ui.webcomponents.main.Dialog.prototype */ {
/**
* Defines the header text.
* <br><br>
* <b>Note:</b> If <code>header</code> slot is provided, the <code>headerText</code> is ignored.
*
* @type {string}
* @defaultvalue ""
* @public
*/
headerText: {
type: String,
},
/**
* Determines whether the <code>ui5-dialog</code> should be stretched to fullscreen.
* <br><br>
* <b>Note:</b> The <code>ui5-dialog</code> will be stretched to approximately
* 90% of the viewport.
*
* @type {boolean}
* @defaultvalue false
* @public
*/
stretch: {
type: Boolean,
},
/**
* Determines whether the <code>ui5-dialog</code> is draggable.
* If this property is set to true, the Dialog will be draggable by its header.
* <br><br>
* <b>Note:</b> The <code>ui5-dialog</code> can be draggable only in desktop mode.
* @defaultvalue false
* @since 1.0.0-rc.9
* @public
*/
draggable: {
type: Boolean,
},
/**
* @private
*/
onPhone: {
type: Boolean,
},
/**
* @private
*/
onDesktop: {
type: Boolean,
},
},
};
/**
* @class
* <h3 class="comment-api-title">Overview</h3>
* The <code>ui5-dialog</code> component is used to temporarily display some information in a
* size-limited window in front of the regular app screen.
* It is used to prompt the user for an action or a confirmation.
* The <code>ui5-dialog</code> interrupts the current app processing as it is the only focused UI element and
* the main screen is dimmed/blocked.
* The dialog combines concepts known from other technologies where the windows have
* names such as dialog box, dialog window, pop-up, pop-up window, alert box, or message box.
* <br><br>
* The <code>ui5-dialog</code> is modal, which means that user action is required before returning to the parent window is possible.
* The content of the <code>ui5-dialog</code> is fully customizable.
*
* <h3>Structure</h3>
* A <code>ui5-dialog</code> consists of a header, content, and a footer for action buttons.
* The <code>ui5-dialog</code> is usually displayed at the center of the screen.
* Its position can be changed by the user. To enable this, you need to set the property <code>draggable</code> accordingly.
*
* <h3>Responsive Behavior</h3>
* The <code>stretch</code> property can be used to stretch the
* <code>ui5-dialog</code> on full screen.
*
* <h3>ES6 Module Import</h3>
*
* <code>import "@ui5/webcomponents/dist/Dialog";</code>
*
* <b>Note:</b> We don't recommend nesting popup-like components (<code>ui5-dialog</code>, <code>ui5-popover</code>) inside <code>ui5-dialog</code>.
* Ideally you should create all popups on the same level inside your HTML page and just open them from one another, rather than nesting them.
*
* @constructor
* @author SAP SE
* @alias sap.ui.webcomponents.main.Dialog
* @extends Popup
* @tagname ui5-dialog
* @public
*/
class Dialog extends Popup {
static get metadata() {
return metadata;
}
static get template() {
return DialogTemplate;
}
static get styles() {
return [PopupsCommonCss, dialogCSS];
}
get isModal() { // Required by Popup.js
return true;
}
get _ariaLabelledBy() { // Required by Popup.js
return this.ariaLabel ? undefined : "ui5-popup-header";
}
get _ariaModal() { // Required by Popup.js
return true;
}
get classes() {
return {
root: {
"ui5-popup-root": true,
},
content: {
"ui5-popup-content": true,
},
};
}
onBeforeRendering() {
this.onPhone = isPhone();
this.onDesktop = isDesktop();
}
onEnterDOM() {
this._dragMouseMoveHandler = this._onDragMouseMove.bind(this);
this._dragMouseUpHandler = this._onDragMouseUp.bind(this);
}
onExitDOM() {
this._dragMouseMoveHandler = null;
this._dragMouseUpHandler = null;
}
/**
* Event handlers
*/
_onDragMouseDown(event) {
if (!(this.draggable && this.onDesktop)) {
return;
}
// only allow dragging on the header's whitespace
if (!event.target.classList.contains("ui5-popup-header-root")
&& event.target.getAttribute("slot") !== "header") {
return;
}
event.preventDefault();
const {
top,
left,
} = this.getBoundingClientRect();
const {
width,
height,
} = window.getComputedStyle(this);
Object.assign(this.style, {
transform: "none",
top: `${top}px`,
left: `${left}px`,
width: `${Math.round(Number(width) * 100) / 100}px`,
height: `${Math.round(Number(height) * 100) / 100}px`,
});
this._x = event.clientX;
this._y = event.clientY;
this._attachDragHandlers();
}
_onDragMouseMove(event) {
event.preventDefault();
const calcX = this._x - event.clientX;
const calcY = this._y - event.clientY;
const {
left,
top,
} = this.getBoundingClientRect();
Object.assign(this.style, {
left: `${Math.floor(left - calcX)}px`,
top: `${Math.floor(top - calcY)}px`,
});
this._x = event.clientX;
this._y = event.clientY;
}
_onDragMouseUp() {
this._x = null;
this._y = null;
this._detachDragHandlers();
}
_attachDragHandlers() {
window.addEventListener("mousemove", this._dragMouseMoveHandler);
window.addEventListener("mouseup", this._dragMouseUpHandler);
this.addEventListener("ui5-before-close", this._recenter);
}
_detachDragHandlers() {
window.removeEventListener("mousemove", this._dragMouseMoveHandler);
window.removeEventListener("mouseup", this._dragMouseUpHandler);
}
_recenter() {
Object.assign(this.style, {
top: "",
left: "",
transform: "",
});
this.removeEventListener("ui5-before-close", this._recenter);
}
}
Dialog.define();
export default Dialog;