]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/wordpress/editor_plugin_src.js
WordPress 3.6.1-scripts
[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                         // Fix bug in iOS Safari where it's impossible to type after a touchstart event on the parent document.
232                         // Happens after zooming in or out while the keyboard is open. See #25131.
233                         if ( tinymce.isIOS5 ) {
234                                 ed.onKeyDown.add( function() {
235                                         if ( document.activeElement == document.body ) {
236                                                 ed.getWin().focus();
237                                         }
238                                 });
239                         }
240
241                         ed.onSaveContent.add(function(ed, o) {
242                                 // If editor is hidden, we just want the textarea's value to be saved
243                                 if ( ed.isHidden() )
244                                         o.content = o.element.value;
245                                 else if ( ed.getParam('wpautop', true) && typeof(switchEditors) == 'object' )
246                                         o.content = switchEditors.pre_wpautop(o.content);
247                         });
248
249                         /* disable for now
250                         ed.onBeforeSetContent.add(function(ed, o) {
251                                 o.content = t._setEmbed(o.content);
252                         });
253
254                         ed.onPostProcess.add(function(ed, o) {
255                                 if ( o.get )
256                                         o.content = t._getEmbed(o.content);
257                         });
258                         */
259
260                         // Add listeners to handle more break
261                         t._handleMoreBreak(ed, url);
262
263                         // Add custom shortcuts
264                         mod_key = 'alt+shift';
265
266                 //      if ( tinymce.isGecko ) // disable for mow, too many shortcuts conflicts
267                 //              mod_key = 'ctrl+alt';
268
269                         ed.addShortcut(mod_key + '+c', 'justifycenter_desc', 'JustifyCenter');
270                         ed.addShortcut(mod_key + '+r', 'justifyright_desc', 'JustifyRight');
271                         ed.addShortcut(mod_key + '+l', 'justifyleft_desc', 'JustifyLeft');
272                         ed.addShortcut(mod_key + '+j', 'justifyfull_desc', 'JustifyFull');
273                         ed.addShortcut(mod_key + '+q', 'blockquote_desc', 'mceBlockQuote');
274                         ed.addShortcut(mod_key + '+u', 'bullist_desc', 'InsertUnorderedList');
275                         ed.addShortcut(mod_key + '+o', 'numlist_desc', 'InsertOrderedList');
276                         ed.addShortcut(mod_key + '+n', 'spellchecker.desc', 'mceSpellCheck');
277                         ed.addShortcut(mod_key + '+a', 'link_desc', 'WP_Link');
278                         ed.addShortcut(mod_key + '+s', 'unlink_desc', 'unlink');
279                         ed.addShortcut(mod_key + '+m', 'image_desc', 'WP_Medialib');
280                         ed.addShortcut(mod_key + '+z', 'wordpress.wp_adv_desc', 'WP_Adv');
281                         ed.addShortcut(mod_key + '+t', 'wordpress.wp_more_desc', 'WP_More');
282                         ed.addShortcut(mod_key + '+d', 'striketrough_desc', 'Strikethrough');
283                         ed.addShortcut(mod_key + '+h', 'help_desc', 'WP_Help');
284                         ed.addShortcut(mod_key + '+p', 'wordpress.wp_page_desc', 'WP_Page');
285                         ed.addShortcut('ctrl+s', 'save_desc', function(){if('function'==typeof autosave)autosave();});
286
287                         if ( /\bwpfullscreen\b/.test(ed.settings.plugins) )
288                                 ed.addShortcut(mod_key + '+w', 'wordpress.wp_fullscreen_desc', 'wpFullScreen');
289                         else if ( /\bfullscreen\b/.test(ed.settings.plugins) )
290                                 ed.addShortcut(mod_key + '+g', 'fullscreen.desc', 'mceFullScreen');
291
292                         // popup buttons for images and the gallery
293                         ed.onInit.add(function(ed) {
294                                 tinymce.dom.Event.add(ed.getWin(), 'scroll', function(e) {
295                                         ed.plugins.wordpress._hideButtons();
296                                 });
297                                 tinymce.dom.Event.add(ed.getBody(), 'dragstart', function(e) {
298                                         ed.plugins.wordpress._hideButtons();
299                                 });
300                         });
301
302                         ed.onBeforeExecCommand.add(function(ed, cmd, ui, val) {
303                                 ed.plugins.wordpress._hideButtons();
304                         });
305
306                         ed.onSaveContent.add(function(ed, o) {
307                                 ed.plugins.wordpress._hideButtons();
308                         });
309
310                         ed.onMouseDown.add(function(ed, e) {
311                                 if ( e.target.nodeName != 'IMG' )
312                                         ed.plugins.wordpress._hideButtons();
313                         });
314
315                         ed.onKeyDown.add(function(ed, e){
316                                 if ( e.which == tinymce.VK.DELETE || e.which == tinymce.VK.BACKSPACE )
317                                         ed.plugins.wordpress._hideButtons();
318                         });
319
320                         closeOnClick = function(e){
321                                 var id;
322
323                                 if ( e.target.id == 'mceModalBlocker' || e.target.className == 'ui-widget-overlay' ) {
324                                         for ( id in ed.windowManager.windows ) {
325                                                 ed.windowManager.close(null, id);
326                                         }
327                                 }
328                         }
329
330                         // close popups when clicking on the background
331                         tinymce.dom.Event.remove(document.body, 'click', closeOnClick);
332                         tinymce.dom.Event.add(document.body, 'click', closeOnClick);
333                 },
334
335                 getInfo : function() {
336                         return {
337                                 longname : 'WordPress Plugin',
338                                 author : 'WordPress', // add Moxiecode?
339                                 authorurl : 'http://wordpress.org',
340                                 infourl : 'http://wordpress.org',
341                                 version : '3.0'
342                         };
343                 },
344
345                 // Internal functions
346                 _setEmbed : function(c) {
347                         return c.replace(/\[embed\]([\s\S]+?)\[\/embed\][\s\u00a0]*/g, function(a,b){
348                                 return '<img width="300" height="200" src="' + tinymce.baseURL + '/plugins/wordpress/img/trans.gif" class="wp-oembed mceItemNoResize" alt="'+b+'" title="'+b+'" />';
349                         });
350                 },
351
352                 _getEmbed : function(c) {
353                         return c.replace(/<img[^>]+>/g, function(a) {
354                                 if ( a.indexOf('class="wp-oembed') != -1 ) {
355                                         var u = a.match(/alt="([^\"]+)"/);
356                                         if ( u[1] )
357                                                 a = '[embed]' + u[1] + '[/embed]';
358                                 }
359                                 return a;
360                         });
361                 },
362
363                 _showButtons : function(n, id) {
364                         var ed = tinyMCE.activeEditor, p1, p2, vp, DOM = tinymce.DOM, X, Y;
365
366                         vp = ed.dom.getViewPort(ed.getWin());
367                         p1 = DOM.getPos(ed.getContentAreaContainer());
368                         p2 = ed.dom.getPos(n);
369
370                         X = Math.max(p2.x - vp.x, 0) + p1.x;
371                         Y = Math.max(p2.y - vp.y, 0) + p1.y;
372
373                         DOM.setStyles(id, {
374                                 'top' : Y+5+'px',
375                                 'left' : X+5+'px',
376                                 'display' : 'block'
377                         });
378                 },
379
380                 _hideButtons : function() {
381                         var DOM = tinymce.DOM;
382                         DOM.hide( DOM.select('#wp_editbtns, #wp_gallerybtns') );
383                 },
384
385                 // Resizes the iframe by a relative height value
386                 _resizeIframe : function(ed, tb_id, dy) {
387                         var ifr = ed.getContentAreaContainer().firstChild;
388
389                         DOM.setStyle(ifr, 'height', ifr.clientHeight + dy); // Resize iframe
390                         ed.theme.deltaHeight += dy; // For resize cookie
391                 },
392
393                 _handleMoreBreak : function(ed, url) {
394                         var moreHTML, nextpageHTML;
395
396                         moreHTML = '<img src="' + url + '/img/trans.gif" alt="$1" class="mce-wp-more mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
397                         nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mce-wp-nextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
398
399                         // Display morebreak instead if img in element path
400                         ed.onPostRender.add(function() {
401                                 if (ed.theme.onResolveName) {
402                                         ed.theme.onResolveName.add(function(th, o) {
403                                                 if (o.node.nodeName == 'IMG') {
404                                                         if ( ed.dom.hasClass(o.node, 'mce-wp-more') )
405                                                                 o.name = 'wpmore';
406                                                         if ( ed.dom.hasClass(o.node, 'mce-wp-nextpage') )
407                                                                 o.name = 'wppage';
408                                                 }
409
410                                         });
411                                 }
412                         });
413
414                         // Replace morebreak with images
415                         ed.onBeforeSetContent.add(function(ed, o) {
416                                 if ( o.content ) {
417                                         o.content = o.content.replace(/<!--more(.*?)-->/g, moreHTML);
418                                         o.content = o.content.replace(/<!--nextpage-->/g, nextpageHTML);
419                                 }
420                         });
421
422                         // Replace images with morebreak
423                         ed.onPostProcess.add(function(ed, o) {
424                                 if (o.get)
425                                         o.content = o.content.replace(/<img[^>]+>/g, function(im) {
426                                                 if (im.indexOf('class="mce-wp-more') !== -1) {
427                                                         var m, moretext = (m = im.match(/alt="(.*?)"/)) ? m[1] : '';
428                                                         im = '<!--more'+moretext+'-->';
429                                                 }
430                                                 if (im.indexOf('class="mce-wp-nextpage') !== -1)
431                                                         im = '<!--nextpage-->';
432
433                                                 return im;
434                                         });
435                         });
436
437                         // Set active buttons if user selected pagebreak or more break
438                         ed.onNodeChange.add(function(ed, cm, n) {
439                                 cm.setActive('wp_page', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mce-wp-nextpage'));
440                                 cm.setActive('wp_more', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mce-wp-more'));
441                         });
442                 }
443         });
444
445         // Register plugin
446         tinymce.PluginManager.add('wordpress', tinymce.plugins.WordPress);
447 })();