]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/themes/modern/theme.js
WordPress 4.5.1
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / themes / modern / theme.js
1 /**
2  * theme.js
3  *
4  * Released under LGPL License.
5  * Copyright (c) 1999-2015 Ephox Corp. All rights reserved
6  *
7  * License: http://www.tinymce.com/license
8  * Contributing: http://www.tinymce.com/contributing
9  */
10
11 /*global tinymce:true */
12
13 tinymce.ThemeManager.add('modern', function(editor) {
14         var self = this, settings = editor.settings, Factory = tinymce.ui.Factory,
15                 each = tinymce.each, DOM = tinymce.DOM, Rect = tinymce.geom.Rect, FloatPanel = tinymce.ui.FloatPanel;
16
17         // Default menus
18         var defaultMenus = {
19                 file: {title: 'File', items: 'newdocument'},
20                 edit: {title: 'Edit', items: 'undo redo | cut copy paste pastetext | selectall'},
21                 insert: {title: 'Insert', items: '|'},
22                 view: {title: 'View', items: 'visualaid |'},
23                 format: {title: 'Format', items: 'bold italic underline strikethrough superscript subscript | formats | removeformat'},
24                 table: {title: 'Table'},
25                 tools: {title: 'Tools'}
26         };
27
28         var defaultToolbar = "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | " +
29                 "bullist numlist outdent indent | link image";
30
31         function createToolbar(items, size) {
32                 var toolbarItems = [], buttonGroup;
33
34                 if (!items) {
35                         return;
36                 }
37
38                 each(items.split(/[ ,]/), function(item) {
39                         var itemName;
40
41                         function bindSelectorChanged() {
42                                 var selection = editor.selection;
43
44                                 function setActiveItem(name) {
45                                         return function(state, args) {
46                                                 var nodeName, i = args.parents.length;
47
48                                                 while (i--) {
49                                                         nodeName = args.parents[i].nodeName;
50                                                         if (nodeName == "OL" || nodeName == "UL") {
51                                                                 break;
52                                                         }
53                                                 }
54
55                                                 item.active(state && nodeName == name);
56                                         };
57                                 }
58
59                                 if (itemName == "bullist") {
60                                         selection.selectorChanged('ul > li', setActiveItem("UL"));
61                                 }
62
63                                 if (itemName == "numlist") {
64                                         selection.selectorChanged('ol > li', setActiveItem("OL"));
65                                 }
66
67                                 if (item.settings.stateSelector) {
68                                         selection.selectorChanged(item.settings.stateSelector, function(state) {
69                                                 item.active(state);
70                                         }, true);
71                                 }
72
73                                 if (item.settings.disabledStateSelector) {
74                                         selection.selectorChanged(item.settings.disabledStateSelector, function(state) {
75                                                 item.disabled(state);
76                                         });
77                                 }
78                         }
79
80                         if (item == "|") {
81                                 buttonGroup = null;
82                         } else {
83                                 if (Factory.has(item)) {
84                                         item = {type: item, size: size};
85                                         toolbarItems.push(item);
86                                         buttonGroup = null;
87                                 } else {
88                                         if (!buttonGroup) {
89                                                 buttonGroup = {type: 'buttongroup', items: []};
90                                                 toolbarItems.push(buttonGroup);
91                                         }
92
93                                         if (editor.buttons[item]) {
94                                                 // TODO: Move control creation to some UI class
95                                                 itemName = item;
96                                                 item = editor.buttons[itemName];
97
98                                                 if (typeof item == "function") {
99                                                         item = item();
100                                                 }
101
102                                                 item.type = item.type || 'button';
103                                                 item.size = size;
104
105                                                 item = Factory.create(item);
106                                                 buttonGroup.items.push(item);
107
108                                                 if (editor.initialized) {
109                                                         bindSelectorChanged();
110                                                 } else {
111                                                         editor.on('init', bindSelectorChanged);
112                                                 }
113                                         }
114                                 }
115                         }
116                 });
117
118                 return {
119                         type: 'toolbar',
120                         layout: 'flow',
121                         items: toolbarItems
122                 };
123         }
124
125         /**
126          * Creates the toolbars from config and returns a toolbar array.
127          *
128          * @param {String} size Optional toolbar item size.
129          * @return {Array} Array with toolbars.
130          */
131         function createToolbars(size) {
132                 var toolbars = [];
133
134                 function addToolbar(items) {
135                         if (items) {
136                                 toolbars.push(createToolbar(items, size));
137                                 return true;
138                         }
139                 }
140
141                 // Convert toolbar array to multiple options
142                 if (tinymce.isArray(settings.toolbar)) {
143                         // Empty toolbar array is the same as a disabled toolbar
144                         if (settings.toolbar.length === 0) {
145                                 return;
146                         }
147
148                         tinymce.each(settings.toolbar, function(toolbar, i) {
149                                 settings["toolbar" + (i + 1)] = toolbar;
150                         });
151
152                         delete settings.toolbar;
153                 }
154
155                 // Generate toolbar<n>
156                 for (var i = 1; i < 10; i++) {
157                         if (!addToolbar(settings["toolbar" + i])) {
158                                 break;
159                         }
160                 }
161
162                 // Generate toolbar or default toolbar unless it's disabled
163                 if (!toolbars.length && settings.toolbar !== false) {
164                         addToolbar(settings.toolbar || defaultToolbar);
165                 }
166
167                 if (toolbars.length) {
168                         return {
169                                 type: 'panel',
170                                 layout: 'stack',
171                                 classes: "toolbar-grp",
172                                 ariaRoot: true,
173                                 ariaRemember: true,
174                                 items: toolbars
175                         };
176                 }
177         }
178
179         /**
180          * Creates the menu buttons based on config.
181          *
182          * @return {Array} Menu buttons array.
183          */
184         function createMenuButtons() {
185                 var name, menuButtons = [];
186
187                 function createMenuItem(name) {
188                         var menuItem;
189
190                         if (name == '|') {
191                                 return {text: '|'};
192                         }
193
194                         menuItem = editor.menuItems[name];
195
196                         return menuItem;
197                 }
198
199                 function createMenu(context) {
200                         var menuButton, menu, menuItems, isUserDefined, removedMenuItems;
201
202                         removedMenuItems = tinymce.makeMap((settings.removed_menuitems || '').split(/[ ,]/));
203
204                         // User defined menu
205                         if (settings.menu) {
206                                 menu = settings.menu[context];
207                                 isUserDefined = true;
208                         } else {
209                                 menu = defaultMenus[context];
210                         }
211
212                         if (menu) {
213                                 menuButton = {text: menu.title};
214                                 menuItems = [];
215
216                                 // Default/user defined items
217                                 each((menu.items || '').split(/[ ,]/), function(item) {
218                                         var menuItem = createMenuItem(item);
219
220                                         if (menuItem && !removedMenuItems[item]) {
221                                                 menuItems.push(createMenuItem(item));
222                                         }
223                                 });
224
225                                 // Added though context
226                                 if (!isUserDefined) {
227                                         each(editor.menuItems, function(menuItem) {
228                                                 if (menuItem.context == context) {
229                                                         if (menuItem.separator == 'before') {
230                                                                 menuItems.push({text: '|'});
231                                                         }
232
233                                                         if (menuItem.prependToContext) {
234                                                                 menuItems.unshift(menuItem);
235                                                         } else {
236                                                                 menuItems.push(menuItem);
237                                                         }
238
239                                                         if (menuItem.separator == 'after') {
240                                                                 menuItems.push({text: '|'});
241                                                         }
242                                                 }
243                                         });
244                                 }
245
246                                 for (var i = 0; i < menuItems.length; i++) {
247                                         if (menuItems[i].text == '|') {
248                                                 if (i === 0 || i == menuItems.length - 1) {
249                                                         menuItems.splice(i, 1);
250                                                 }
251                                         }
252                                 }
253
254                                 menuButton.menu = menuItems;
255
256                                 if (!menuButton.menu.length) {
257                                         return null;
258                                 }
259                         }
260
261                         return menuButton;
262                 }
263
264                 var defaultMenuBar = [];
265                 if (settings.menu) {
266                         for (name in settings.menu) {
267                                 defaultMenuBar.push(name);
268                         }
269                 } else {
270                         for (name in defaultMenus) {
271                                 defaultMenuBar.push(name);
272                         }
273                 }
274
275                 var enabledMenuNames = typeof settings.menubar == "string" ? settings.menubar.split(/[ ,]/) : defaultMenuBar;
276                 for (var i = 0; i < enabledMenuNames.length; i++) {
277                         var menu = enabledMenuNames[i];
278                         menu = createMenu(menu);
279
280                         if (menu) {
281                                 menuButtons.push(menu);
282                         }
283                 }
284
285                 return menuButtons;
286         }
287
288         /**
289          * Adds accessibility shortcut keys to panel.
290          *
291          * @param {tinymce.ui.Panel} panel Panel to add focus to.
292          */
293         function addAccessibilityKeys(panel) {
294                 function focus(type) {
295                         var item = panel.find(type)[0];
296
297                         if (item) {
298                                 item.focus(true);
299                         }
300                 }
301
302                 editor.shortcuts.add('Alt+F9', '', function() {
303                         focus('menubar');
304                 });
305
306                 editor.shortcuts.add('Alt+F10', '', function() {
307                         focus('toolbar');
308                 });
309
310                 editor.shortcuts.add('Alt+F11', '', function() {
311                         focus('elementpath');
312                 });
313
314                 panel.on('cancel', function() {
315                         editor.focus();
316                 });
317         }
318
319         /**
320          * Resizes the editor to the specified width, height.
321          */
322         function resizeTo(width, height) {
323                 var containerElm, iframeElm, containerSize, iframeSize;
324
325                 function getSize(elm) {
326                         return {
327                                 width: elm.clientWidth,
328                                 height: elm.clientHeight
329                         };
330                 }
331
332                 containerElm = editor.getContainer();
333                 iframeElm = editor.getContentAreaContainer().firstChild;
334                 containerSize = getSize(containerElm);
335                 iframeSize = getSize(iframeElm);
336
337                 if (width !== null) {
338                         width = Math.max(settings.min_width || 100, width);
339                         width = Math.min(settings.max_width || 0xFFFF, width);
340
341                         DOM.setStyle(containerElm, 'width', width + (containerSize.width - iframeSize.width));
342                         DOM.setStyle(iframeElm, 'width', width);
343                 }
344
345                 height = Math.max(settings.min_height || 100, height);
346                 height = Math.min(settings.max_height || 0xFFFF, height);
347                 DOM.setStyle(iframeElm, 'height', height);
348
349                 editor.fire('ResizeEditor');
350         }
351
352         function resizeBy(dw, dh) {
353                 var elm = editor.getContentAreaContainer();
354                 self.resizeTo(elm.clientWidth + dw, elm.clientHeight + dh);
355         }
356
357         /**
358          * Handles contextual toolbars.
359          */
360         function addContextualToolbars() {
361                 var scrollContainer;
362
363                 function getContextToolbars() {
364                         return editor.contextToolbars || [];
365                 }
366
367                 function getElementRect(elm) {
368                         var pos, targetRect, root;
369
370                         pos = tinymce.DOM.getPos(editor.getContentAreaContainer());
371                         targetRect = editor.dom.getRect(elm);
372                         root = editor.dom.getRoot();
373
374                         // Adjust targetPos for scrolling in the editor
375                         if (root.nodeName == 'BODY') {
376                                 targetRect.x -= root.ownerDocument.documentElement.scrollLeft || root.scrollLeft;
377                                 targetRect.y -= root.ownerDocument.documentElement.scrollTop || root.scrollTop;
378                         }
379
380                         targetRect.x += pos.x;
381                         targetRect.y += pos.y;
382
383                         return targetRect;
384                 }
385
386                 function hideAllFloatingPanels() {
387                         each(editor.contextToolbars, function(toolbar) {
388                                 if (toolbar.panel) {
389                                         toolbar.panel.hide();
390                                 }
391                         });
392                 }
393
394                 function togglePositionClass(panel, relPos, predicate) {
395                         relPos = relPos ? relPos.substr(0, 2) : '';
396
397                         each({
398                                 t: 'down',
399                                 b: 'up'
400                         }, function(cls, pos) {
401                                 panel.classes.toggle('arrow-' + cls, predicate(pos, relPos.substr(0, 1)));
402                         });
403
404                         each({
405                                 l: 'left',
406                                 r: 'right'
407                         }, function(cls, pos) {
408                                 panel.classes.toggle('arrow-' + cls, predicate(pos, relPos.substr(1, 1)));
409                         });
410                 }
411
412                 function toClientRect(geomRect) {
413                         return {
414                                 left: geomRect.x,
415                                 top: geomRect.y,
416                                 width: geomRect.w,
417                                 height: geomRect.h,
418                                 right: geomRect.x + geomRect.w,
419                                 bottom: geomRect.y + geomRect.h
420                         };
421                 }
422
423                 function userConstrain(x, y, elementRect, contentAreaRect, panelRect) {
424                                 panelRect = toClientRect({x: x, y: y, w: panelRect.w, h: panelRect.h});
425
426                                 if (settings.inline_toolbar_position_handler) {
427                                         panelRect = settings.inline_toolbar_position_handler({
428                                                 elementRect: toClientRect(elementRect),
429                                                 contentAreaRect: toClientRect(contentAreaRect),
430                                                 panelRect: panelRect
431                                         });
432                                 }
433
434                                 return panelRect;
435                 }
436
437                 function movePanelTo(panel, pos) {
438                         panel.moveTo(pos.left, pos.top);
439                 }
440
441                 function reposition(match) {
442                         var relPos, panelRect, elementRect, contentAreaRect, panel, relRect, testPositions;
443
444                         if (editor.removed) {
445                                 return;
446                         }
447
448                         if (!match || !match.toolbar.panel) {
449                                 hideAllFloatingPanels();
450                                 return;
451                         }
452
453                         testPositions = [
454                                 'bc-tc', 'tc-bc',
455                                 'tl-bl', 'bl-tl',
456                                 'tr-br', 'br-tr'
457                         ];
458
459                         panel = match.toolbar.panel;
460                         panel.show();
461
462                         elementRect = getElementRect(match.element);
463                         panelRect = tinymce.DOM.getRect(panel.getEl());
464                         contentAreaRect = tinymce.DOM.getRect(editor.getContentAreaContainer() || editor.getBody());
465
466                         // We need to use these instead of the rect values since the style
467                         // size properites might not be the same as the real size for a table
468                         elementRect.w = match.element.clientWidth;
469                         elementRect.h = match.element.clientHeight;
470
471                         if (!editor.inline) {
472                                 contentAreaRect.w = editor.getDoc().documentElement.offsetWidth;
473                         }
474
475                         // Inflate the elementRect so it doesn't get placed above resize handles
476                         if (editor.selection.controlSelection.isResizable(match.element)) {
477                                 elementRect = Rect.inflate(elementRect, 0, 8);
478                         }
479
480                         relPos = Rect.findBestRelativePosition(panelRect, elementRect, contentAreaRect, testPositions);
481                         elementRect = Rect.clamp(elementRect, contentAreaRect);
482
483                         if (relPos) {
484                                 relRect = Rect.relativePosition(panelRect, elementRect, relPos);
485                                 movePanelTo(panel, userConstrain(relRect.x, relRect.y, elementRect, contentAreaRect, panelRect));
486                         } else {
487                                 // Allow overflow below the editor to avoid placing toolbars ontop of tables
488                                 contentAreaRect.h += 40;
489
490                                 elementRect = Rect.intersect(contentAreaRect, elementRect);
491                                 if (elementRect) {
492                                         relPos = Rect.findBestRelativePosition(panelRect, elementRect, contentAreaRect, [
493                                                 'bc-tc', 'bl-tl', 'br-tr'
494                                         ]);
495
496                                         if (relPos) {
497                                                 relRect = Rect.relativePosition(panelRect, elementRect, relPos);
498                                                 movePanelTo(panel, userConstrain(relRect.x, relRect.y, elementRect, contentAreaRect, panelRect));
499                                         } else {
500                                                 movePanelTo(panel, userConstrain(elementRect.x, elementRect.y, elementRect, contentAreaRect, panelRect));
501                                         }
502                                 } else {
503                                         panel.hide();
504                                 }
505                         }
506
507                         togglePositionClass(panel, relPos, function(pos1, pos2) {
508                                 return (!elementRect || elementRect.w > 40) && pos1 === pos2;
509                         });
510
511                         //drawRect(contentAreaRect, 'blue');
512                         //drawRect(elementRect, 'red');
513                         //drawRect(panelRect, 'green');
514                 }
515
516                 function repositionHandler() {
517                         function execute() {
518                                 if (editor.selection) {
519                                         reposition(findFrontMostMatch(editor.selection.getNode()));
520                                 }
521                         }
522
523                         tinymce.util.Delay.requestAnimationFrame(execute);
524                 }
525
526                 function bindScrollEvent() {
527                         if (!scrollContainer) {
528                                 scrollContainer = editor.selection.getScrollContainer() || editor.getWin();
529                                 tinymce.$(scrollContainer).on('scroll', repositionHandler);
530
531                                 editor.on('remove', function() {
532                                         tinymce.$(scrollContainer).off('scroll');
533                                 });
534                         }
535                 }
536
537                 function showContextToolbar(match) {
538                         var panel;
539
540                         if (match.toolbar.panel) {
541                                 match.toolbar.panel.show();
542                                 reposition(match);
543                                 return;
544                         }
545
546                         bindScrollEvent();
547
548                         panel = Factory.create({
549                                 type: 'floatpanel',
550                                 role: 'dialog',
551                                 classes: 'tinymce tinymce-inline arrow',
552                                 ariaLabel: 'Inline toolbar',
553                                 layout: 'flex',
554                                 direction: 'column',
555                                 align: 'stretch',
556                                 autohide: false,
557                                 autofix: true,
558                                 fixed: true,
559                                 border: 1,
560                                 items: createToolbar(match.toolbar.items),
561                                 oncancel: function() {
562                                         editor.focus();
563                                 }
564                         });
565
566                         match.toolbar.panel = panel;
567                         panel.renderTo(document.body).reflow();
568                         reposition(match);
569                 }
570
571                 function hideAllContextToolbars() {
572                         tinymce.each(getContextToolbars(), function(toolbar) {
573                                 if (toolbar.panel) {
574                                         toolbar.panel.hide();
575                                 }
576                         });
577                 }
578
579                 function findFrontMostMatch(targetElm) {
580                         var i, y, parentsAndSelf, toolbars = getContextToolbars();
581
582                         parentsAndSelf = editor.$(targetElm).parents().add(targetElm);
583                         for (i = parentsAndSelf.length - 1; i >= 0; i--) {
584                                 for (y = toolbars.length - 1; y >= 0; y--) {
585                                         if (toolbars[y].predicate(parentsAndSelf[i])) {
586                                                 return {
587                                                         toolbar: toolbars[y],
588                                                         element: parentsAndSelf[i]
589                                                 };
590                                         }
591                                 }
592                         }
593
594                         return null;
595                 }
596
597                 editor.on('click keyup setContent', function(e) {
598                         // Only act on partial inserts
599                         if (e.type == 'setcontent' && !e.selection) {
600                                 return;
601                         }
602
603                         // Needs to be delayed to avoid Chrome img focus out bug
604                         tinymce.util.Delay.setEditorTimeout(editor, function() {
605                                 var match;
606
607                                 match = findFrontMostMatch(editor.selection.getNode());
608                                 if (match) {
609                                         hideAllContextToolbars();
610                                         showContextToolbar(match);
611                                 } else {
612                                         hideAllContextToolbars();
613                                 }
614                         });
615                 });
616
617                 editor.on('blur hide', hideAllContextToolbars);
618
619                 editor.on('ObjectResizeStart', function() {
620                         var match = findFrontMostMatch(editor.selection.getNode());
621
622                         if (match && match.toolbar.panel) {
623                                 match.toolbar.panel.hide();
624                         }
625                 });
626
627                 editor.on('nodeChange ResizeEditor ResizeWindow', repositionHandler);
628
629                 editor.on('remove', function() {
630                         tinymce.each(getContextToolbars(), function(toolbar) {
631                                 if (toolbar.panel) {
632                                         toolbar.panel.remove();
633                                 }
634                         });
635
636                         editor.contextToolbars = {};
637                 });
638
639                 editor.shortcuts.add('ctrl+shift+e > ctrl+shift+p', '', function() {
640                         var match = findFrontMostMatch(editor.selection.getNode());
641                         if (match && match.toolbar.panel) {
642                                 match.toolbar.panel.items()[0].focus();
643                         }
644                 });
645         }
646
647         function fireSkinLoaded(editor) {
648                 return function() {
649                         if (editor.initialized) {
650                                 editor.fire('SkinLoaded');
651                         } else {
652                                 editor.on('init', function() {
653                                         editor.fire('SkinLoaded');
654                                 });
655                         }
656                 };
657         }
658
659         /**
660          * Renders the inline editor UI.
661          *
662          * @return {Object} Name/value object with theme data.
663          */
664         function renderInlineUI(args) {
665                 var panel, inlineToolbarContainer;
666
667                 if (settings.fixed_toolbar_container) {
668                         inlineToolbarContainer = DOM.select(settings.fixed_toolbar_container)[0];
669                 }
670
671                 function reposition() {
672                         if (panel && panel.moveRel && panel.visible() && !panel._fixed) {
673                                 // TODO: This is kind of ugly and doesn't handle multiple scrollable elements
674                                 var scrollContainer = editor.selection.getScrollContainer(), body = editor.getBody();
675                                 var deltaX = 0, deltaY = 0;
676
677                                 if (scrollContainer) {
678                                         var bodyPos = DOM.getPos(body), scrollContainerPos = DOM.getPos(scrollContainer);
679
680                                         deltaX = Math.max(0, scrollContainerPos.x - bodyPos.x);
681                                         deltaY = Math.max(0, scrollContainerPos.y - bodyPos.y);
682                                 }
683
684                                 panel.fixed(false).moveRel(body, editor.rtl ? ['tr-br', 'br-tr'] : ['tl-bl', 'bl-tl', 'tr-br']).moveBy(deltaX, deltaY);
685                         }
686                 }
687
688                 function show() {
689                         if (panel) {
690                                 panel.show();
691                                 reposition();
692                                 DOM.addClass(editor.getBody(), 'mce-edit-focus');
693                         }
694                 }
695
696                 function hide() {
697                         if (panel) {
698                                 // We require two events as the inline float panel based toolbar does not have autohide=true
699                                 panel.hide();
700
701                                 // All other autohidden float panels will be closed below.
702                                 FloatPanel.hideAll();
703
704                                 DOM.removeClass(editor.getBody(), 'mce-edit-focus');
705                         }
706                 }
707
708                 function render() {
709                         if (panel) {
710                                 if (!panel.visible()) {
711                                         show();
712                                 }
713
714                                 return;
715                         }
716
717                         // Render a plain panel inside the inlineToolbarContainer if it's defined
718                         panel = self.panel = Factory.create({
719                                 type: inlineToolbarContainer ? 'panel' : 'floatpanel',
720                                 role: 'application',
721                                 classes: 'tinymce tinymce-inline',
722                                 layout: 'flex',
723                                 direction: 'column',
724                                 align: 'stretch',
725                                 autohide: false,
726                                 autofix: true,
727                                 fixed: !!inlineToolbarContainer,
728                                 border: 1,
729                                 items: [
730                                         settings.menubar === false ? null : {type: 'menubar', border: '0 0 1 0', items: createMenuButtons()},
731                                         createToolbars(settings.toolbar_items_size)
732                                 ]
733                         });
734
735                         // Add statusbar
736                         /*if (settings.statusbar !== false) {
737                                 panel.add({type: 'panel', classes: 'statusbar', layout: 'flow', border: '1 0 0 0', items: [
738                                         {type: 'elementpath'}
739                                 ]});
740                         }*/
741
742                         editor.fire('BeforeRenderUI');
743                         panel.renderTo(inlineToolbarContainer || document.body).reflow();
744
745                         addAccessibilityKeys(panel);
746                         show();
747                         addContextualToolbars();
748
749                         editor.on('nodeChange', reposition);
750                         editor.on('activate', show);
751                         editor.on('deactivate', hide);
752
753                         editor.nodeChanged();
754                 }
755
756                 settings.content_editable = true;
757
758                 editor.on('focus', function() {
759                         // Render only when the CSS file has been loaded
760                         if (args.skinUiCss) {
761                                 tinymce.DOM.styleSheetLoader.load(args.skinUiCss, render, render);
762                         } else {
763                                 render();
764                         }
765                 });
766
767                 editor.on('blur hide', hide);
768
769                 // Remove the panel when the editor is removed
770                 editor.on('remove', function() {
771                         if (panel) {
772                                 panel.remove();
773                                 panel = null;
774                         }
775                 });
776
777                 // Preload skin css
778                 if (args.skinUiCss) {
779                         tinymce.DOM.styleSheetLoader.load(args.skinUiCss, fireSkinLoaded(editor));
780                 }
781
782                 return {};
783         }
784
785         /**
786          * Renders the iframe editor UI.
787          *
788          * @param {Object} args Details about target element etc.
789          * @return {Object} Name/value object with theme data.
790          */
791         function renderIframeUI(args) {
792                 var panel, resizeHandleCtrl, startSize;
793
794                 function switchMode() {
795                         return function(e) {
796                                 if (e.mode == 'readonly') {
797                                         panel.find('*').disabled(true);
798                                 } else {
799                                         panel.find('*').disabled(false);
800                                 }
801                         };
802                 }
803
804                 if (args.skinUiCss) {
805                         tinymce.DOM.styleSheetLoader.load(args.skinUiCss, fireSkinLoaded(editor));
806                 }
807
808                 // Basic UI layout
809                 panel = self.panel = Factory.create({
810                         type: 'panel',
811                         role: 'application',
812                         classes: 'tinymce',
813                         style: 'visibility: hidden',
814                         layout: 'stack',
815                         border: 1,
816                         items: [
817                                 settings.menubar === false ? null : {type: 'menubar', border: '0 0 1 0', items: createMenuButtons()},
818                                 createToolbars(settings.toolbar_items_size),
819                                 {type: 'panel', name: 'iframe', layout: 'stack', classes: 'edit-area', html: '', border: '1 0 0 0'}
820                         ]
821                 });
822
823                 if (settings.resize !== false) {
824                         resizeHandleCtrl = {
825                                 type: 'resizehandle',
826                                 direction: settings.resize,
827
828                                 onResizeStart: function() {
829                                         var elm = editor.getContentAreaContainer().firstChild;
830
831                                         startSize = {
832                                                 width: elm.clientWidth,
833                                                 height: elm.clientHeight
834                                         };
835                                 },
836
837                                 onResize: function(e) {
838                                         if (settings.resize == 'both') {
839                                                 resizeTo(startSize.width + e.deltaX, startSize.height + e.deltaY);
840                                         } else {
841                                                 resizeTo(null, startSize.height + e.deltaY);
842                                         }
843                                 }
844                         };
845                 }
846
847                 // Add statusbar if needed
848                 if (settings.statusbar !== false) {
849                         panel.add({type: 'panel', name: 'statusbar', classes: 'statusbar', layout: 'flow', border: '1 0 0 0', ariaRoot: true, items: [
850                                 {type: 'elementpath'},
851                                 resizeHandleCtrl
852                         ]});
853                 }
854
855                 editor.fire('BeforeRenderUI');
856                 editor.on('SwitchMode', switchMode());
857                 panel.renderBefore(args.targetNode).reflow();
858
859                 if (settings.readonly) {
860                         editor.setMode('readonly');
861                 }
862
863                 if (settings.width) {
864                         tinymce.DOM.setStyle(panel.getEl(), 'width', settings.width);
865                 }
866
867                 // Remove the panel when the editor is removed
868                 editor.on('remove', function() {
869                         panel.remove();
870                         panel = null;
871                 });
872
873                 // Add accesibility shortcuts
874                 addAccessibilityKeys(panel);
875                 addContextualToolbars();
876
877                 return {
878                         iframeContainer: panel.find('#iframe')[0].getEl(),
879                         editorContainer: panel.getEl()
880                 };
881         }
882
883         /**
884          * Renders the UI for the theme. This gets called by the editor.
885          *
886          * @param {Object} args Details about target element etc.
887          * @return {Object} Theme UI data items.
888          */
889         self.renderUI = function(args) {
890                 var skin = settings.skin !== false ? settings.skin || 'lightgray' : false;
891
892                 if (skin) {
893                         var skinUrl = settings.skin_url;
894
895                         if (skinUrl) {
896                                 skinUrl = editor.documentBaseURI.toAbsolute(skinUrl);
897                         } else {
898                                 skinUrl = tinymce.baseURL + '/skins/' + skin;
899                         }
900
901                         // Load special skin for IE7
902                         // TODO: Remove this when we drop IE7 support
903                         if (tinymce.Env.documentMode <= 7) {
904                                 args.skinUiCss = skinUrl + '/skin.ie7.min.css';
905                         } else {
906                                 args.skinUiCss = skinUrl + '/skin.min.css';
907                         }
908
909                         // Load content.min.css or content.inline.min.css
910                         editor.contentCSS.push(skinUrl + '/content' + (editor.inline ? '.inline' : '') + '.min.css');
911                 }
912
913                 // Handle editor setProgressState change
914                 editor.on('ProgressState', function(e) {
915                         self.throbber = self.throbber || new tinymce.ui.Throbber(self.panel.getEl('body'));
916
917                         if (e.state) {
918                                 self.throbber.show(e.time);
919                         } else {
920                                 self.throbber.hide();
921                         }
922                 });
923
924                 if (settings.inline) {
925                         return renderInlineUI(args);
926                 }
927
928                 return renderIframeUI(args);
929         };
930
931         self.resizeTo = resizeTo;
932         self.resizeBy = resizeBy;
933 });