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