tiny_mce_popup.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /**
  2. * tinymce_mce_popup.js
  3. *
  4. * Released under LGPL License.
  5. * Copyright (c) 1999-2017 Ephox Corp. All rights reserved
  6. *
  7. * License: http://www.tinymce.com/license
  8. * Contributing: http://www.tinymce.com/contributing
  9. */
  10. var tinymce, tinyMCE;
  11. /**
  12. * TinyMCE popup/dialog helper class. This gives you easy access to the
  13. * parent editor instance and a bunch of other things. It's higly recommended
  14. * that you load this script into your dialogs.
  15. *
  16. * @static
  17. * @class tinyMCEPopup
  18. */
  19. var tinyMCEPopup = {
  20. /**
  21. * Initializes the popup this will be called automatically.
  22. *
  23. * @method init
  24. */
  25. init: function () {
  26. var self = this, parentWin, settings, uiWindow;
  27. // Find window & API
  28. parentWin = self.getWin();
  29. tinymce = tinyMCE = parentWin.tinymce;
  30. self.editor = tinymce.EditorManager.activeEditor;
  31. self.params = self.editor.windowManager.getParams();
  32. uiWindow = self.editor.windowManager.windows[self.editor.windowManager.windows.length - 1];
  33. self.features = uiWindow.features;
  34. self.uiWindow = uiWindow;
  35. settings = self.editor.settings;
  36. // Setup popup CSS path(s)
  37. if (settings.popup_css !== false) {
  38. if (settings.popup_css) {
  39. settings.popup_css = self.editor.documentBaseURI.toAbsolute(settings.popup_css);
  40. } else {
  41. settings.popup_css = self.editor.baseURI.toAbsolute("plugins/compat3x/css/dialog.css");
  42. }
  43. }
  44. if (settings.popup_css_add) {
  45. settings.popup_css += ',' + self.editor.documentBaseURI.toAbsolute(settings.popup_css_add);
  46. }
  47. // Setup local DOM
  48. self.dom = self.editor.windowManager.createInstance('tinymce.dom.DOMUtils', document, {
  49. ownEvents: true,
  50. proxy: tinyMCEPopup._eventProxy
  51. });
  52. self.dom.bind(window, 'ready', self._onDOMLoaded, self);
  53. // Enables you to skip loading the default css
  54. if (self.features.popup_css !== false) {
  55. self.dom.loadCSS(self.features.popup_css || self.editor.settings.popup_css);
  56. }
  57. // Setup on init listeners
  58. self.listeners = [];
  59. /**
  60. * Fires when the popup is initialized.
  61. *
  62. * @event onInit
  63. * @param {tinymce.Editor} editor Editor instance.
  64. * @example
  65. * // Alerts the selected contents when the dialog is loaded
  66. * tinyMCEPopup.onInit.add(function(ed) {
  67. * alert(ed.selection.getContent());
  68. * });
  69. *
  70. * // Executes the init method on page load in some object using the SomeObject scope
  71. * tinyMCEPopup.onInit.add(SomeObject.init, SomeObject);
  72. */
  73. self.onInit = {
  74. add: function (func, scope) {
  75. self.listeners.push({ func: func, scope: scope });
  76. }
  77. };
  78. self.isWindow = !self.getWindowArg('mce_inline');
  79. self.id = self.getWindowArg('mce_window_id');
  80. },
  81. /**
  82. * Returns the reference to the parent window that opened the dialog.
  83. *
  84. * @method getWin
  85. * @return {Window} Reference to the parent window that opened the dialog.
  86. */
  87. getWin: function () {
  88. // Added frameElement check to fix bug: #2817583
  89. return (!window.frameElement && window.dialogArguments) || opener || parent || top;
  90. },
  91. /**
  92. * Returns a window argument/parameter by name.
  93. *
  94. * @method getWindowArg
  95. * @param {String} name Name of the window argument to retrieve.
  96. * @param {String} defaultValue Optional default value to return.
  97. * @return {String} Argument value or default value if it wasn't found.
  98. */
  99. getWindowArg: function (name, defaultValue) {
  100. var value = this.params[name];
  101. return tinymce.is(value) ? value : defaultValue;
  102. },
  103. /**
  104. * Returns a editor parameter/config option value.
  105. *
  106. * @method getParam
  107. * @param {String} name Name of the editor config option to retrieve.
  108. * @param {String} defaultValue Optional default value to return.
  109. * @return {String} Parameter value or default value if it wasn't found.
  110. */
  111. getParam: function (name, defaultValue) {
  112. return this.editor.getParam(name, defaultValue);
  113. },
  114. /**
  115. * Returns a language item by key.
  116. *
  117. * @method getLang
  118. * @param {String} name Language item like mydialog.something.
  119. * @param {String} defaultValue Optional default value to return.
  120. * @return {String} Language value for the item like "my string" or the default value if it wasn't found.
  121. */
  122. getLang: function (name, defaultValue) {
  123. return this.editor.getLang(name, defaultValue);
  124. },
  125. /**
  126. * Executed a command on editor that opened the dialog/popup.
  127. *
  128. * @method execCommand
  129. * @param {String} cmd Command to execute.
  130. * @param {Boolean} ui Optional boolean value if the UI for the command should be presented or not.
  131. * @param {Object} val Optional value to pass with the comman like an URL.
  132. * @param {Object} a Optional arguments object.
  133. */
  134. execCommand: function (cmd, ui, val, args) {
  135. args = args || {};
  136. args.skip_focus = 1;
  137. this.restoreSelection();
  138. return this.editor.execCommand(cmd, ui, val, args);
  139. },
  140. /**
  141. * Resizes the dialog to the inner size of the window. This is needed since various browsers
  142. * have different border sizes on windows.
  143. *
  144. * @method resizeToInnerSize
  145. */
  146. resizeToInnerSize: function () {
  147. /*var self = this;
  148. // Detach it to workaround a Chrome specific bug
  149. // https://sourceforge.net/tracker/?func=detail&atid=635682&aid=2926339&group_id=103281
  150. setTimeout(function() {
  151. var vp = self.dom.getViewPort(window);
  152. self.editor.windowManager.resizeBy(
  153. self.getWindowArg('mce_width') - vp.w,
  154. self.getWindowArg('mce_height') - vp.h,
  155. self.id || window
  156. );
  157. }, 10);*/
  158. },
  159. /**
  160. * Will executed the specified string when the page has been loaded. This function
  161. * was added for compatibility with the 2.x branch.
  162. *
  163. * @method executeOnLoad
  164. * @param {String} evil String to evalutate on init.
  165. */
  166. executeOnLoad: function (evil) {
  167. this.onInit.add(function () {
  168. eval(evil);
  169. });
  170. },
  171. /**
  172. * Stores the current editor selection for later restoration. This can be useful since some browsers
  173. * looses it's selection if a control element is selected/focused inside the dialogs.
  174. *
  175. * @method storeSelection
  176. */
  177. storeSelection: function () {
  178. this.editor.windowManager.bookmark = tinyMCEPopup.editor.selection.getBookmark(1);
  179. },
  180. /**
  181. * Restores any stored selection. This can be useful since some browsers
  182. * looses it's selection if a control element is selected/focused inside the dialogs.
  183. *
  184. * @method restoreSelection
  185. */
  186. restoreSelection: function () {
  187. var self = tinyMCEPopup;
  188. if (!self.isWindow && tinymce.isIE) {
  189. self.editor.selection.moveToBookmark(self.editor.windowManager.bookmark);
  190. }
  191. },
  192. /**
  193. * Loads a specific dialog language pack. If you pass in plugin_url as a argument
  194. * when you open the window it will load the <plugin url>/langs/<code>_dlg.js lang pack file.
  195. *
  196. * @method requireLangPack
  197. */
  198. requireLangPack: function () {
  199. var self = this, url = self.getWindowArg('plugin_url') || self.getWindowArg('theme_url'), settings = self.editor.settings, lang;
  200. if (settings.language !== false) {
  201. lang = settings.language || "en";
  202. }
  203. if (url && lang && self.features.translate_i18n !== false && settings.language_load !== false) {
  204. url += '/langs/' + lang + '_dlg.js';
  205. if (!tinymce.ScriptLoader.isDone(url)) {
  206. document.write('<script type="text/javascript" src="' + url + '"></script>');
  207. tinymce.ScriptLoader.markDone(url);
  208. }
  209. }
  210. },
  211. /**
  212. * Executes a color picker on the specified element id. When the user
  213. * then selects a color it will be set as the value of the specified element.
  214. *
  215. * @method pickColor
  216. * @param {DOMEvent} e DOM event object.
  217. * @param {string} element_id Element id to be filled with the color value from the picker.
  218. */
  219. pickColor: function (e, element_id) {
  220. var el = document.getElementById(element_id), colorPickerCallback = this.editor.settings.color_picker_callback;
  221. if (colorPickerCallback) {
  222. colorPickerCallback.call(
  223. this.editor,
  224. function (value) {
  225. el.value = value;
  226. try {
  227. el.onchange();
  228. } catch (ex) {
  229. // Try fire event, ignore errors
  230. }
  231. },
  232. el.value
  233. );
  234. }
  235. },
  236. /**
  237. * Opens a filebrowser/imagebrowser this will set the output value from
  238. * the browser as a value on the specified element.
  239. *
  240. * @method openBrowser
  241. * @param {string} element_id Id of the element to set value in.
  242. * @param {string} type Type of browser to open image/file/flash.
  243. * @param {string} option Option name to get the file_broswer_callback function name from.
  244. */
  245. openBrowser: function (element_id, type) {
  246. tinyMCEPopup.restoreSelection();
  247. this.editor.execCallback('file_browser_callback', element_id, document.getElementById(element_id).value, type, window);
  248. },
  249. /**
  250. * Creates a confirm dialog. Please don't use the blocking behavior of this
  251. * native version use the callback method instead then it can be extended.
  252. *
  253. * @method confirm
  254. * @param {String} t Title for the new confirm dialog.
  255. * @param {function} cb Callback function to be executed after the user has selected ok or cancel.
  256. * @param {Object} s Optional scope to execute the callback in.
  257. */
  258. confirm: function (t, cb, s) {
  259. this.editor.windowManager.confirm(t, cb, s, window);
  260. },
  261. /**
  262. * Creates a alert dialog. Please don't use the blocking behavior of this
  263. * native version use the callback method instead then it can be extended.
  264. *
  265. * @method alert
  266. * @param {String} tx Title for the new alert dialog.
  267. * @param {function} cb Callback function to be executed after the user has selected ok.
  268. * @param {Object} s Optional scope to execute the callback in.
  269. */
  270. alert: function (tx, cb, s) {
  271. this.editor.windowManager.alert(tx, cb, s, window);
  272. },
  273. /**
  274. * Closes the current window.
  275. *
  276. * @method close
  277. */
  278. close: function () {
  279. var t = this;
  280. // To avoid domain relaxing issue in Opera
  281. function close() {
  282. t.editor.windowManager.close(window);
  283. tinymce = tinyMCE = t.editor = t.params = t.dom = t.dom.doc = null; // Cleanup
  284. }
  285. if (tinymce.isOpera) {
  286. t.getWin().setTimeout(close, 0);
  287. } else {
  288. close();
  289. }
  290. },
  291. // Internal functions
  292. _restoreSelection: function () {
  293. var e = window.event.srcElement;
  294. if (e.nodeName == 'INPUT' && (e.type == 'submit' || e.type == 'button')) {
  295. tinyMCEPopup.restoreSelection();
  296. }
  297. },
  298. /* _restoreSelection : function() {
  299. var e = window.event.srcElement;
  300. // If user focus a non text input or textarea
  301. if ((e.nodeName != 'INPUT' && e.nodeName != 'TEXTAREA') || e.type != 'text')
  302. tinyMCEPopup.restoreSelection();
  303. },*/
  304. _onDOMLoaded: function () {
  305. var t = tinyMCEPopup, ti = document.title, h, nv;
  306. // Translate page
  307. if (t.features.translate_i18n !== false) {
  308. var map = {
  309. "update": "Ok",
  310. "insert": "Ok",
  311. "cancel": "Cancel",
  312. "not_set": "--",
  313. "class_name": "Class name",
  314. "browse": "Browse"
  315. };
  316. var langCode = (tinymce.settings ? tinymce.settings : t.editor.settings).language || 'en';
  317. for (var key in map) {
  318. tinymce.i18n.data[langCode + "." + key] = tinymce.i18n.translate(map[key]);
  319. }
  320. h = document.body.innerHTML;
  321. // Replace a=x with a="x" in IE
  322. if (tinymce.isIE) {
  323. h = h.replace(/ (value|title|alt)=([^"][^\s>]+)/gi, ' $1="$2"');
  324. }
  325. document.dir = t.editor.getParam('directionality', '');
  326. if ((nv = t.editor.translate(h)) && nv != h) {
  327. document.body.innerHTML = nv;
  328. }
  329. if ((nv = t.editor.translate(ti)) && nv != ti) {
  330. document.title = ti = nv;
  331. }
  332. }
  333. if (!t.editor.getParam('browser_preferred_colors', false) || !t.isWindow) {
  334. t.dom.addClass(document.body, 'forceColors');
  335. }
  336. document.body.style.display = '';
  337. // Restore selection in IE when focus is placed on a non textarea or input element of the type text
  338. if (tinymce.Env.ie) {
  339. if (tinymce.Env.ie < 11) {
  340. document.attachEvent('onmouseup', tinyMCEPopup._restoreSelection);
  341. // Add base target element for it since it would fail with modal dialogs
  342. t.dom.add(t.dom.select('head')[0], 'base', { target: '_self' });
  343. } else {
  344. document.addEventListener('mouseup', tinyMCEPopup._restoreSelection, false);
  345. }
  346. }
  347. t.restoreSelection();
  348. t.resizeToInnerSize();
  349. // Set inline title
  350. if (!t.isWindow) {
  351. t.editor.windowManager.setTitle(window, ti);
  352. } else {
  353. window.focus();
  354. }
  355. if (!tinymce.isIE && !t.isWindow) {
  356. t.dom.bind(document, 'focus', function () {
  357. t.editor.windowManager.focus(t.id);
  358. });
  359. }
  360. // Patch for accessibility
  361. tinymce.each(t.dom.select('select'), function (e) {
  362. e.onkeydown = tinyMCEPopup._accessHandler;
  363. });
  364. // Call onInit
  365. // Init must be called before focus so the selection won't get lost by the focus call
  366. tinymce.each(t.listeners, function (o) {
  367. o.func.call(o.scope, t.editor);
  368. });
  369. // Move focus to window
  370. if (t.getWindowArg('mce_auto_focus', true)) {
  371. window.focus();
  372. // Focus element with mceFocus class
  373. tinymce.each(document.forms, function (f) {
  374. tinymce.each(f.elements, function (e) {
  375. if (t.dom.hasClass(e, 'mceFocus') && !e.disabled) {
  376. e.focus();
  377. return false; // Break loop
  378. }
  379. });
  380. });
  381. }
  382. document.onkeyup = tinyMCEPopup._closeWinKeyHandler;
  383. if ('textContent' in document) {
  384. t.uiWindow.getEl('head').firstChild.textContent = document.title;
  385. } else {
  386. t.uiWindow.getEl('head').firstChild.innerText = document.title;
  387. }
  388. },
  389. _accessHandler: function (e) {
  390. e = e || window.event;
  391. if (e.keyCode == 13 || e.keyCode == 32) {
  392. var elm = e.target || e.srcElement;
  393. if (elm.onchange) {
  394. elm.onchange();
  395. }
  396. return tinymce.dom.Event.cancel(e);
  397. }
  398. },
  399. _closeWinKeyHandler: function (e) {
  400. e = e || window.event;
  401. if (e.keyCode == 27) {
  402. tinyMCEPopup.close();
  403. }
  404. },
  405. _eventProxy: function (id) {
  406. return function (evt) {
  407. tinyMCEPopup.dom.events.callNativeHandler(id, evt);
  408. };
  409. }
  410. };
  411. tinyMCEPopup.init();
  412. tinymce.util.Dispatcher = function (scope) {
  413. this.scope = scope || this;
  414. this.listeners = [];
  415. this.add = function (callback, scope) {
  416. this.listeners.push({ cb: callback, scope: scope || this.scope });
  417. return callback;
  418. };
  419. this.addToTop = function (callback, scope) {
  420. var self = this, listener = { cb: callback, scope: scope || self.scope };
  421. // Create new listeners if addToTop is executed in a dispatch loop
  422. if (self.inDispatch) {
  423. self.listeners = [listener].concat(self.listeners);
  424. } else {
  425. self.listeners.unshift(listener);
  426. }
  427. return callback;
  428. };
  429. this.remove = function (callback) {
  430. var listeners = this.listeners, output = null;
  431. tinymce.each(listeners, function (listener, i) {
  432. if (callback == listener.cb) {
  433. output = listener;
  434. listeners.splice(i, 1);
  435. return false;
  436. }
  437. });
  438. return output;
  439. };
  440. this.dispatch = function () {
  441. var self = this, returnValue, args = arguments, i, listeners = self.listeners, listener;
  442. self.inDispatch = true;
  443. // Needs to be a real loop since the listener count might change while looping
  444. // And this is also more efficient
  445. for (i = 0; i < listeners.length; i++) {
  446. listener = listeners[i];
  447. returnValue = listener.cb.apply(listener.scope, args.length > 0 ? args : [listener.scope]);
  448. if (returnValue === false) {
  449. break;
  450. }
  451. }
  452. self.inDispatch = false;
  453. return returnValue;
  454. };
  455. };