| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
ve.ui.PositionedTargetToolbar = function VeUiPositionedTargetToolbar( target, config ) { |
| 19 |
config = config || {}; |
| 20 |
|
| 21 |
|
| 22 |
ve.ui.PositionedTargetToolbar.super.apply( this, arguments ); |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
if ( !config.$overlay ) { |
| 27 |
this.$overlay = this.$bar.append( this.$popups ); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 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 |
|
| 38 |
this.$element.addClass( 've-ui-positionedTargetToolbar' ); |
| 39 |
}; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
OO.inheritClass( ve.ui.PositionedTargetToolbar, ve.ui.TargetToolbar ); |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
ve.ui.PositionedTargetToolbar.prototype.setup = function ( groups, surface ) { |
| 51 |
|
| 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 |
|
| 73 |
|
| 74 |
ve.ui.PositionedTargetToolbar.prototype.detach = function () { |
| 75 |
|
| 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 |
|
| 85 |
ve.ui.PositionedTargetToolbar.super.prototype.detach.apply( this, arguments ); |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
ve.ui.PositionedTargetToolbar.prototype.onWindowResize = function () { |
| 96 |
ve.ui.Toolbar.super.prototype.onWindowResize.call( this ); |
| 97 |
|
| 98 |
|
| 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 |
|
| 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 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
ve.ui.PositionedTargetToolbar.prototype.getHeight = function () { |
| 125 |
return this.height; |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
ve.ui.PositionedTargetToolbar.prototype.getElementOffset = function () { |
| 134 |
if ( !this.elementOffset ) { |
| 135 |
this.calculateOffset(); |
| 136 |
} |
| 137 |
return this.elementOffset; |
| 138 |
}; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
ve.ui.PositionedTargetToolbar.prototype.float = function () { |
| 144 |
if ( !this.floating ) { |
| 145 |
this.height = this.$bar[ 0 ].offsetHeight; |
| 146 |
|
| 147 |
|
| 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 |
|
| 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 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
ve.ui.PositionedTargetToolbar.prototype.isFloating = function () { |
| 183 |
return this.floating; |
| 184 |
}; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
ve.ui.PositionedTargetToolbar.prototype.isFloatable = function () { |
| 192 |
return this.floatable; |
| 193 |
}; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
ve.ui.PositionedTargetToolbar.prototype.onToolbarDialogsOpeningOrClosing = function ( win, openingOrClosing ) { |
| 203 |
const $surface = this.getSurface().$element, |
| 204 |
transitionDuration = OO.ui.theme.getDialogTransitionDuration(); |
| 205 |
|
| 206 |
|
| 207 |
if ( win.constructor.static.position === 'side' && win.isOpened() ) { |
| 208 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 248 |
setTimeout( () => { |
| 249 |
if ( this.floating ) { |
| 250 |
|
| 251 |
this.unfloat(); |
| 252 |
this.float(); |
| 253 |
} |
| 254 |
}, transitionDuration ); |
| 255 |
} ); |
| 256 |
}; |
| 257 |
|
| 258 |
|
| 259 |
|
| 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 |
|
| 282 |
|
| 283 |
ve.ui.PositionedTargetToolbar.prototype.onWindowScroll = function () { |
| 284 |
if ( !this.floating ) { |
| 285 |
this.onViewportResize(); |
| 286 |
} |
| 287 |
}; |