This repository has been archived by the owner on Nov 16, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
balloonpanelview.js
361 lines (319 loc) · 10.3 KB
/
balloonpanelview.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
* @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/* globals window, document, Range, HTMLElement */
import View from '../view.js';
import Template from '../template.js';
import toUnit from '../../utils/dom/tounit.js';
const toPx = toUnit( 'px' );
const arrowLeftOffset = 30;
const arrowTopOffset = 15;
/**
* The balloon panel view class.
*
* See {@link ui.balloonPanel.BalloonPanel}.
*
* @memberOf ui.balloonPanel
* @extends ui.View
*/
export default class BalloonPanelView extends View {
/**
* @inheritDoc
*/
constructor( locale ) {
super( locale );
const bind = this.bindTemplate;
this.template = new Template( {
tag: 'div',
attributes: {
class: [
'ck-balloon-panel',
'ck-link-balloon-panel',
bind.to( 'arrow', ( value ) => `ck-balloon-panel_arrow_${ value }` ),
bind.if( 'isVisible', 'ck-balloon-panel_visible' )
],
style: {
top: bind.to( 'top', toPx ),
left: bind.to( 'left', toPx ),
maxWidth: bind.to( 'maxWidth', toPx )
},
// Make this element `focusable` to be available for adding to FocusTracker.
tabindex: -1
}
} );
this.register( 'content', el => el );
/**
* The absolute top position of the balloon panel in pixels.
*
* @observable
* @member {Number} ui.balloonPanel.BalloonPanelView#top
*/
/**
* The absolute left position of the balloon panel in pixels.
*
* @observable
* @member {Number} ui.balloonPanel.BalloonPanelView#left
*/
/**
* The maximum width of the balloon panel, as in CSS.
*
* @observable
* @member {Number} ui.balloonPanel.BalloonPanelView#maxWidth
*/
/**
* Balloon panel arrow direction.
*
* @observable
* @member {'se'|'sw'|'ne'|'nw'} ui.balloonPanel.BalloonPanelView#arrow
*/
/**
* Controls whether the balloon panel is visible or not.
*
* @observable
* @member {Boolean} ui.balloonPanel.BalloonPanelView#isVisible
*/
}
/**
* Shows the balloon panel.
*
* See {@link ui.balloonPanel.BalloonPanelView#isVisible}.
*/
show() {
this.isVisible = true;
}
/**
* Hides the balloon panel.
*
* See {@link ui.balloonPanel.BalloonPanelView#isVisible}.
*/
hide() {
this.isVisible = false;
}
/**
* Attaches the balloon panel to a specified DOM element or range with a smart heuristics.
*
* **Notes**:
*
* * The algorithm takes the geometry of the "limiter element" into consideration so,
* if possible, the balloon is positioned within the rect of that element.
* * If possible, the balloon is positioned within the area of the "limiter element"
* fitting into the browser viewport visible to the user. It prevents the panel from
* appearing off screen.
*
* The heuristics chooses from among 4 available positions relative to the target DOM element or range:
*
* * South east:
*
* [ Target ]
* ^
* +-----------------+
* | |
* +-----------------+
*
*
* * South west:
*
* [ Target ]
* ^
* +-----------------+
* | |
* +-----------------+
*
*
* * North east:
*
* +-----------------+
* | |
* +-----------------+
* V
* [ Target ]
*
*
* * North west:
*
* +-----------------+
* | |
* +-----------------+
* V
* [ Target ]
*
* See {@ link ui.balloonPanel.BalloonPanelView#arrow}.
*
* @param {HTMLElement|Range} elementOrRange Target DOM element or range to which the balloon will be attached.
* @param {HTMLElement} limiterElement The DOM element beyond which area the balloon panel should not be positioned, if possible.
*/
attachTo( elementOrRange, limiterElement ) {
this.show();
const elementOrRangeRect = new AbsoluteDomRect( elementOrRange, 'elementOrRange' );
const panelRect = new AbsoluteDomRect( this.element );
const visibleRect = getAbsoluteRectVisisbleInTheViewport( limiterElement );
// Create a rect for each of the possible balloon positions and feed them to _smartAttachTo,
// which will use whichever is the optimal.
const possiblePanelRects = {
// The absolute rect for "South-East" position.
se: panelRect.clone().moveTo( {
top: elementOrRangeRect.bottom + arrowTopOffset,
left: elementOrRangeRect.left + elementOrRangeRect.width / 2 - arrowLeftOffset
} ),
// The absolute rect for "South west" position.
sw: panelRect.clone().moveTo( {
top: elementOrRangeRect.bottom + arrowTopOffset,
left: elementOrRangeRect.left + elementOrRangeRect.width / 2 - panelRect.width + arrowLeftOffset
} ),
// The absolute rect for "North east" position.
ne: panelRect.clone().moveTo( {
top: elementOrRangeRect.top - panelRect.height - arrowTopOffset,
left: elementOrRangeRect.left + elementOrRangeRect.width / 2 - arrowLeftOffset
} ),
// The absolute rect for "North west" position.
nw: panelRect.clone().moveTo( {
top: elementOrRangeRect.top - panelRect.height - arrowTopOffset,
left: elementOrRangeRect.left + elementOrRangeRect.width / 2 - panelRect.width + arrowLeftOffset
} )
};
this._smartAttachTo( possiblePanelRects, visibleRect );
}
/**
* For the given `Array` of possible rects, choses the one which fits the best into
* `visibleViewportRect`, which is when their intersection has the biggest area.
*
* @private
* @param {Array<{AbsoluteDomRect}>} rects List of positions where balloon can be placed.
* @param {AbsoluteDomRect} visibleViewportRect The absolute rect of the visible part of the browser viewport.
*/
_smartAttachTo( rects, visibleViewportRect ) {
let maxIntersectRectPos;
let maxIntersectArea = -1;
// Get best place.
for ( let rectPos in rects ) {
const intersectArea = rects[ rectPos ].getIntersectArea( visibleViewportRect );
if ( intersectArea > maxIntersectArea ) {
maxIntersectRectPos = rectPos;
maxIntersectArea = intersectArea;
}
}
// Move the balloon panel.
this.arrow = maxIntersectRectPos;
this.top = rects[ maxIntersectRectPos ].top;
this.left = rects[ maxIntersectRectPos ].left;
}
}
/**
* An abstract class which represents a client rect of an HTMLElement or a Range in DOM.
*
* **Note**: The geometry used by each instance corresponds with coordinates of an object
* with `position: absolute` relative to the `<body>` (`document.body`), and hence
* it is useful to manage such objects.
*
* @private
*/
class AbsoluteDomRect {
/**
* Create instance of AbsoluteDomRect class.
*
* @param {HTMLElement|Range|Object} elementOrRangeOrRect Source object to create the rect.
*/
constructor( elementOrRangeOrRect ) {
Object.assign( this, getAbsoluteRect( elementOrRangeOrRect ) );
}
/**
* Clone instance of this class.
*
* @param {String} newName Name of new instance.
* @returns {AbsoluteDomRect}
*/
clone( newName ) {
return new AbsoluteDomRect( this, newName );
}
/**
* Move current box to specified position.
*
* @param {Number} top New to position.
* @param {Number} left New left position.
* @returns {AbsoluteDomRect}
*/
moveTo( { top, left } ) {
this.top = top;
this.right = left + this.width;
this.bottom = top + this.height;
this.left = left;
return this;
}
/**
* Get intersect surface area of this AbsoluteDomRect and other AbsoluteDomRect.
*
* @param {AbsoluteDomRect} rect
* @returns {Number} Overlap surface area.
*/
getIntersectArea( rect ) {
const hOverlap = Math.max( 0, Math.min( this.right, rect.right ) - Math.max( this.left, rect.left ) );
const vOverlap = Math.max( 0, Math.min( this.bottom, rect.bottom ) - Math.max( this.top, rect.top ) );
return hOverlap * vOverlap;
}
}
/**
* Returns the client rect of an HTMLElement, Range, or rect. The obtained geometry of the rect
* corresponds with `position: absolute` relative to the `<body>` (`document.body`).
*
* @private
* @param {HTMLElement|Range|Object} elementOrRangeOrRect Target object witch rect is to be determined.
* @returns {Object} Client rect object.
*/
function getAbsoluteRect( elementOrRangeOrRect ) {
if ( elementOrRangeOrRect instanceof HTMLElement || elementOrRangeOrRect instanceof Range ) {
const elementRect = elementOrRangeOrRect.getBoundingClientRect();
const bodyRect = document.body.getBoundingClientRect();
return {
top: elementRect.top - bodyRect.top,
right: elementRect.right - bodyRect.left,
bottom: elementRect.bottom - bodyRect.top,
left: elementRect.left - bodyRect.left,
width: elementRect.width,
height: elementRect.height
};
}
// The rect has been passed.
const absoluteRect = Object.assign( {}, elementOrRangeOrRect );
if ( absoluteRect.width === undefined ) {
absoluteRect.width = absoluteRect.right - absoluteRect.left;
}
if ( absoluteRect.height === undefined ) {
absoluteRect.height = absoluteRect.bottom - absoluteRect.top;
}
return absoluteRect;
}
/**
* Returns the client rect of the element limited by the visible (to the user)
* viewport of the browser window.
*
* [Browser viewport]
* +---------------------------------------+
* | [Element] |
* | +----------------------+
* | |##############| |
* | |##############| |
* | |#######^######| |
* | +-------|--------------+
* | | |
* +--------------------------------|------+
* |
* \- [Element rect visible in the viewport]
*
* @private
* @param {HTMLElement|Object} element Object which visible area rect is to be determined.
* @returns {AbsoluteDomRect} An absolute rect of the area visible in the viewport.
*/
function getAbsoluteRectVisisbleInTheViewport( element ) {
const limiterRect = new AbsoluteDomRect( element, 'limiterElement' );
const windowScrollX = window.scrollX;
const windowScrollY = window.scrollY;
const bodyWidth = document.body.clientWidth;
const bodyHeight = document.body.clientHeight;
return new AbsoluteDomRect( {
top: Math.max( limiterRect.top, windowScrollY ),
left: Math.max( limiterRect.left, windowScrollX ),
right: Math.min( limiterRect.right, bodyWidth + windowScrollX ),
bottom: Math.min( limiterRect.bottom, bodyHeight + windowScrollY )
} );
}