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