| 1 |
const Dialog = require( './Dialog.js' ), |
| 2 |
DataModule = require( 'ext.templateDataGenerator.data' ), |
| 3 |
Model = DataModule.Model, |
| 4 |
SourceHandler = DataModule.SourceHandler; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
function Target( $textarea, config ) { |
| 18 |
|
| 19 |
Target.super.call( this, config ); |
| 20 |
|
| 21 |
|
| 22 |
OO.EventEmitter.call( this ); |
| 23 |
|
| 24 |
this.pageName = config.pageName; |
| 25 |
this.parentPage = config.parentPage; |
| 26 |
this.isPageSubLevel = !!config.isPageSubLevel; |
| 27 |
this.isDocPage = !!config.isDocPage; |
| 28 |
this.docSubpage = config.docSubpage; |
| 29 |
this.$textarea = $textarea; |
| 30 |
|
| 31 |
this.editOpenDialogButton = new OO.ui.ButtonWidget( { |
| 32 |
label: mw.msg( 'templatedata-editbutton' ) |
| 33 |
} ); |
| 34 |
|
| 35 |
this.editNoticeMessage = new OO.ui.MessageWidget( { |
| 36 |
classes: [ 'tdg-editscreen-edit-notice' ] |
| 37 |
} ) |
| 38 |
.toggle( false ); |
| 39 |
|
| 40 |
const $helpLink = $( '<a>' ) |
| 41 |
.attr( { |
| 42 |
href: mw.msg( 'templatedata-helplink-target' ), |
| 43 |
target: '_blank' |
| 44 |
} ) |
| 45 |
.addClass( 'tdg-editscreen-main-helplink' ) |
| 46 |
.text( mw.msg( 'templatedata-helplink' ) ); |
| 47 |
|
| 48 |
this.windowManager = new OO.ui.WindowManager(); |
| 49 |
|
| 50 |
|
| 51 |
OO.ui.getWindowManager().$element.before( this.windowManager.$element ); |
| 52 |
|
| 53 |
|
| 54 |
this.tdgDialog = new Dialog( config ); |
| 55 |
this.windowManager.addWindows( [ this.tdgDialog ] ); |
| 56 |
|
| 57 |
this.sourceHandler = new SourceHandler( { |
| 58 |
fullPageName: this.pageName, |
| 59 |
parentPage: this.parentPage, |
| 60 |
isPageSubLevel: this.isPageSubLevel |
| 61 |
} ); |
| 62 |
|
| 63 |
|
| 64 |
const relatedPage = this.isDocPage ? this.parentPage : this.pageName + '/' + this.docSubpage; |
| 65 |
this.sourceHandler.getApi( relatedPage ) |
| 66 |
.then( ( result ) => { |
| 67 |
const response = result.query.pages[ result.query.pageids[ 0 ] ]; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
if ( response.missing === undefined ) { |
| 74 |
const content = response.revisions[ 0 ][ '*' ]; |
| 75 |
|
| 76 |
if ( /<templatedata>/i.test( content ) ) { |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
let msg = mw.message( 'templatedata-exists-on-related-page', relatedPage ).plain(); |
| 82 |
mw.messages.set( { 'templatedata-string-exists-hack-message': msg } ); |
| 83 |
msg = new OO.ui.HtmlSnippet( |
| 84 |
mw.message( 'templatedata-string-exists-hack-message' ).parse() |
| 85 |
); |
| 86 |
|
| 87 |
this.setEditNoticeMessage( msg, 'warning' ); |
| 88 |
} |
| 89 |
} |
| 90 |
} ); |
| 91 |
|
| 92 |
|
| 93 |
this.editOpenDialogButton.connect( this, { click: 'onEditOpenDialogButton' } ); |
| 94 |
this.tdgDialog.connect( this, { apply: 'onDialogApply' } ); |
| 95 |
|
| 96 |
this.$element |
| 97 |
.addClass( 'tdg-editscreen-main' ) |
| 98 |
.append( |
| 99 |
this.editOpenDialogButton.$element, |
| 100 |
$helpLink, |
| 101 |
this.editNoticeMessage.$element |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
OO.inheritClass( Target, OO.ui.Element ); |
| 108 |
|
| 109 |
OO.mixinClass( Target, OO.EventEmitter ); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
Target.prototype.destroy = function () { |
| 117 |
this.windowManager.destroy(); |
| 118 |
this.$element.remove(); |
| 119 |
}; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
Target.prototype.setEditNoticeMessage = function ( label, type ) { |
| 129 |
this.editNoticeMessage.setLabel( label ); |
| 130 |
this.editNoticeMessage.setType( type ); |
| 131 |
this.editNoticeMessage.toggle( true ); |
| 132 |
}; |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
Target.prototype.openEditDialog = function ( dataModel ) { |
| 142 |
|
| 143 |
this.windowManager.openWindow( 'TemplateDataDialog', { |
| 144 |
model: dataModel, |
| 145 |
editNoticeMessage: this.editNoticeMessage |
| 146 |
} ); |
| 147 |
}; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
Target.prototype.onEditOpenDialogButton = function () { |
| 155 |
this.originalWikitext = this.$textarea.textSelection( 'getContents' ); |
| 156 |
|
| 157 |
|
| 158 |
this.sourceHandler.buildModel( this.originalWikitext ) |
| 159 |
.then( |
| 160 |
|
| 161 |
( model ) => { |
| 162 |
this.openEditDialog( model ); |
| 163 |
}, |
| 164 |
|
| 165 |
() => { |
| 166 |
|
| 167 |
OO.ui.getWindowManager().openWindow( 'message', { |
| 168 |
title: mw.msg( 'templatedata-modal-title' ), |
| 169 |
message: mw.msg( 'templatedata-errormsg-jsonbadformat' ), |
| 170 |
verbose: true, |
| 171 |
actions: [ |
| 172 |
{ |
| 173 |
action: 'accept', |
| 174 |
label: mw.msg( 'templatedata-modal-json-error-replace' ), |
| 175 |
flags: [ 'primary', 'destructive' ] |
| 176 |
}, |
| 177 |
{ |
| 178 |
action: 'reject', |
| 179 |
label: OO.ui.deferMsg( 'ooui-dialog-message-reject' ), |
| 180 |
flags: 'safe' |
| 181 |
} |
| 182 |
] |
| 183 |
} ).closed.then( ( data ) => { |
| 184 |
if ( data && data.action === 'accept' ) { |
| 185 |
|
| 186 |
const model = Model.static.newFromObject( |
| 187 |
null, |
| 188 |
this.sourceHandler.getTemplateSourceCodeParams() |
| 189 |
); |
| 190 |
this.openEditDialog( model ); |
| 191 |
} |
| 192 |
} ); |
| 193 |
} |
| 194 |
); |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
Target.prototype.replaceTemplateData = function ( newTemplateData ) { |
| 205 |
const templateDataJSON = JSON.stringify( newTemplateData, null, '\t' ), |
| 206 |
templatedataPattern = /(<templatedata>\s*)([\s\S]*?)\s*<\/templatedata>/i; |
| 207 |
|
| 208 |
let matches, templateDataOutput; |
| 209 |
if ( ( matches = this.originalWikitext.match( templatedataPattern ) ) ) { |
| 210 |
|
| 211 |
this.$textarea.textSelection( 'setSelection', { |
| 212 |
start: matches.index + matches[ 1 ].length, |
| 213 |
end: matches.index + matches[ 1 ].length + matches[ 2 ].length |
| 214 |
} ); |
| 215 |
templateDataOutput = templateDataJSON; |
| 216 |
} else { |
| 217 |
this.$textarea.textSelection( 'setSelection', { start: this.originalWikitext.length } ); |
| 218 |
|
| 219 |
templateDataOutput = '<templatedata>\n' + templateDataJSON + '\n</templatedata>'; |
| 220 |
|
| 221 |
if ( !this.isPageSubLevel ) { |
| 222 |
if ( ( matches = this.originalWikitext.match( /<\/noinclude>\s*$/ ) ) ) { |
| 223 |
|
| 224 |
this.$textarea.textSelection( 'setSelection', { start: matches.index } ); |
| 225 |
} else { |
| 226 |
|
| 227 |
templateDataOutput = '<noinclude>\n' + templateDataOutput + '\n</noinclude>\n'; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
if ( this.originalWikitext.slice( -1 ) !== '\n' ) { |
| 232 |
templateDataOutput = '\n' + templateDataOutput; |
| 233 |
} |
| 234 |
} |
| 235 |
this.$textarea.textSelection( 'replaceSelection', templateDataOutput ); |
| 236 |
}; |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
Target.prototype.onDialogApply = function ( templateData ) { |
| 245 |
if ( |
| 246 |
Object.keys( templateData ).length > 1 || |
| 247 |
Object.keys( templateData.params ).length > 0 |
| 248 |
) { |
| 249 |
this.replaceTemplateData( templateData ); |
| 250 |
} else { |
| 251 |
this.windowManager.closeWindow( this.windowManager.getCurrentWindow() ); |
| 252 |
OO.ui.getWindowManager().openWindow( 'message', { |
| 253 |
title: mw.msg( 'templatedata-modal-title' ), |
| 254 |
message: mw.msg( 'templatedata-errormsg-insertblank' ), |
| 255 |
actions: [ |
| 256 |
{ |
| 257 |
label: mw.msg( 'templatedata-modal-button-cancel' ), |
| 258 |
flags: [ 'primary', 'safe' ] |
| 259 |
}, |
| 260 |
{ |
| 261 |
action: 'apply', |
| 262 |
label: mw.msg( 'templatedata-modal-button-apply' ) |
| 263 |
} |
| 264 |
] |
| 265 |
} ).closed.then( ( data ) => { |
| 266 |
if ( data && data.action === 'apply' ) { |
| 267 |
this.replaceTemplateData( templateData ); |
| 268 |
} |
| 269 |
} ); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
if ( !$( 'input[name="TemplateDataGeneratorUsed"]' ).length ) { |
| 275 |
$( '<input>' ).attr( { |
| 276 |
type: 'hidden', |
| 277 |
value: 1, |
| 278 |
name: 'TemplateDataGeneratorUsed' |
| 279 |
} ).insertAfter( '#wpTextbox1' ); |
| 280 |
} |
| 281 |
}; |
| 282 |
|
| 283 |
module.exports = Target; |