| 1 |
'use strict'; |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
ve.ui.MWReferenceDialog = function VeUiMWReferenceDialog( config ) { |
| 18 |
|
| 19 |
ve.ui.MWReferenceDialog.super.call( this, config ); |
| 20 |
|
| 21 |
|
| 22 |
this.reuseReference = false; |
| 23 |
}; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
OO.inheritClass( ve.ui.MWReferenceDialog, ve.ui.NodeDialog ); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
ve.ui.MWReferenceDialog.static.name = 'reference'; |
| 32 |
|
| 33 |
ve.ui.MWReferenceDialog.static.title = |
| 34 |
OO.ui.deferMsg( 'cite-ve-dialog-reference-title' ); |
| 35 |
|
| 36 |
ve.ui.MWReferenceDialog.static.actions = [ |
| 37 |
{ |
| 38 |
action: 'done', |
| 39 |
label: OO.ui.deferMsg( 'visualeditor-dialog-action-apply' ), |
| 40 |
flags: [ 'progressive', 'primary' ], |
| 41 |
modes: 'edit' |
| 42 |
}, |
| 43 |
{ |
| 44 |
action: 'insert', |
| 45 |
label: OO.ui.deferMsg( 'visualeditor-dialog-action-insert' ), |
| 46 |
flags: [ 'progressive', 'primary' ], |
| 47 |
modes: 'insert' |
| 48 |
}, |
| 49 |
{ |
| 50 |
label: OO.ui.deferMsg( 'visualeditor-dialog-action-cancel' ), |
| 51 |
flags: [ 'safe', 'close' ], |
| 52 |
modes: [ 'readonly', 'insert', 'edit', 'insert-select' ] |
| 53 |
} |
| 54 |
]; |
| 55 |
|
| 56 |
ve.ui.MWReferenceDialog.static.modelClasses = [ ve.dm.MWReferenceNode ]; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
ve.ui.MWReferenceDialog.prototype.onEditPanelInputChange = function ( change ) { |
| 67 |
this.actions.setAbilities( { |
| 68 |
done: change.isModified, |
| 69 |
insert: change.hasContent |
| 70 |
} ); |
| 71 |
|
| 72 |
if ( !this.trackedInputChange ) { |
| 73 |
ve.track( 'activity.' + this.constructor.static.name, { action: 'input' } ); |
| 74 |
this.trackedInputChange = true; |
| 75 |
} |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
ve.ui.MWReferenceDialog.prototype.onReuseSearchResultsReuse = function ( ref ) { |
| 84 |
if ( this.selectedNode instanceof ve.dm.MWReferenceNode ) { |
| 85 |
this.getFragment().removeContent(); |
| 86 |
this.selectedNode = null; |
| 87 |
} |
| 88 |
|
| 89 |
this.insertReference( ref ); |
| 90 |
|
| 91 |
ve.track( 'activity.' + this.constructor.static.name, { action: 'reuse-choose' } ); |
| 92 |
|
| 93 |
this.close( { action: 'insert' } ); |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
ve.ui.MWReferenceDialog.prototype.onReuseSearchResultsExtends = function ( originalRef ) { |
| 102 |
const newRef = new ve.dm.MWReferenceModel( this.getFragment().getDocument() ); |
| 103 |
newRef.extendsRef = originalRef.getListKey(); |
| 104 |
newRef.group = originalRef.getGroup(); |
| 105 |
|
| 106 |
this.actions.setMode( 'insert' ); |
| 107 |
this.panels.setItem( this.editPanel ); |
| 108 |
this.title.setLabel( ve.msg( 'cite-ve-dialog-reference-title-add-details' ) ); |
| 109 |
|
| 110 |
const docRefs = ve.dm.MWDocumentReferences.static.refsForDoc( |
| 111 |
this.getFragment().getDocument() |
| 112 |
); |
| 113 |
this.editPanel.setDocumentReferences( docRefs ); |
| 114 |
|
| 115 |
this.actions.setAbilities( { insert: false } ); |
| 116 |
|
| 117 |
this.editPanel.setReferenceForEditing( newRef ); |
| 118 |
this.editPanel.setReadOnly( this.isReadOnly() ); |
| 119 |
|
| 120 |
this.trackedInputChange = false; |
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
ve.ui.MWReferenceDialog.prototype.getReadyProcess = function ( data ) { |
| 127 |
return ve.ui.MWReferenceDialog.super.prototype.getReadyProcess.call( this, data ) |
| 128 |
.next( () => { |
| 129 |
if ( this.reuseReference ) { |
| 130 |
this.reuseSearch.getQuery().focus().select(); |
| 131 |
} else { |
| 132 |
this.editPanel.focus(); |
| 133 |
} |
| 134 |
} ); |
| 135 |
}; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
ve.ui.MWReferenceDialog.prototype.getBodyHeight = function () { |
| 141 |
|
| 142 |
return Math.min( |
| 143 |
400, |
| 144 |
Math.max( |
| 145 |
300, |
| 146 |
Math.ceil( this.panels.getCurrentItem().$element[ 0 ].scrollHeight ) |
| 147 |
) |
| 148 |
); |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
ve.ui.MWReferenceDialog.prototype.initialize = function () { |
| 155 |
|
| 156 |
ve.ui.MWReferenceDialog.super.prototype.initialize.call( this ); |
| 157 |
|
| 158 |
|
| 159 |
this.panels = new OO.ui.StackLayout(); |
| 160 |
this.editPanel = new ve.ui.MWReferenceEditPanel( { $overlay: this.$overlay } ); |
| 161 |
this.reuseSearchPanel = new OO.ui.PanelLayout(); |
| 162 |
|
| 163 |
this.reuseSearch = new ve.ui.MWReferenceSearchWidget( { $overlay: this.$overlay } ); |
| 164 |
|
| 165 |
|
| 166 |
this.reuseSearch.connect( this, { |
| 167 |
reuse: 'onReuseSearchResultsReuse', |
| 168 |
extends: 'onReuseSearchResultsExtends' |
| 169 |
} ); |
| 170 |
this.editPanel.connect( this, { change: 'onEditPanelInputChange' } ); |
| 171 |
|
| 172 |
|
| 173 |
this.$content.addClass( 've-ui-mwReferenceDialog' ); |
| 174 |
|
| 175 |
this.panels.addItems( [ this.editPanel, this.reuseSearchPanel ] ); |
| 176 |
this.reuseSearchPanel.$element.append( this.reuseSearch.$element ); |
| 177 |
this.$body.append( this.panels.$element ); |
| 178 |
}; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
ve.ui.MWReferenceDialog.prototype.openReusePanel = function () { |
| 184 |
this.actions.setMode( 'insert-select' ); |
| 185 |
this.reuseSearch.buildIndex(); |
| 186 |
this.panels.setItem( this.reuseSearchPanel ); |
| 187 |
|
| 188 |
|
| 189 |
ve.track( 'activity.' + this.constructor.static.name, { action: 'dialog-open-reuse' } ); |
| 190 |
}; |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
ve.ui.MWReferenceDialog.prototype.insertReference = function ( ref ) { |
| 199 |
const surfaceModel = this.getFragment().getSurface(); |
| 200 |
|
| 201 |
if ( !ref.findInternalItem( surfaceModel ) ) { |
| 202 |
ref.insertInternalItem( surfaceModel ); |
| 203 |
} |
| 204 |
|
| 205 |
this.fragment = this.getFragment().collapseToEnd(); |
| 206 |
ref.insertReferenceNode( this.getFragment() ); |
| 207 |
}; |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
ve.ui.MWReferenceDialog.prototype.getActionProcess = function ( action ) { |
| 213 |
if ( action === 'insert' || action === 'done' ) { |
| 214 |
return new OO.ui.Process( () => { |
| 215 |
const ref = this.editPanel.getReferenceFromEditing(); |
| 216 |
|
| 217 |
if ( !( this.selectedNode instanceof ve.dm.MWReferenceNode ) ) { |
| 218 |
this.insertReference( ref ); |
| 219 |
} |
| 220 |
|
| 221 |
ref.updateInternalItem( this.getFragment().getSurface() ); |
| 222 |
|
| 223 |
this.close( { action: action } ); |
| 224 |
} ); |
| 225 |
} |
| 226 |
return ve.ui.MWReferenceDialog.super.prototype.getActionProcess.call( this, action ); |
| 227 |
}; |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
ve.ui.MWReferenceDialog.prototype.getSetupProcess = function ( data ) { |
| 236 |
data = data || {}; |
| 237 |
return ve.ui.MWReferenceDialog.super.prototype.getSetupProcess.call( this, data ) |
| 238 |
.next( () => { |
| 239 |
this.reuseReference = !!data.reuseReference; |
| 240 |
if ( this.reuseReference ) { |
| 241 |
this.reuseSearch.setInternalList( this.getFragment().getDocument().getInternalList() ); |
| 242 |
this.openReusePanel(); |
| 243 |
} else if ( data.createSubRef ) { |
| 244 |
if ( this.selectedNode instanceof ve.dm.MWReferenceNode && |
| 245 |
this.selectedNode.getAttribute( 'placeholder' ) ) { |
| 246 |
|
| 247 |
this.getFragment().removeContent(); |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
this.selectedNode = null; |
| 252 |
this.onReuseSearchResultsExtends( data.createSubRef ); |
| 253 |
} else { |
| 254 |
this.panels.setItem( this.editPanel ); |
| 255 |
const docRefs = ve.dm.MWDocumentReferences.static.refsForDoc( |
| 256 |
this.getFragment().getDocument() |
| 257 |
); |
| 258 |
this.editPanel.setDocumentReferences( docRefs ); |
| 259 |
|
| 260 |
let ref; |
| 261 |
if ( this.selectedNode instanceof ve.dm.MWReferenceNode ) { |
| 262 |
|
| 263 |
ref = ve.dm.MWReferenceModel.static.newFromReferenceNode( this.selectedNode ); |
| 264 |
if ( ref.extendsRef ) { |
| 265 |
this.title.setLabel( ve.msg( 'cite-ve-dialog-reference-title-edit-details' ) ); |
| 266 |
} |
| 267 |
this.actions.setAbilities( { done: false } ); |
| 268 |
} else { |
| 269 |
|
| 270 |
ref = new ve.dm.MWReferenceModel( this.getFragment().getDocument() ); |
| 271 |
this.actions.setAbilities( { insert: false } ); |
| 272 |
} |
| 273 |
this.editPanel.setReferenceForEditing( ref ); |
| 274 |
this.editPanel.setReadOnly( this.isReadOnly() ); |
| 275 |
} |
| 276 |
|
| 277 |
this.trackedInputChange = false; |
| 278 |
} ); |
| 279 |
}; |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
ve.ui.MWReferenceDialog.prototype.getTeardownProcess = function ( data ) { |
| 285 |
return ve.ui.MWReferenceDialog.super.prototype.getTeardownProcess.call( this, data ) |
| 286 |
.first( () => { |
| 287 |
this.editPanel.clear(); |
| 288 |
this.reuseSearch.clearSearch(); |
| 289 |
} ); |
| 290 |
}; |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
ve.ui.windowFactory.register( ve.ui.MWReferenceDialog ); |