/static/scripts/tiny_mce/plugins/inlinepopups/editor_plugin_src.js

http://n23.googlecode.com/ · JavaScript · 623 lines · 448 code · 130 blank · 45 comment · 110 complexity · 27a011dae282e446caa7b3bd88b69170 MD5 · raw file

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