]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/wordpress/editor_plugin.js
Wordpress 2.6.2
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / wordpress / editor_plugin.js
1 /**
2  * WordPress plugin.
3  */
4
5 (function() {
6         var DOM = tinymce.DOM;
7
8         // Load plugin specific language pack
9         tinymce.PluginManager.requireLangPack('wordpress');
10
11         tinymce.create('tinymce.plugins.WordPress', {
12                 init : function(ed, url) {
13                         var t = this, tbId = ed.getParam('wordpress_adv_toolbar', 'toolbar2');
14                         var moreHTML = '<img src="' + url + '/img/trans.gif" class="mceWPmore mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
15                         var nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mceWPnextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
16
17                         if ( tinymce.util.Cookie.get('kitchenSink') == '1' )
18                                 ed.settings.wordpress_adv_hidden = 0;
19
20                         // Hides the specified toolbar and resizes the iframe
21                         ed.onPostRender.add(function() {
22                                 if ( ed.getParam('wordpress_adv_hidden', 1) ) {
23                                         DOM.hide(ed.controlManager.get(tbId).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 id = ed.controlManager.get(tbId).id, cm = ed.controlManager, cook = tinymce.util.Cookie, date;
48
49                                 date = new Date();
50                                 date.setTime(date.getTime()+(10*365*24*60*60*1000));
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                                         cook.set('kitchenSink', '1', date);
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                                         cook.set('kitchenSink', '0', date);
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                                 if ( 'mceFullScreen' != cmd ) return;
132                                 if ( 'mce_fullscreen' != ed.id )
133                                         ed.settings.theme_advanced_buttons1 += ',|,add_image,add_video,add_audio,add_media';
134                         });
135
136                         // Add class "alignleft", "alignright" and "aligncenter" when selecting align for images.
137                         ed.addCommand('JustifyLeft', function() {
138                                 var n = ed.selection.getNode();
139
140                                 if ( n.nodeName != 'IMG' )
141                                         ed.editorCommands.mceJustify('JustifyLeft', 'left');
142                                 else ed.plugins.wordpress.do_align(n, 'alignleft');
143                         });
144
145                         ed.addCommand('JustifyRight', function() {
146                                 var n = ed.selection.getNode();
147
148                                 if ( n.nodeName != 'IMG' )
149                                         ed.editorCommands.mceJustify('JustifyRight', 'right');
150                                 else ed.plugins.wordpress.do_align(n, 'alignright');
151                         });
152
153                         ed.addCommand('JustifyCenter', function() {
154                                 var n = ed.selection.getNode(), P = ed.dom.getParent(n, 'p'), DL = ed.dom.getParent(n, 'dl');
155
156                                 if ( n.nodeName == 'IMG' && ( P || DL ) )
157                                         ed.plugins.wordpress.do_align(n, 'aligncenter');
158                                 else ed.editorCommands.mceJustify('JustifyCenter', 'center');
159                         });
160
161                         // Word count if script is loaded
162                         if ( 'undefined' != typeof wpWordCount ) {
163                                 var last = 0;
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                         // Add listeners to handle more break
172                         t._handleMoreBreak(ed, url);
173
174                         // Add custom shortcuts
175                         ed.addShortcut('alt+shift+c', ed.getLang('justifycenter_desc'), 'JustifyCenter');
176                         ed.addShortcut('alt+shift+r', ed.getLang('justifyright_desc'), 'JustifyRight');
177                         ed.addShortcut('alt+shift+l', ed.getLang('justifyleft_desc'), 'JustifyLeft');
178                         ed.addShortcut('alt+shift+j', ed.getLang('justifyfull_desc'), 'JustifyFull');
179                         ed.addShortcut('alt+shift+q', ed.getLang('blockquote_desc'), 'mceBlockQuote');
180                         ed.addShortcut('alt+shift+u', ed.getLang('bullist_desc'), 'InsertUnorderedList');
181                         ed.addShortcut('alt+shift+o', ed.getLang('numlist_desc'), 'InsertOrderedList');
182                         ed.addShortcut('alt+shift+d', ed.getLang('striketrough_desc'), 'Strikethrough');
183                         ed.addShortcut('alt+shift+n', ed.getLang('spellchecker.desc'), 'mceSpellCheck');
184                         ed.addShortcut('alt+shift+a', ed.getLang('link_desc'), 'mceLink');
185                         ed.addShortcut('alt+shift+s', ed.getLang('unlink_desc'), 'unlink');
186                         ed.addShortcut('alt+shift+m', ed.getLang('image_desc'), 'mceImage');
187                         ed.addShortcut('alt+shift+g', ed.getLang('fullscreen.desc'), 'mceFullScreen');
188                         ed.addShortcut('alt+shift+z', ed.getLang('wp_adv_desc'), 'WP_Adv');
189                         ed.addShortcut('alt+shift+h', ed.getLang('help_desc'), 'WP_Help');
190                         ed.addShortcut('alt+shift+t', ed.getLang('wp_more_desc'), 'WP_More');
191                         ed.addShortcut('alt+shift+p', ed.getLang('wp_page_desc'), 'WP_Page');
192
193                         if ( tinymce.isWebKit ) {
194                                 ed.addShortcut('alt+shift+b', ed.getLang('bold_desc'), 'Bold');
195                                 ed.addShortcut('alt+shift+i', ed.getLang('italic_desc'), 'Italic');
196                         }
197                 },
198
199                 getInfo : function() {
200                         return {
201                                 longname : 'WordPress Plugin',
202                                 author : 'WordPress', // add Moxiecode?
203                                 authorurl : 'http://wordpress.org',
204                                 infourl : 'http://wordpress.org',
205                                 version : '3.0'
206                         };
207                 },
208
209                 // Internal functions
210                 do_align : function(n, a) {
211                         var P, DL, DIV, cls, c, ed = tinyMCE.activeEditor;
212
213                         P = ed.dom.getParent(n, 'p');
214                         DL = ed.dom.getParent(n, 'dl');
215                         DIV = ed.dom.getParent(n, 'div');
216
217                         if ( DL && DIV ) {
218                                 cls = ed.dom.hasClass(DL, a) ? 'alignnone' : a;
219                                 DL.className = DL.className.replace(/align[^ '"]+\s?/g, '');
220                                 ed.dom.addClass(DL, cls);
221                                 c = (cls == 'aligncenter') ? ed.dom.addClass(DIV, 'mceIEcenter') : ed.dom.removeClass(DIV, 'mceIEcenter');
222                         } else if ( P ) {
223                                 cls = ed.dom.hasClass(n, a) ? 'alignnone' : a;
224                                 n.className = n.className.replace(/align[^ '"]+\s?/g, '');
225                                 ed.dom.addClass(n, cls);
226                                 if ( cls == 'aligncenter' )
227                                         ed.dom.setStyle(P, 'textAlign', 'center');
228                                 else if (P.style && P.style.textAlign == 'center')
229                                         ed.dom.setStyle(P, 'textAlign', '');
230                         }
231
232                         ed.execCommand('mceRepaint');
233                 },
234
235                 // Resizes the iframe by a relative height value
236                 _resizeIframe : function(ed, tb_id, dy) {
237                         var ifr = ed.getContentAreaContainer().firstChild;
238
239                         DOM.setStyle(ifr, 'height', ifr.clientHeight + dy); // Resize iframe
240                         ed.theme.deltaHeight += dy; // For resize cookie
241                 },
242
243                 _handleMoreBreak : function(ed, url) {
244                         var moreHTML = '<img src="' + url + '/img/trans.gif" alt="$1" class="mceWPmore mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
245                         var nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mceWPnextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
246
247                         // Load plugin specific CSS into editor
248                         ed.onInit.add(function() {
249                                 ed.dom.loadCSS(url + '/css/content.css');
250                         });
251
252                         // Display morebreak instead if img in element path
253                         ed.onPostRender.add(function() {
254                                 if (ed.theme.onResolveName) {
255                                         ed.theme.onResolveName.add(function(th, o) {
256                                                 if (o.node.nodeName == 'IMG') {
257                                                         if ( ed.dom.hasClass(o.node, 'mceWPmore') )
258                                                                 o.name = 'wpmore';
259                                                         if ( ed.dom.hasClass(o.node, 'mceWPnextpage') )
260                                                                 o.name = 'wppage';
261                                                 }
262
263                                         });
264                                 }
265                         });
266
267                         // Replace morebreak with images
268                         ed.onBeforeSetContent.add(function(ed, o) {
269                                 o.content = o.content.replace(/<!--more(.*?)-->/g, moreHTML);
270                                 o.content = o.content.replace(/<!--nextpage-->/g, nextpageHTML);
271                         });
272
273                         // Replace images with morebreak
274                         ed.onPostProcess.add(function(ed, o) {
275                                 if (o.get)
276                                         o.content = o.content.replace(/<img[^>]+>/g, function(im) {
277                                                 if (im.indexOf('class="mceWPmore') !== -1) {
278                                                         var m, moretext = (m = im.match(/alt="(.*?)"/)) ? m[1] : '';
279                                                         im = '<!--more'+moretext+'-->';
280                                                 }
281                                                 if (im.indexOf('class="mceWPnextpage') !== -1)
282                                                         im = '<!--nextpage-->';
283
284                                                 return im;
285                                         });
286                         });
287
288                         // Set active buttons if user selected pagebreak or more break
289                         ed.onNodeChange.add(function(ed, cm, n) {
290                                 cm.setActive('wp_page', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPnextpage'));
291                                 cm.setActive('wp_more', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPmore'));
292                         });
293                 }
294         });
295
296         // Register plugin
297         tinymce.PluginManager.add('wordpress', tinymce.plugins.WordPress);
298 })();