Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/ve.ui.PositionedTargetToolbar.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface PositionedTargetToolbar class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * UserInterface positioned target toolbar.
9 *
10 * @class
11 * @extends ve.ui.TargetToolbar
12 *
13 * @constructor
14 * @param {ve.init.Target} target
15 * @param {Object} [config] Configuration options
16 * @param {boolean} [config.floatable] Toolbar can float when scrolled off the page
17 */
18 ve.ui.PositionedTargetToolbar = function VeUiPositionedTargetToolbar( target, config ) {
19 config = config || {};
20
21 // Parent constructor
22 ve.ui.PositionedTargetToolbar.super.apply( this, arguments );
23
24 // Change default overlay to be this.$bar, instead of this.$element (T209192)
25 // TODO: Upstream to OOUI
26 if ( !config.$overlay ) {
27 this.$overlay = this.$bar.append( this.$popups );
28 }
29
30 // Properties
31 this.floating = false;
32 this.floatable = !!config.floatable;
33 this.height = 0;
34 this.elementOffset = null;
35 this.onWindowScrollThrottled = ve.throttle( this.onWindowScroll.bind( this ), 250 );
36
37 // Initialization
38 this.$element.addClass( 've-ui-positionedTargetToolbar' );
39 };
40
41 /* Inheritance */
42
43 OO.inheritClass( ve.ui.PositionedTargetToolbar, ve.ui.TargetToolbar );
44
45 /* Methods */
46
47 /**
48 * @inheritdoc
49 */
50 ve.ui.PositionedTargetToolbar.prototype.setup = function ( groups, surface ) {
51 // Parent method
52 ve.ui.PositionedTargetToolbar.super.prototype.setup.apply( this, arguments );
53
54 [ 'above', 'below', 'side', 'inline' ].forEach( ( dialogPosition ) => {
55 const toolbarDialogs = surface.getToolbarDialogs( dialogPosition );
56 if ( this.position === 'bottom' ) {
57 this.$bar.prepend( toolbarDialogs.$element );
58 } else {
59 this.$bar.append( toolbarDialogs.$element );
60 }
61 toolbarDialogs.connect( this, {
62 opening: 'onToolbarDialogsOpeningOrClosing',
63 closing: 'onToolbarDialogsOpeningOrClosing'
64 } );
65 } );
66 if ( this.isFloatable() ) {
67 this.target.$scrollListener[ 0 ].addEventListener( 'scroll', this.onWindowScrollThrottled, { passive: true } );
68 }
69 };
70
71 /**
72 * @inheritdoc
73 */
74 ve.ui.PositionedTargetToolbar.prototype.detach = function () {
75 // Events
76 if ( this.getSurface() ) {
77 [ 'above', 'below', 'side', 'inline' ].forEach( ( dialogPosition ) => {
78 this.getSurface().getToolbarDialogs( dialogPosition ).disconnect( this );
79 this.getSurface().getToolbarDialogs( dialogPosition ).clearWindows();
80 } );
81 }
82 this.target.$scrollListener[ 0 ].removeEventListener( 'scroll', this.onWindowScrollThrottled );
83
84 // Parent method
85 ve.ui.PositionedTargetToolbar.super.prototype.detach.apply( this, arguments );
86 };
87
88 /**
89 * While toolbar floating is enabled,
90 * the toolbar will stick to the top of the screen unless it would be over or under the last visible
91 * branch node in the root of the document being edited, at which point it will stop just above it.
92 *
93 * @inheritdoc
94 */
95 ve.ui.PositionedTargetToolbar.prototype.onWindowResize = function () {
96 ve.ui.Toolbar.super.prototype.onWindowResize.call( this );
97
98 // Update offsets after resize (see #float)
99 this.calculateOffset();
100
101 if ( this.floating ) {
102 this.$bar.css( {
103 left: this.elementOffset.left,
104 right: this.elementOffset.right
105 } );
106 }
107
108 this.onViewportResize();
109 };
110
111 /**
112 * Calculate the left and right offsets of the toolbar
113 */
114 ve.ui.PositionedTargetToolbar.prototype.calculateOffset = function () {
115 this.elementOffset = this.$element.offset();
116 this.elementOffset.right = document.documentElement.clientWidth - this.$element[ 0 ].offsetWidth - this.elementOffset.left;
117 };
118
119 /**
120 * Get height of the toolbar while floating
121 *
122 * @return {number} Height of the toolbar
123 */
124 ve.ui.PositionedTargetToolbar.prototype.getHeight = function () {
125 return this.height;
126 };
127
128 /**
129 * Get toolbar element's offsets
130 *
131 * @return {Object} Toolbar element's offsets
132 */
133 ve.ui.PositionedTargetToolbar.prototype.getElementOffset = function () {
134 if ( !this.elementOffset ) {
135 this.calculateOffset();
136 }
137 return this.elementOffset;
138 };
139
140 /**
141 * Float the toolbar.
142 */
143 ve.ui.PositionedTargetToolbar.prototype.float = function () {
144 if ( !this.floating ) {
145 this.height = this.$bar[ 0 ].offsetHeight;
146 // When switching into floating mode, set the height of the wrapper and
147 // move the bar to the same offset as the in-flow element
148 this.$element
149 .css( 'height', this.height )
150 .addClass( 've-ui-toolbar-floating' );
151 this.$bar.css( {
152 left: this.elementOffset.left,
153 right: this.elementOffset.right
154 } );
155 this.floating = true;
156 this.emit( 'resize' );
157 this.onViewportResize();
158 }
159 };
160
161 /**
162 * Reset the toolbar to it's default non-floating position.
163 */
164 ve.ui.PositionedTargetToolbar.prototype.unfloat = function () {
165 if ( this.floating ) {
166 this.height = 0;
167 this.$element
168 .css( 'height', '' )
169 .removeClass( 've-ui-toolbar-floating' );
170 this.$bar.css( { left: '', right: '' } );
171 this.floating = false;
172 this.emit( 'resize' );
173 this.onViewportResize();
174 }
175 };
176
177 /**
178 * Check if the toolbar is floating
179 *
180 * @return {boolean} The toolbar is floating
181 */
182 ve.ui.PositionedTargetToolbar.prototype.isFloating = function () {
183 return this.floating;
184 };
185
186 /**
187 * Check if the toolbar can float
188 *
189 * @return {boolean} The toolbar can float
190 */
191 ve.ui.PositionedTargetToolbar.prototype.isFloatable = function () {
192 return this.floatable;
193 };
194
195 /**
196 * Handle windows opening or closing in the toolbar window manager.
197 *
198 * @param {OO.ui.Window} win
199 * @param {jQuery.Promise} openingOrClosing
200 * @param {Object} data
201 */
202 ve.ui.PositionedTargetToolbar.prototype.onToolbarDialogsOpeningOrClosing = function ( win, openingOrClosing ) {
203 const $surface = this.getSurface().$element,
204 transitionDuration = OO.ui.theme.getDialogTransitionDuration();
205
206 // win.isOpened before promise means we are closing
207 if ( win.constructor.static.position === 'side' && win.isOpened() ) {
208 // First closing transition
209 $surface.css(
210 $surface.css( 'direction' ) === 'rtl' ? 'margin-left' : 'margin-right',
211 ''
212 );
213 win.$element.css( 'width', '' );
214 }
215
216 openingOrClosing.then( () => {
217 if ( win.constructor.static.position === 'side' ) {
218 // win.isOpened after promise means we are opening
219 if ( win.isOpened() ) {
220 const margin = $surface.css( 'direction' ) === 'rtl' ? 'margin-left' : 'margin-right';
221 const originalMargin = parseFloat( $surface.css( margin ) );
222 const width = win.getSizeProperties().width;
223 this.getSurface().$element
224 .addClass( 've-ui-surface-toolbarDialog-side' )
225 .css( margin, width + originalMargin );
226 win.$element.css( 'width', width );
227 } else {
228 // Second closing transition
229 this.getSurface().$element.removeClass( 've-ui-surface-toolbarDialog-side' );
230 }
231
232 this.onViewportResize();
233 setTimeout( () => {
234 this.onViewportResize();
235 this.getSurface().getView().emit( 'position' );
236 }, transitionDuration );
237 this.getSurface().getView().emit( 'position' );
238 } else if (
239 win.constructor.static.position === 'above' ||
240 win.constructor.static.position === 'below'
241 ) {
242 setTimeout( () => {
243 this.onViewportResize();
244 this.getSurface().getView().emit( 'position' );
245 }, transitionDuration );
246 }
247 // Wait for window transition
248 setTimeout( () => {
249 if ( this.floating ) {
250 // Re-calculate height
251 this.unfloat();
252 this.float();
253 }
254 }, transitionDuration );
255 } );
256 };
257
258 /**
259 * Handle the visible part of the surface viewport change dimensions
260 */
261 ve.ui.PositionedTargetToolbar.prototype.onViewportResize = function () {
262 const surface = this.getSurface();
263
264 if ( !surface ) {
265 return;
266 }
267
268 const sideWindow = surface.getToolbarDialogs( 'side' ).getCurrentWindow();
269
270 if ( sideWindow ) {
271 const viewportDimensions = surface.getViewportDimensions();
272 if ( viewportDimensions ) {
273 sideWindow.$frame.css(
274 'height', Math.min( surface.getBoundingClientRect().height, viewportDimensions.height )
275 );
276 }
277 }
278 };
279
280 /**
281 * Handle window scroll events
282 */
283 ve.ui.PositionedTargetToolbar.prototype.onWindowScroll = function () {
284 if ( !this.floating ) {
285 this.onViewportResize();
286 }
287 };