2 * editor_plugin_src.js
\r
4 * Copyright 2009, Moxiecode Systems AB
\r
5 * Released under LGPL License.
\r
7 * License: http://tinymce.moxiecode.com/license
\r
8 * Contributing: http://tinymce.moxiecode.com/contributing
\r
12 var DOM = tinymce.DOM, Element = tinymce.dom.Element, Event = tinymce.dom.Event, each = tinymce.each, is = tinymce.is;
\r
14 tinymce.create('tinymce.plugins.InlinePopups', {
\r
15 init : function(ed, url) {
\r
16 // Replace window manager
\r
17 ed.onBeforeRenderUI.add(function() {
\r
18 ed.windowManager = new tinymce.InlineWindowManager(ed);
\r
19 DOM.loadCSS(url + '/skins/' + (ed.settings.inlinepopups_skin || 'clearlooks2') + "/window.css");
\r
23 getInfo : function() {
\r
25 longname : 'InlinePopups',
\r
26 author : 'Moxiecode Systems AB',
\r
27 authorurl : 'http://tinymce.moxiecode.com',
\r
28 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/inlinepopups',
\r
29 version : tinymce.majorVersion + "." + tinymce.minorVersion
\r
34 tinymce.create('tinymce.InlineWindowManager:tinymce.WindowManager', {
\r
35 InlineWindowManager : function(ed) {
\r
44 open : function(f, p) {
\r
45 var t = this, id, opt = '', ed = t.editor, dw = 0, dh = 0, vp, po, mdf, clf, we, w, u, parentWindow;
\r
50 // Run native windows
\r
52 return t.parent(f, p);
\r
54 parentWindow = t._frontWindow();
\r
55 if (parentWindow && DOM.get(parentWindow.id + '_ifr')) {
\r
56 parentWindow.focussedElement = DOM.get(parentWindow.id + '_ifr').contentWindow.document.activeElement;
\r
59 // Only store selection if the type is a normal window
\r
61 t.bookmark = ed.selection.getBookmark(1);
\r
63 id = DOM.uniqueId();
\r
64 vp = DOM.getViewPort();
\r
65 f.width = parseInt(f.width || 320);
\r
66 f.height = parseInt(f.height || 240) + (tinymce.isIE ? 8 : 0);
\r
67 f.min_width = parseInt(f.min_width || 150);
\r
68 f.min_height = parseInt(f.min_height || 100);
\r
69 f.max_width = parseInt(f.max_width || 2000);
\r
70 f.max_height = parseInt(f.max_height || 2000);
\r
71 f.left = f.left || Math.round(Math.max(vp.x, vp.x + (vp.w / 2.0) - (f.width / 2.0)));
\r
72 f.top = f.top || Math.round(Math.max(vp.y, vp.y + (vp.h / 2.0) - (f.height / 2.0)));
\r
73 f.movable = f.resizable = true;
\r
74 p.mce_width = f.width;
\r
75 p.mce_height = f.height;
\r
76 p.mce_inline = true;
\r
77 p.mce_window_id = id;
\r
78 p.mce_auto_focus = f.auto_focus;
\r
81 // po = DOM.getPos(ed.getContainer());
\r
87 t.onOpen.dispatch(t, f, p);
\r
93 opt += ' mce' + f.type.substring(0, 1).toUpperCase() + f.type.substring(1);
\r
95 f.resizable = false;
\r
99 opt += ' mceStatusbar';
\r
102 opt += ' mceResizable';
\r
105 opt += ' mceMinimizable';
\r
108 opt += ' mceMaximizable';
\r
111 opt += ' mceMovable';
\r
113 // Create DOM objects
\r
114 t._addAll(DOM.doc.body,
\r
115 ['div', {id : id, role : 'dialog', 'aria-labelledby': f.type ? id + '_content' : id + '_title', 'class' : (ed.settings.inlinepopups_skin || 'clearlooks2') + (tinymce.isIE && window.getSelection ? ' ie9' : ''), style : 'width:100px;height:100px'},
\r
116 ['div', {id : id + '_wrapper', 'class' : 'mceWrapper' + opt},
\r
117 ['div', {id : id + '_top', 'class' : 'mceTop'},
\r
118 ['div', {'class' : 'mceLeft'}],
\r
119 ['div', {'class' : 'mceCenter'}],
\r
120 ['div', {'class' : 'mceRight'}],
\r
121 ['span', {id : id + '_title'}, f.title || '']
\r
124 ['div', {id : id + '_middle', 'class' : 'mceMiddle'},
\r
125 ['div', {id : id + '_left', 'class' : 'mceLeft', tabindex : '0'}],
\r
126 ['span', {id : id + '_content'}],
\r
127 ['div', {id : id + '_right', 'class' : 'mceRight', tabindex : '0'}]
\r
130 ['div', {id : id + '_bottom', 'class' : 'mceBottom'},
\r
131 ['div', {'class' : 'mceLeft'}],
\r
132 ['div', {'class' : 'mceCenter'}],
\r
133 ['div', {'class' : 'mceRight'}],
\r
134 ['span', {id : id + '_status'}, 'Content']
\r
137 ['a', {'class' : 'mceMove', tabindex : '-1', href : 'javascript:;'}],
\r
138 ['a', {'class' : 'mceMin', tabindex : '-1', href : 'javascript:;', onmousedown : 'return false;'}],
\r
139 ['a', {'class' : 'mceMax', tabindex : '-1', href : 'javascript:;', onmousedown : 'return false;'}],
\r
140 ['a', {'class' : 'mceMed', tabindex : '-1', href : 'javascript:;', onmousedown : 'return false;'}],
\r
141 ['a', {'class' : 'mceClose', tabindex : '-1', href : 'javascript:;', onmousedown : 'return false;'}],
\r
142 ['a', {id : id + '_resize_n', 'class' : 'mceResize mceResizeN', tabindex : '-1', href : 'javascript:;'}],
\r
143 ['a', {id : id + '_resize_s', 'class' : 'mceResize mceResizeS', tabindex : '-1', href : 'javascript:;'}],
\r
144 ['a', {id : id + '_resize_w', 'class' : 'mceResize mceResizeW', tabindex : '-1', href : 'javascript:;'}],
\r
145 ['a', {id : id + '_resize_e', 'class' : 'mceResize mceResizeE', tabindex : '-1', href : 'javascript:;'}],
\r
146 ['a', {id : id + '_resize_nw', 'class' : 'mceResize mceResizeNW', tabindex : '-1', href : 'javascript:;'}],
\r
147 ['a', {id : id + '_resize_ne', 'class' : 'mceResize mceResizeNE', tabindex : '-1', href : 'javascript:;'}],
\r
148 ['a', {id : id + '_resize_sw', 'class' : 'mceResize mceResizeSW', tabindex : '-1', href : 'javascript:;'}],
\r
149 ['a', {id : id + '_resize_se', 'class' : 'mceResize mceResizeSE', tabindex : '-1', href : 'javascript:;'}]
\r
154 DOM.setStyles(id, {top : -10000, left : -10000});
\r
156 // Fix gecko rendering bug, where the editors iframe messed with window contents
\r
157 if (tinymce.isGecko)
\r
158 DOM.setStyle(id, 'overflow', 'auto');
\r
162 dw += DOM.get(id + '_left').clientWidth;
\r
163 dw += DOM.get(id + '_right').clientWidth;
\r
164 dh += DOM.get(id + '_top').clientHeight;
\r
165 dh += DOM.get(id + '_bottom').clientHeight;
\r
169 DOM.setStyles(id, {top : f.top, left : f.left, width : f.width + dw, height : f.height + dh});
\r
171 u = f.url || f.file;
\r
173 if (tinymce.relaxedDomain)
\r
174 u += (u.indexOf('?') == -1 ? '?' : '&') + 'mce_rdomain=' + tinymce.relaxedDomain;
\r
176 u = tinymce._addVer(u);
\r
180 DOM.add(id + '_content', 'iframe', {id : id + '_ifr', src : 'javascript:""', frameBorder : 0, style : 'border:0;width:10px;height:10px'});
\r
181 DOM.setStyles(id + '_ifr', {width : f.width, height : f.height});
\r
182 DOM.setAttrib(id + '_ifr', 'src', u);
\r
184 DOM.add(id + '_wrapper', 'a', {id : id + '_ok', 'class' : 'mceButton mceOk', href : 'javascript:;', onmousedown : 'return false;'}, 'Ok');
\r
186 if (f.type == 'confirm')
\r
187 DOM.add(id + '_wrapper', 'a', {'class' : 'mceButton mceCancel', href : 'javascript:;', onmousedown : 'return false;'}, 'Cancel');
\r
189 DOM.add(id + '_middle', 'div', {'class' : 'mceIcon'});
\r
190 DOM.setHTML(id + '_content', f.content.replace('\n', '<br />'));
\r
192 Event.add(id, 'keyup', function(evt) {
\r
193 var VK_ESCAPE = 27;
\r
194 if (evt.keyCode === VK_ESCAPE) {
\r
195 f.button_func(false);
\r
196 return Event.cancel(evt);
\r
200 Event.add(id, 'keydown', function(evt) {
\r
201 var cancelButton, VK_TAB = 9;
\r
202 if (evt.keyCode === VK_TAB) {
\r
203 cancelButton = DOM.select('a.mceCancel', id + '_wrapper')[0];
\r
204 if (cancelButton && cancelButton !== evt.target) {
\r
205 cancelButton.focus();
\r
207 DOM.get(id + '_ok').focus();
\r
209 return Event.cancel(evt);
\r
215 mdf = Event.add(id, 'mousedown', function(e) {
\r
216 var n = e.target, w, vp;
\r
221 if (n.nodeName == 'A' || n.nodeName == 'a') {
\r
222 if (n.className == 'mceMax') {
\r
223 w.oldPos = w.element.getXY();
\r
224 w.oldSize = w.element.getSize();
\r
226 vp = DOM.getViewPort();
\r
228 // Reduce viewport size to avoid scrollbars
\r
232 w.element.moveTo(vp.x, vp.y);
\r
233 w.element.resizeTo(vp.w, vp.h);
\r
234 DOM.setStyles(id + '_ifr', {width : vp.w - w.deltaWidth, height : vp.h - w.deltaHeight});
\r
235 DOM.addClass(id + '_wrapper', 'mceMaximized');
\r
236 } else if (n.className == 'mceMed') {
\r
237 // Reset to old size
\r
238 w.element.moveTo(w.oldPos.x, w.oldPos.y);
\r
239 w.element.resizeTo(w.oldSize.w, w.oldSize.h);
\r
240 w.iframeElement.resizeTo(w.oldSize.w - w.deltaWidth, w.oldSize.h - w.deltaHeight);
\r
242 DOM.removeClass(id + '_wrapper', 'mceMaximized');
\r
243 } else if (n.className == 'mceMove')
\r
244 return t._startDrag(id, e, n.className);
\r
245 else if (DOM.hasClass(n, 'mceResize'))
\r
246 return t._startDrag(id, e, n.className.substring(13));
\r
250 clf = Event.add(id, 'click', function(e) {
\r
255 if (n.nodeName == 'A' || n.nodeName == 'a') {
\r
256 switch (n.className) {
\r
259 return Event.cancel(e);
\r
261 case 'mceButton mceOk':
\r
262 case 'mceButton mceCancel':
\r
263 f.button_func(n.className == 'mceButton mceOk');
\r
264 return Event.cancel(e);
\r
269 // Make sure the tab order loops within the dialog.
\r
270 Event.add([id + '_left', id + '_right'], 'focus', function(evt) {
\r
271 var iframe = DOM.get(id + '_ifr');
\r
273 var body = iframe.contentWindow.document.body;
\r
274 var focusable = DOM.select(':input:enabled,*[tabindex=0]', body);
\r
275 if (evt.target.id === (id + '_left')) {
\r
276 focusable[focusable.length - 1].focus();
\r
278 focusable[0].focus();
\r
281 DOM.get(id + '_ok').focus();
\r
286 w = t.windows[id] = {
\r
288 mousedown_func : mdf,
\r
290 element : new Element(id, {blocker : 1, container : ed.getContainer()}),
\r
291 iframeElement : new Element(id + '_ifr'),
\r
297 w.iframeElement.on('focus', function() {
\r
302 if (t.count == 0 && t.editor.getParam('dialog_type', 'modal') == 'modal') {
\r
303 DOM.add(DOM.doc.body, 'div', {
\r
304 id : 'mceModalBlocker',
\r
305 'class' : (t.editor.settings.inlinepopups_skin || 'clearlooks2') + '_modalBlocker',
\r
306 style : {zIndex : t.zIndex - 1}
\r
309 DOM.show('mceModalBlocker'); // Reduces flicker in IE
\r
310 DOM.setAttrib(DOM.doc.body, 'aria-hidden', 'true');
\r
312 DOM.setStyle('mceModalBlocker', 'z-index', t.zIndex - 1);
\r
314 if (tinymce.isIE6 || /Firefox\/2\./.test(navigator.userAgent) || (tinymce.isIE && !DOM.boxModel))
\r
315 DOM.setStyles('mceModalBlocker', {position : 'absolute', left : vp.x, top : vp.y, width : vp.w - 2, height : vp.h - 2});
\r
317 DOM.setAttrib(id, 'aria-hidden', 'false');
\r
319 t._fixIELayout(id, 1);
\r
322 if (DOM.get(id + '_ok'))
\r
323 DOM.get(id + '_ok').focus();
\r
329 focus : function(id) {
\r
332 if (w = t.windows[id]) {
\r
333 w.zIndex = this.zIndex++;
\r
334 w.element.setStyle('zIndex', w.zIndex);
\r
335 w.element.update();
\r
337 id = id + '_wrapper';
\r
338 DOM.removeClass(t.lastId, 'mceFocus');
\r
339 DOM.addClass(id, 'mceFocus');
\r
342 if (w.focussedElement) {
\r
343 w.focussedElement.focus();
\r
344 } else if (DOM.get(id + '_ok')) {
\r
345 DOM.get(w.id + '_ok').focus();
\r
346 } else if (DOM.get(w.id + '_ifr')) {
\r
347 DOM.get(w.id + '_ifr').focus();
\r
352 _addAll : function(te, ne) {
\r
353 var i, n, t = this, dom = tinymce.DOM;
\r
355 if (is(ne, 'string'))
\r
356 te.appendChild(dom.doc.createTextNode(ne));
\r
357 else if (ne.length) {
\r
358 te = te.appendChild(dom.create(ne[0], ne[1]));
\r
360 for (i=2; i<ne.length; i++)
\r
361 t._addAll(te, ne[i]);
\r
365 _startDrag : function(id, se, ac) {
\r
366 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;
\r
368 // Get positons and sizes
\r
369 // cp = DOM.getPos(t.editor.getContainer());
\r
370 cp = {x : 0, y : 0};
\r
371 vp = DOM.getViewPort();
\r
373 // Reduce viewport size to avoid scrollbars while dragging
\r
379 dx = dy = dw = dh = 0;
\r
382 mu = Event.add(d, 'mouseup', function(e) {
\r
383 Event.remove(d, 'mouseup', mu);
\r
384 Event.remove(d, 'mousemove', mm);
\r
390 we.resizeBy(dw, dh);
\r
392 DOM.setStyles(id + '_ifr', {width : sz.w - w.deltaWidth, height : sz.h - w.deltaHeight});
\r
393 t._fixIELayout(id, 1);
\r
395 return Event.cancel(e);
\r
401 function startMove() {
\r
405 t._fixIELayout(id, 0);
\r
407 // Setup event blocker
\r
408 DOM.add(d.body, 'div', {
\r
409 id : 'mceEventBlocker',
\r
410 'class' : 'mceEventBlocker ' + (t.editor.settings.inlinepopups_skin || 'clearlooks2'),
\r
411 style : {zIndex : t.zIndex + 1}
\r
414 if (tinymce.isIE6 || (tinymce.isIE && !DOM.boxModel))
\r
415 DOM.setStyles('mceEventBlocker', {position : 'absolute', left : vp.x, top : vp.y, width : vp.w - 2, height : vp.h - 2});
\r
417 eb = new Element('mceEventBlocker');
\r
420 // Setup placeholder
\r
423 sx = cp.x + p.x - vp.x;
\r
424 sy = cp.y + p.y - vp.y;
\r
425 DOM.add(eb.get(), 'div', {id : 'mcePlaceHolder', 'class' : 'mcePlaceHolder', style : {left : sx, top : sy, width : sz.w, height : sz.h}});
\r
426 ph = new Element('mcePlaceHolder');
\r
429 // Handle mouse move/drag
\r
430 mm = Event.add(d, 'mousemove', function(e) {
\r
435 x = e.screenX - sex;
\r
436 y = e.screenY - sey;
\r
451 if (ac == "ResizeNW") {
\r
454 } else if (ac == "ResizeNE")
\r
464 if (ac == "ResizeSW") {
\r
467 } else if (ac == "ResizeSE")
\r
480 if (dw < (v = w.features.min_width - sz.w)) {
\r
487 if (dh < (v = w.features.min_height - sz.h)) {
\r
494 dw = Math.min(dw, w.features.max_width - sz.w);
\r
495 dh = Math.min(dh, w.features.max_height - sz.h);
\r
496 dx = Math.max(dx, vp.x - (sx + vp.x));
\r
497 dy = Math.max(dy, vp.y - (sy + vp.y));
\r
498 dx = Math.min(dx, (vp.w + vp.x) - (sx + sz.w + vp.x));
\r
499 dy = Math.min(dy, (vp.h + vp.y) - (sy + sz.h + vp.y));
\r
502 if (dx + dy !== 0) {
\r
509 ph.moveTo(sx + dx, sy + dy);
\r
512 // Resize if needed
\r
514 ph.resizeTo(sz.w + dw, sz.h + dh);
\r
516 return Event.cancel(e);
\r
519 return Event.cancel(se);
\r
522 resizeBy : function(dw, dh, id) {
\r
523 var w = this.windows[id];
\r
526 w.element.resizeBy(dw, dh);
\r
527 w.iframeElement.resizeBy(dw, dh);
\r
531 close : function(win, id) {
\r
532 var t = this, w, d = DOM.doc, fw, id;
\r
534 id = t._findId(id || win);
\r
536 // Probably not inline
\r
537 if (!t.windows[id]) {
\r
544 if (t.count == 0) {
\r
545 DOM.remove('mceModalBlocker');
\r
546 DOM.setAttrib(DOM.doc.body, 'aria-hidden', 'false');
\r
550 if (w = t.windows[id]) {
\r
551 t.onClose.dispatch(t);
\r
552 Event.remove(d, 'mousedown', w.mousedownFunc);
\r
553 Event.remove(d, 'click', w.clickFunc);
\r
555 Event.clear(id + '_ifr');
\r
557 DOM.setAttrib(id + '_ifr', 'src', 'javascript:""'); // Prevent leak
\r
558 w.element.remove();
\r
559 delete t.windows[id];
\r
561 fw = t._frontWindow();
\r
568 // Find front most window
\r
569 _frontWindow : function() {
\r
571 // Find front most window and focus that
\r
572 each (this.windows, function(w) {
\r
573 if (w.zIndex > ix) {
\r
581 setTitle : function(w, ti) {
\r
584 w = this._findId(w);
\r
586 if (e = DOM.get(w + '_title'))
\r
587 e.innerHTML = DOM.encode(ti);
\r
590 alert : function(txt, cb, s) {
\r
596 button_func : function(s) {
\r
598 cb.call(s || t, s);
\r
600 t.close(null, w.id);
\r
602 content : DOM.encode(t.editor.getLang(txt, txt)),
\r
609 confirm : function(txt, cb, s) {
\r
615 button_func : function(s) {
\r
617 cb.call(s || t, s);
\r
619 t.close(null, w.id);
\r
621 content : DOM.encode(t.editor.getLang(txt, txt)),
\r
628 // Internal functions
\r
630 _findId : function(w) {
\r
633 if (typeof(w) == 'string')
\r
636 each(t.windows, function(wo) {
\r
637 var ifr = DOM.get(wo.id + '_ifr');
\r
639 if (ifr && w == ifr.contentWindow) {
\r
648 _fixIELayout : function(id, s) {
\r
651 if (!tinymce.isIE6)
\r
654 // Fixes the bug where hover flickers and does odd things in IE6
\r
655 each(['n','s','w','e','nw','ne','sw','se'], function(v) {
\r
656 var e = DOM.get(id + '_resize_' + v);
\r
659 width : s ? e.clientWidth : '',
\r
660 height : s ? e.clientHeight : '',
\r
661 cursor : DOM.getStyle(e, 'cursor', 1)
\r
664 DOM.setStyle(id + "_bottom", 'bottom', '-1px');
\r
669 // Fixes graphics glitch
\r
670 if (w = this.windows[id]) {
\r
671 // Fixes rendering bug after resize
\r
675 // Forced a repaint of the window
\r
676 //DOM.get(id).style.filter = '';
\r
678 // IE has a bug where images used in CSS won't get loaded
\r
679 // sometimes when the cache in the browser is disabled
\r
680 // This fix tries to solve it by loading the images using the image object
\r
681 each(DOM.select('div,a', id), function(e, i) {
\r
682 if (e.currentStyle.backgroundImage != 'none') {
\r
684 img.src = e.currentStyle.backgroundImage.replace(/url\(\"(.+)\"\)/, '$1');
\r
688 DOM.get(id).style.filter = '';
\r
694 tinymce.PluginManager.add('inlinepopups', tinymce.plugins.InlinePopups);
\r