| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
var dialogsModule = { |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
api: { |
| 12 |
addDialog: function ( context, data ) { |
| 13 |
dialogsModule.fn.create( context, data ); |
| 14 |
}, |
| 15 |
openDialog: function ( context, module ) { |
| 16 |
if ( module in dialogsModule.modules ) { |
| 17 |
var mod = dialogsModule.modules[ module ]; |
| 18 |
var $dialog = $( '#' + mod.id ); |
| 19 |
if ( $dialog.length === 0 ) { |
| 20 |
dialogsModule.fn.reallyCreate( context, mod, module ); |
| 21 |
$dialog = $( '#' + mod.id ); |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
$dialog.closest( '.ui-dialog' ) |
| 26 |
.find( '.ui-dialog-titlebar-close' ) |
| 27 |
.removeClass( 'ui-state-focus' ); |
| 28 |
|
| 29 |
$dialog.dialog( 'open' ); |
| 30 |
} |
| 31 |
}, |
| 32 |
closeDialog: function ( context, module ) { |
| 33 |
if ( module in dialogsModule.modules ) { |
| 34 |
$( '#' + dialogsModule.modules[ module ].id ).dialog( 'close' ); |
| 35 |
} |
| 36 |
} |
| 37 |
}, |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
fn: { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
create: function ( context, config ) { |
| 50 |
|
| 51 |
for ( var mod in config ) { |
| 52 |
var module = config[ mod ]; |
| 53 |
|
| 54 |
var filtered = false; |
| 55 |
if ( typeof module.filters !== 'undefined' ) { |
| 56 |
for ( var i = 0; i < module.filters.length; i++ ) { |
| 57 |
if ( $( module.filters[ i ] ).length === 0 ) { |
| 58 |
filtered = true; |
| 59 |
break; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
var $existingDialog = $( '#' + module.id ); |
| 65 |
if ( $existingDialog.length > 0 && $existingDialog.data( 'context' ).$textarea !== context.$textarea ) { |
| 66 |
$existingDialog.remove(); |
| 67 |
} |
| 68 |
|
| 69 |
$existingDialog = $( '#' + module.id ); |
| 70 |
if ( !filtered && $existingDialog.length === 0 ) { |
| 71 |
dialogsModule.modules[ mod ] = module; |
| 72 |
context.$textarea.trigger( 'wikiEditor-dialogs-setup-' + mod ); |
| 73 |
|
| 74 |
if ( typeof module.immediateCreate !== 'undefined' && module.immediateCreate ) { |
| 75 |
dialogsModule.fn.reallyCreate( context, module, mod ); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
}, |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
reallyCreate: function ( context, module, name ) { |
| 89 |
var configuration = module.dialog; |
| 90 |
|
| 91 |
configuration.bgiframe = true; |
| 92 |
configuration.autoOpen = false; |
| 93 |
|
| 94 |
if ( typeof configuration.modal === 'undefined' ) { |
| 95 |
configuration.modal = true; |
| 96 |
} |
| 97 |
configuration.title = $.wikiEditor.autoSafeMsg( module, 'title' ); |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
configuration.newButtons = {}; |
| 102 |
for ( var msg in configuration.buttons ) { |
| 103 |
|
| 104 |
configuration.newButtons[ mw.msg( msg ) ] = configuration.buttons[ msg ]; |
| 105 |
} |
| 106 |
configuration.buttons = configuration.newButtons; |
| 107 |
var $content; |
| 108 |
if ( module.htmlTemplate ) { |
| 109 |
$content = mw.template.get( 'ext.wikiEditor', module.htmlTemplate ).render(); |
| 110 |
} else if ( module.html instanceof $ ) { |
| 111 |
$content = module.html; |
| 112 |
} else { |
| 113 |
$content = $( $.parseHTML( module.html ) ); |
| 114 |
} |
| 115 |
|
| 116 |
var $dialogDiv = $( '<div>' ) |
| 117 |
.attr( 'id', module.id ) |
| 118 |
.append( $content ) |
| 119 |
.data( 'context', context ) |
| 120 |
.appendTo( document.body ) |
| 121 |
.each( module.init ) |
| 122 |
.dialog( configuration ); |
| 123 |
|
| 124 |
$dialogDiv.on( 'dialogclose', function () { |
| 125 |
context.fn.restoreSelection(); |
| 126 |
} ); |
| 127 |
|
| 128 |
|
| 129 |
context.$textarea.trigger( 'wikiEditor-dialogs-loaded-' + name ); |
| 130 |
} |
| 131 |
}, |
| 132 |
|
| 133 |
|
| 134 |
modules: {}, |
| 135 |
|
| 136 |
quickDialog: function ( body, settings ) { |
| 137 |
$( '<div>' ) |
| 138 |
.text( body ) |
| 139 |
.appendTo( document.body ) |
| 140 |
.dialog( Object.assign( { |
| 141 |
bgiframe: true, |
| 142 |
modal: true |
| 143 |
}, settings ) ) |
| 144 |
.dialog( 'open' ); |
| 145 |
} |
| 146 |
|
| 147 |
}; |
| 148 |
|
| 149 |
module.exports = dialogsModule; |