WindowManager.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * WindowManager.js
  3. *
  4. * Copyright, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. /**
  11. * This class handles the creation of native windows and dialogs. This class can be extended to provide for example inline dialogs.
  12. *
  13. * @class tinymce.WindowManager
  14. * @example
  15. * // Opens a new dialog with the file.htm file and the size 320x240
  16. * // It also adds a custom parameter this can be retrieved by using tinyMCEPopup.getWindowArg inside the dialog.
  17. * tinymce.activeEditor.windowManager.open({
  18. * url: 'file.htm',
  19. * width: 320,
  20. * height: 240
  21. * }, {
  22. * custom_param: 1
  23. * });
  24. *
  25. * // Displays an alert box using the active editors window manager instance
  26. * tinymce.activeEditor.windowManager.alert('Hello world!');
  27. *
  28. * // Displays an confirm box and an alert message will be displayed depending on what you choose in the confirm
  29. * tinymce.activeEditor.windowManager.confirm("Do you want to do something", function(s) {
  30. * if (s)
  31. * tinymce.activeEditor.windowManager.alert("Ok");
  32. * else
  33. * tinymce.activeEditor.windowManager.alert("Cancel");
  34. * });
  35. */
  36. define("tinymce/WindowManager", [
  37. "tinymce/ui/Window",
  38. "tinymce/ui/MessageBox"
  39. ], function(Window, MessageBox) {
  40. return function(editor) {
  41. var self = this, windows = [];
  42. function getTopMostWindow() {
  43. if (windows.length) {
  44. return windows[windows.length - 1];
  45. }
  46. }
  47. self.windows = windows;
  48. /**
  49. * Opens a new window.
  50. *
  51. * @method open
  52. * @param {Object} args Optional name/value settings collection contains things like width/height/url etc.
  53. * @option {String} title Window title.
  54. * @option {String} file URL of the file to open in the window.
  55. * @option {Number} width Width in pixels.
  56. * @option {Number} height Height in pixels.
  57. * @option {Boolean} resizable Specifies whether the popup window is resizable or not.
  58. * @option {Boolean} maximizable Specifies whether the popup window has a "maximize" button and can get maximized or not.
  59. * @option {String/Boolean} scrollbars Specifies whether the popup window can have scrollbars if required (i.e. content
  60. * larger than the popup size specified).
  61. */
  62. self.open = function(args, params) {
  63. var win;
  64. editor.editorManager.activeEditor = editor;
  65. args.title = args.title || ' ';
  66. // Handle URL
  67. args.url = args.url || args.file; // Legacy
  68. if (args.url) {
  69. args.width = parseInt(args.width || 320, 10);
  70. args.height = parseInt(args.height || 240, 10);
  71. }
  72. // Handle body
  73. if (args.body) {
  74. args.items = {
  75. defaults: args.defaults,
  76. type: args.bodyType || 'form',
  77. items: args.body
  78. };
  79. }
  80. if (!args.url && !args.buttons) {
  81. args.buttons = [
  82. {text: 'Ok', subtype: 'primary', onclick: function() {
  83. win.find('form')[0].submit();
  84. }},
  85. {text: 'Cancel', onclick: function() {
  86. win.close();
  87. }}
  88. ];
  89. }
  90. win = new Window(args);
  91. windows.push(win);
  92. win.on('close', function() {
  93. var i = windows.length;
  94. while (i--) {
  95. if (windows[i] === win) {
  96. windows.splice(i, 1);
  97. }
  98. }
  99. editor.focus();
  100. });
  101. // Handle data
  102. if (args.data) {
  103. win.on('postRender', function() {
  104. this.find('*').each(function(ctrl) {
  105. var name = ctrl.name();
  106. if (name in args.data) {
  107. ctrl.value(args.data[name]);
  108. }
  109. });
  110. });
  111. }
  112. // store args and parameters
  113. win.features = args || {};
  114. win.params = params || {};
  115. // Takes a snapshot in the FocusManager of the selection before focus is lost to dialog
  116. editor.nodeChanged();
  117. return win.renderTo().reflow();
  118. };
  119. /**
  120. * Creates a alert dialog. Please don't use the blocking behavior of this
  121. * native version use the callback method instead then it can be extended.
  122. *
  123. * @method alert
  124. * @param {String} message Text to display in the new alert dialog.
  125. * @param {function} callback Callback function to be executed after the user has selected ok.
  126. * @param {Object} scope Optional scope to execute the callback in.
  127. * @example
  128. * // Displays an alert box using the active editors window manager instance
  129. * tinymce.activeEditor.windowManager.alert('Hello world!');
  130. */
  131. self.alert = function(message, callback, scope) {
  132. MessageBox.alert(message, function() {
  133. if (callback) {
  134. callback.call(scope || this);
  135. } else {
  136. editor.focus();
  137. }
  138. });
  139. };
  140. /**
  141. * Creates a confirm dialog. Please don't use the blocking behavior of this
  142. * native version use the callback method instead then it can be extended.
  143. *
  144. * @method confirm
  145. * @param {String} messageText to display in the new confirm dialog.
  146. * @param {function} callback Callback function to be executed after the user has selected ok or cancel.
  147. * @param {Object} scope Optional scope to execute the callback in.
  148. * @example
  149. * // Displays an confirm box and an alert message will be displayed depending on what you choose in the confirm
  150. * tinymce.activeEditor.windowManager.confirm("Do you want to do something", function(s) {
  151. * if (s)
  152. * tinymce.activeEditor.windowManager.alert("Ok");
  153. * else
  154. * tinymce.activeEditor.windowManager.alert("Cancel");
  155. * });
  156. */
  157. self.confirm = function(message, callback, scope) {
  158. MessageBox.confirm(message, function(state) {
  159. callback.call(scope || this, state);
  160. });
  161. };
  162. /**
  163. * Closes the top most window.
  164. *
  165. * @method close
  166. */
  167. self.close = function() {
  168. if (getTopMostWindow()) {
  169. getTopMostWindow().close();
  170. }
  171. };
  172. /**
  173. * Returns the params of the last window open call. This can be used in iframe based
  174. * dialog to get params passed from the tinymce plugin.
  175. *
  176. * @example
  177. * var dialogArguments = top.tinymce.activeEditor.windowManager.getParams();
  178. *
  179. * @method getParams
  180. * @return {Object} Name/value object with parameters passed from windowManager.open call.
  181. */
  182. self.getParams = function() {
  183. return getTopMostWindow() ? getTopMostWindow().params : null;
  184. };
  185. /**
  186. * Sets the params of the last opened window.
  187. *
  188. * @method setParams
  189. * @param {Object} params Params object to set for the last opened window.
  190. */
  191. self.setParams = function(params) {
  192. if (getTopMostWindow()) {
  193. getTopMostWindow().params = params;
  194. }
  195. };
  196. /**
  197. * Returns the currently opened window objects.
  198. *
  199. * @method getWindows
  200. * @return {Array} Array of the currently opened windows.
  201. */
  202. self.getWindows = function() {
  203. return windows;
  204. };
  205. };
  206. });