1/* global tinymce */
2/**
3 * Included for back-compat.
4 * The default WindowManager in TinyMCE 4.0 supports three types of dialogs:
5 * - With HTML created from JS.
6 * - With inline HTML (like WPWindowManager).
7 * - Old type iframe based dialogs.
8 * For examples see the default plugins: https://github.com/tinymce/tinymce/tree/master/js/tinymce/plugins
9 */
10tinymce.WPWindowManager = tinymce.InlineWindowManager = function( editor ) {
11 if ( this.wp ) {
12 return this;
13 }
14
15 this.wp = {};
16 this.parent = editor.windowManager;
17 this.editor = editor;
18
19 tinymce.extend( this, this.parent );
20
21 this.open = function( args, params ) {
22 var $element,
23 self = this,
24 wp = this.wp;
25
26 if ( ! args.wpDialog ) {
27 return this.parent.open.apply( this, arguments );
28 } else if ( ! args.id ) {
29 return;
30 }
31
32 if ( typeof jQuery === 'undefined' || ! jQuery.wp || ! jQuery.wp.wpdialog ) {
33 // wpdialog.js is not loaded.
34 if ( window.console && window.console.error ) {
35 window.console.error('wpdialog.js is not loaded. Please set "wpdialogs" as dependency for your script when calling wp_enqueue_script(). You may also want to enqueue the "wp-jquery-ui-dialog" stylesheet.');
36 }
37
38 return;
39 }
40
41 wp.$element = $element = jQuery( '#' + args.id );
42
43 if ( ! $element.length ) {
44 return;
45 }
46
47 if ( window.console && window.console.log ) {
48 window.console.log('tinymce.WPWindowManager is deprecated. Use the default editor.windowManager to open dialogs with inline HTML.');
49 }
50
51 wp.features = args;
52 wp.params = params;
53
54 // Store selection. Takes a snapshot in the FocusManager of the selection before focus is moved to the dialog.
55 editor.nodeChanged();
56
57 // Create the dialog if necessary.
58 if ( ! $element.data('wpdialog') ) {
59 $element.wpdialog({
60 title: args.title,
61 width: args.width,
62 height: args.height,
63 modal: true,
64 dialogClass: 'wp-dialog',
65 zIndex: 300000
66 });
67 }
68
69 $element.wpdialog('open');
70
71 $element.on( 'wpdialogclose', function() {
72 if ( self.wp.$element ) {
73 self.wp = {};
74 }
75 });
76 };
77
78 this.close = function() {
79 if ( ! this.wp.features || ! this.wp.features.wpDialog ) {
80 return this.parent.close.apply( this, arguments );
81 }
82
83 this.wp.$element.wpdialog('close');
84 };
85};
86
87tinymce.PluginManager.add( 'wpdialogs', function( editor ) {
88 // Replace window manager.
89 editor.on( 'init', function() {
90 editor.windowManager = new tinymce.WPWindowManager( editor );
91 });
92});
93