]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/wordpress/plugin.js
Wordpress 4.6-scripts
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / wordpress / plugin.js
1 /* global getUserSetting, setUserSetting */
2 ( function( tinymce ) {
3 // Set the minimum value for the modals z-index higher than #wpadminbar (100000)
4 tinymce.ui.FloatPanel.zIndex = 100100;
5
6 tinymce.PluginManager.add( 'wordpress', function( editor ) {
7         var wpAdvButton, style,
8                 DOM = tinymce.DOM,
9                 each = tinymce.each,
10                 __ = editor.editorManager.i18n.translate,
11                 $ = window.jQuery,
12                 wp = window.wp,
13                 hasWpautop = ( wp && wp.editor && wp.editor.autop && editor.getParam( 'wpautop', true ) );
14
15         if ( $ ) {
16                 $( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] );
17         }
18
19         function toggleToolbars( state ) {
20                 var iframe, initial, toolbars,
21                         pixels = 0;
22
23                 initial = ( state === 'hide' );
24
25                 if ( editor.theme.panel ) {
26                         toolbars = editor.theme.panel.find('.toolbar:not(.menubar)');
27                 }
28
29                 if ( ! toolbars || toolbars.length < 2 || ( state === 'hide' && ! toolbars[1].visible() ) ) {
30                         return;
31                 }
32
33                 if ( ! state && toolbars[1].visible() ) {
34                         state = 'hide';
35                 }
36
37                 each( toolbars, function( toolbar, i ) {
38                         if ( i > 0 ) {
39                                 if ( state === 'hide' ) {
40                                         toolbar.hide();
41                                         pixels += 30;
42                                 } else {
43                                         toolbar.show();
44                                         pixels -= 30;
45                                 }
46                         }
47                 });
48
49                 if ( pixels && ! initial ) {
50                         // Resize iframe, not needed in iOS
51                         if ( ! tinymce.Env.iOS ) {
52                                 iframe = editor.getContentAreaContainer().firstChild;
53                                 DOM.setStyle( iframe, 'height', iframe.clientHeight + pixels );
54                         }
55
56                         if ( state === 'hide' ) {
57                                 setUserSetting('hidetb', '0');
58                                 wpAdvButton && wpAdvButton.active( false );
59                         } else {
60                                 setUserSetting('hidetb', '1');
61                                 wpAdvButton && wpAdvButton.active( true );
62                         }
63                 }
64
65                 editor.fire( 'wp-toolbar-toggle' );
66         }
67
68         // Add the kitchen sink button :)
69         editor.addButton( 'wp_adv', {
70                 tooltip: 'Toolbar Toggle',
71                 cmd: 'WP_Adv',
72                 onPostRender: function() {
73                         wpAdvButton = this;
74                         wpAdvButton.active( getUserSetting( 'hidetb' ) === '1' ? true : false );
75                 }
76         });
77
78         // Hide the toolbars after loading
79         editor.on( 'PostRender', function() {
80                 if ( editor.getParam( 'wordpress_adv_hidden', true ) && getUserSetting( 'hidetb', '0' ) === '0' ) {
81                         toggleToolbars( 'hide' );
82                 }
83         });
84
85         editor.addCommand( 'WP_Adv', function() {
86                 toggleToolbars();
87         });
88
89         editor.on( 'focus', function() {
90         window.wpActiveEditor = editor.id;
91     });
92
93         editor.on( 'BeforeSetContent', function( event ) {
94                 var title;
95
96                 if ( event.content ) {
97                         if ( event.content.indexOf( '<!--more' ) !== -1 ) {
98                                 title = __( 'Read more...' );
99
100                                 event.content = event.content.replace( /<!--more(.*?)-->/g, function( match, moretext ) {
101                                         return '<img src="' + tinymce.Env.transparentSrc + '" data-wp-more="more" data-wp-more-text="' + moretext + '" ' +
102                                                 'class="wp-more-tag mce-wp-more" alt="" title="' + title + '" data-mce-resize="false" data-mce-placeholder="1" />';
103                                 });
104                         }
105
106                         if ( event.content.indexOf( '<!--nextpage-->' ) !== -1 ) {
107                                 title = __( 'Page break' );
108
109                                 event.content = event.content.replace( /<!--nextpage-->/g,
110                                         '<img src="' + tinymce.Env.transparentSrc + '" data-wp-more="nextpage" class="wp-more-tag mce-wp-nextpage" ' +
111                                                 'alt="" title="' + title + '" data-mce-resize="false" data-mce-placeholder="1" />' );
112                         }
113
114                         if ( event.load && event.format !== 'raw' && hasWpautop ) {
115                                 event.content = wp.editor.autop( event.content );
116                         }
117
118                         if ( event.content.indexOf( '<script' ) !== -1 || event.content.indexOf( '<style' ) !== -1 ) {
119                                 event.content = event.content.replace( /<(script|style)[^>]*>[\s\S]*?<\/\1>/g, function( match, tag ) {
120                                         return '<img ' +
121                                                 'src="' + tinymce.Env.transparentSrc + '" ' +
122                                                 'data-wp-preserve="' + encodeURIComponent( match ) + '" ' +
123                                                 'data-mce-resize="false" ' +
124                                                 'data-mce-placeholder="1" '+
125                                                 'class="mce-object" ' +
126                                                 'width="20" height="20" '+
127                                                 'alt="&lt;' + tag + '&gt;" ' +
128                                                 'title="&lt;' + tag + '&gt;" ' +
129                                         '/>';
130                                 } );
131                         }
132
133                         // Remove spaces from empty paragraphs.
134                         // Avoid backtracking, can freeze the editor. See #35890.
135                         // (This is also quite faster than using only one regex.)
136                         event.content = event.content.replace( /<p>([^<>]+)<\/p>/gi, function( tag, text ) {
137                                 if ( /^(&nbsp;|\s|\u00a0|\ufeff)+$/i.test( text ) ) {
138                                         return '<p><br /></p>';
139                                 }
140
141                                 return tag;
142                         });
143                 }
144         });
145
146         editor.on( 'PostProcess', function( event ) {
147                 if ( event.get ) {
148                         event.content = event.content.replace(/<img[^>]+>/g, function( image ) {
149                                 var match,
150                                         string,
151                                         moretext = '';
152
153                                 if ( image.indexOf( 'data-wp-more="more"' ) !== -1 ) {
154                                         if ( match = image.match( /data-wp-more-text="([^"]+)"/ ) ) {
155                                                 moretext = match[1];
156                                         }
157
158                                         string = '<!--more' + moretext + '-->';
159                                 } else if ( image.indexOf( 'data-wp-more="nextpage"' ) !== -1 ) {
160                                         string = '<!--nextpage-->';
161                                 } else if ( image.indexOf( 'data-wp-preserve' ) !== -1 ) {
162                                         if ( match = image.match( / data-wp-preserve="([^"]+)"/ ) ) {
163                                                 string = decodeURIComponent( match[1] );
164                                         }
165                                 }
166
167                                 return string || image;
168                         });
169                 }
170         });
171
172         // Display the tag name instead of img in element path
173         editor.on( 'ResolveName', function( event ) {
174                 var attr;
175
176                 if ( event.target.nodeName === 'IMG' && ( attr = editor.dom.getAttrib( event.target, 'data-wp-more' ) ) ) {
177                         event.name = attr;
178                 }
179         });
180
181         // Register commands
182         editor.addCommand( 'WP_More', function( tag ) {
183                 var parent, html, title,
184                         classname = 'wp-more-tag',
185                         dom = editor.dom,
186                         node = editor.selection.getNode();
187
188                 tag = tag || 'more';
189                 classname += ' mce-wp-' + tag;
190                 title = tag === 'more' ? 'Read more...' : 'Next page';
191                 title = __( title );
192                 html = '<img src="' + tinymce.Env.transparentSrc + '" alt="" title="' + title + '" class="' + classname + '" ' +
193                         'data-wp-more="' + tag + '" data-mce-resize="false" data-mce-placeholder="1" />';
194
195                 // Most common case
196                 if ( node.nodeName === 'BODY' || ( node.nodeName === 'P' && node.parentNode.nodeName === 'BODY' ) ) {
197                         editor.insertContent( html );
198                         return;
199                 }
200
201                 // Get the top level parent node
202                 parent = dom.getParent( node, function( found ) {
203                         if ( found.parentNode && found.parentNode.nodeName === 'BODY' ) {
204                                 return true;
205                         }
206
207                         return false;
208                 }, editor.getBody() );
209
210                 if ( parent ) {
211                         if ( parent.nodeName === 'P' ) {
212                                 parent.appendChild( dom.create( 'p', null, html ).firstChild );
213                         } else {
214                                 dom.insertAfter( dom.create( 'p', null, html ), parent );
215                         }
216
217                         editor.nodeChanged();
218                 }
219         });
220
221         editor.addCommand( 'WP_Code', function() {
222                 editor.formatter.toggle('code');
223         });
224
225         editor.addCommand( 'WP_Page', function() {
226                 editor.execCommand( 'WP_More', 'nextpage' );
227         });
228
229         editor.addCommand( 'WP_Help', function() {
230                 var access = tinymce.Env.mac ? __( 'Ctrl + Alt + letter:' ) : __( 'Shift + Alt + letter:' ),
231                         meta = tinymce.Env.mac ? __( 'Cmd + letter:' ) : __( 'Ctrl + letter:' ),
232                         table1 = [],
233                         table2 = [],
234                         header, html, dialog, $wrap;
235
236                 each( [
237                         { c: 'Copy',      x: 'Cut'              },
238                         { v: 'Paste',     a: 'Select all'       },
239                         { z: 'Undo',      y: 'Redo'             },
240                         { b: 'Bold',      i: 'Italic'           },
241                         { u: 'Underline', k: 'Insert/edit link' }
242                 ], function( row ) {
243                         table1.push( tr( row ) );
244                 } );
245
246                 each( [
247                         { 1: 'Heading 1',             2: 'Heading 2'                     },
248                         { 3: 'Heading 3',             4: 'Heading 4'                     },
249                         { 5: 'Heading 5',             6: 'Heading 6'                     },
250                         { l: 'Align left',            c: 'Align center'                  },
251                         { r: 'Align right',           j: 'Justify'                       },
252                         { d: 'Strikethrough',         q: 'Blockquote'                    },
253                         { u: 'Bullet list',           o: 'Numbered list'                 },
254                         { a: 'Insert/edit link',      s: 'Remove link'                   },
255                         { m: 'Insert/edit image',     t: 'Insert Read More tag'          },
256                         { h: 'Keyboard Shortcuts',    x: 'Code'                          },
257                         { p: 'Insert Page Break tag', w: 'Distraction-free writing mode' }
258                 ], function( row ) {
259                         table2.push( tr( row ) );
260                 } );
261
262                 function tr( row ) {
263                         var out = '<tr>';
264
265                         each( row, function( text, key ) {
266                                 if ( ! text ) {
267                                         out += '<td></td><td></td>';
268                                 } else {
269                                         out += '<td><kbd>' + key + '</kbd></td><td>' + __( text ) + '</td>';
270                                 }
271                         });
272
273                         return out + '</tr>';
274                 }
275
276                 header = [ __( 'Letter' ), __( 'Action' ), __( 'Letter' ), __( 'Action' ) ];
277                 header = '<tr><th>' + header.join( '</th><th>' ) + '</th></tr>';
278
279                 html = '<div class="wp-editor-help">';
280
281                 // Main section, default and additional shortcuts
282                 html = html +
283                         '<h2>' + __( 'Default shortcuts,' ) + ' ' + meta + '</h2>' +
284                         '<table class="wp-help-th-center fixed">' +
285                                 header +
286                                 table1.join('') +
287                         '</table>' +
288                         '<h2>' + __( 'Additional shortcuts,' ) + ' ' + access + '</h2>' +
289                         '<table class="wp-help-th-center fixed">' +
290                                 header +
291                                 table2.join('') +
292                         '</table>';
293
294                 if ( editor.plugins.wptextpattern && ( ! tinymce.Env.ie || tinymce.Env.ie > 8 ) ) {
295                         // Text pattern section
296                         html = html +
297                                 '<h2>' + __( 'When starting a new paragraph with one of these formatting shortcuts followed by a space, the formatting will be applied automatically. Press Backspace or Escape to undo.' ) + '</h2>' +
298                                 '<table class="wp-help-th-center fixed">' +
299                                         tr({ '*':  'Bullet list', '1.':  'Numbered list' }) +
300                                         tr({ '-':  'Bullet list', '1)':  'Numbered list' }) +
301                                 '</table>';
302
303                         html = html +
304                                 '<h2>' + __( 'The following formatting shortcuts are replaced when pressing Enter. Press Escape or the Undo button to undo.' ) + '</h2>' +
305                                 '<table class="wp-help-single">' +
306                                         tr({ '>': 'Blockquote' }) +
307                                         tr({ '##': 'Heading 2' }) +
308                                         tr({ '###': 'Heading 3' }) +
309                                         tr({ '####': 'Heading 4' }) +
310                                         tr({ '#####': 'Heading 5' }) +
311                                         tr({ '######': 'Heading 6' }) +
312                                         tr({ '---': 'Horizontal line' }) +
313                                 '</table>';
314                 }
315
316                 // Focus management section
317                 html = html +
318                         '<h2>' + __( 'Focus shortcuts:' ) + '</h2>' +
319                         '<table class="wp-help-single">' +
320                                 tr({ 'Alt + F8':  'Inline toolbar (when an image, link or preview is selected)' }) +
321                                 tr({ 'Alt + F9':  'Editor menu (when enabled)' }) +
322                                 tr({ 'Alt + F10': 'Editor toolbar' }) +
323                                 tr({ 'Alt + F11': 'Elements path' }) +
324                         '</table>' +
325                         '<p>' + __( 'To move focus to other buttons use Tab or the arrow keys. To return focus to the editor press Escape or use one of the buttons.' ) + '</p>';
326
327                 html += '</div>';
328
329                 dialog = editor.windowManager.open( {
330                         title: 'Keyboard Shortcuts',
331                         items: {
332                                 type: 'container',
333                                 classes: 'wp-help',
334                                 html: html
335                         },
336                         buttons: {
337                                 text: 'Close',
338                                 onclick: 'close'
339                         }
340                 } );
341
342                 if ( dialog.$el ) {
343                         dialog.$el.find( 'div[role="application"]' ).attr( 'role', 'document' );
344                         $wrap = dialog.$el.find( '.mce-wp-help' );
345
346                         if ( $wrap[0] ) {
347                                 $wrap.attr( 'tabindex', '0' );
348                                 $wrap[0].focus();
349                                 $wrap.on( 'keydown', function( event ) {
350                                         // Prevent use of: page up, page down, end, home, left arrow, up arrow, right arrow, down arrow
351                                         // in the dialog keydown handler.
352                                         if ( event.keyCode >= 33 && event.keyCode <= 40 ) {
353                                                 event.stopPropagation();
354                                         }
355                                 });
356                         }
357                 }
358         } );
359
360         editor.addCommand( 'WP_Medialib', function() {
361                 if ( wp && wp.media && wp.media.editor ) {
362                         wp.media.editor.open( editor.id );
363                 }
364         });
365
366         // Register buttons
367         editor.addButton( 'wp_more', {
368                 tooltip: 'Insert Read More tag',
369                 onclick: function() {
370                         editor.execCommand( 'WP_More', 'more' );
371                 }
372         });
373
374         editor.addButton( 'wp_page', {
375                 tooltip: 'Page break',
376                 onclick: function() {
377                         editor.execCommand( 'WP_More', 'nextpage' );
378                 }
379         });
380
381         editor.addButton( 'wp_help', {
382                 tooltip: 'Keyboard Shortcuts',
383                 cmd: 'WP_Help'
384         });
385
386         editor.addButton( 'wp_code', {
387                 tooltip: 'Code',
388                 cmd: 'WP_Code',
389                 stateSelector: 'code'
390         });
391
392         // Menubar
393         // Insert->Add Media
394         if ( wp && wp.media && wp.media.editor ) {
395                 editor.addMenuItem( 'add_media', {
396                         text: 'Add Media',
397                         icon: 'wp-media-library',
398                         context: 'insert',
399                         cmd: 'WP_Medialib'
400                 });
401         }
402
403         // Insert "Read More..."
404         editor.addMenuItem( 'wp_more', {
405                 text: 'Insert Read More tag',
406                 icon: 'wp_more',
407                 context: 'insert',
408                 onclick: function() {
409                         editor.execCommand( 'WP_More', 'more' );
410                 }
411         });
412
413         // Insert "Next Page"
414         editor.addMenuItem( 'wp_page', {
415                 text: 'Page break',
416                 icon: 'wp_page',
417                 context: 'insert',
418                 onclick: function() {
419                         editor.execCommand( 'WP_More', 'nextpage' );
420                 }
421         });
422
423         editor.on( 'BeforeExecCommand', function(e) {
424                 if ( tinymce.Env.webkit && ( e.command === 'InsertUnorderedList' || e.command === 'InsertOrderedList' ) ) {
425                         if ( ! style ) {
426                                 style = editor.dom.create( 'style', {'type': 'text/css'},
427                                         '#tinymce,#tinymce span,#tinymce li,#tinymce li>span,#tinymce p,#tinymce p>span{font:medium sans-serif;color:#000;line-height:normal;}');
428                         }
429
430                         editor.getDoc().head.appendChild( style );
431                 }
432         });
433
434         editor.on( 'ExecCommand', function( e ) {
435                 if ( tinymce.Env.webkit && style &&
436                         ( 'InsertUnorderedList' === e.command || 'InsertOrderedList' === e.command ) ) {
437
438                         editor.dom.remove( style );
439                 }
440         });
441
442         editor.on( 'init', function() {
443                 var env = tinymce.Env,
444                         bodyClass = ['mceContentBody'], // back-compat for themes that use this in editor-style.css...
445                         doc = editor.getDoc(),
446                         dom = editor.dom;
447
448                 if ( env.iOS ) {
449                         dom.addClass( doc.documentElement, 'ios' );
450                 }
451
452                 if ( editor.getParam( 'directionality' ) === 'rtl' ) {
453                         bodyClass.push('rtl');
454                         dom.setAttrib( doc.documentElement, 'dir', 'rtl' );
455                 }
456
457                 dom.setAttrib( doc.documentElement, 'lang', editor.getParam( 'wp_lang_attr' ) );
458
459                 if ( env.ie ) {
460                         if ( parseInt( env.ie, 10 ) === 9 ) {
461                                 bodyClass.push('ie9');
462                         } else if ( parseInt( env.ie, 10 ) === 8 ) {
463                                 bodyClass.push('ie8');
464                         } else if ( env.ie < 8 ) {
465                                 bodyClass.push('ie7');
466                         }
467                 } else if ( env.webkit ) {
468                         bodyClass.push('webkit');
469                 }
470
471                 bodyClass.push('wp-editor');
472
473                 each( bodyClass, function( cls ) {
474                         if ( cls ) {
475                                 dom.addClass( doc.body, cls );
476                         }
477                 });
478
479                 // Remove invalid parent paragraphs when inserting HTML
480                 editor.on( 'BeforeSetContent', function( event ) {
481                         if ( event.content ) {
482                                 event.content = event.content.replace( /<p>\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)( [^>]*)?>/gi, '<$1$2>' )
483                                         .replace( /<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)>\s*<\/p>/gi, '</$1>' );
484                         }
485                 });
486
487                 if ( $ ) {
488                         $( document ).triggerHandler( 'tinymce-editor-init', [editor] );
489                 }
490
491                 if ( window.tinyMCEPreInit && window.tinyMCEPreInit.dragDropUpload ) {
492                         dom.bind( doc, 'dragstart dragend dragover drop', function( event ) {
493                                 if ( $ ) {
494                                         // Trigger the jQuery handlers.
495                                         $( document ).trigger( new $.Event( event ) );
496                                 }
497                         });
498                 }
499
500                 if ( editor.getParam( 'wp_paste_filters', true ) ) {
501                         editor.on( 'PastePreProcess', function( event ) {
502                                 // Remove trailing <br> added by WebKit browsers to the clipboard
503                                 event.content = event.content.replace( /<br class="?Apple-interchange-newline"?>/gi, '' );
504
505                                 // In WebKit this is handled by removeWebKitStyles()
506                                 if ( ! tinymce.Env.webkit ) {
507                                         // Remove all inline styles
508                                         event.content = event.content.replace( /(<[^>]+) style="[^"]*"([^>]*>)/gi, '$1$2' );
509
510                                         // Put back the internal styles
511                                         event.content = event.content.replace(/(<[^>]+) data-mce-style=([^>]+>)/gi, '$1 style=$2' );
512                                 }
513                         });
514
515                         editor.on( 'PastePostProcess', function( event ) {
516                                 // Remove empty paragraphs
517                                 each( dom.select( 'p', event.node ), function( node ) {
518                                         if ( dom.isEmpty( node ) ) {
519                                                 dom.remove( node );
520                                         }
521                                 });
522                         });
523                 }
524         });
525
526         editor.on( 'SaveContent', function( event ) {
527                 // If editor is hidden, we just want the textarea's value to be saved
528                 if ( ! editor.inline && editor.isHidden() ) {
529                         event.content = event.element.value;
530                         return;
531                 }
532
533                 // Keep empty paragraphs :(
534                 event.content = event.content.replace( /<p>(?:<br ?\/?>|\u00a0|\uFEFF| )*<\/p>/g, '<p>&nbsp;</p>' );
535
536                 if ( hasWpautop ) {
537                         event.content = wp.editor.removep( event.content );
538                 }
539         });
540
541         editor.on( 'preInit', function() {
542                 var validElementsSetting = '@[id|accesskey|class|dir|lang|style|tabindex|' +
543                         'title|contenteditable|draggable|dropzone|hidden|spellcheck|translate],' + // Global attributes.
544                         'i,' + // Don't replace <i> with <em> and <b> with <strong> and don't remove them when empty.
545                         'b,' +
546                         'script[src|async|defer|type|charset|crossorigin|integrity]'; // Add support for <script>.
547
548                 editor.schema.addValidElements( validElementsSetting );
549
550                 if ( tinymce.Env.iOS ) {
551                         editor.settings.height = 300;
552                 }
553
554                 each( {
555                         c: 'JustifyCenter',
556                         r: 'JustifyRight',
557                         l: 'JustifyLeft',
558                         j: 'JustifyFull',
559                         q: 'mceBlockQuote',
560                         u: 'InsertUnorderedList',
561                         o: 'InsertOrderedList',
562                         m: 'WP_Medialib',
563                         z: 'WP_Adv',
564                         t: 'WP_More',
565                         d: 'Strikethrough',
566                         h: 'WP_Help',
567                         p: 'WP_Page',
568                         x: 'WP_Code'
569                 }, function( command, key ) {
570                         editor.shortcuts.add( 'access+' + key, '', command );
571                 } );
572
573                 editor.addShortcut( 'meta+s', '', function() {
574                         if ( wp && wp.autosave ) {
575                                 wp.autosave.server.triggerSave();
576                         }
577                 } );
578
579                 if ( window.getUserSetting( 'editor_plain_text_paste_warning' ) > 1 ) {
580                         editor.settings.paste_plaintext_inform = false;
581                 }
582
583                 // Change the editor iframe title on MacOS, add the correct help shortcut.
584                 if ( tinymce.Env.mac ) {
585                         tinymce.$( editor.iframeElement ).attr( 'title', __( 'Rich Text Area. Press Control-Option-H for help.' ) );
586                 }
587         } );
588
589         editor.on( 'PastePlainTextToggle', function( event ) {
590                 // Warn twice, then stop.
591                 if ( event.state === true ) {
592                         var times = parseInt( window.getUserSetting( 'editor_plain_text_paste_warning' ), 10 ) || 0;
593
594                         if ( times < 2 ) {
595                                 window.setUserSetting( 'editor_plain_text_paste_warning', ++times );
596                         }
597                 }
598         });
599
600         /**
601          * Experimental: create a floating toolbar.
602          * This functionality will change in the next releases. Not recommended for use by plugins.
603          */
604         editor.on( 'preinit', function() {
605                 var Factory = tinymce.ui.Factory,
606                         settings = editor.settings,
607                         activeToolbar,
608                         currentSelection,
609                         timeout,
610                         container = editor.getContainer(),
611                         wpAdminbar = document.getElementById( 'wpadminbar' ),
612                         mceIframe = document.getElementById( editor.id + '_ifr' ),
613                         mceToolbar,
614                         mceStatusbar,
615                         wpStatusbar;
616
617                         if ( container ) {
618                                 mceToolbar = tinymce.$( '.mce-toolbar-grp', container )[0];
619                                 mceStatusbar = tinymce.$( '.mce-statusbar', container )[0];
620                         }
621
622                         if ( editor.id === 'content' ) {
623                                 wpStatusbar = document.getElementById( 'post-status-info' );
624                         }
625
626                 function create( buttons, bottom ) {
627                         var toolbar,
628                                 toolbarItems = [],
629                                 buttonGroup;
630
631                         each( buttons, function( item ) {
632                                 var itemName;
633
634                                 function bindSelectorChanged() {
635                                         var selection = editor.selection;
636
637                                         if ( itemName === 'bullist' ) {
638                                                 selection.selectorChanged( 'ul > li', function( state, args ) {
639                                                         var i = args.parents.length,
640                                                                 nodeName;
641
642                                                         while ( i-- ) {
643                                                                 nodeName = args.parents[ i ].nodeName;
644
645                                                                 if ( nodeName === 'OL' || nodeName == 'UL' ) {
646                                                                         break;
647                                                                 }
648                                                         }
649
650                                                         item.active( state && nodeName === 'UL' );
651                                                 } );
652                                         }
653
654                                         if ( itemName === 'numlist' ) {
655                                                 selection.selectorChanged( 'ol > li', function( state, args ) {
656                                                         var i = args.parents.length,
657                                                                 nodeName;
658
659                                                         while ( i-- ) {
660                                                                 nodeName = args.parents[ i ].nodeName;
661
662                                                                 if ( nodeName === 'OL' || nodeName === 'UL' ) {
663                                                                         break;
664                                                                 }
665                                                         }
666
667                                                         item.active( state && nodeName === 'OL' );
668                                                 } );
669                                         }
670
671                                         if ( item.settings.stateSelector ) {
672                                                 selection.selectorChanged( item.settings.stateSelector, function( state ) {
673                                                         item.active( state );
674                                                 }, true );
675                                         }
676
677                                         if ( item.settings.disabledStateSelector ) {
678                                                 selection.selectorChanged( item.settings.disabledStateSelector, function( state ) {
679                                                         item.disabled( state );
680                                                 } );
681                                         }
682                                 }
683
684                                 if ( item === '|' ) {
685                                         buttonGroup = null;
686                                 } else {
687                                         if ( Factory.has( item ) ) {
688                                                 item = {
689                                                         type: item
690                                                 };
691
692                                                 if ( settings.toolbar_items_size ) {
693                                                         item.size = settings.toolbar_items_size;
694                                                 }
695
696                                                 toolbarItems.push( item );
697
698                                                 buttonGroup = null;
699                                         } else {
700                                                 if ( ! buttonGroup ) {
701                                                         buttonGroup = {
702                                                                 type: 'buttongroup',
703                                                                 items: []
704                                                         };
705
706                                                         toolbarItems.push( buttonGroup );
707                                                 }
708
709                                                 if ( editor.buttons[ item ] ) {
710                                                         itemName = item;
711                                                         item = editor.buttons[ itemName ];
712
713                                                         if ( typeof item === 'function' ) {
714                                                                 item = item();
715                                                         }
716
717                                                         item.type = item.type || 'button';
718
719                                                         if ( settings.toolbar_items_size ) {
720                                                                 item.size = settings.toolbar_items_size;
721                                                         }
722
723                                                         item = Factory.create( item );
724
725                                                         buttonGroup.items.push( item );
726
727                                                         if ( editor.initialized ) {
728                                                                 bindSelectorChanged();
729                                                         } else {
730                                                                 editor.on( 'init', bindSelectorChanged );
731                                                         }
732                                                 }
733                                         }
734                                 }
735                         } );
736
737                         toolbar = Factory.create( {
738                                 type: 'panel',
739                                 layout: 'stack',
740                                 classes: 'toolbar-grp inline-toolbar-grp',
741                                 ariaRoot: true,
742                                 ariaRemember: true,
743                                 items: [ {
744                                         type: 'toolbar',
745                                         layout: 'flow',
746                                         items: toolbarItems
747                                 } ]
748                         } );
749
750                         toolbar.bottom = bottom;
751
752                         function reposition() {
753                                 if ( ! currentSelection ) {
754                                         return this;
755                                 }
756
757                                 var scrollX = window.pageXOffset || document.documentElement.scrollLeft,
758                                         scrollY = window.pageYOffset || document.documentElement.scrollTop,
759                                         windowWidth = window.innerWidth,
760                                         windowHeight = window.innerHeight,
761                                         iframeRect = mceIframe ? mceIframe.getBoundingClientRect() : {
762                                                 top: 0,
763                                                 right: windowWidth,
764                                                 bottom: windowHeight,
765                                                 left: 0,
766                                                 width: windowWidth,
767                                                 height: windowHeight
768                                         },
769                                         toolbar = this.getEl(),
770                                         toolbarWidth = toolbar.offsetWidth,
771                                         toolbarHeight = toolbar.clientHeight,
772                                         selection = currentSelection.getBoundingClientRect(),
773                                         selectionMiddle = ( selection.left + selection.right ) / 2,
774                                         buffer = 5,
775                                         spaceNeeded = toolbarHeight + buffer,
776                                         wpAdminbarBottom = wpAdminbar ? wpAdminbar.getBoundingClientRect().bottom : 0,
777                                         mceToolbarBottom = mceToolbar ? mceToolbar.getBoundingClientRect().bottom : 0,
778                                         mceStatusbarTop = mceStatusbar ? windowHeight - mceStatusbar.getBoundingClientRect().top : 0,
779                                         wpStatusbarTop = wpStatusbar ? windowHeight - wpStatusbar.getBoundingClientRect().top : 0,
780                                         blockedTop = Math.max( 0, wpAdminbarBottom, mceToolbarBottom, iframeRect.top ),
781                                         blockedBottom = Math.max( 0, mceStatusbarTop, wpStatusbarTop, windowHeight - iframeRect.bottom ),
782                                         spaceTop = selection.top + iframeRect.top - blockedTop,
783                                         spaceBottom = windowHeight - iframeRect.top - selection.bottom - blockedBottom,
784                                         editorHeight = windowHeight - blockedTop - blockedBottom,
785                                         className = '',
786                                         iosOffsetTop = 0,
787                                         iosOffsetBottom = 0,
788                                         top, left;
789
790                                 if ( spaceTop >= editorHeight || spaceBottom >= editorHeight ) {
791                                         this.scrolling = true;
792                                         this.hide();
793                                         this.scrolling = false;
794                                         return this;
795                                 }
796
797                                 // Add offset in iOS to move the menu over the image, out of the way of the default iOS menu.
798                                 if ( tinymce.Env.iOS && currentSelection.nodeName === 'IMG' ) {
799                                         iosOffsetTop = 54;
800                                         iosOffsetBottom = 46;
801                                 }
802
803                                 if ( this.bottom ) {
804                                         if ( spaceBottom >= spaceNeeded ) {
805                                                 className = ' mce-arrow-up';
806                                                 top = selection.bottom + iframeRect.top + scrollY - iosOffsetBottom;
807                                         } else if ( spaceTop >= spaceNeeded ) {
808                                                 className = ' mce-arrow-down';
809                                                 top = selection.top + iframeRect.top + scrollY - toolbarHeight + iosOffsetTop;
810                                         }
811                                 } else {
812                                         if ( spaceTop >= spaceNeeded ) {
813                                                 className = ' mce-arrow-down';
814                                                 top = selection.top + iframeRect.top + scrollY - toolbarHeight + iosOffsetTop;
815                                         } else if ( spaceBottom >= spaceNeeded && editorHeight / 2 > selection.bottom + iframeRect.top - blockedTop ) {
816                                                 className = ' mce-arrow-up';
817                                                 top = selection.bottom + iframeRect.top + scrollY - iosOffsetBottom;
818                                         }
819                                 }
820
821                                 if ( typeof top === 'undefined' ) {
822                                         top = scrollY + blockedTop + buffer + iosOffsetBottom;
823                                 }
824
825                                 left = selectionMiddle - toolbarWidth / 2 + iframeRect.left + scrollX;
826
827                                 if ( selection.left < 0 || selection.right > iframeRect.width ) {
828                                         left = iframeRect.left + scrollX + ( iframeRect.width - toolbarWidth ) / 2;
829                                 } else if ( toolbarWidth >= windowWidth ) {
830                                         className += ' mce-arrow-full';
831                                         left = 0;
832                                 } else if ( ( left < 0 && selection.left + toolbarWidth > windowWidth ) || ( left + toolbarWidth > windowWidth && selection.right - toolbarWidth < 0 ) ) {
833                                         left = ( windowWidth - toolbarWidth ) / 2;
834                                 } else if ( left < iframeRect.left + scrollX ) {
835                                         className += ' mce-arrow-left';
836                                         left = selection.left + iframeRect.left + scrollX;
837                                 } else if ( left + toolbarWidth > iframeRect.width + iframeRect.left + scrollX ) {
838                                         className += ' mce-arrow-right';
839                                         left = selection.right - toolbarWidth + iframeRect.left + scrollX;
840                                 }
841
842                                 // No up/down arrows on the menu over images in iOS.
843                                 if ( tinymce.Env.iOS && currentSelection.nodeName === 'IMG' ) {
844                                         className = className.replace( / ?mce-arrow-(up|down)/g, '' );
845                                 }
846
847                                 toolbar.className = toolbar.className.replace( / ?mce-arrow-[\w]+/g, '' ) + className;
848
849                                 DOM.setStyles( toolbar, {
850                                         'left': left,
851                                         'top': top
852                                 } );
853
854                                 return this;
855                         }
856
857                         toolbar.on( 'show', function() {
858                                 this.reposition();
859                         } );
860
861                         toolbar.on( 'keydown', function( event ) {
862                                 if ( event.keyCode === 27 ) {
863                                         this.hide();
864                                         editor.focus();
865                                 }
866                         } );
867
868                         editor.on( 'remove', function() {
869                                 toolbar.remove();
870                         } );
871
872                         toolbar.reposition = reposition;
873                         toolbar.hide().renderTo( document.body );
874
875                         return toolbar;
876                 }
877
878                 editor.shortcuts.add( 'alt+119', '', function() {
879                         var node;
880
881                         if ( activeToolbar ) {
882                                 node = activeToolbar.find( 'toolbar' )[0];
883                                 node && node.focus( true );
884                         }
885                 } );
886
887                 editor.on( 'nodechange', function( event ) {
888                         var collapsed = editor.selection.isCollapsed();
889
890                         var args = {
891                                 element: event.element,
892                                 parents: event.parents,
893                                 collapsed: collapsed
894                         };
895
896                         editor.fire( 'wptoolbar', args );
897
898                         currentSelection = args.selection || args.element;
899
900                         if ( activeToolbar && activeToolbar !== args.toolbar ) {
901                                 activeToolbar.hide();
902                         }
903
904                         if ( args.toolbar ) {
905                                 if ( activeToolbar !== args.toolbar ) {
906                                         activeToolbar = args.toolbar;
907                                         activeToolbar.show();
908                                 } else {
909                                         activeToolbar.reposition();
910                                 }
911                         } else {
912                                 activeToolbar = false;
913                         }
914                 } );
915
916                 editor.on( 'focus', function() {
917                         if ( activeToolbar ) {
918                                 activeToolbar.show();
919                         }
920                 } );
921
922                 function hide( event ) {
923                         if ( activeToolbar ) {
924                                 if ( activeToolbar.tempHide || event.type === 'hide' ) {
925                                         activeToolbar.hide();
926                                         activeToolbar = false;
927                                 } else if ( (
928                                         event.type === 'resizewindow' ||
929                                         event.type === 'scrollwindow' ||
930                                         event.type === 'resize' ||
931                                         event.type === 'scroll'
932                                 ) && ! activeToolbar.blockHide ) {
933                                         clearTimeout( timeout );
934
935                                         timeout = setTimeout( function() {
936                                                 if ( activeToolbar && typeof activeToolbar.show === 'function' ) {
937                                                         activeToolbar.scrolling = false;
938                                                         activeToolbar.show();
939                                                 }
940                                         }, 250 );
941
942                                         activeToolbar.scrolling = true;
943                                         activeToolbar.hide();
944                                 }
945                         }
946                 }
947
948                 // For full height editor.
949                 editor.on( 'resizewindow scrollwindow', hide );
950                 // For scrollable editor.
951                 editor.dom.bind( editor.getWin(), 'resize scroll', hide );
952
953                 editor.on( 'remove', function() {
954                         editor.off( 'resizewindow scrollwindow', hide );
955                         editor.dom.unbind( editor.getWin(), 'resize scroll', hide );
956                 } );
957
958                 editor.on( 'blur hide', hide );
959
960                 editor.wp = editor.wp || {};
961                 editor.wp._createToolbar = create;
962         }, true );
963
964         function noop() {}
965
966         // Expose some functions (back-compat)
967         return {
968                 _showButtons: noop,
969                 _hideButtons: noop,
970                 _setEmbed: noop,
971                 _getEmbed: noop
972         };
973 });
974
975 }( window.tinymce ));