/fa/jquery/jquery-ui/tiny_mce/plugins/inlinepopups/editor_plugin_src.js

https://bitbucket.org/gawel/fajquery · JavaScript · 635 lines · 454 code · 133 blank · 48 comment · 116 complexity · d56834d4575ad417e587201164dda415 MD5 · raw file

  1. /**
  2. * editor_plugin_src.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() {
  11. var DOM = tinymce.DOM, Element = tinymce.dom.Element, Event = tinymce.dom.Event, each = tinymce.each, is = tinymce.is;
  12. tinymce.create('tinymce.plugins.InlinePopups', {
  13. init : function(ed, url) {
  14. // Replace window manager
  15. ed.onBeforeRenderUI.add(function() {
  16. ed.windowManager = new tinymce.InlineWindowManager(ed);
  17. DOM.loadCSS(url + '/skins/' + (ed.settings.inlinepopups_skin || 'clearlooks2') + "/window.css");
  18. });
  19. },
  20. getInfo : function() {
  21. return {
  22. longname : 'InlinePopups',
  23. author : 'Moxiecode Systems AB',
  24. authorurl : 'http://tinymce.moxiecode.com',
  25. infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/inlinepopups',
  26. version : tinymce.majorVersion + "." + tinymce.minorVersion
  27. };
  28. }
  29. });
  30. tinymce.create('tinymce.InlineWindowManager:tinymce.WindowManager', {
  31. InlineWindowManager : function(ed) {
  32. var t = this;
  33. t.parent(ed);
  34. t.zIndex = 300000;
  35. t.count = 0;
  36. t.windows = {};
  37. },
  38. open : function(f, p) {
  39. var t = this, id, opt = '', ed = t.editor, dw = 0, dh = 0, vp, po, mdf, clf, we, w, u;
  40. f = f || {};
  41. p = p || {};
  42. // Run native windows
  43. if (!f.inline)
  44. return t.parent(f, p);
  45. // Only store selection if the type is a normal window
  46. if (!f.type)
  47. t.bookmark = ed.selection.getBookmark(1);
  48. id = DOM.uniqueId();
  49. vp = DOM.getViewPort();
  50. f.width = parseInt(f.width || 320);
  51. f.height = parseInt(f.height || 240) + (tinymce.isIE ? 8 : 0);
  52. f.min_width = parseInt(f.min_width || 150);
  53. f.min_height = parseInt(f.min_height || 100);
  54. f.max_width = parseInt(f.max_width || 2000);
  55. f.max_height = parseInt(f.max_height || 2000);
  56. f.left = f.left || Math.round(Math.max(vp.x, vp.x + (vp.w / 2.0) - (f.width / 2.0)));
  57. f.top = f.top || Math.round(Math.max(vp.y, vp.y + (vp.h / 2.0) - (f.height / 2.0)));
  58. f.movable = f.resizable = true;
  59. p.mce_width = f.width;
  60. p.mce_height = f.height;
  61. p.mce_inline = true;
  62. p.mce_window_id = id;
  63. p.mce_auto_focus = f.auto_focus;
  64. // Transpose
  65. // po = DOM.getPos(ed.getContainer());
  66. // f.left -= po.x;
  67. // f.top -= po.y;
  68. t.features = f;
  69. t.params = p;
  70. t.onOpen.dispatch(t, f, p);
  71. if (f.type) {
  72. opt += ' mceModal';
  73. if (f.type)
  74. opt += ' mce' + f.type.substring(0, 1).toUpperCase() + f.type.substring(1);
  75. f.resizable = false;
  76. }
  77. if (f.statusbar)
  78. opt += ' mceStatusbar';
  79. if (f.resizable)
  80. opt += ' mceResizable';
  81. if (f.minimizable)
  82. opt += ' mceMinimizable';
  83. if (f.maximizable)
  84. opt += ' mceMaximizable';
  85. if (f.movable)
  86. opt += ' mceMovable';
  87. // Create DOM objects
  88. t._addAll(DOM.doc.body,
  89. ['div', {id : id, 'class' : ed.settings.inlinepopups_skin || 'clearlooks2', style : 'width:100px;height:100px'},
  90. ['div', {id : id + '_wrapper', 'class' : 'mceWrapper' + opt},
  91. ['div', {id : id + '_top', 'class' : 'mceTop'},
  92. ['div', {'class' : 'mceLeft'}],
  93. ['div', {'class' : 'mceCenter'}],
  94. ['div', {'class' : 'mceRight'}],
  95. ['span', {id : id + '_title'}, f.title || '']
  96. ],
  97. ['div', {id : id + '_middle', 'class' : 'mceMiddle'},
  98. ['div', {id : id + '_left', 'class' : 'mceLeft'}],
  99. ['span', {id : id + '_content'}],
  100. ['div', {id : id + '_right', 'class' : 'mceRight'}]
  101. ],
  102. ['div', {id : id + '_bottom', 'class' : 'mceBottom'},
  103. ['div', {'class' : 'mceLeft'}],
  104. ['div', {'class' : 'mceCenter'}],
  105. ['div', {'class' : 'mceRight'}],
  106. ['span', {id : id + '_status'}, 'Content']
  107. ],
  108. ['a', {'class' : 'mceMove', tabindex : '-1', href : 'javascript:;'}],
  109. ['a', {'class' : 'mceMin', tabindex : '-1', href : 'javascript:;', onmousedown : 'return false;'}],
  110. ['a', {'class' : 'mceMax', tabindex : '-1', href : 'javascript:;', onmousedown : 'return false;'}],
  111. ['a', {'class' : 'mceMed', tabindex : '-1', href : 'javascript:;', onmousedown : 'return false;'}],
  112. ['a', {'class' : 'mceClose', tabindex : '-1', href : 'javascript:;', onmousedown : 'return false;'}],
  113. ['a', {id : id + '_resize_n', 'class' : 'mceResize mceResizeN', tabindex : '-1', href : 'javascript:;'}],
  114. ['a', {id : id + '_resize_s', 'class' : 'mceResize mceResizeS', tabindex : '-1', href : 'javascript:;'}],
  115. ['a', {id : id + '_resize_w', 'class' : 'mceResize mceResizeW', tabindex : '-1', href : 'javascript:;'}],
  116. ['a', {id : id + '_resize_e', 'class' : 'mceResize mceResizeE', tabindex : '-1', href : 'javascript:;'}],
  117. ['a', {id : id + '_resize_nw', 'class' : 'mceResize mceResizeNW', tabindex : '-1', href : 'javascript:;'}],
  118. ['a', {id : id + '_resize_ne', 'class' : 'mceResize mceResizeNE', tabindex : '-1', href : 'javascript:;'}],
  119. ['a', {id : id + '_resize_sw', 'class' : 'mceResize mceResizeSW', tabindex : '-1', href : 'javascript:;'}],
  120. ['a', {id : id + '_resize_se', 'class' : 'mceResize mceResizeSE', tabindex : '-1', href : 'javascript:;'}]
  121. ]
  122. ]
  123. );
  124. DOM.setStyles(id, {top : -10000, left : -10000});
  125. // Fix gecko rendering bug, where the editors iframe messed with window contents
  126. if (tinymce.isGecko)
  127. DOM.setStyle(id, 'overflow', 'auto');
  128. // Measure borders
  129. if (!f.type) {
  130. dw += DOM.get(id + '_left').clientWidth;
  131. dw += DOM.get(id + '_right').clientWidth;
  132. dh += DOM.get(id + '_top').clientHeight;
  133. dh += DOM.get(id + '_bottom').clientHeight;
  134. }
  135. // Resize window
  136. DOM.setStyles(id, {top : f.top, left : f.left, width : f.width + dw, height : f.height + dh});
  137. u = f.url || f.file;
  138. if (u) {
  139. if (tinymce.relaxedDomain)
  140. u += (u.indexOf('?') == -1 ? '?' : '&') + 'mce_rdomain=' + tinymce.relaxedDomain;
  141. u = tinymce._addVer(u);
  142. }
  143. if (!f.type) {
  144. DOM.add(id + '_content', 'iframe', {id : id + '_ifr', src : 'javascript:""', frameBorder : 0, style : 'border:0;width:10px;height:10px'});
  145. DOM.setStyles(id + '_ifr', {width : f.width, height : f.height});
  146. DOM.setAttrib(id + '_ifr', 'src', u);
  147. } else {
  148. DOM.add(id + '_wrapper', 'a', {id : id + '_ok', 'class' : 'mceButton mceOk', href : 'javascript:;', onmousedown : 'return false;'}, 'Ok');
  149. if (f.type == 'confirm')
  150. DOM.add(id + '_wrapper', 'a', {'class' : 'mceButton mceCancel', href : 'javascript:;', onmousedown : 'return false;'}, 'Cancel');
  151. DOM.add(id + '_middle', 'div', {'class' : 'mceIcon'});
  152. DOM.setHTML(id + '_content', f.content.replace('\n', '<br />'));
  153. }
  154. // Register events
  155. mdf = Event.add(id, 'mousedown', function(e) {
  156. var n = e.target, w, vp;
  157. w = t.windows[id];
  158. t.focus(id);
  159. if (n.nodeName == 'A' || n.nodeName == 'a') {
  160. if (n.className == 'mceMax') {
  161. w.oldPos = w.element.getXY();
  162. w.oldSize = w.element.getSize();
  163. vp = DOM.getViewPort();
  164. // Reduce viewport size to avoid scrollbars
  165. vp.w -= 2;
  166. vp.h -= 2;
  167. w.element.moveTo(vp.x, vp.y);
  168. w.element.resizeTo(vp.w, vp.h);
  169. DOM.setStyles(id + '_ifr', {width : vp.w - w.deltaWidth, height : vp.h - w.deltaHeight});
  170. DOM.addClass(id + '_wrapper', 'mceMaximized');
  171. } else if (n.className == 'mceMed') {
  172. // Reset to old size
  173. w.element.moveTo(w.oldPos.x, w.oldPos.y);
  174. w.element.resizeTo(w.oldSize.w, w.oldSize.h);
  175. w.iframeElement.resizeTo(w.oldSize.w - w.deltaWidth, w.oldSize.h - w.deltaHeight);
  176. DOM.removeClass(id + '_wrapper', 'mceMaximized');
  177. } else if (n.className == 'mceMove')
  178. return t._startDrag(id, e, n.className);
  179. else if (DOM.hasClass(n, 'mceResize'))
  180. return t._startDrag(id, e, n.className.substring(13));
  181. }
  182. });
  183. clf = Event.add(id, 'click', function(e) {
  184. var n = e.target;
  185. t.focus(id);
  186. if (n.nodeName == 'A' || n.nodeName == 'a') {
  187. switch (n.className) {
  188. case 'mceClose':
  189. t.close(null, id);
  190. return Event.cancel(e);
  191. case 'mceButton mceOk':
  192. case 'mceButton mceCancel':
  193. f.button_func(n.className == 'mceButton mceOk');
  194. return Event.cancel(e);
  195. }
  196. }
  197. });
  198. // Add window
  199. w = t.windows[id] = {
  200. id : id,
  201. mousedown_func : mdf,
  202. click_func : clf,
  203. element : new Element(id, {blocker : 1, container : ed.getContainer()}),
  204. iframeElement : new Element(id + '_ifr'),
  205. features : f,
  206. deltaWidth : dw,
  207. deltaHeight : dh
  208. };
  209. w.iframeElement.on('focus', function() {
  210. t.focus(id);
  211. });
  212. // Setup blocker
  213. if (t.count == 0 && t.editor.getParam('dialog_type', 'modal') == 'modal') {
  214. DOM.add(DOM.doc.body, 'div', {
  215. id : 'mceModalBlocker',
  216. 'class' : (t.editor.settings.inlinepopups_skin || 'clearlooks2') + '_modalBlocker',
  217. style : {zIndex : t.zIndex - 1}
  218. });
  219. DOM.show('mceModalBlocker'); // Reduces flicker in IE
  220. } else
  221. DOM.setStyle('mceModalBlocker', 'z-index', t.zIndex - 1);
  222. if (tinymce.isIE6 || /Firefox\/2\./.test(navigator.userAgent) || (tinymce.isIE && !DOM.boxModel))
  223. DOM.setStyles('mceModalBlocker', {position : 'absolute', left : vp.x, top : vp.y, width : vp.w - 2, height : vp.h - 2});
  224. t.focus(id);
  225. t._fixIELayout(id, 1);
  226. // Focus ok button
  227. if (DOM.get(id + '_ok'))
  228. DOM.get(id + '_ok').focus();
  229. t.count++;
  230. return w;
  231. },
  232. focus : function(id) {
  233. var t = this, w;
  234. if (w = t.windows[id]) {
  235. w.zIndex = this.zIndex++;
  236. w.element.setStyle('zIndex', w.zIndex);
  237. w.element.update();
  238. id = id + '_wrapper';
  239. DOM.removeClass(t.lastId, 'mceFocus');
  240. DOM.addClass(id, 'mceFocus');
  241. t.lastId = id;
  242. }
  243. },
  244. _addAll : function(te, ne) {
  245. var i, n, t = this, dom = tinymce.DOM;
  246. if (is(ne, 'string'))
  247. te.appendChild(dom.doc.createTextNode(ne));
  248. else if (ne.length) {
  249. te = te.appendChild(dom.create(ne[0], ne[1]));
  250. for (i=2; i<ne.length; i++)
  251. t._addAll(te, ne[i]);
  252. }
  253. },
  254. _startDrag : function(id, se, ac) {
  255. var t = this, mu, mm, d = DOM.doc, eb, w = t.windows[id], we = w.element, sp = we.getXY(), p, sz, ph, cp, vp, sx, sy, sex, sey, dx, dy, dw, dh;
  256. // Get positons and sizes
  257. // cp = DOM.getPos(t.editor.getContainer());
  258. cp = {x : 0, y : 0};
  259. vp = DOM.getViewPort();
  260. // Reduce viewport size to avoid scrollbars while dragging
  261. vp.w -= 2;
  262. vp.h -= 2;
  263. sex = se.screenX;
  264. sey = se.screenY;
  265. dx = dy = dw = dh = 0;
  266. // Handle mouse up
  267. mu = Event.add(d, 'mouseup', function(e) {
  268. Event.remove(d, 'mouseup', mu);
  269. Event.remove(d, 'mousemove', mm);
  270. if (eb)
  271. eb.remove();
  272. we.moveBy(dx, dy);
  273. we.resizeBy(dw, dh);
  274. sz = we.getSize();
  275. DOM.setStyles(id + '_ifr', {width : sz.w - w.deltaWidth, height : sz.h - w.deltaHeight});
  276. t._fixIELayout(id, 1);
  277. return Event.cancel(e);
  278. });
  279. if (ac != 'Move')
  280. startMove();
  281. function startMove() {
  282. if (eb)
  283. return;
  284. t._fixIELayout(id, 0);
  285. // Setup event blocker
  286. DOM.add(d.body, 'div', {
  287. id : 'mceEventBlocker',
  288. 'class' : 'mceEventBlocker ' + (t.editor.settings.inlinepopups_skin || 'clearlooks2'),
  289. style : {zIndex : t.zIndex + 1}
  290. });
  291. if (tinymce.isIE6 || (tinymce.isIE && !DOM.boxModel))
  292. DOM.setStyles('mceEventBlocker', {position : 'absolute', left : vp.x, top : vp.y, width : vp.w - 2, height : vp.h - 2});
  293. eb = new Element('mceEventBlocker');
  294. eb.update();
  295. // Setup placeholder
  296. p = we.getXY();
  297. sz = we.getSize();
  298. sx = cp.x + p.x - vp.x;
  299. sy = cp.y + p.y - vp.y;
  300. DOM.add(eb.get(), 'div', {id : 'mcePlaceHolder', 'class' : 'mcePlaceHolder', style : {left : sx, top : sy, width : sz.w, height : sz.h}});
  301. ph = new Element('mcePlaceHolder');
  302. };
  303. // Handle mouse move/drag
  304. mm = Event.add(d, 'mousemove', function(e) {
  305. var x, y, v;
  306. startMove();
  307. x = e.screenX - sex;
  308. y = e.screenY - sey;
  309. switch (ac) {
  310. case 'ResizeW':
  311. dx = x;
  312. dw = 0 - x;
  313. break;
  314. case 'ResizeE':
  315. dw = x;
  316. break;
  317. case 'ResizeN':
  318. case 'ResizeNW':
  319. case 'ResizeNE':
  320. if (ac == "ResizeNW") {
  321. dx = x;
  322. dw = 0 - x;
  323. } else if (ac == "ResizeNE")
  324. dw = x;
  325. dy = y;
  326. dh = 0 - y;
  327. break;
  328. case 'ResizeS':
  329. case 'ResizeSW':
  330. case 'ResizeSE':
  331. if (ac == "ResizeSW") {
  332. dx = x;
  333. dw = 0 - x;
  334. } else if (ac == "ResizeSE")
  335. dw = x;
  336. dh = y;
  337. break;
  338. case 'mceMove':
  339. dx = x;
  340. dy = y;
  341. break;
  342. }
  343. // Boundary check
  344. if (dw < (v = w.features.min_width - sz.w)) {
  345. if (dx !== 0)
  346. dx += dw - v;
  347. dw = v;
  348. }
  349. if (dh < (v = w.features.min_height - sz.h)) {
  350. if (dy !== 0)
  351. dy += dh - v;
  352. dh = v;
  353. }
  354. dw = Math.min(dw, w.features.max_width - sz.w);
  355. dh = Math.min(dh, w.features.max_height - sz.h);
  356. dx = Math.max(dx, vp.x - (sx + vp.x));
  357. dy = Math.max(dy, vp.y - (sy + vp.y));
  358. dx = Math.min(dx, (vp.w + vp.x) - (sx + sz.w + vp.x));
  359. dy = Math.min(dy, (vp.h + vp.y) - (sy + sz.h + vp.y));
  360. // Move if needed
  361. if (dx + dy !== 0) {
  362. if (sx + dx < 0)
  363. dx = 0;
  364. if (sy + dy < 0)
  365. dy = 0;
  366. ph.moveTo(sx + dx, sy + dy);
  367. }
  368. // Resize if needed
  369. if (dw + dh !== 0)
  370. ph.resizeTo(sz.w + dw, sz.h + dh);
  371. return Event.cancel(e);
  372. });
  373. return Event.cancel(se);
  374. },
  375. resizeBy : function(dw, dh, id) {
  376. var w = this.windows[id];
  377. if (w) {
  378. w.element.resizeBy(dw, dh);
  379. w.iframeElement.resizeBy(dw, dh);
  380. }
  381. },
  382. close : function(win, id) {
  383. var t = this, w, d = DOM.doc, ix = 0, fw, id;
  384. id = t._findId(id || win);
  385. // Probably not inline
  386. if (!t.windows[id]) {
  387. t.parent(win);
  388. return;
  389. }
  390. t.count--;
  391. if (t.count == 0)
  392. DOM.remove('mceModalBlocker');
  393. if (w = t.windows[id]) {
  394. t.onClose.dispatch(t);
  395. Event.remove(d, 'mousedown', w.mousedownFunc);
  396. Event.remove(d, 'click', w.clickFunc);
  397. Event.clear(id);
  398. Event.clear(id + '_ifr');
  399. DOM.setAttrib(id + '_ifr', 'src', 'javascript:""'); // Prevent leak
  400. w.element.remove();
  401. delete t.windows[id];
  402. // Find front most window and focus that
  403. each (t.windows, function(w) {
  404. if (w.zIndex > ix) {
  405. fw = w;
  406. ix = w.zIndex;
  407. }
  408. });
  409. if (fw)
  410. t.focus(fw.id);
  411. }
  412. },
  413. setTitle : function(w, ti) {
  414. var e;
  415. w = this._findId(w);
  416. if (e = DOM.get(w + '_title'))
  417. e.innerHTML = DOM.encode(ti);
  418. },
  419. alert : function(txt, cb, s) {
  420. var t = this, w;
  421. w = t.open({
  422. title : t,
  423. type : 'alert',
  424. button_func : function(s) {
  425. if (cb)
  426. cb.call(s || t, s);
  427. t.close(null, w.id);
  428. },
  429. content : DOM.encode(t.editor.getLang(txt, txt)),
  430. inline : 1,
  431. width : 400,
  432. height : 130
  433. });
  434. },
  435. confirm : function(txt, cb, s) {
  436. var t = this, w;
  437. w = t.open({
  438. title : t,
  439. type : 'confirm',
  440. button_func : function(s) {
  441. if (cb)
  442. cb.call(s || t, s);
  443. t.close(null, w.id);
  444. },
  445. content : DOM.encode(t.editor.getLang(txt, txt)),
  446. inline : 1,
  447. width : 400,
  448. height : 130
  449. });
  450. },
  451. // Internal functions
  452. _findId : function(w) {
  453. var t = this;
  454. if (typeof(w) == 'string')
  455. return w;
  456. each(t.windows, function(wo) {
  457. var ifr = DOM.get(wo.id + '_ifr');
  458. if (ifr && w == ifr.contentWindow) {
  459. w = wo.id;
  460. return false;
  461. }
  462. });
  463. return w;
  464. },
  465. _fixIELayout : function(id, s) {
  466. var w, img;
  467. if (!tinymce.isIE6)
  468. return;
  469. // Fixes the bug where hover flickers and does odd things in IE6
  470. each(['n','s','w','e','nw','ne','sw','se'], function(v) {
  471. var e = DOM.get(id + '_resize_' + v);
  472. DOM.setStyles(e, {
  473. width : s ? e.clientWidth : '',
  474. height : s ? e.clientHeight : '',
  475. cursor : DOM.getStyle(e, 'cursor', 1)
  476. });
  477. DOM.setStyle(id + "_bottom", 'bottom', '-1px');
  478. e = 0;
  479. });
  480. // Fixes graphics glitch
  481. if (w = this.windows[id]) {
  482. // Fixes rendering bug after resize
  483. w.element.hide();
  484. w.element.show();
  485. // Forced a repaint of the window
  486. //DOM.get(id).style.filter = '';
  487. // IE has a bug where images used in CSS won't get loaded
  488. // sometimes when the cache in the browser is disabled
  489. // This fix tries to solve it by loading the images using the image object
  490. each(DOM.select('div,a', id), function(e, i) {
  491. if (e.currentStyle.backgroundImage != 'none') {
  492. img = new Image();
  493. img.src = e.currentStyle.backgroundImage.replace(/url\(\"(.+)\"\)/, '$1');
  494. }
  495. });
  496. DOM.get(id).style.filter = '';
  497. }
  498. }
  499. });
  500. // Register plugin
  501. tinymce.PluginManager.add('inlinepopups', tinymce.plugins.InlinePopups);
  502. })();