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