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