WindowManager.js 7.8 KB

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