| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
ve.ui.MWExtensionWindow = function VeUiMWExtensionWindow() { |
| 18 |
this.whitespace = null; |
| 19 |
this.input = null; |
| 20 |
this.originalMwData = null; |
| 21 |
|
| 22 |
this.onChangeHandler = ve.debounce( this.onChange.bind( this ) ); |
| 23 |
}; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
OO.initClass( ve.ui.MWExtensionWindow ); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
ve.ui.MWExtensionWindow.static.allowedEmpty = false; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
ve.ui.MWExtensionWindow.static.selfCloseEmptyBody = false; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
ve.ui.MWExtensionWindow.static.dir = null; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
ve.ui.MWExtensionWindow.prototype.initialize = function () { |
| 68 |
this.input = new ve.ui.WhitespacePreservingTextInputWidget( { |
| 69 |
limit: 1, |
| 70 |
classes: [ 've-ui-mwExtensionWindow-input' ] |
| 71 |
} ); |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
ve.ui.MWExtensionWindow.prototype.getInputPlaceholder = function () { |
| 80 |
return ''; |
| 81 |
}; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
ve.ui.MWExtensionWindow.prototype.getSetupProcess = function ( data, process ) { |
| 87 |
data = data || {}; |
| 88 |
return process.next( () => { |
| 89 |
|
| 90 |
this.whitespace = [ '', '' ]; |
| 91 |
|
| 92 |
if ( this.selectedNode ) { |
| 93 |
const mwData = this.selectedNode.getAttribute( 'mw' ); |
| 94 |
|
| 95 |
this.input.setValueAndWhitespace( ( mwData.body && mwData.body.extsrc ) || '' ); |
| 96 |
this.originalMwData = mwData; |
| 97 |
} else { |
| 98 |
if ( !this.constructor.static.modelClasses[ 0 ].static.isContent ) { |
| 99 |
|
| 100 |
this.input.setWhitespace( [ '\n', '\n' ] ); |
| 101 |
} |
| 102 |
this.input.setValue( '' ); |
| 103 |
} |
| 104 |
|
| 105 |
this.input.$input.attr( 'placeholder', this.getInputPlaceholder() ); |
| 106 |
|
| 107 |
const dir = this.constructor.static.dir || data.dir; |
| 108 |
this.input.setDir( dir ); |
| 109 |
this.input.setReadOnly( this.isReadOnly() ); |
| 110 |
|
| 111 |
this.actions.setAbilities( { done: false } ); |
| 112 |
this.input.connect( this, { change: 'onChangeHandler' } ); |
| 113 |
} ); |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
ve.ui.MWExtensionWindow.prototype.getReadyProcess = function ( data, process ) { |
| 120 |
return process; |
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
ve.ui.MWExtensionWindow.prototype.getTeardownProcess = function ( data, process ) { |
| 127 |
return process.next( () => { |
| 128 |
|
| 129 |
this.originalMwData = null; |
| 130 |
this.input.disconnect( this, { change: 'onChangeHandler' } ); |
| 131 |
} ); |
| 132 |
}; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
ve.ui.MWExtensionWindow.prototype.getActionProcess = function ( action, process ) { |
| 138 |
return process.first( () => { |
| 139 |
if ( action === 'done' ) { |
| 140 |
if ( this.constructor.static.allowedEmpty || this.input.getValue() !== '' ) { |
| 141 |
this.insertOrUpdateNode(); |
| 142 |
} else if ( this.selectedNode && !this.constructor.static.allowedEmpty ) { |
| 143 |
|
| 144 |
|
| 145 |
this.removeNode(); |
| 146 |
} |
| 147 |
} |
| 148 |
} ); |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
ve.ui.MWExtensionWindow.prototype.onChange = function () { |
| 155 |
this.updateActions(); |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
ve.ui.MWExtensionWindow.prototype.updateActions = function () { |
| 162 |
this.actions.setAbilities( { done: this.isSaveable() } ); |
| 163 |
}; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
ve.ui.MWExtensionWindow.prototype.isSaveable = function () { |
| 173 |
let modified; |
| 174 |
if ( this.originalMwData ) { |
| 175 |
const mwDataCopy = ve.copy( this.originalMwData ); |
| 176 |
this.updateMwData( mwDataCopy ); |
| 177 |
modified = !ve.compare( this.originalMwData, mwDataCopy ); |
| 178 |
} else { |
| 179 |
modified = true; |
| 180 |
} |
| 181 |
return modified; |
| 182 |
}; |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
ve.ui.MWExtensionWindow.prototype.isModified = ve.ui.MWExtensionWindow.prototype.isSaveable; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
ve.ui.MWExtensionWindow.prototype.hasMeaningfulEdits = function () { |
| 200 |
let mwDataBaseline; |
| 201 |
if ( this.originalMwData ) { |
| 202 |
mwDataBaseline = this.originalMwData; |
| 203 |
} else { |
| 204 |
mwDataBaseline = this.getNewElement().attributes.mw; |
| 205 |
} |
| 206 |
const mwDataCopy = ve.copy( mwDataBaseline ); |
| 207 |
this.updateMwData( mwDataCopy ); |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
const changed = OO.getProp( mwDataCopy, 'body', 'extsrc' ); |
| 217 |
if ( changed !== undefined ) { |
| 218 |
OO.setProp( mwDataCopy, 'body', 'extsrc', changed.trim() ); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
const baselineChanged = OO.getProp( mwDataBaseline, 'body', 'extsrc' ); |
| 225 |
if ( baselineChanged !== undefined ) { |
| 226 |
OO.setProp( mwDataBaseline, 'body', 'extsrc', baselineChanged.trim() ); |
| 227 |
} |
| 228 |
|
| 229 |
return !ve.compare( mwDataBaseline, mwDataCopy ); |
| 230 |
}; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
ve.ui.MWExtensionWindow.prototype.getNewElement = function () { |
| 238 |
|
| 239 |
|
| 240 |
const modelClass = this.constructor.static.modelClasses[ 0 ]; |
| 241 |
return { |
| 242 |
type: modelClass.static.name, |
| 243 |
attributes: { |
| 244 |
mw: { |
| 245 |
name: modelClass.static.extensionName, |
| 246 |
attrs: {}, |
| 247 |
body: { |
| 248 |
extsrc: '' |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
}; |
| 253 |
}; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
ve.ui.MWExtensionWindow.prototype.insertOrUpdateNode = function () { |
| 259 |
const surfaceModel = this.getFragment().getSurface(); |
| 260 |
if ( this.selectedNode ) { |
| 261 |
const mwData = ve.copy( this.selectedNode.getAttribute( 'mw' ) ); |
| 262 |
this.updateMwData( mwData ); |
| 263 |
surfaceModel.change( |
| 264 |
ve.dm.TransactionBuilder.static.newFromAttributeChanges( |
| 265 |
surfaceModel.getDocument(), |
| 266 |
this.selectedNode.getOuterRange().start, |
| 267 |
{ mw: mwData } |
| 268 |
) |
| 269 |
); |
| 270 |
} else { |
| 271 |
const element = this.getNewElement(); |
| 272 |
this.updateMwData( element.attributes.mw ); |
| 273 |
|
| 274 |
this.fragment = this.getFragment().collapseToEnd(); |
| 275 |
this.getFragment().insertContent( [ |
| 276 |
element, |
| 277 |
{ type: '/' + element.type } |
| 278 |
] ); |
| 279 |
} |
| 280 |
}; |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
ve.ui.MWExtensionWindow.prototype.removeNode = function () { |
| 286 |
this.getFragment().removeContent(); |
| 287 |
}; |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
ve.ui.MWExtensionWindow.prototype.updateMwData = function ( mwData ) { |
| 295 |
const tagName = mwData.name; |
| 296 |
let value = this.input.getValueAndWhitespace(); |
| 297 |
|
| 298 |
|
| 299 |
'. |
| 300 |
|
| 301 |
value = value.replace( new RegExp( '<(/' + tagName + '\\s*>)', 'gi' ), '<$1' ); |
| 302 |
|
| 303 |
if ( value.trim() === '' && this.constructor.static.selfCloseEmptyBody ) { |
| 304 |
delete mwData.body; |
| 305 |
} else { |
| 306 |
mwData.body = mwData.body || {}; |
| 307 |
mwData.body.extsrc = value; |
| 308 |
} |
| 309 |
}; |