]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/wordpress/editor_plugin.dev.js
Wordpress 3.2
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / wordpress / editor_plugin.dev.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;
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                         // Register buttons
68                         ed.addButton('wp_more', {
69                                 title : 'wordpress.wp_more_desc',
70                                 cmd : 'WP_More'
71                         });
72
73                         ed.addButton('wp_page', {
74                                 title : 'wordpress.wp_page_desc',
75                                 image : url + '/img/page.gif',
76                                 cmd : 'WP_Page'
77                         });
78
79                         ed.addButton('wp_help', {
80                                 title : 'wordpress.wp_help_desc',
81                                 cmd : 'WP_Help'
82                         });
83
84                         ed.addButton('wp_adv', {
85                                 title : 'wordpress.wp_adv_desc',
86                                 cmd : 'WP_Adv'
87                         });
88
89                         // Add Media buttons
90                         ed.addButton('add_media', {
91                                 title : 'wordpress.add_media',
92                                 image : url + '/img/media.gif',
93                                 onclick : function() {
94                                         tb_show('', tinymce.DOM.get('add_media').href);
95                                         tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
96                                 }
97                         });
98
99                         ed.addButton('add_image', {
100                                 title : 'wordpress.add_image',
101                                 image : url + '/img/image.gif',
102                                 onclick : function() {
103                                         tb_show('', tinymce.DOM.get('add_image').href);
104                                         tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
105                                 }
106                         });
107
108                         ed.addButton('add_video', {
109                                 title : 'wordpress.add_video',
110                                 image : url + '/img/video.gif',
111                                 onclick : function() {
112                                         tb_show('', tinymce.DOM.get('add_video').href);
113                                         tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
114                                 }
115                         });
116
117                         ed.addButton('add_audio', {
118                                 title : 'wordpress.add_audio',
119                                 image : url + '/img/audio.gif',
120                                 onclick : function() {
121                                         tb_show('', tinymce.DOM.get('add_audio').href);
122                                         tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
123                                 }
124                         });
125
126                         // Add Media buttons to fullscreen and handle align buttons for image captions
127                         ed.onBeforeExecCommand.add(function(ed, cmd, ui, val, o) {
128                                 var DOM = tinymce.DOM, n, DL, DIV, cls, a, align;
129                                 if ( 'mceFullScreen' == cmd ) {
130                                         if ( 'mce_fullscreen' != ed.id && DOM.get('add_audio') && DOM.get('add_video') && DOM.get('add_image') && DOM.get('add_media') )
131                                                 ed.settings.theme_advanced_buttons1 += ',|,add_image,add_video,add_audio,add_media';
132                                 }
133
134                                 if ( 'JustifyLeft' == cmd || 'JustifyRight' == cmd || 'JustifyCenter' == cmd ) {
135                                         n = ed.selection.getNode();
136
137                                         if ( n.nodeName == 'IMG' ) {
138                                                 align = cmd.substr(7).toLowerCase();
139                                                 a = 'align' + align;
140                                                 DL = ed.dom.getParent(n, 'dl.wp-caption');
141                                                 DIV = ed.dom.getParent(n, 'div.mceTemp');
142
143                                                 if ( DL && DIV ) {
144                                                         cls = ed.dom.hasClass(DL, a) ? 'alignnone' : a;
145                                                         DL.className = DL.className.replace(/align[^ '"]+\s?/g, '');
146                                                         ed.dom.addClass(DL, cls);
147
148                                                         if (cls == 'aligncenter')
149                                                                 ed.dom.addClass(DIV, 'mceIEcenter');
150                                                         else
151                                                                 ed.dom.removeClass(DIV, 'mceIEcenter');
152
153                                                         o.terminate = true;
154                                                         ed.execCommand('mceRepaint');
155                                                 } else {
156                                                         if ( ed.dom.hasClass(n, a) )
157                                                                 ed.dom.addClass(n, 'alignnone');
158                                                         else
159                                                                 ed.dom.removeClass(n, 'alignnone');
160                                                 }
161                                         }
162                                 }
163                         });
164                         
165                         ed.onInit.add(function(ed) {
166                                 // make sure these run last
167                                 ed.onNodeChange.add( function(ed, cm, e) {
168                                         var DL;
169
170                                         if ( e.nodeName == 'IMG' ) {
171                                                 DL = ed.dom.getParent(e, 'dl.wp-caption');
172                                         } else if ( e.nodeName == 'DIV' && ed.dom.hasClass(e, 'mceTemp') ) {
173                                                 DL = e.firstChild;
174
175                                                 if ( ! ed.dom.hasClass(DL, 'wp-caption') )
176                                                         DL = false;
177                                         }
178
179                                         if ( DL ) {
180                                                 if ( ed.dom.hasClass(DL, 'alignleft') )
181                                                         cm.setActive('justifyleft', 1);
182                                                 else if ( ed.dom.hasClass(DL, 'alignright') )
183                                                         cm.setActive('justifyright', 1);
184                                                 else if ( ed.dom.hasClass(DL, 'aligncenter') )
185                                                         cm.setActive('justifycenter', 1);
186                                         }
187                                 });
188
189                                 if ( ed.id != 'wp_mce_fullscreen' )
190                                         ed.dom.addClass(ed.getBody(), 'wp-editor');
191
192                                 // remove invalid parent paragraphs when pasting HTML and/or switching to the HTML editor and back
193                                 ed.onBeforeSetContent.add(function(ed, o) {
194                                         if ( o.content ) {
195                                                 o.content = o.content.replace(/<p>\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)( [^>]*)?>/gi, '<$1$2>');
196                                                 o.content = o.content.replace(/<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)>\s*<\/p>/gi, '</$1>');
197                                         }
198                                 });
199                         });
200
201                         // Word count
202                         if ( 'undefined' != typeof(jQuery) ) {
203                                 ed.onKeyUp.add(function(ed, e) {
204                                         var k = e.keyCode || e.charCode;
205
206                                         if ( k == last )
207                                                 return;
208
209                                         if ( 13 == k || 8 == last || 46 == last )
210                                                 jQuery(document).triggerHandler('wpcountwords', [ ed.getContent({format : 'raw'}) ]);
211
212                                         last = k;
213                                 });
214                         };
215
216                         // keep empty paragraphs :(
217                         ed.onSaveContent.addToTop(function(ed, o) {
218                                 o.content = o.content.replace(/<p>(<br ?\/?>|\u00a0|\uFEFF)?<\/p>/g, '<p>&nbsp;</p>');
219                         });
220
221                         ed.onSaveContent.add(function(ed, o) {
222                                 if ( typeof(switchEditors) == 'object' ) {
223                                         if ( ed.isHidden() )
224                                                 o.content = o.element.value;
225                                         else
226                                                 o.content = switchEditors.pre_wpautop(o.content);
227                                 }
228                         });
229
230                         /* disable for now
231                         ed.onBeforeSetContent.add(function(ed, o) {
232                                 o.content = t._setEmbed(o.content);
233                         });
234
235                         ed.onPostProcess.add(function(ed, o) {
236                                 if ( o.get )
237                                         o.content = t._getEmbed(o.content);
238                         });
239                         */
240
241                         // Add listeners to handle more break
242                         t._handleMoreBreak(ed, url);
243
244                         // Add custom shortcuts
245                         ed.addShortcut('alt+shift+c', ed.getLang('justifycenter_desc'), 'JustifyCenter');
246                         ed.addShortcut('alt+shift+r', ed.getLang('justifyright_desc'), 'JustifyRight');
247                         ed.addShortcut('alt+shift+l', ed.getLang('justifyleft_desc'), 'JustifyLeft');
248                         ed.addShortcut('alt+shift+j', ed.getLang('justifyfull_desc'), 'JustifyFull');
249                         ed.addShortcut('alt+shift+q', ed.getLang('blockquote_desc'), 'mceBlockQuote');
250                         ed.addShortcut('alt+shift+u', ed.getLang('bullist_desc'), 'InsertUnorderedList');
251                         ed.addShortcut('alt+shift+o', ed.getLang('numlist_desc'), 'InsertOrderedList');
252                         ed.addShortcut('alt+shift+d', ed.getLang('striketrough_desc'), 'Strikethrough');
253                         ed.addShortcut('alt+shift+n', ed.getLang('spellchecker.desc'), 'mceSpellCheck');
254                         ed.addShortcut('alt+shift+a', ed.getLang('link_desc'), 'mceLink');
255                         ed.addShortcut('alt+shift+s', ed.getLang('unlink_desc'), 'unlink');
256                         ed.addShortcut('alt+shift+m', ed.getLang('image_desc'), 'mceImage');
257                         ed.addShortcut('alt+shift+g', ed.getLang('fullscreen.desc'), 'mceFullScreen');
258                         ed.addShortcut('alt+shift+z', ed.getLang('wp_adv_desc'), 'WP_Adv');
259                         ed.addShortcut('alt+shift+h', ed.getLang('help_desc'), 'WP_Help');
260                         ed.addShortcut('alt+shift+t', ed.getLang('wp_more_desc'), 'WP_More');
261                         ed.addShortcut('alt+shift+p', ed.getLang('wp_page_desc'), 'WP_Page');
262                         ed.addShortcut('ctrl+s', ed.getLang('save_desc'), function(){if('function'==typeof autosave)autosave();});
263
264                         if ( tinymce.isWebKit ) {
265                                 ed.addShortcut('alt+shift+b', ed.getLang('bold_desc'), 'Bold');
266                                 ed.addShortcut('alt+shift+i', ed.getLang('italic_desc'), 'Italic');
267                         }
268
269                         ed.onInit.add(function(ed) {
270                                 tinymce.dom.Event.add(ed.getWin(), 'scroll', function(e) {
271                                         ed.plugins.wordpress._hideButtons();
272                                 });
273                                 tinymce.dom.Event.add(ed.getBody(), 'dragstart', function(e) {
274                                         ed.plugins.wordpress._hideButtons();
275                                 });
276                         });
277
278                         ed.onBeforeExecCommand.add(function(ed, cmd, ui, val) {
279                                 ed.plugins.wordpress._hideButtons();
280                         });
281
282                         ed.onSaveContent.add(function(ed, o) {
283                                 ed.plugins.wordpress._hideButtons();
284                         });
285
286                         ed.onMouseDown.add(function(ed, e) {
287                                 if ( e.target.nodeName != 'IMG' )
288                                         ed.plugins.wordpress._hideButtons();
289                         });
290                 },
291
292                 getInfo : function() {
293                         return {
294                                 longname : 'WordPress Plugin',
295                                 author : 'WordPress', // add Moxiecode?
296                                 authorurl : 'http://wordpress.org',
297                                 infourl : 'http://wordpress.org',
298                                 version : '3.0'
299                         };
300                 },
301
302                 // Internal functions
303                 _setEmbed : function(c) {
304                         return c.replace(/\[embed\]([\s\S]+?)\[\/embed\][\s\u00a0]*/g, function(a,b){
305                                 return '<img width="300" height="200" src="' + tinymce.baseURL + '/plugins/wordpress/img/trans.gif" class="wp-oembed mceItemNoResize" alt="'+b+'" title="'+b+'" />';
306                         });
307                 },
308
309                 _getEmbed : function(c) {
310                         return c.replace(/<img[^>]+>/g, function(a) {
311                                 if ( a.indexOf('class="wp-oembed') != -1 ) {
312                                         var u = a.match(/alt="([^\"]+)"/);
313                                         if ( u[1] )
314                                                 a = '[embed]' + u[1] + '[/embed]';
315                                 }
316                                 return a;
317                         });
318                 },
319
320                 _showButtons : function(n, id) {
321                         var ed = tinyMCE.activeEditor, p1, p2, vp, DOM = tinymce.DOM, X, Y;
322
323                         vp = ed.dom.getViewPort(ed.getWin());
324                         p1 = DOM.getPos(ed.getContentAreaContainer());
325                         p2 = ed.dom.getPos(n);
326
327                         X = Math.max(p2.x - vp.x, 0) + p1.x;
328                         Y = Math.max(p2.y - vp.y, 0) + p1.y;
329
330                         DOM.setStyles(id, {
331                                 'top' : Y+5+'px',
332                                 'left' : X+5+'px',
333                                 'display' : 'block'
334                         });
335
336                         if ( this.mceTout )
337                                 clearTimeout(this.mceTout);
338
339                         this.mceTout = setTimeout( function(){ed.plugins.wordpress._hideButtons();}, 5000 );
340                 },
341
342                 _hideButtons : function() {
343                         if ( !this.mceTout )
344                                 return;
345
346                         if ( document.getElementById('wp_editbtns') )
347                                 tinymce.DOM.hide('wp_editbtns');
348
349                         if ( document.getElementById('wp_gallerybtns') )
350                                 tinymce.DOM.hide('wp_gallerybtns');
351
352                         clearTimeout(this.mceTout);
353                         this.mceTout = 0;
354                 },
355
356                 // Resizes the iframe by a relative height value
357                 _resizeIframe : function(ed, tb_id, dy) {
358                         var ifr = ed.getContentAreaContainer().firstChild;
359
360                         DOM.setStyle(ifr, 'height', ifr.clientHeight + dy); // Resize iframe
361                         ed.theme.deltaHeight += dy; // For resize cookie
362                 },
363
364                 _handleMoreBreak : function(ed, url) {
365                         var moreHTML, nextpageHTML;
366
367                         moreHTML = '<img src="' + url + '/img/trans.gif" alt="$1" class="mceWPmore mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
368                         nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mceWPnextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
369
370                         // Load plugin specific CSS into editor
371                         ed.onInit.add(function() {
372                                 ed.dom.loadCSS(url + '/css/content.css');
373                         });
374
375                         // Display morebreak instead if img in element path
376                         ed.onPostRender.add(function() {
377                                 if (ed.theme.onResolveName) {
378                                         ed.theme.onResolveName.add(function(th, o) {
379                                                 if (o.node.nodeName == 'IMG') {
380                                                         if ( ed.dom.hasClass(o.node, 'mceWPmore') )
381                                                                 o.name = 'wpmore';
382                                                         if ( ed.dom.hasClass(o.node, 'mceWPnextpage') )
383                                                                 o.name = 'wppage';
384                                                 }
385
386                                         });
387                                 }
388                         });
389
390                         // Replace morebreak with images
391                         ed.onBeforeSetContent.add(function(ed, o) {
392                                 if ( o.content ) {
393                                         o.content = o.content.replace(/<!--more(.*?)-->/g, moreHTML);
394                                         o.content = o.content.replace(/<!--nextpage-->/g, nextpageHTML);
395                                 }
396                         });
397
398                         // Replace images with morebreak
399                         ed.onPostProcess.add(function(ed, o) {
400                                 if (o.get)
401                                         o.content = o.content.replace(/<img[^>]+>/g, function(im) {
402                                                 if (im.indexOf('class="mceWPmore') !== -1) {
403                                                         var m, moretext = (m = im.match(/alt="(.*?)"/)) ? m[1] : '';
404                                                         im = '<!--more'+moretext+'-->';
405                                                 }
406                                                 if (im.indexOf('class="mceWPnextpage') !== -1)
407                                                         im = '<!--nextpage-->';
408
409                                                 return im;
410                                         });
411                         });
412
413                         // Set active buttons if user selected pagebreak or more break
414                         ed.onNodeChange.add(function(ed, cm, n) {
415                                 cm.setActive('wp_page', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPnextpage'));
416                                 cm.setActive('wp_more', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPmore'));
417                         });
418                 }
419         });
420
421         // Register plugin
422         tinymce.PluginManager.add('wordpress', tinymce.plugins.WordPress);
423 })();