| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
ve.ui.LinearContext = function VeUiLinearContext() { |
| 19 |
|
| 20 |
ve.ui.LinearContext.super.apply( this, arguments ); |
| 21 |
|
| 22 |
|
| 23 |
this.inspector = null; |
| 24 |
this.inspectors = this.createInspectorWindowManager(); |
| 25 |
this.isOpening = false; |
| 26 |
this.lastSelectedNode = null; |
| 27 |
this.afterContextChangeTimeout = null; |
| 28 |
this.afterContextChangeHandler = this.afterContextChange.bind( this ); |
| 29 |
this.updateDimensionsDebounced = ve.debounce( this.updateDimensions.bind( this ) ); |
| 30 |
this.persistentSources = []; |
| 31 |
|
| 32 |
|
| 33 |
this.surface.getModel().connect( this, { |
| 34 |
contextChange: 'onContextChange', |
| 35 |
documentUpdate: 'onDocumentUpdate' |
| 36 |
} ); |
| 37 |
this.inspectors.connect( this, { opening: 'onInspectorOpening' } ); |
| 38 |
}; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
OO.inheritClass( ve.ui.LinearContext, ve.ui.Context ); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
ve.ui.LinearContext.prototype.onContextChange = function () { |
| 60 |
if ( this.inspector && ( this.inspector.isOpening() || this.inspector.isClosing() ) ) { |
| 61 |
|
| 62 |
clearTimeout( this.afterContextChangeTimeout ); |
| 63 |
this.afterContextChangeTimeout = null; |
| 64 |
this.lastSelectedNode = this.surface.getModel().getSelectedNode(); |
| 65 |
} else { |
| 66 |
if ( this.afterContextChangeTimeout === null ) { |
| 67 |
|
| 68 |
this.afterContextChangeTimeout = setTimeout( this.afterContextChangeHandler ); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
this.relatedSources = null; |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
ve.ui.LinearContext.prototype.onDocumentUpdate = function () { |
| 79 |
|
| 80 |
if ( this.isVisible() && !this.isEmpty() ) { |
| 81 |
|
| 82 |
this.onContextChange(); |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
ve.ui.LinearContext.prototype.afterContextChange = function () { |
| 90 |
const selectedNode = this.surface.getModel().getSelectedNode(); |
| 91 |
|
| 92 |
|
| 93 |
this.afterContextChangeTimeout = null; |
| 94 |
|
| 95 |
if ( this.isVisible() ) { |
| 96 |
if ( !this.isEmpty() ) { |
| 97 |
if ( this.isInspectable() ) { |
| 98 |
|
| 99 |
|
| 100 |
const previousItems = this.items.slice(); |
| 101 |
this.teardownMenuItems(); |
| 102 |
this.setupMenuItems( previousItems ); |
| 103 |
this.updateDimensionsDebounced(); |
| 104 |
} else { |
| 105 |
|
| 106 |
this.toggleMenu( false ); |
| 107 |
this.toggle( false ); |
| 108 |
} |
| 109 |
} else if ( |
| 110 |
this.inspector && |
| 111 |
( !selectedNode || ( selectedNode !== this.lastSelectedNode ) ) |
| 112 |
) { |
| 113 |
|
| 114 |
|
| 115 |
this.inspector.close(); |
| 116 |
} |
| 117 |
} else { |
| 118 |
if ( this.isInspectable() ) { |
| 119 |
|
| 120 |
this.toggleMenu( true ); |
| 121 |
this.toggle( true ); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
this.lastSelectedNode = selectedNode; |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
ve.ui.LinearContext.prototype.onInspectorOpening = function ( win, opening ) { |
| 138 |
const observer = this.surface.getView().surfaceObserver; |
| 139 |
|
| 140 |
this.isOpening = true; |
| 141 |
this.inspector = win; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
observer.pollOnce(); |
| 147 |
observer.stopTimerLoop(); |
| 148 |
|
| 149 |
opening |
| 150 |
.progress( ( data ) => { |
| 151 |
this.isOpening = false; |
| 152 |
if ( data.state === 'setup' ) { |
| 153 |
if ( !this.isVisible() ) { |
| 154 |
|
| 155 |
this.toggle( true ); |
| 156 |
} |
| 157 |
if ( !this.isEmpty() ) { |
| 158 |
|
| 159 |
this.toggleMenu( false ); |
| 160 |
} |
| 161 |
} |
| 162 |
this.updateDimensionsDebounced(); |
| 163 |
} ) |
| 164 |
.then( ( opened ) => { |
| 165 |
opened.then( ( closed ) => { |
| 166 |
closed.always( () => { |
| 167 |
|
| 168 |
|
| 169 |
if ( this.isOpening ) { |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
this.inspector = null; |
| 174 |
|
| 175 |
|
| 176 |
observer.startTimerLoop(); |
| 177 |
|
| 178 |
if ( this.isInspectable() ) { |
| 179 |
|
| 180 |
this.toggleMenu( true ); |
| 181 |
this.updateDimensionsDebounced(); |
| 182 |
} else { |
| 183 |
|
| 184 |
this.toggle( false ); |
| 185 |
} |
| 186 |
} ); |
| 187 |
} ); |
| 188 |
} ); |
| 189 |
}; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
ve.ui.LinearContext.prototype.isVisible = function () { |
| 197 |
return this.visible; |
| 198 |
}; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
ve.ui.LinearContext.prototype.isInspectable = function () { |
| 206 |
return !!this.getRelatedSources().length; |
| 207 |
}; |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
ve.ui.LinearContext.prototype.addPersistentSource = function ( source ) { |
| 216 |
this.persistentSources.push( |
| 217 |
ve.extendObject( { type: 'persistent' }, source ) |
| 218 |
); |
| 219 |
|
| 220 |
this.onContextChange(); |
| 221 |
}; |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
ve.ui.LinearContext.prototype.removePersistentSource = function ( name ) { |
| 229 |
this.persistentSources = this.persistentSources.filter( ( source ) => source.name !== name ); |
| 230 |
|
| 231 |
this.onContextChange(); |
| 232 |
}; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
ve.ui.LinearContext.prototype.getRelatedSources = function () { |
| 238 |
const surfaceModel = this.surface.getModel(), |
| 239 |
selection = surfaceModel.getSelection(); |
| 240 |
let selectedModels = []; |
| 241 |
|
| 242 |
if ( !this.relatedSources ) { |
| 243 |
this.relatedSources = []; |
| 244 |
if ( selection instanceof ve.dm.LinearSelection ) { |
| 245 |
selectedModels = this.surface.getView().getSelectedModels(); |
| 246 |
} else if ( selection instanceof ve.dm.TableSelection ) { |
| 247 |
selectedModels = [ surfaceModel.getSelectedNode() ]; |
| 248 |
} |
| 249 |
if ( selectedModels.length ) { |
| 250 |
this.relatedSources = this.getRelatedSourcesFromModels( selectedModels ); |
| 251 |
} |
| 252 |
this.relatedSources.push( ...this.persistentSources ); |
| 253 |
} |
| 254 |
|
| 255 |
return this.relatedSources; |
| 256 |
}; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
ve.ui.LinearContext.prototype.getRelatedSourcesFromModels = function ( selectedModels ) { |
| 265 |
const models = [], |
| 266 |
relatedSources = [], |
| 267 |
items = ve.ui.contextItemFactory.getRelatedItems( selectedModels ); |
| 268 |
|
| 269 |
let i, len; |
| 270 |
for ( i = 0, len = items.length; i < len; i++ ) { |
| 271 |
if ( !items[ i ].model.isInspectable() ) { |
| 272 |
continue; |
| 273 |
} |
| 274 |
if ( ve.ui.contextItemFactory.isExclusive( items[ i ].name ) ) { |
| 275 |
models.push( items[ i ].model ); |
| 276 |
} |
| 277 |
relatedSources.push( { |
| 278 |
type: 'item', |
| 279 |
embeddable: ve.ui.contextItemFactory.isEmbeddable( items[ i ].name ), |
| 280 |
name: items[ i ].name, |
| 281 |
model: items[ i ].model |
| 282 |
} ); |
| 283 |
} |
| 284 |
const tools = ve.ui.toolFactory.getRelatedItems( selectedModels ); |
| 285 |
for ( i = 0, len = tools.length; i < len; i++ ) { |
| 286 |
if ( !tools[ i ].model.isInspectable() ) { |
| 287 |
continue; |
| 288 |
} |
| 289 |
if ( models.indexOf( tools[ i ].model ) === -1 ) { |
| 290 |
const toolClass = ve.ui.toolFactory.lookup( tools[ i ].name ); |
| 291 |
relatedSources.push( { |
| 292 |
type: 'tool', |
| 293 |
embeddable: !toolClass || toolClass.static.makesEmbeddableContextItem, |
| 294 |
name: tools[ i ].name, |
| 295 |
model: tools[ i ].model |
| 296 |
} ); |
| 297 |
} |
| 298 |
} |
| 299 |
return relatedSources; |
| 300 |
}; |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
ve.ui.LinearContext.prototype.getInspectors = function () { |
| 308 |
return this.inspectors; |
| 309 |
}; |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
ve.ui.LinearContext.prototype.createInspectorWindowManager = null; |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
ve.ui.LinearContext.prototype.destroy = function () { |
| 323 |
|
| 324 |
this.surface.getModel().disconnect( this ); |
| 325 |
this.inspectors.disconnect( this ); |
| 326 |
|
| 327 |
|
| 328 |
this.inspectors.destroy(); |
| 329 |
|
| 330 |
|
| 331 |
clearTimeout( this.afterContextChangeTimeout ); |
| 332 |
|
| 333 |
|
| 334 |
return ve.ui.LinearContext.super.prototype.destroy.call( this ); |
| 335 |
}; |