| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
var hasOwn = Object.prototype.hasOwnProperty, |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
fallbackChain = ( function () { |
| 22 |
|
| 23 |
var isRTL = $( document.body ).hasClass( 'rtl' ), |
| 24 |
chain = mw.language.getFallbackLanguageChain(); |
| 25 |
|
| 26 |
|
| 27 |
if ( chain.length >= 2 && !/^en-/.test( chain[ chain.length - 2 ] ) ) { |
| 28 |
chain.pop(); |
| 29 |
} |
| 30 |
if ( isRTL ) { |
| 31 |
chain.push( 'default-rtl' ); |
| 32 |
} |
| 33 |
chain.push( 'default' ); |
| 34 |
return chain; |
| 35 |
}() ); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
function deprecateAutoMsg( property, key ) { |
| 46 |
var searchParam = mw.config.get( 'wgSearchType' ) === 'CirrusSearch' ? |
| 47 |
'insource:/' + property + 'Msg: \'' + key + '\'/' : |
| 48 |
property + 'Msg: ' + key; |
| 49 |
var searchUri = mw.config.get( 'wgServer' ) + |
| 50 |
mw.util.getUrl( |
| 51 |
'Special:Search', |
| 52 |
{ search: searchParam, ns2: 1, ns8: 1 } |
| 53 |
); |
| 54 |
if ( searchUri.slice( 0, 2 ) === '//' ) { |
| 55 |
searchUri = location.protocol + searchUri; |
| 56 |
} |
| 57 |
|
| 58 |
var messageMethod; |
| 59 |
if ( property === 'html' || property === 'text' || property === 'title' ) { |
| 60 |
messageMethod = 'mw.message( ' + JSON.stringify( key ) + ' ).parse()'; |
| 61 |
} else { |
| 62 |
messageMethod = 'mw.msg( ' + JSON.stringify( key ) + ' )'; |
| 63 |
} |
| 64 |
var deprecationMsg = mw.log.makeDeprecated( |
| 65 |
'wikiEditor_autoMsg', |
| 66 |
'WikiEditor: Use `' + property + ': ' + messageMethod + '` instead of `' + property + 'Msg: ' + JSON.stringify( key ) + '`.\nSearch: ' + searchUri |
| 67 |
); |
| 68 |
deprecationMsg(); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
$.wikiEditor = { |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
modules: { |
| 81 |
toolbar: require( './jquery.wikiEditor.toolbar.js' ), |
| 82 |
dialogs: require( './jquery.wikiEditor.dialogs.js' ) |
| 83 |
}, |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
extensions: {}, |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
instances: [], |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
imgPath: mw.config.get( 'wgExtensionAssetsPath' ) + '/WikiEditor/modules/images/', |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
isRequired: function ( module, requirement ) { |
| 112 |
if ( typeof module.req !== 'undefined' ) { |
| 113 |
for ( var req in module.req ) { |
| 114 |
if ( module.req[ req ] === requirement ) { |
| 115 |
return true; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
return false; |
| 120 |
}, |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
autoMsg: function ( object, property ) { |
| 139 |
|
| 140 |
if ( typeof property === 'object' ) { |
| 141 |
for ( var i in property ) { |
| 142 |
if ( property[ i ] in object || property[ i ] + 'Msg' in object ) { |
| 143 |
property = property[ i ]; |
| 144 |
break; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
if ( property in object ) { |
| 149 |
return object[ property ]; |
| 150 |
} else if ( property + 'Msg' in object ) { |
| 151 |
var p = object[ property + 'Msg' ]; |
| 152 |
if ( Array.isArray( p ) && p.length >= 2 ) { |
| 153 |
deprecateAutoMsg( property, p[ 0 ] ); |
| 154 |
return mw.message.apply( mw.message, p ).text(); |
| 155 |
} else { |
| 156 |
deprecateAutoMsg( property, p ); |
| 157 |
|
| 158 |
return mw.message( p ).text(); |
| 159 |
} |
| 160 |
} else { |
| 161 |
return ''; |
| 162 |
} |
| 163 |
}, |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
autoSafeMsg: function ( object, property ) { |
| 180 |
|
| 181 |
if ( typeof property === 'object' ) { |
| 182 |
for ( var i in property ) { |
| 183 |
if ( property[ i ] in object || property[ i ] + 'Msg' in object ) { |
| 184 |
property = property[ i ]; |
| 185 |
break; |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
if ( property in object ) { |
| 190 |
return object[ property ]; |
| 191 |
} else if ( property + 'Msg' in object ) { |
| 192 |
var p = object[ property + 'Msg' ]; |
| 193 |
if ( Array.isArray( p ) && p.length >= 2 ) { |
| 194 |
deprecateAutoMsg( property, p[ 0 ] ); |
| 195 |
return mw.message.apply( mw.message, p ).escaped(); |
| 196 |
} else { |
| 197 |
deprecateAutoMsg( property, p ); |
| 198 |
|
| 199 |
return mw.message( p ).escaped(); |
| 200 |
} |
| 201 |
} else { |
| 202 |
return ''; |
| 203 |
} |
| 204 |
}, |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
autoLang: function ( object ) { |
| 216 |
for ( var i = 0; i < fallbackChain.length; i++ ) { |
| 217 |
var key = fallbackChain[ i ]; |
| 218 |
if ( hasOwn.call( object, key ) ) { |
| 219 |
return object[ key ]; |
| 220 |
} |
| 221 |
} |
| 222 |
return object; |
| 223 |
}, |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
autoIcon: function ( icon, path ) { |
| 234 |
path = path || $.wikiEditor.imgPath; |
| 235 |
|
| 236 |
for ( var i = 0; i < fallbackChain.length; i++ ) { |
| 237 |
var key = fallbackChain[ i ]; |
| 238 |
if ( icon && hasOwn.call( icon, key ) ) { |
| 239 |
var src = icon[ key ]; |
| 240 |
|
| 241 |
|
| 242 |
if ( src.slice( 0, 5 ) === 'data:' ) { |
| 243 |
return src; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
if ( src.slice( 0, 7 ) !== 'http://' && src.slice( 0, 8 ) !== 'https://' && src[ 0 ] !== '/' ) { |
| 248 |
src = path + src; |
| 249 |
} |
| 250 |
return src; |
| 251 |
} |
| 252 |
} |
| 253 |
return icon; |
| 254 |
} |
| 255 |
}; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
$.fn.wikiEditor = function () { |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
var context = $( this ).data( 'wikiEditor-context' ); |
| 268 |
|
| 269 |
if ( !context ) { |
| 270 |
|
| 271 |
|
| 272 |
context = { |
| 273 |
|
| 274 |
$textarea: $( this ), |
| 275 |
|
| 276 |
$focusedElem: null, |
| 277 |
|
| 278 |
views: {}, |
| 279 |
|
| 280 |
modules: {}, |
| 281 |
|
| 282 |
data: {}, |
| 283 |
|
| 284 |
instance: $.wikiEditor.instances.push( $( this ) ) - 1, |
| 285 |
|
| 286 |
savedSelection: null, |
| 287 |
|
| 288 |
extensions: [] |
| 289 |
}; |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
context.api = { |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
addModule: function ( ctx, data ) { |
| 306 |
var modules = {}; |
| 307 |
if ( typeof data === 'string' ) { |
| 308 |
modules[ data ] = {}; |
| 309 |
} else if ( typeof data === 'object' ) { |
| 310 |
modules = data; |
| 311 |
} |
| 312 |
for ( var module in modules ) { |
| 313 |
|
| 314 |
if ( typeof module === 'string' && typeof $.wikiEditor.modules[ module ] !== 'undefined' ) { |
| 315 |
|
| 316 |
if ( 'api' in $.wikiEditor.modules[ module ] ) { |
| 317 |
for ( var call in $.wikiEditor.modules[ module ].api ) { |
| 318 |
|
| 319 |
if ( !( call in ctx.api ) ) { |
| 320 |
ctx.api[ call ] = $.wikiEditor.modules[ module ].api[ call ]; |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
if ( 'fn' in $.wikiEditor.modules[ module ] && |
| 326 |
'create' in $.wikiEditor.modules[ module ].fn && |
| 327 |
typeof ctx.modules[ module ] === 'undefined' |
| 328 |
) { |
| 329 |
|
| 330 |
ctx.modules[ module ] = {}; |
| 331 |
|
| 332 |
$.wikiEditor.modules[ module ].fn.create( ctx, modules[ module ] ); |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
}; |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
context.evt = { |
| 347 |
|
| 348 |
}; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
context.fn = { |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
trigger: function ( name, event ) { |
| 361 |
|
| 362 |
event = event || { type: 'custom' }; |
| 363 |
|
| 364 |
event.data = event.data || {}; |
| 365 |
|
| 366 |
|
| 367 |
if ( name in context.evt ) { |
| 368 |
if ( !context.evt[ name ]( event ) ) { |
| 369 |
return false; |
| 370 |
} |
| 371 |
} |
| 372 |
var returnFromModules = true; |
| 373 |
|
| 374 |
|
| 375 |
for ( var module in context.modules ) { |
| 376 |
if ( |
| 377 |
module in $.wikiEditor.modules && |
| 378 |
'evt' in $.wikiEditor.modules[ module ] && |
| 379 |
name in $.wikiEditor.modules[ module ].evt |
| 380 |
) { |
| 381 |
var ret = $.wikiEditor.modules[ module ].evt[ name ]( context, event ); |
| 382 |
if ( ret !== null ) { |
| 383 |
|
| 384 |
returnFromModules = returnFromModules && ret; |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
return returnFromModules; |
| 389 |
}, |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
addButton: function ( options ) { |
| 398 |
|
| 399 |
context.$controls.show(); |
| 400 |
context.$buttons.show(); |
| 401 |
return $( '<button>' ) |
| 402 |
.text( $.wikiEditor.autoMsg( options, 'caption' ) ) |
| 403 |
.on( 'click', options.action ) |
| 404 |
.appendTo( context.$buttons ); |
| 405 |
}, |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
addView: function ( options ) { |
| 415 |
|
| 416 |
function addTab( opts ) { |
| 417 |
|
| 418 |
context.$controls.show(); |
| 419 |
context.$tabs.show(); |
| 420 |
|
| 421 |
return $( '<div>' ) |
| 422 |
.attr( 'rel', 'wikiEditor-ui-view-' + opts.name ) |
| 423 |
.addClass( context.view === opts.name ? 'current' : null ) |
| 424 |
.append( $( '<a>' ) |
| 425 |
.attr( 'tabindex', 0 ) |
| 426 |
.on( 'mousedown', function () { |
| 427 |
|
| 428 |
return false; |
| 429 |
} ) |
| 430 |
.on( 'click keydown', function ( event ) { |
| 431 |
if ( |
| 432 |
event.type === 'click' || |
| 433 |
event.type === 'keydown' && event.key === 'Enter' |
| 434 |
) { |
| 435 |
context.$ui.find( '.wikiEditor-ui-view' ).hide(); |
| 436 |
context.$ui.find( '.' + $( this ).parent().attr( 'rel' ) ).show(); |
| 437 |
context.$tabs.find( 'div' ).removeClass( 'current' ); |
| 438 |
$( this ).parent().addClass( 'current' ); |
| 439 |
$( this ).trigger( 'blur' ); |
| 440 |
if ( 'init' in opts && typeof opts.init === 'function' ) { |
| 441 |
opts.init( context ); |
| 442 |
} |
| 443 |
event.preventDefault(); |
| 444 |
return false; |
| 445 |
} |
| 446 |
} ) |
| 447 |
.text( $.wikiEditor.autoMsg( opts, 'title' ) ) |
| 448 |
) |
| 449 |
.appendTo( context.$tabs ); |
| 450 |
} |
| 451 |
|
| 452 |
if ( !context.$tabs.children().length ) { |
| 453 |
addTab( { name: 'wikitext', title: mw.message( 'wikieditor-wikitext-tab' ).parse() } ); |
| 454 |
} |
| 455 |
|
| 456 |
addTab( options ); |
| 457 |
|
| 458 |
|
| 459 |
return $( '<div>' ) |
| 460 |
.addClass( 'wikiEditor-ui-view wikiEditor-ui-view-' + options.name ) |
| 461 |
.hide() |
| 462 |
.appendTo( context.$ui ); |
| 463 |
}, |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
saveSelection: function () { |
| 469 |
context.$focusedElem = $( ':focus' ); |
| 470 |
context.$textarea.trigger( 'focus' ); |
| 471 |
context.savedSelection = context.$textarea.textSelection( 'getCaretPosition', { startAndEnd: true } ); |
| 472 |
}, |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
restoreSelection: function () { |
| 478 |
if ( context.savedSelection ) { |
| 479 |
context.$textarea.trigger( 'focus' ); |
| 480 |
context.$textarea.textSelection( 'setSelection', { start: context.savedSelection[ 0 ], end: context.savedSelection[ 1 ] } ); |
| 481 |
context.savedSelection = null; |
| 482 |
} |
| 483 |
if ( context.$focusedElem ) { |
| 484 |
context.$focusedElem.trigger( 'focus' ); |
| 485 |
} |
| 486 |
} |
| 487 |
}; |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
var hasFocus = context.$textarea.is( ':focus' ); |
| 499 |
var cursorPos = context.$textarea.textSelection( 'getCaretPosition', { startAndEnd: true } ); |
| 500 |
|
| 501 |
context.$textarea |
| 502 |
.wrapAll( $( '<div>' ).addClass( 'wikiEditor-ui' ) ) |
| 503 |
.wrapAll( $( '<div>' ).addClass( 'wikiEditor-ui-view wikiEditor-ui-view-wikitext' ) ) |
| 504 |
.wrapAll( $( '<div>' ).addClass( 'wikiEditor-ui-left' ) ) |
| 505 |
.wrapAll( $( '<div>' ).addClass( 'wikiEditor-ui-bottom' ) ) |
| 506 |
.wrapAll( $( '<div>' ).addClass( 'wikiEditor-ui-text' ) ); |
| 507 |
|
| 508 |
context.$textarea.prop( 'scrollTop', $( '#wpScrolltop' ).val() ); |
| 509 |
|
| 510 |
if ( hasFocus ) { |
| 511 |
context.$textarea.trigger( 'focus' ); |
| 512 |
context.$textarea.textSelection( 'setSelection', { start: cursorPos[ 0 ], end: cursorPos[ 1 ] } ); |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
context.$ui = context.$textarea.parent().parent().parent().parent().parent(); |
| 517 |
context.$wikitext = context.$textarea.parent().parent().parent().parent(); |
| 518 |
|
| 519 |
context.$wikitext.before( |
| 520 |
$( '<div>' ).addClass( 'wikiEditor-ui-controls' ).append( |
| 521 |
$( '<div>' ).addClass( 'wikiEditor-ui-tabs' ).hide(), |
| 522 |
$( '<div>' ).addClass( 'wikiEditor-ui-buttons' ) |
| 523 |
), |
| 524 |
$( '<div>' ).addClass( 'wikiEditor-ui-clear' ) |
| 525 |
); |
| 526 |
|
| 527 |
context.$controls = context.$ui.find( '.wikiEditor-ui-buttons' ).hide(); |
| 528 |
context.$buttons = context.$ui.find( '.wikiEditor-ui-buttons' ); |
| 529 |
context.$tabs = context.$ui.find( '.wikiEditor-ui-tabs' ); |
| 530 |
|
| 531 |
context.$ui.after( $( '<div>' ).addClass( 'wikiEditor-ui-clear' ) ); |
| 532 |
|
| 533 |
context.$wikitext.append( |
| 534 |
$( '<div>' ).addClass( 'wikiEditor-ui-right' ), |
| 535 |
$( '<div>' ).addClass( 'wikiEditor-ui-clear' ) |
| 536 |
); |
| 537 |
|
| 538 |
context.$wikitext.find( '.wikiEditor-ui-left' ).prepend( $( '<div>' ).addClass( 'wikiEditor-ui-top' ) ); |
| 539 |
|
| 540 |
context.view = 'wikitext'; |
| 541 |
|
| 542 |
$( window ).on( 'resize', function ( event ) { |
| 543 |
context.fn.trigger( 'resize', event ); |
| 544 |
} ); |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
var args = $.makeArray( arguments ); |
| 551 |
|
| 552 |
|
| 553 |
if ( args[ 0 ] === 'addModule' && typeof args[ 1 ] !== 'undefined' ) { |
| 554 |
var modulesArg = args[ 1 ]; |
| 555 |
if ( typeof modulesArg !== 'object' ) { |
| 556 |
modulesArg = {}; |
| 557 |
modulesArg[ args[ 1 ] ] = ''; |
| 558 |
} |
| 559 |
for ( var m in modulesArg ) { |
| 560 |
if ( m in $.wikiEditor.modules ) { |
| 561 |
|
| 562 |
for ( var extension in $.wikiEditor.extensions ) { |
| 563 |
if ( |
| 564 |
$.wikiEditor.isRequired( $.wikiEditor.modules[ m ], extension ) && |
| 565 |
context.extensions.indexOf( extension ) === -1 |
| 566 |
) { |
| 567 |
context.extensions[ context.extensions.length ] = extension; |
| 568 |
$.wikiEditor.extensions[ extension ]( context ); |
| 569 |
} |
| 570 |
} |
| 571 |
break; |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
if ( args.length > 0 ) { |
| 578 |
|
| 579 |
var callArg = args.shift(); |
| 580 |
if ( callArg in context.api ) { |
| 581 |
context.api[ callArg ]( context, args[ 0 ] || {} ); |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
return $( this ).data( 'wikiEditor-context', context ); |
| 587 |
|
| 588 |
}; |