| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
ve.ui.Toolbar = function VeUiToolbar( config ) { |
| 17 |
config = config || {}; |
| 18 |
|
| 19 |
|
| 20 |
ve.ui.Toolbar.super.call( this, ve.ui.toolFactory, ve.ui.toolGroupFactory, config ); |
| 21 |
|
| 22 |
this.updateToolStateDebounced = ve.debounce( this.updateToolState.bind( this ) ); |
| 23 |
|
| 24 |
this.groups = null; |
| 25 |
|
| 26 |
this.contextDirection = { inline: 'ltr', block: 'ltr' }; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
this.$element |
| 33 |
.addClass( 've-ui-toolbar' ) |
| 34 |
.addClass( 've-ui-dir-inline-' + this.contextDirection.inline ) |
| 35 |
.addClass( 've-ui-dir-block-' + this.contextDirection.block ); |
| 36 |
}; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
OO.inheritClass( ve.ui.Toolbar, OO.ui.Toolbar ); |
| 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 |
ve.ui.Toolbar.prototype.setup = function ( groups, surface ) { |
| 72 |
let oldSurface, |
| 73 |
surfaceChange = false; |
| 74 |
|
| 75 |
this.detach(); |
| 76 |
|
| 77 |
if ( surface !== this.surface ) { |
| 78 |
|
| 79 |
oldSurface = this.surface; |
| 80 |
this.surface = surface; |
| 81 |
surfaceChange = true; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
if ( groups !== this.groups ) { |
| 87 |
|
| 88 |
groups = groups.map( ( group ) => { |
| 89 |
if ( group.name ) { |
| 90 |
group.classes = group.classes || []; |
| 91 |
group.classes.push( 've-ui-toolbar-group-' + group.name ); |
| 92 |
} else { |
| 93 |
OO.ui.warnDeprecation( 'No name: ' + JSON.stringify( group ) ); |
| 94 |
} |
| 95 |
return group; |
| 96 |
} ); |
| 97 |
ve.ui.Toolbar.super.prototype.setup.call( this, groups ); |
| 98 |
} |
| 99 |
|
| 100 |
this.groups = groups; |
| 101 |
|
| 102 |
if ( groups.length ) { |
| 103 |
this.$element.removeClass( 've-ui-toolbar-empty' ); |
| 104 |
} else { |
| 105 |
this.$element.addClass( 've-ui-toolbar-empty' ); |
| 106 |
} |
| 107 |
|
| 108 |
if ( surfaceChange ) { |
| 109 |
|
| 110 |
this.emit( 'surfaceChange', oldSurface, surface ); |
| 111 |
|
| 112 |
this.emit( 'resize' ); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
this.getSurface().getModel().connect( this, { contextChange: 'onContextChange' } ); |
| 117 |
this.getSurface().getDialogs().connect( this, { |
| 118 |
opening: 'onInspectorOrDialogOpeningOrClosing', |
| 119 |
closing: 'onInspectorOrDialogOpeningOrClosing' |
| 120 |
} ); |
| 121 |
this.getSurface().getToolbarDialogs().connect( this, { |
| 122 |
opening: 'onInspectorOrDialogOpeningOrClosing', |
| 123 |
closing: 'onInspectorOrDialogOpeningOrClosing' |
| 124 |
} ); |
| 125 |
this.getSurface().getContext().getInspectors().connect( this, { |
| 126 |
opening: 'onInspectorOrDialogOpeningOrClosing', |
| 127 |
closing: 'onInspectorOrDialogOpeningOrClosing' |
| 128 |
} ); |
| 129 |
}; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
ve.ui.Toolbar.prototype.isToolAvailable = function ( name ) { |
| 135 |
if ( !ve.ui.Toolbar.super.prototype.isToolAvailable.apply( this, arguments ) ) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
const tool = this.getToolFactory().lookup( name ); |
| 140 |
if ( !tool ) { |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
const commandName = tool.static.commandName; |
| 145 |
return !commandName || this.getCommands().indexOf( commandName ) !== -1; |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
ve.ui.Toolbar.prototype.onInspectorOrDialogOpeningOrClosing = function ( win, openingOrClosing ) { |
| 156 |
openingOrClosing.then( () => { |
| 157 |
this.updateToolStateDebounced(); |
| 158 |
} ); |
| 159 |
}; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
ve.ui.Toolbar.prototype.onContextChange = function () { |
| 167 |
this.updateToolStateDebounced(); |
| 168 |
}; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
ve.ui.Toolbar.prototype.updateToolState = function () { |
| 176 |
if ( !this.getSurface() ) { |
| 177 |
this.emit( 'updateState', null, null ); |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
const fragment = this.getSurface().getModel().getFragment(); |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
let dirInline = this.surface.getView().getSelectionDirectionality(); |
| 187 |
const dirBlock = dirInline; |
| 188 |
|
| 189 |
|
| 190 |
const fragmentAnnotation = fragment.getAnnotations(); |
| 191 |
if ( fragmentAnnotation.hasAnnotationWithName( 'meta/language' ) ) { |
| 192 |
dirInline = fragmentAnnotation.getAnnotationsByName( 'meta/language' ).get( 0 ).getAttribute( 'dir' ); |
| 193 |
} |
| 194 |
|
| 195 |
if ( dirInline !== this.contextDirection.inline ) { |
| 196 |
|
| 197 |
this.$element.removeClass( 've-ui-dir-inline-rtl ve-ui-dir-inline-ltr' ); |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
this.$element.addClass( 've-ui-dir-inline-' + dirInline ); |
| 202 |
this.contextDirection.inline = dirInline; |
| 203 |
} |
| 204 |
if ( dirBlock !== this.contextDirection.block ) { |
| 205 |
this.$element.removeClass( 've-ui-dir-block-rtl ve-ui-dir-block-ltr' ); |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
this.$element.addClass( 've-ui-dir-block-' + dirBlock ); |
| 210 |
this.contextDirection.block = dirBlock; |
| 211 |
} |
| 212 |
|
| 213 |
const activeDialogs = [ |
| 214 |
this.surface.getDialogs(), |
| 215 |
this.surface.getContext().getInspectors(), |
| 216 |
this.surface.getToolbarDialogs() |
| 217 |
].map( ( windowManager ) => { |
| 218 |
if ( windowManager.getCurrentWindow() ) { |
| 219 |
return windowManager.getCurrentWindow().constructor.static.name; |
| 220 |
} |
| 221 |
return null; |
| 222 |
} ).filter( ( name ) => name !== null ); |
| 223 |
|
| 224 |
this.emit( 'updateState', fragment, this.contextDirection, activeDialogs ); |
| 225 |
}; |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
ve.ui.Toolbar.prototype.getTriggers = function ( name ) { |
| 234 |
return this.getSurface().triggerListener.getTriggers( name ); |
| 235 |
}; |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
ve.ui.Toolbar.prototype.getCommands = function () { |
| 243 |
return this.getSurface().getCommands(); |
| 244 |
}; |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
ve.ui.Toolbar.prototype.getToolAccelerator = function ( name ) { |
| 250 |
const messages = ve.ui.triggerRegistry.getMessages( name ); |
| 251 |
|
| 252 |
return messages ? messages.join( ', ' ) : undefined; |
| 253 |
}; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
ve.ui.Toolbar.prototype.getSurface = function () { |
| 263 |
return this.surface; |
| 264 |
}; |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
ve.ui.Toolbar.prototype.detach = function () { |
| 270 |
|
| 271 |
if ( this.getSurface() ) { |
| 272 |
this.getSurface().getModel().disconnect( this ); |
| 273 |
this.surface = null; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
this.setNarrow( false ); |
| 279 |
this.narrowThreshold = null; |
| 280 |
}; |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
ve.ui.Toolbar.prototype.destroy = function () { |
| 288 |
|
| 289 |
ve.ui.Toolbar.super.prototype.destroy.call( this ); |
| 290 |
|
| 291 |
|
| 292 |
this.detach(); |
| 293 |
}; |