]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/tiny_mce/classes/WindowManager.js
Release 6.5.0
[Github/sugarcrm.git] / include / javascript / tiny_mce / classes / WindowManager.js
1 /**
2  * WindowManager.js
3  *
4  * Copyright 2009, Moxiecode Systems AB
5  * Released under LGPL License.
6  *
7  * License: http://tinymce.moxiecode.com/license
8  * Contributing: http://tinymce.moxiecode.com/contributing
9  */
10
11 (function(tinymce) {
12         var Dispatcher = tinymce.util.Dispatcher, each = tinymce.each, isIE = tinymce.isIE, isOpera = tinymce.isOpera;
13
14         /**
15          * This class handles the creation of native windows and dialogs. This class can be extended to provide for example inline dialogs.
16          *
17          * @class tinymce.WindowManager
18          * @example
19          * // Opens a new dialog with the file.htm file and the size 320x240
20          * // It also adds a custom parameter this can be retrieved by using tinyMCEPopup.getWindowArg inside the dialog.
21          * tinyMCE.activeEditor.windowManager.open({
22          *    url : 'file.htm',
23          *    width : 320,
24          *    height : 240
25          * }, {
26          *    custom_param : 1
27          * });
28          * 
29          * // Displays an alert box using the active editors window manager instance
30          * tinyMCE.activeEditor.windowManager.alert('Hello world!');
31          * 
32          * // Displays an confirm box and an alert message will be displayed depending on what you choose in the confirm
33          * tinyMCE.activeEditor.windowManager.confirm("Do you want to do something", function(s) {
34          *    if (s)
35          *       tinyMCE.activeEditor.windowManager.alert("Ok");
36          *    else
37          *       tinyMCE.activeEditor.windowManager.alert("Cancel");
38          * });
39          */
40         tinymce.create('tinymce.WindowManager', {
41                 /**
42                  * Constructs a new window manager instance.
43                  *
44                  * @constructor
45                  * @method WindowManager
46                  * @param {tinymce.Editor} ed Editor instance that the windows are bound to.
47                  */
48                 WindowManager : function(ed) {
49                         var t = this;
50
51                         t.editor = ed;
52                         t.onOpen = new Dispatcher(t);
53                         t.onClose = new Dispatcher(t);
54                         t.params = {};
55                         t.features = {};
56                 },
57
58                 /**
59                  * Opens a new window.
60                  *
61                  * @method open
62                  * @param {Object} s Optional name/value settings collection contains things like width/height/url etc.
63                  * @option {String} title Window title. 
64                  * @option {String} file URL of the file to open in the window. 
65                  * @option {Number} width Width in pixels. 
66                  * @option {Number} height Height in pixels. 
67                  * @option {Boolean} resizable Specifies whether the popup window is resizable or not. 
68                  * @option {Boolean} maximizable Specifies whether the popup window has a "maximize" button and can get maximized or not. 
69                  * @option {Boolean} inline Specifies whether to display in-line (set to 1 or true for in-line display; requires inlinepopups plugin). 
70                  * @option {String/Boolean} popup_css Optional CSS to use in the popup. Set to false to remove the default one. 
71                  * @option {Boolean} translate_i18n Specifies whether translation should occur or not of i18 key strings. Default is true. 
72                  * @option {String/bool} close_previous Specifies whether a previously opened popup window is to be closed or not (like when calling the file browser window over the advlink popup). 
73                  * @option {String/bool} scrollbars Specifies whether the popup window can have scrollbars if required (i.e. content larger than the popup size specified). 
74                  * @param {Object} p Optional parameters/arguments collection can be used by the dialogs to retrive custom parameters.
75                  * @option {String} plugin_url url to plugin if opening plugin window that calls tinyMCEPopup.requireLangPack() and needs access to the plugin language js files 
76                  */
77                 open : function(s, p) {
78                         var t = this, f = '', x, y, mo = t.editor.settings.dialog_type == 'modal', w, sw, sh, vp = tinymce.DOM.getViewPort(), u;
79
80                         // Default some options
81                         s = s || {};
82                         p = p || {};
83                         sw = isOpera ? vp.w : screen.width; // Opera uses windows inside the Opera window
84                         sh = isOpera ? vp.h : screen.height;
85                         s.name = s.name || 'mc_' + new Date().getTime();
86                         s.width = parseInt(s.width || 320);
87                         s.height = parseInt(s.height || 240);
88                         s.resizable = true;
89                         s.left = s.left || parseInt(sw / 2.0) - (s.width / 2.0);
90                         s.top = s.top || parseInt(sh / 2.0) - (s.height / 2.0);
91                         p.inline = false;
92                         p.mce_width = s.width;
93                         p.mce_height = s.height;
94                         p.mce_auto_focus = s.auto_focus;
95
96                         if (mo) {
97                                 if (isIE) {
98                                         s.center = true;
99                                         s.help = false;
100                                         s.dialogWidth = s.width + 'px';
101                                         s.dialogHeight = s.height + 'px';
102                                         s.scroll = s.scrollbars || false;
103                                 }
104                         }
105
106                         // Build features string
107                         each(s, function(v, k) {
108                                 if (tinymce.is(v, 'boolean'))
109                                         v = v ? 'yes' : 'no';
110
111                                 if (!/^(name|url)$/.test(k)) {
112                                         if (isIE && mo)
113                                                 f += (f ? ';' : '') + k + ':' + v;
114                                         else
115                                                 f += (f ? ',' : '') + k + '=' + v;
116                                 }
117                         });
118
119                         t.features = s;
120                         t.params = p;
121                         t.onOpen.dispatch(t, s, p);
122
123                         u = s.url || s.file;
124                         u = tinymce._addVer(u);
125
126                         try {
127                                 if (isIE && mo) {
128                                         w = 1;
129                                         window.showModalDialog(u, window, f);
130                                 } else
131                                         w = window.open(u, s.name, f);
132                         } catch (ex) {
133                                 // Ignore
134                         }
135
136                         if (!w)
137                                 alert(t.editor.getLang('popup_blocked'));
138                 },
139
140                 /**
141                  * Closes the specified window. This will also dispatch out a onClose event.
142                  *
143                  * @method close
144                  * @param {Window} w Native window object to close.
145                  */
146                 close : function(w) {
147                         w.close();
148                         this.onClose.dispatch(this);
149                 },
150
151                 /**
152                  * Creates a instance of a class. This method was needed since IE can't create instances
153                  * of classes from a parent window due to some reference problem. Any arguments passed after the class name
154                  * will be passed as arguments to the constructor.
155                  *
156                  * @method createInstance
157                  * @param {String} cl Class name to create an instance of.
158                  * @return {Object} Instance of the specified class.
159                  * @example
160                  * var uri = tinyMCEPopup.editor.windowManager.createInstance('tinymce.util.URI', 'http://www.somesite.com');
161                  * alert(uri.getURI());
162                  */
163                 createInstance : function(cl, a, b, c, d, e) {
164                         var f = tinymce.resolve(cl);
165
166                         return new f(a, b, c, d, e);
167                 },
168
169                 /**
170                  * Creates a confirm dialog. Please don't use the blocking behavior of this
171                  * native version use the callback method instead then it can be extended.
172                  *
173                  * @method confirm
174                  * @param {String} t Title for the new confirm dialog.
175                  * @param {function} cb Callback function to be executed after the user has selected ok or cancel.
176                  * @param {Object} s Optional scope to execute the callback in.
177                  * @example
178                  * // Displays an confirm box and an alert message will be displayed depending on what you choose in the confirm
179                  * tinyMCE.activeEditor.windowManager.confirm("Do you want to do something", function(s) {
180                  *    if (s)
181                  *       tinyMCE.activeEditor.windowManager.alert("Ok");
182                  *    else
183                  *       tinyMCE.activeEditor.windowManager.alert("Cancel");
184                  * });
185                  */
186                 confirm : function(t, cb, s, w) {
187                         w = w || window;
188
189                         cb.call(s || this, w.confirm(this._decode(this.editor.getLang(t, t))));
190                 },
191
192                 /**
193                  * Creates a alert dialog. Please don't use the blocking behavior of this
194                  * native version use the callback method instead then it can be extended.
195                  *
196                  * @method alert
197                  * @param {String} t Title for the new alert dialog.
198                  * @param {function} cb Callback function to be executed after the user has selected ok.
199                  * @param {Object} s Optional scope to execute the callback in.
200                  * @example
201                  * // Displays an alert box using the active editors window manager instance
202                  * tinyMCE.activeEditor.windowManager.alert('Hello world!');
203                  */
204                 alert : function(tx, cb, s, w) {
205                         var t = this;
206
207                         w = w || window;
208                         w.alert(t._decode(t.editor.getLang(tx, tx)));
209
210                         if (cb)
211                                 cb.call(s || t);
212                 },
213
214                 /**
215                  * Resizes the specified window or id.
216                  *
217                  * @param {Number} dw Delta width.
218                  * @param {Number} dh Delta height.
219                  * @param {window/id} win Window if the dialog isn't inline. Id if the dialog is inline.
220                  */
221                 resizeBy : function(dw, dh, win) {
222                         win.resizeBy(dw, dh);
223                 },
224
225                 // Internal functions
226
227                 _decode : function(s) {
228                         return tinymce.DOM.decode(s).replace(/\\n/g, '\n');
229                 }
230         });
231 }(tinymce));