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