1
- import { isPhone } from "@ui5/webcomponents-base/dist/Device.js" ;
1
+ import { isPhone , isDesktop } from "@ui5/webcomponents-base/dist/Device.js" ;
2
2
import Popup from "./Popup.js" ;
3
3
4
4
// Template
@@ -52,7 +52,7 @@ const metadata = {
52
52
/**
53
53
* Determines whether the <code>ui5-dialog</code> should be stretched to fullscreen.
54
54
* <br><br>
55
- * <b>Note:</b> The <code>ui5-dialog</code> will be stretched to aproximetly
55
+ * <b>Note:</b> The <code>ui5-dialog</code> will be stretched to approximately
56
56
* 90% of the viewport.
57
57
*
58
58
* @type {boolean }
@@ -63,12 +63,32 @@ const metadata = {
63
63
type : Boolean ,
64
64
} ,
65
65
66
+ /**
67
+ * Determines whether the <code>ui5-dialog</code> is draggable.
68
+ * If this property is set to true, the Dialog will be draggable by its header.
69
+ * <br><br>
70
+ * <b>Note:</b> The <code>ui5-dialog</code> can be draggable only in desktop mode.
71
+ * @defaultvalue false
72
+ * @since 1.0.0-rc.9
73
+ * @public
74
+ */
75
+ draggable : {
76
+ type : Boolean ,
77
+ } ,
78
+
66
79
/**
67
80
* @private
68
81
*/
69
82
onPhone : {
70
83
type : Boolean ,
71
84
} ,
85
+
86
+ /**
87
+ * @private
88
+ */
89
+ onDesktop : {
90
+ type : Boolean ,
91
+ } ,
72
92
} ,
73
93
} ;
74
94
@@ -89,6 +109,9 @@ const metadata = {
89
109
* <h3>Structure</h3>
90
110
* A <code>ui5-dialog</code> consists of a header, content, and a footer for action buttons.
91
111
* The <code>ui5-dialog</code> is usually displayed at the center of the screen.
112
+ * Its position can be changed by the user. To enable this, you need to set the property <code>draggable</code> accordingly.
113
+
114
+
92
115
*
93
116
* <h3>Responsive Behavior</h3>
94
117
* The <code>stretch</code> property can be used to stretch the
@@ -121,10 +144,6 @@ class Dialog extends Popup {
121
144
return [ PopupsCommonCss , dialogCSS ] ;
122
145
}
123
146
124
- onBeforeRendering ( ) {
125
- this . onPhone = isPhone ( ) ;
126
- }
127
-
128
147
get isModal ( ) { // Required by Popup.js
129
148
return true ;
130
149
}
@@ -147,6 +166,107 @@ class Dialog extends Popup {
147
166
} ,
148
167
} ;
149
168
}
169
+
170
+ onBeforeRendering ( ) {
171
+ this . onPhone = isPhone ( ) ;
172
+ this . onDesktop = isDesktop ( ) ;
173
+ }
174
+
175
+ onEnterDOM ( ) {
176
+ this . _dragMouseMoveHandler = this . _onDragMouseMove . bind ( this ) ;
177
+ this . _dragMouseUpHandler = this . _onDragMouseUp . bind ( this ) ;
178
+ }
179
+
180
+ onExitDOM ( ) {
181
+ this . _dragMouseMoveHandler = null ;
182
+ this . _dragMouseUpHandler = null ;
183
+ }
184
+
185
+ /**
186
+ * Event handlers
187
+ */
188
+ _onDragMouseDown ( event ) {
189
+ if ( ! ( this . draggable && this . onDesktop ) ) {
190
+ return ;
191
+ }
192
+
193
+ // only allow dragging on the header's whitespace
194
+ if ( ! event . target . classList . contains ( "ui5-popup-header-root" )
195
+ && event . target . getAttribute ( "slot" ) !== "header" ) {
196
+ return ;
197
+ }
198
+
199
+ event . preventDefault ( ) ;
200
+
201
+ const {
202
+ top,
203
+ left,
204
+ } = this . getBoundingClientRect ( ) ;
205
+ const {
206
+ width,
207
+ height,
208
+ } = window . getComputedStyle ( this ) ;
209
+
210
+ Object . assign ( this . style , {
211
+ transform : "none" ,
212
+ top : `${ top } px` ,
213
+ left : `${ left } px` ,
214
+ width : `${ Math . round ( Number ( width ) * 100 ) / 100 } px` ,
215
+ height : `${ Math . round ( Number ( height ) * 100 ) / 100 } px` ,
216
+ } ) ;
217
+
218
+ this . _x = event . clientX ;
219
+ this . _y = event . clientY ;
220
+
221
+ this . _attachDragHandlers ( ) ;
222
+ }
223
+
224
+ _onDragMouseMove ( event ) {
225
+ event . preventDefault ( ) ;
226
+
227
+ const calcX = this . _x - event . clientX ;
228
+ const calcY = this . _y - event . clientY ;
229
+ const {
230
+ left,
231
+ top,
232
+ } = this . getBoundingClientRect ( ) ;
233
+
234
+
235
+ Object . assign ( this . style , {
236
+ left : `${ Math . floor ( left - calcX ) } px` ,
237
+ top : `${ Math . floor ( top - calcY ) } px` ,
238
+ } ) ;
239
+
240
+ this . _x = event . clientX ;
241
+ this . _y = event . clientY ;
242
+ }
243
+
244
+ _onDragMouseUp ( ) {
245
+ this . _x = null ;
246
+ this . _y = null ;
247
+
248
+ this . _detachDragHandlers ( ) ;
249
+ }
250
+
251
+ _attachDragHandlers ( ) {
252
+ window . addEventListener ( "mousemove" , this . _dragMouseMoveHandler ) ;
253
+ window . addEventListener ( "mouseup" , this . _dragMouseUpHandler ) ;
254
+ this . addEventListener ( "ui5-before-close" , this . _recenter ) ;
255
+ }
256
+
257
+ _detachDragHandlers ( ) {
258
+ window . removeEventListener ( "mousemove" , this . _dragMouseMoveHandler ) ;
259
+ window . removeEventListener ( "mouseup" , this . _dragMouseUpHandler ) ;
260
+ }
261
+
262
+ _recenter ( ) {
263
+ Object . assign ( this . style , {
264
+ top : "" ,
265
+ left : "" ,
266
+ transform : "" ,
267
+ } ) ;
268
+ this . removeEventListener ( "ui5-before-close" , this . _recenter ) ;
269
+ }
150
270
}
151
271
152
272
Dialog . define ( ) ;
0 commit comments