| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
ve.ui.WindowAction = function VeUiWindowAction() { |
| 17 |
|
| 18 |
ve.ui.WindowAction.super.apply( this, arguments ); |
| 19 |
}; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
OO.inheritClass( ve.ui.WindowAction, ve.ui.Action ); |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
ve.ui.WindowAction.static.name = 'window'; |
| 28 |
|
| 29 |
ve.ui.WindowAction.static.methods = [ 'open', 'close', 'toggle' ]; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
ve.ui.WindowAction.prototype.open = function ( name, data, action ) { |
| 42 |
data = data || {}; |
| 43 |
const windowType = this.getWindowType( name ), |
| 44 |
windowManager = this.getWindowManager( windowType ), |
| 45 |
currentWindow = windowManager.getCurrentWindow(), |
| 46 |
autoClosePromises = [], |
| 47 |
surface = this.surface, |
| 48 |
surfaceFragment = data.fragment || surface.getModel().getFragment( undefined, true ), |
| 49 |
dir = surface.getView().getSelectionDirectionality(), |
| 50 |
windowClass = ve.ui.windowFactory.lookup( name ), |
| 51 |
isFragmentWindow = !!windowClass.prototype.getFragment, |
| 52 |
mayRequireFragment = isFragmentWindow || |
| 53 |
|
| 54 |
windowType.name === 'toolbar', |
| 55 |
|
| 56 |
sourceMode = surface.getMode() === 'source' && !windowClass.static.handlesSource, |
| 57 |
openDeferred = ve.createDeferred(), |
| 58 |
openPromise = openDeferred.promise(); |
| 59 |
|
| 60 |
ve.track( |
| 61 |
'activity.' + name, |
| 62 |
{ action: 'window-open-from-' + ( this.source || 'command' ) } |
| 63 |
); |
| 64 |
|
| 65 |
if ( !windowManager ) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
let fragmentPromise; |
| 70 |
let originalFragment; |
| 71 |
if ( !mayRequireFragment ) { |
| 72 |
fragmentPromise = ve.createDeferred().resolve().promise(); |
| 73 |
} else if ( sourceMode ) { |
| 74 |
const text = surfaceFragment.getText( true ); |
| 75 |
originalFragment = surfaceFragment; |
| 76 |
|
| 77 |
fragmentPromise = surfaceFragment.convertFromSource( text ).then( ( selectionDocument ) => { |
| 78 |
const tempSurfaceModel = new ve.dm.Surface( selectionDocument ), |
| 79 |
tempFragment = tempSurfaceModel.getLinearFragment( |
| 80 |
|
| 81 |
new ve.Range( |
| 82 |
1, |
| 83 |
Math.max( 1, selectionDocument.getDocumentRange().end - 1 ) |
| 84 |
) |
| 85 |
); |
| 86 |
|
| 87 |
return tempFragment; |
| 88 |
} ); |
| 89 |
} else { |
| 90 |
fragmentPromise = ve.createDeferred().resolve( surfaceFragment ).promise(); |
| 91 |
} |
| 92 |
|
| 93 |
data = ve.extendObject( { dir: dir }, data, { surface: surface, $returnFocusTo: null } ); |
| 94 |
|
| 95 |
if ( windowType.name === 'toolbar' || windowType.name === 'inspector' ) { |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
if ( currentWindow && currentWindow.constructor.static.name !== name ) { |
| 100 |
autoClosePromises.push( windowManager.closeWindow( currentWindow ).closed ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
if ( windowType.name === 'dialog' ) { |
| 106 |
const inspectorWindowManager = this.getWindowManager( { name: 'inspector' } ); |
| 107 |
const currentInspector = inspectorWindowManager.getCurrentWindow(); |
| 108 |
if ( currentInspector ) { |
| 109 |
autoClosePromises.push( inspectorWindowManager.closeWindow( currentInspector ).closed ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
fragmentPromise.then( ( fragment ) => { |
| 114 |
ve.extendObject( data, { fragment: fragment } ); |
| 115 |
|
| 116 |
ve.promiseAll( autoClosePromises ).always( () => { |
| 117 |
windowManager.getWindow( name ).then( ( win ) => { |
| 118 |
const instance = windowManager.openWindow( win, data ); |
| 119 |
|
| 120 |
if ( sourceMode ) { |
| 121 |
win.sourceMode = sourceMode; |
| 122 |
} |
| 123 |
|
| 124 |
if ( !win.constructor.static.activeSurface ) { |
| 125 |
surface.getView().deactivate( false ); |
| 126 |
} |
| 127 |
|
| 128 |
instance.opened.then( () => { |
| 129 |
if ( sourceMode ) { |
| 130 |
|
| 131 |
win.initialFragment = null; |
| 132 |
win.previousSelection = null; |
| 133 |
} |
| 134 |
} ); |
| 135 |
instance.opened.always( () => { |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
if ( action ) { |
| 140 |
win.executeAction( action ); |
| 141 |
} |
| 142 |
openDeferred.resolve( instance ); |
| 143 |
} ); |
| 144 |
|
| 145 |
if ( !win.constructor.static.activeSurface ) { |
| 146 |
windowManager.once( 'closing', () => { |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
if ( OO.ui.isMobile() && surface.getModel().getSelection().isCollapsed() ) { |
| 151 |
surface.getView().activate(); |
| 152 |
} else { |
| 153 |
|
| 154 |
|
| 155 |
instance.closed.then( () => { |
| 156 |
|
| 157 |
if ( !( OO.ui.isMobile() && !surface.getModel().getSelection().isCollapsed() ) ) { |
| 158 |
surface.getView().activate(); |
| 159 |
} |
| 160 |
} ); |
| 161 |
} |
| 162 |
} ); |
| 163 |
} |
| 164 |
|
| 165 |
instance.closed.then( ( closedData ) => { |
| 166 |
|
| 167 |
if ( data.strippedSequence && !( closedData && closedData.action ) ) { |
| 168 |
surface.getModel().undo(); |
| 169 |
|
| 170 |
surface.getModel().truncateUndoStack(); |
| 171 |
surface.getModel().emit( 'history' ); |
| 172 |
} |
| 173 |
if ( sourceMode && fragment && fragment.getSurface().hasBeenModified() ) { |
| 174 |
|
| 175 |
originalFragment.setAutoSelect( true ); |
| 176 |
originalFragment.insertDocument( fragment.getDocument() ); |
| 177 |
} |
| 178 |
surface.getView().emit( 'position' ); |
| 179 |
} ); |
| 180 |
} ); |
| 181 |
} ); |
| 182 |
} ); |
| 183 |
|
| 184 |
return openPromise; |
| 185 |
}; |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
ve.ui.WindowAction.prototype.close = function ( name, data ) { |
| 195 |
const windowType = this.getWindowType( name ), |
| 196 |
windowManager = this.getWindowManager( windowType ); |
| 197 |
|
| 198 |
if ( !windowManager ) { |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
windowManager.closeWindow( name, data ); |
| 203 |
return true; |
| 204 |
}; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
ve.ui.WindowAction.prototype.toggle = function ( name, data ) { |
| 214 |
const windowType = this.getWindowType( name ), |
| 215 |
windowManager = this.getWindowManager( windowType ); |
| 216 |
|
| 217 |
if ( !windowManager ) { |
| 218 |
return false; |
| 219 |
} |
| 220 |
|
| 221 |
const win = windowManager.getCurrentWindow(); |
| 222 |
if ( !win || win.constructor.static.name !== name ) { |
| 223 |
this.open( name, data ); |
| 224 |
} else { |
| 225 |
this.close( name, data ); |
| 226 |
} |
| 227 |
return true; |
| 228 |
}; |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
ve.ui.WindowAction.prototype.getWindowType = function ( name ) { |
| 244 |
const windowClass = ve.ui.windowFactory.lookup( name ); |
| 245 |
if ( !windowClass ) { |
| 246 |
throw new Error( 'No window class registered with the name "' + name + '"' ); |
| 247 |
} |
| 248 |
if ( windowClass.prototype instanceof ve.ui.FragmentInspector ) { |
| 249 |
return { name: 'inspector' }; |
| 250 |
} else if ( windowClass.prototype instanceof ve.ui.ToolbarDialog ) { |
| 251 |
return { |
| 252 |
name: 'toolbar', |
| 253 |
position: windowClass.static.position |
| 254 |
}; |
| 255 |
} else if ( windowClass.prototype instanceof OO.ui.Dialog ) { |
| 256 |
return { name: 'dialog' }; |
| 257 |
} |
| 258 |
return { name: null }; |
| 259 |
}; |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
ve.ui.WindowAction.prototype.getWindowManager = function ( windowType ) { |
| 268 |
switch ( windowType.name ) { |
| 269 |
case 'inspector': |
| 270 |
return this.surface.getContext().getInspectors(); |
| 271 |
case 'toolbar': |
| 272 |
return this.surface.getToolbarDialogs( windowType.position ); |
| 273 |
case 'dialog': |
| 274 |
return this.surface.getDialogs(); |
| 275 |
} |
| 276 |
return null; |
| 277 |
}; |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
ve.ui.actionFactory.register( ve.ui.WindowAction ); |