]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/wordpress/editor_plugin_src.js
WordPress 3.5-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;
11                         moreHTML = '<img src="' + url + '/img/trans.gif" class="mceWPmore mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
12                         nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mceWPnextpage 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
138                         ed.onInit.add(function(ed) {
139                                 var bodyClass = ed.getParam('body_class', ''), body = ed.getBody();
140
141                                 // add body classes
142                                 if ( bodyClass )
143                                         bodyClass = bodyClass.split(' ');
144                                 else
145                                         bodyClass = [];
146
147                                 if ( ed.getParam('directionality', '') == 'rtl' )
148                                         bodyClass.push('rtl');
149
150                                 if ( tinymce.isIE9 )
151                                         bodyClass.push('ie9');
152                                 else if ( tinymce.isIE8 )
153                                         bodyClass.push('ie8');
154                                 else if ( tinymce.isIE7 )
155                                         bodyClass.push('ie7');
156
157                                 if ( ed.id != 'wp_mce_fullscreen' && ed.id != 'mce_fullscreen' )
158                                         bodyClass.push('wp-editor');
159                                 else if ( ed.id == 'mce_fullscreen' )
160                                         bodyClass.push('mce-fullscreen');
161
162                                 tinymce.each( bodyClass, function(cls){
163                                         if ( cls )
164                                                 ed.dom.addClass(body, cls);
165                                 });
166
167                                 // make sure these run last
168                                 ed.onNodeChange.add( function(ed, cm, e) {
169                                         var DL;
170
171                                         if ( e.nodeName == 'IMG' ) {
172                                                 DL = ed.dom.getParent(e, 'dl.wp-caption');
173                                         } else if ( e.nodeName == 'DIV' && ed.dom.hasClass(e, 'mceTemp') ) {
174                                                 DL = e.firstChild;
175
176                                                 if ( ! ed.dom.hasClass(DL, 'wp-caption') )
177                                                         DL = false;
178                                         }
179
180                                         if ( DL ) {
181                                                 if ( ed.dom.hasClass(DL, 'alignleft') )
182                                                         cm.setActive('justifyleft', 1);
183                                                 else if ( ed.dom.hasClass(DL, 'alignright') )
184                                                         cm.setActive('justifyright', 1);
185                                                 else if ( ed.dom.hasClass(DL, 'aligncenter') )
186                                                         cm.setActive('justifycenter', 1);
187                                         }
188                                 });
189
190                                 // remove invalid parent paragraphs when pasting HTML and/or switching to the HTML editor and back
191                                 ed.onBeforeSetContent.add(function(ed, o) {
192                                         if ( o.content ) {
193                                                 o.content = o.content.replace(/<p>\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)( [^>]*)?>/gi, '<$1$2>');
194                                                 o.content = o.content.replace(/<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)>\s*<\/p>/gi, '</$1>');
195                                         }
196                                 });
197                         });
198
199                         // Word count
200                         if ( 'undefined' != typeof(jQuery) ) {
201                                 ed.onKeyUp.add(function(ed, e) {
202                                         var k = e.keyCode || e.charCode;
203
204                                         if ( k == last )
205                                                 return;
206
207                                         if ( 13 == k || 8 == last || 46 == last )
208                                                 jQuery(document).triggerHandler('wpcountwords', [ ed.getContent({format : 'raw'}) ]);
209
210                                         last = k;
211                                 });
212                         };
213
214                         // keep empty paragraphs :(
215                         ed.onSaveContent.addToTop(function(ed, o) {
216                                 o.content = o.content.replace(/<p>(<br ?\/?>|\u00a0|\uFEFF)?<\/p>/g, '<p>&nbsp;</p>');
217                         });
218
219                         ed.onSaveContent.add(function(ed, o) {
220                                 if ( ed.getParam('wpautop', true) && typeof(switchEditors) == 'object' ) {
221                                         if ( ed.isHidden() )
222                                                 o.content = o.element.value;
223                                         else
224                                                 o.content = switchEditors.pre_wpautop(o.content);
225                                 }
226                         });
227
228                         /* disable for now
229                         ed.onBeforeSetContent.add(function(ed, o) {
230                                 o.content = t._setEmbed(o.content);
231                         });
232
233                         ed.onPostProcess.add(function(ed, o) {
234                                 if ( o.get )
235                                         o.content = t._getEmbed(o.content);
236                         });
237                         */
238
239                         // Add listeners to handle more break
240                         t._handleMoreBreak(ed, url);
241
242                         // Add custom shortcuts
243                         mod_key = 'alt+shift';
244
245                 //      if ( tinymce.isGecko ) // disable for mow, too many shortcuts conflicts
246                 //              mod_key = 'ctrl+alt';
247
248                         ed.addShortcut(mod_key + '+c', 'justifycenter_desc', 'JustifyCenter');
249                         ed.addShortcut(mod_key + '+r', 'justifyright_desc', 'JustifyRight');
250                         ed.addShortcut(mod_key + '+l', 'justifyleft_desc', 'JustifyLeft');
251                         ed.addShortcut(mod_key + '+j', 'justifyfull_desc', 'JustifyFull');
252                         ed.addShortcut(mod_key + '+q', 'blockquote_desc', 'mceBlockQuote');
253                         ed.addShortcut(mod_key + '+u', 'bullist_desc', 'InsertUnorderedList');
254                         ed.addShortcut(mod_key + '+o', 'numlist_desc', 'InsertOrderedList');
255                         ed.addShortcut(mod_key + '+n', 'spellchecker.desc', 'mceSpellCheck');
256                         ed.addShortcut(mod_key + '+a', 'link_desc', 'WP_Link');
257                         ed.addShortcut(mod_key + '+s', 'unlink_desc', 'unlink');
258                         ed.addShortcut(mod_key + '+m', 'image_desc', 'WP_Medialib');
259                         ed.addShortcut(mod_key + '+z', 'wordpress.wp_adv_desc', 'WP_Adv');
260                         ed.addShortcut(mod_key + '+t', 'wordpress.wp_more_desc', 'WP_More');
261                         ed.addShortcut(mod_key + '+d', 'striketrough_desc', 'Strikethrough');
262                         ed.addShortcut(mod_key + '+h', 'help_desc', 'WP_Help');
263                         ed.addShortcut(mod_key + '+p', 'wordpress.wp_page_desc', 'WP_Page');
264                         ed.addShortcut('ctrl+s', 'save_desc', function(){if('function'==typeof autosave)autosave();});
265
266                         if ( /\bwpfullscreen\b/.test(ed.settings.plugins) )
267                                 ed.addShortcut(mod_key + '+w', 'wordpress.wp_fullscreen_desc', 'wpFullScreen');
268                         else if ( /\bfullscreen\b/.test(ed.settings.plugins) )
269                                 ed.addShortcut(mod_key + '+g', 'fullscreen.desc', 'mceFullScreen');
270
271                         // popup buttons for images and the gallery
272                         ed.onInit.add(function(ed) {
273                                 tinymce.dom.Event.add(ed.getWin(), 'scroll', function(e) {
274                                         ed.plugins.wordpress._hideButtons();
275                                 });
276                                 tinymce.dom.Event.add(ed.getBody(), 'dragstart', function(e) {
277                                         ed.plugins.wordpress._hideButtons();
278                                 });
279                         });
280
281                         ed.onBeforeExecCommand.add(function(ed, cmd, ui, val) {
282                                 ed.plugins.wordpress._hideButtons();
283                         });
284
285                         ed.onSaveContent.add(function(ed, o) {
286                                 ed.plugins.wordpress._hideButtons();
287                         });
288
289                         ed.onMouseDown.add(function(ed, e) {
290                                 if ( e.target.nodeName != 'IMG' )
291                                         ed.plugins.wordpress._hideButtons();
292                         });
293
294                         ed.onKeyDown.add(function(ed, e){
295                                 if ( e.which == tinymce.VK.DELETE || e.which == tinymce.VK.BACKSPACE )
296                                         ed.plugins.wordpress._hideButtons();
297                         });
298
299                         closeOnClick = function(e){
300                                 var id;
301
302                                 if ( e.target.id == 'mceModalBlocker' || e.target.className == 'ui-widget-overlay' ) {
303                                         for ( id in ed.windowManager.windows ) {
304                                                 ed.windowManager.close(null, id);
305                                         }
306                                 }
307                         }
308
309                         // close popups when clicking on the background
310                         tinymce.dom.Event.remove(document.body, 'click', closeOnClick);
311                         tinymce.dom.Event.add(document.body, 'click', closeOnClick);
312                 },
313
314                 getInfo : function() {
315                         return {
316                                 longname : 'WordPress Plugin',
317                                 author : 'WordPress', // add Moxiecode?
318                                 authorurl : 'http://wordpress.org',
319                                 infourl : 'http://wordpress.org',
320                                 version : '3.0'
321                         };
322                 },
323
324                 // Internal functions
325                 _setEmbed : function(c) {
326                         return c.replace(/\[embed\]([\s\S]+?)\[\/embed\][\s\u00a0]*/g, function(a,b){
327                                 return '<img width="300" height="200" src="' + tinymce.baseURL + '/plugins/wordpress/img/trans.gif" class="wp-oembed mceItemNoResize" alt="'+b+'" title="'+b+'" />';
328                         });
329                 },
330
331                 _getEmbed : function(c) {
332                         return c.replace(/<img[^>]+>/g, function(a) {
333                                 if ( a.indexOf('class="wp-oembed') != -1 ) {
334                                         var u = a.match(/alt="([^\"]+)"/);
335                                         if ( u[1] )
336                                                 a = '[embed]' + u[1] + '[/embed]';
337                                 }
338                                 return a;
339                         });
340                 },
341
342                 _showButtons : function(n, id) {
343                         var ed = tinyMCE.activeEditor, p1, p2, vp, DOM = tinymce.DOM, X, Y;
344
345                         vp = ed.dom.getViewPort(ed.getWin());
346                         p1 = DOM.getPos(ed.getContentAreaContainer());
347                         p2 = ed.dom.getPos(n);
348
349                         X = Math.max(p2.x - vp.x, 0) + p1.x;
350                         Y = Math.max(p2.y - vp.y, 0) + p1.y;
351
352                         DOM.setStyles(id, {
353                                 'top' : Y+5+'px',
354                                 'left' : X+5+'px',
355                                 'display' : 'block'
356                         });
357                 },
358
359                 _hideButtons : function() {
360                         var DOM = tinymce.DOM;
361                         DOM.hide( DOM.select('#wp_editbtns, #wp_gallerybtns') );
362                 },
363
364                 // Resizes the iframe by a relative height value
365                 _resizeIframe : function(ed, tb_id, dy) {
366                         var ifr = ed.getContentAreaContainer().firstChild;
367
368                         DOM.setStyle(ifr, 'height', ifr.clientHeight + dy); // Resize iframe
369                         ed.theme.deltaHeight += dy; // For resize cookie
370                 },
371
372                 _handleMoreBreak : function(ed, url) {
373                         var moreHTML, nextpageHTML;
374
375                         moreHTML = '<img src="' + url + '/img/trans.gif" alt="$1" class="mceWPmore mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
376                         nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mceWPnextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
377
378                         // Display morebreak instead if img in element path
379                         ed.onPostRender.add(function() {
380                                 if (ed.theme.onResolveName) {
381                                         ed.theme.onResolveName.add(function(th, o) {
382                                                 if (o.node.nodeName == 'IMG') {
383                                                         if ( ed.dom.hasClass(o.node, 'mceWPmore') )
384                                                                 o.name = 'wpmore';
385                                                         if ( ed.dom.hasClass(o.node, 'mceWPnextpage') )
386                                                                 o.name = 'wppage';
387                                                 }
388
389                                         });
390                                 }
391                         });
392
393                         // Replace morebreak with images
394                         ed.onBeforeSetContent.add(function(ed, o) {
395                                 if ( o.content ) {
396                                         o.content = o.content.replace(/<!--more(.*?)-->/g, moreHTML);
397                                         o.content = o.content.replace(/<!--nextpage-->/g, nextpageHTML);
398                                 }
399                         });
400
401                         // Replace images with morebreak
402                         ed.onPostProcess.add(function(ed, o) {
403                                 if (o.get)
404                                         o.content = o.content.replace(/<img[^>]+>/g, function(im) {
405                                                 if (im.indexOf('class="mceWPmore') !== -1) {
406                                                         var m, moretext = (m = im.match(/alt="(.*?)"/)) ? m[1] : '';
407                                                         im = '<!--more'+moretext+'-->';
408                                                 }
409                                                 if (im.indexOf('class="mceWPnextpage') !== -1)
410                                                         im = '<!--nextpage-->';
411
412                                                 return im;
413                                         });
414                         });
415
416                         // Set active buttons if user selected pagebreak or more break
417                         ed.onNodeChange.add(function(ed, cm, n) {
418                                 cm.setActive('wp_page', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPnextpage'));
419                                 cm.setActive('wp_more', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPmore'));
420                         });
421                 }
422         });
423
424         // Register plugin
425         tinymce.PluginManager.add('wordpress', tinymce.plugins.WordPress);
426 })();