Debian Codegraph @

extensions/WikiEditor/modules/jquery.wikiEditor.dialogs.js

raw symbols SCIP
1 /**
2 * Dialog Module for wikiEditor
3 *
4 * @memberof module:ext.wikiEditor
5 */
6 var dialogsModule = {
7
8 /**
9 * API accessible functions
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 // Workaround for bug in jQuery UI: close button in top right retains focus
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 * Internally used functions
41 */
42 fn: {
43 /**
44 * Creates a dialog module within a wikiEditor
45 *
46 * @param {Object} context Context object of editor to create module in
47 * @param {Object} config Configuration object to create module from
48 */
49 create: function ( context, config ) {
50 // Defer building of modules, unless they require immediate creation
51 for ( var mod in config ) {
52 var module = config[ mod ];
53 // Only create the dialog if it isn't filtered and doesn't exist yet
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 // If the dialog already exists, but for another textarea, simply remove it
64 var $existingDialog = $( '#' + module.id );
65 if ( $existingDialog.length > 0 && $existingDialog.data( 'context' ).$textarea !== context.$textarea ) {
66 $existingDialog.remove();
67 }
68 // Re-select from the DOM, we might have removed the dialog just now
69 $existingDialog = $( '#' + module.id );
70 if ( !filtered && $existingDialog.length === 0 ) {
71 dialogsModule.modules[ mod ] = module;
72 context.$textarea.trigger( 'wikiEditor-dialogs-setup-' + mod );
73 // If this dialog requires immediate creation, create it now
74 if ( typeof module.immediateCreate !== 'undefined' && module.immediateCreate ) {
75 dialogsModule.fn.reallyCreate( context, module, mod );
76 }
77 }
78 }
79 },
80
81 /**
82 * Build the actual dialog. This done on-demand rather than in create()
83 *
84 * @param {Object} context Context object of editor dialog belongs to
85 * @param {Object} module Dialog module object
86 * @param {string} name Dialog name (key in dialogsModule.modules)
87 */
88 reallyCreate: function ( context, module, name ) {
89 var configuration = module.dialog;
90 // Add some stuff to configuration
91 configuration.bgiframe = true;
92 configuration.autoOpen = false;
93 // By default our dialogs are modal, unless explicitly defined in their specific configuration.
94 if ( typeof configuration.modal === 'undefined' ) {
95 configuration.modal = true;
96 }
97 configuration.title = $.wikiEditor.autoSafeMsg( module, 'title' );
98 // Transform messages in keys
99 // Stupid JS won't let us do stuff like
100 // foo = { mw.msg( 'bar' ): baz }
101 configuration.newButtons = {};
102 for ( var msg in configuration.buttons ) {
103 // eslint-disable-next-line mediawiki/msg-doc
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 // Create the dialog <div>
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 // Let the outside world know we set up this dialog
129 context.$textarea.trigger( 'wikiEditor-dialogs-loaded-' + name );
130 }
131 },
132
133 // This stuff is just hanging here, perhaps we could come up with a better home for this stuff
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;