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