]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/wordpress/editor_plugin_src.js
WordPress 3.8
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / wordpress / editor_plugin_src.js
1 /* global tinymce, getUserSetting, setUserSetting, switchEditors, autosave */
2 /**
3  * WordPress plugin.
4  */
5
6 (function() {
7         var DOM = tinymce.DOM;
8
9         tinymce.create('tinymce.plugins.WordPress', {
10                 init : function(ed, url) {
11                         var t = this, tbId = ed.getParam('wordpress_adv_toolbar', 'toolbar2'), last = 0, moreHTML, nextpageHTML, closeOnClick, mod_key, style;
12                         moreHTML = '<img src="' + url + '/img/trans.gif" class="mce-wp-more mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
13                         nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mce-wp-nextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
14
15                         if ( getUserSetting('hidetb', '0') == '1' )
16                                 ed.settings.wordpress_adv_hidden = 0;
17
18                         // Hides the specified toolbar and resizes the iframe
19                         ed.onPostRender.add(function() {
20                                 var adv_toolbar = ed.controlManager.get(tbId);
21                                 if ( ed.getParam('wordpress_adv_hidden', 1) && adv_toolbar ) {
22                                         DOM.hide(adv_toolbar.id);
23                                         t._resizeIframe(ed, tbId, 28);
24                                 }
25                         });
26
27                         // Register commands
28                         ed.addCommand('WP_More', function() {
29                                 ed.execCommand('mceInsertContent', 0, moreHTML);
30                         });
31
32                         ed.addCommand('WP_Page', function() {
33                                 ed.execCommand('mceInsertContent', 0, nextpageHTML);
34                         });
35
36                         ed.addCommand('WP_Help', function() {
37                                 ed.windowManager.open({
38                                         url : tinymce.baseURL + '/wp-mce-help.php',
39                                         width : 450,
40                                         height : 420,
41                                         inline : 1
42                                 });
43                         });
44
45                         ed.addCommand('WP_Adv', function() {
46                                 var cm = ed.controlManager, id = cm.get(tbId).id;
47
48                                 if ( 'undefined' == id )
49                                         return;
50
51                                 if ( DOM.isHidden(id) ) {
52                                         cm.setActive('wp_adv', 1);
53                                         DOM.show(id);
54                                         t._resizeIframe(ed, tbId, -28);
55                                         ed.settings.wordpress_adv_hidden = 0;
56                                         setUserSetting('hidetb', '1');
57                                 } else {
58                                         cm.setActive('wp_adv', 0);
59                                         DOM.hide(id);
60                                         t._resizeIframe(ed, tbId, 28);
61                                         ed.settings.wordpress_adv_hidden = 1;
62                                         setUserSetting('hidetb', '0');
63                                 }
64                         });
65
66                         ed.addCommand('WP_Medialib', function() {
67                                 if ( typeof wp !== 'undefined' && wp.media && wp.media.editor )
68                                         wp.media.editor.open( ed.id );
69                         });
70
71                         // Register buttons
72                         ed.addButton('wp_more', {
73                                 title : 'wordpress.wp_more_desc',
74                                 cmd : 'WP_More'
75                         });
76
77                         ed.addButton('wp_page', {
78                                 title : 'wordpress.wp_page_desc',
79                                 image : url + '/img/page.gif',
80                                 cmd : 'WP_Page'
81                         });
82
83                         ed.addButton('wp_help', {
84                                 title : 'wordpress.wp_help_desc',
85                                 cmd : 'WP_Help'
86                         });
87
88                         ed.addButton('wp_adv', {
89                                 title : 'wordpress.wp_adv_desc',
90                                 cmd : 'WP_Adv'
91                         });
92
93                         // Add Media button
94                         ed.addButton('add_media', {
95                                 title : 'wordpress.add_media',
96                                 image : url + '/img/image.gif',
97                                 cmd : 'WP_Medialib'
98                         });
99
100                         // Add Media buttons to fullscreen and handle align buttons for image captions
101                         ed.onBeforeExecCommand.add(function(ed, cmd, ui, val, o) {
102                                 var DOM = tinymce.DOM, n, DL, DIV, cls, a, align;
103                                 if ( 'mceFullScreen' == cmd ) {
104                                         if ( 'mce_fullscreen' != ed.id && DOM.select('a.thickbox').length )
105                                                 ed.settings.theme_advanced_buttons1 += ',|,add_media';
106                                 }
107
108                                 if ( 'JustifyLeft' == cmd || 'JustifyRight' == cmd || 'JustifyCenter' == cmd ) {
109                                         n = ed.selection.getNode();
110
111                                         if ( n.nodeName == 'IMG' ) {
112                                                 align = cmd.substr(7).toLowerCase();
113                                                 a = 'align' + align;
114                                                 DL = ed.dom.getParent(n, 'dl.wp-caption');
115                                                 DIV = ed.dom.getParent(n, 'div.mceTemp');
116
117                                                 if ( DL && DIV ) {
118                                                         cls = ed.dom.hasClass(DL, a) ? 'alignnone' : a;
119                                                         DL.className = DL.className.replace(/align[^ '"]+\s?/g, '');
120                                                         ed.dom.addClass(DL, cls);
121
122                                                         if (cls == 'aligncenter')
123                                                                 ed.dom.addClass(DIV, 'mceIEcenter');
124                                                         else
125                                                                 ed.dom.removeClass(DIV, 'mceIEcenter');
126
127                                                         o.terminate = true;
128                                                         ed.execCommand('mceRepaint');
129                                                 } else {
130                                                         if ( ed.dom.hasClass(n, a) )
131                                                                 ed.dom.addClass(n, 'alignnone');
132                                                         else
133                                                                 ed.dom.removeClass(n, 'alignnone');
134                                                 }
135                                         }
136                                 }
137
138                                 if ( tinymce.isWebKit && ( 'InsertUnorderedList' == cmd || 'InsertOrderedList' == cmd ) ) {
139                                         if ( !style )
140                                                 style = ed.dom.create('style', {'type': 'text/css'}, '#tinymce,#tinymce span,#tinymce li,#tinymce li>span,#tinymce p,#tinymce p>span{font:medium sans-serif;color:#000;line-height:normal;}');
141
142                                         ed.getDoc().head.appendChild( style );
143                                 }
144                         });
145
146                         ed.onExecCommand.add( function( ed, cmd ) {
147                                 if ( tinymce.isWebKit && style && ( 'InsertUnorderedList' == cmd || 'InsertOrderedList' == cmd ) )
148                                         ed.dom.remove( style );
149                         });
150
151                         ed.onInit.add(function(ed) {
152                                 var bodyClass = ed.getParam('body_class', ''), body = ed.getBody();
153
154                                 // add body classes
155                                 if ( bodyClass )
156                                         bodyClass = bodyClass.split(' ');
157                                 else
158                                         bodyClass = [];
159
160                                 if ( ed.getParam('directionality', '') == 'rtl' )
161                                         bodyClass.push('rtl');
162
163                                 if ( tinymce.isIE9 )
164                                         bodyClass.push('ie9');
165                                 else if ( tinymce.isIE8 )
166                                         bodyClass.push('ie8');
167                                 else if ( tinymce.isIE7 )
168                                         bodyClass.push('ie7');
169
170                                 if ( ed.id != 'wp_mce_fullscreen' && ed.id != 'mce_fullscreen' )
171                                         bodyClass.push('wp-editor');
172                                 else if ( ed.id == 'mce_fullscreen' )
173                                         bodyClass.push('mce-fullscreen');
174
175                                 tinymce.each( bodyClass, function(cls){
176                                         if ( cls )
177                                                 ed.dom.addClass(body, cls);
178                                 });
179
180                                 // make sure these run last
181                                 ed.onNodeChange.add( function(ed, cm, e) {
182                                         var DL;
183
184                                         if ( e.nodeName == 'IMG' ) {
185                                                 DL = ed.dom.getParent(e, 'dl.wp-caption');
186                                         } else if ( e.nodeName == 'DIV' && ed.dom.hasClass(e, 'mceTemp') ) {
187                                                 DL = e.firstChild;
188
189                                                 if ( ! ed.dom.hasClass(DL, 'wp-caption') )
190                                                         DL = false;
191                                         }
192
193                                         if ( DL ) {
194                                                 if ( ed.dom.hasClass(DL, 'alignleft') )
195                                                         cm.setActive('justifyleft', 1);
196                                                 else if ( ed.dom.hasClass(DL, 'alignright') )
197                                                         cm.setActive('justifyright', 1);
198                                                 else if ( ed.dom.hasClass(DL, 'aligncenter') )
199                                                         cm.setActive('justifycenter', 1);
200                                         }
201                                 });
202
203                                 // remove invalid parent paragraphs when pasting HTML and/or switching to the HTML editor and back
204                                 ed.onBeforeSetContent.add(function(ed, o) {
205                                         if ( o.content ) {
206                                                 o.content = o.content.replace(/<p>\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)( [^>]*)?>/gi, '<$1$2>');
207                                                 o.content = o.content.replace(/<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)>\s*<\/p>/gi, '</$1>');
208                                         }
209                                 });
210                         });
211
212                         // Word count
213                         if ( 'undefined' != typeof(jQuery) ) {
214                                 ed.onKeyUp.add(function(ed, e) {
215                                         var k = e.keyCode || e.charCode;
216
217                                         if ( k == last )
218                                                 return;
219
220                                         if ( 13 == k || 8 == last || 46 == last )
221                                                 jQuery(document).triggerHandler('wpcountwords', [ ed.getContent({format : 'raw'}) ]);
222
223                                         last = k;
224                                 });
225                         }
226
227                         // keep empty paragraphs :(
228                         ed.onSaveContent.addToTop(function(ed, o) {
229                                 o.content = o.content.replace(/<p>(<br ?\/?>|\u00a0|\uFEFF)?<\/p>/g, '<p>&nbsp;</p>');
230                         });
231
232                         // Fix bug in iOS Safari where it's impossible to type after a touchstart event on the parent document.
233                         // Happens after zooming in or out while the keyboard is open. See #25131.
234                         if ( tinymce.isIOS5 ) {
235                                 ed.onKeyDown.add( function() {
236                                         if ( document.activeElement == document.body ) {
237                                                 ed.getWin().focus();
238                                         }
239                                 });
240                         }
241
242                         ed.onSaveContent.add(function(ed, o) {
243                                 // If editor is hidden, we just want the textarea's value to be saved
244                                 if ( ed.isHidden() )
245                                         o.content = o.element.value;
246                                 else if ( ed.getParam('wpautop', true) && typeof(switchEditors) == 'object' )
247                                         o.content = switchEditors.pre_wpautop(o.content);
248                         });
249
250                         /* disable for now
251                         ed.onBeforeSetContent.add(function(ed, o) {
252                                 o.content = t._setEmbed(o.content);
253                         });
254
255                         ed.onPostProcess.add(function(ed, o) {
256                                 if ( o.get )
257                                         o.content = t._getEmbed(o.content);
258                         });
259                         */
260
261                         // Add listeners to handle more break
262                         t._handleMoreBreak(ed, url);
263
264                         // Add custom shortcuts
265                         mod_key = 'alt+shift';
266
267                 //      if ( tinymce.isGecko ) // disable for mow, too many shortcuts conflicts
268                 //              mod_key = 'ctrl+alt';
269
270                         ed.addShortcut(mod_key + '+c', 'justifycenter_desc', 'JustifyCenter');
271                         ed.addShortcut(mod_key + '+r', 'justifyright_desc', 'JustifyRight');
272                         ed.addShortcut(mod_key + '+l', 'justifyleft_desc', 'JustifyLeft');
273                         ed.addShortcut(mod_key + '+j', 'justifyfull_desc', 'JustifyFull');
274                         ed.addShortcut(mod_key + '+q', 'blockquote_desc', 'mceBlockQuote');
275                         ed.addShortcut(mod_key + '+u', 'bullist_desc', 'InsertUnorderedList');
276                         ed.addShortcut(mod_key + '+o', 'numlist_desc', 'InsertOrderedList');
277                         ed.addShortcut(mod_key + '+n', 'spellchecker.desc', 'mceSpellCheck');
278                         ed.addShortcut(mod_key + '+a', 'link_desc', 'WP_Link');
279                         ed.addShortcut(mod_key + '+s', 'unlink_desc', 'unlink');
280                         ed.addShortcut(mod_key + '+m', 'image_desc', 'WP_Medialib');
281                         ed.addShortcut(mod_key + '+z', 'wordpress.wp_adv_desc', 'WP_Adv');
282                         ed.addShortcut(mod_key + '+t', 'wordpress.wp_more_desc', 'WP_More');
283                         ed.addShortcut(mod_key + '+d', 'striketrough_desc', 'Strikethrough');
284                         ed.addShortcut(mod_key + '+h', 'help_desc', 'WP_Help');
285                         ed.addShortcut(mod_key + '+p', 'wordpress.wp_page_desc', 'WP_Page');
286                         ed.addShortcut('ctrl+s', 'save_desc', function(){if('function'==typeof autosave)autosave();});
287
288                         if ( /\bwpfullscreen\b/.test(ed.settings.plugins) )
289                                 ed.addShortcut(mod_key + '+w', 'wordpress.wp_fullscreen_desc', 'wpFullScreen');
290                         else if ( /\bfullscreen\b/.test(ed.settings.plugins) )
291                                 ed.addShortcut(mod_key + '+g', 'fullscreen.desc', 'mceFullScreen');
292
293                         // popup buttons for images and the gallery
294                         ed.onInit.add(function(ed) {
295                                 tinymce.dom.Event.add(ed.getWin(), 'scroll', function() {
296                                         ed.plugins.wordpress._hideButtons();
297                                 });
298                                 tinymce.dom.Event.add(ed.getBody(), 'dragstart', function() {
299                                         ed.plugins.wordpress._hideButtons();
300                                 });
301                         });
302
303                         ed.onBeforeExecCommand.add( function( ed ) {
304                                 ed.plugins.wordpress._hideButtons();
305                         });
306
307                         ed.onSaveContent.add( function( ed ) {
308                                 ed.plugins.wordpress._hideButtons();
309                         });
310
311                         ed.onMouseDown.add(function(ed, e) {
312                                 if ( e.target.nodeName != 'IMG' )
313                                         ed.plugins.wordpress._hideButtons();
314                         });
315
316                         ed.onKeyDown.add(function(ed, e){
317                                 if ( e.which == tinymce.VK.DELETE || e.which == tinymce.VK.BACKSPACE )
318                                         ed.plugins.wordpress._hideButtons();
319                         });
320
321                         closeOnClick = function(e){
322                                 var id;
323
324                                 if ( e.target.id == 'mceModalBlocker' || e.target.className == 'ui-widget-overlay' ) {
325                                         for ( id in ed.windowManager.windows ) {
326                                                 ed.windowManager.close(null, id);
327                                         }
328                                 }
329                         };
330
331                         // close popups when clicking on the background
332                         tinymce.dom.Event.remove(document.body, 'click', closeOnClick);
333                         tinymce.dom.Event.add(document.body, 'click', closeOnClick);
334                 },
335
336                 getInfo : function() {
337                         return {
338                                 longname : 'WordPress Plugin',
339                                 author : 'WordPress', // add Moxiecode?
340                                 authorurl : 'http://wordpress.org',
341                                 infourl : 'http://wordpress.org',
342                                 version : '3.0'
343                         };
344                 },
345
346                 // Internal functions
347                 _setEmbed : function(c) {
348                         return c.replace(/\[embed\]([\s\S]+?)\[\/embed\][\s\u00a0]*/g, function(a,b){
349                                 return '<img width="300" height="200" src="' + tinymce.baseURL + '/plugins/wordpress/img/trans.gif" class="wp-oembed mceItemNoResize" alt="'+b+'" title="'+b+'" />';
350                         });
351                 },
352
353                 _getEmbed : function(c) {
354                         return c.replace(/<img[^>]+>/g, function(a) {
355                                 if ( a.indexOf('class="wp-oembed') != -1 ) {
356                                         var u = a.match(/alt="([^\"]+)"/);
357                                         if ( u[1] )
358                                                 a = '[embed]' + u[1] + '[/embed]';
359                                 }
360                                 return a;
361                         });
362                 },
363
364                 _showButtons : function(n, id) {
365                         var ed = tinymce.activeEditor, p1, p2, vp, DOM = tinymce.DOM, X, Y;
366
367                         vp = ed.dom.getViewPort(ed.getWin());
368                         p1 = DOM.getPos(ed.getContentAreaContainer());
369                         p2 = ed.dom.getPos(n);
370
371                         X = Math.max(p2.x - vp.x, 0) + p1.x;
372                         Y = Math.max(p2.y - vp.y, 0) + p1.y;
373
374                         DOM.setStyles(id, {
375                                 'top' : Y+5+'px',
376                                 'left' : X+5+'px',
377                                 'display' : 'block'
378                         });
379                 },
380
381                 _hideButtons : function() {
382                         var DOM = tinymce.DOM;
383                         DOM.hide( DOM.select('#wp_editbtns, #wp_gallerybtns') );
384                 },
385
386                 // Resizes the iframe by a relative height value
387                 _resizeIframe : function(ed, tb_id, dy) {
388                         var ifr = ed.getContentAreaContainer().firstChild;
389
390                         DOM.setStyle(ifr, 'height', ifr.clientHeight + dy); // Resize iframe
391                         ed.theme.deltaHeight += dy; // For resize cookie
392                 },
393
394                 _handleMoreBreak : function(ed, url) {
395                         var moreHTML, nextpageHTML;
396
397                         moreHTML = '<img src="' + url + '/img/trans.gif" alt="$1" class="mce-wp-more mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
398                         nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mce-wp-nextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
399
400                         // Display morebreak instead if img in element path
401                         ed.onPostRender.add(function() {
402                                 if (ed.theme.onResolveName) {
403                                         ed.theme.onResolveName.add(function(th, o) {
404                                                 if (o.node.nodeName == 'IMG') {
405                                                         if ( ed.dom.hasClass(o.node, 'mce-wp-more') )
406                                                                 o.name = 'wpmore';
407                                                         if ( ed.dom.hasClass(o.node, 'mce-wp-nextpage') )
408                                                                 o.name = 'wppage';
409                                                 }
410
411                                         });
412                                 }
413                         });
414
415                         // Replace morebreak with images
416                         ed.onBeforeSetContent.add(function(ed, o) {
417                                 if ( o.content ) {
418                                         o.content = o.content.replace(/<!--more(.*?)-->/g, moreHTML);
419                                         o.content = o.content.replace(/<!--nextpage-->/g, nextpageHTML);
420                                 }
421                         });
422
423                         // Replace images with morebreak
424                         ed.onPostProcess.add(function(ed, o) {
425                                 if (o.get)
426                                         o.content = o.content.replace(/<img[^>]+>/g, function(im) {
427                                                 if (im.indexOf('class="mce-wp-more') !== -1) {
428                                                         var m, moretext = (m = im.match(/alt="(.*?)"/)) ? m[1] : '';
429                                                         im = '<!--more'+moretext+'-->';
430                                                 }
431                                                 if (im.indexOf('class="mce-wp-nextpage') !== -1)
432                                                         im = '<!--nextpage-->';
433
434                                                 return im;
435                                         });
436                         });
437
438                         // Set active buttons if user selected pagebreak or more break
439                         ed.onNodeChange.add(function(ed, cm, n) {
440                                 cm.setActive('wp_page', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mce-wp-nextpage'));
441                                 cm.setActive('wp_more', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mce-wp-more'));
442                         });
443                 }
444         });
445
446         // Register plugin
447         tinymce.PluginManager.add('wordpress', tinymce.plugins.WordPress);
448 })();