]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/wpeditimage/editor_plugin.js
Wordpress 2.7.1
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / wpeditimage / editor_plugin.js
1
2 (function() {
3         tinymce.create('tinymce.plugins.wpEditImage', {
4
5                 init : function(ed, url) {
6                         var t = this;
7
8                         t.url = url;
9                         t._createButtons();
10
11                         // Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('...');
12                         ed.addCommand('WP_EditImage', function() {
13                                 var el = ed.selection.getNode(), vp = tinymce.DOM.getViewPort(), H = vp.h, W = ( 720 < vp.w ) ? 720 : vp.w;
14                                 var cls = ed.dom.getAttrib(el, 'class');
15
16                                 if ( cls.indexOf('mceItem') != -1 || cls.indexOf('wpGallery') != -1 || el.nodeName != 'IMG' )
17                                         return;
18
19                                 tb_show('', url + '/editimage.html?ver=321&TB_iframe=true');
20                                 tinymce.DOM.setStyles('TB_window', {
21                                         'width':( W - 50 )+'px',
22                                         'height':( H - 45 )+'px',
23                                         'margin-left':'-'+parseInt((( W - 50 ) / 2),10) + 'px'
24                                 });
25
26                                 if ( ! tinymce.isIE6 ) {
27                                         tinymce.DOM.setStyles('TB_window', {
28                                                 'top':'20px',
29                                                 'marginTop':'0'
30                                         });
31                                 }
32
33                                 tinymce.DOM.setStyles('TB_iframeContent', {
34                                         'width':( W - 50 )+'px',
35                                         'height':( H - 75 )+'px'
36                                 });
37                                 tinymce.DOM.setStyle( ['TB_overlay','TB_window','TB_load'], 'z-index', '999999' );
38                         });
39
40                         ed.onInit.add(function(ed) {
41                                 tinymce.dom.Event.add(ed.getWin(), 'scroll', function(e) {
42                                         ed.plugins.wpeditimage.hideButtons();
43                                 });
44                         });
45
46                         ed.onBeforeExecCommand.add(function(ed, cmd, ui, val) {
47                                 ed.plugins.wpeditimage.hideButtons();
48                         });
49
50                         ed.onSaveContent.add(function(ed, o) {
51                                 ed.plugins.wpeditimage.hideButtons();
52                         });
53
54                         ed.onMouseUp.add(function(ed, e) {
55                                 if ( tinymce.isOpera ) {
56                                         if ( e.target.nodeName == 'IMG' )
57                                                 ed.plugins.wpeditimage.showButtons(e.target);
58                                 } else if ( ! tinymce.isWebKit ) {
59                                         var n = ed.selection.getNode(), DL;
60                                         
61                                         if ( n.nodeName == 'IMG' && (DL = ed.dom.getParent(n, 'DL')) ) {                                        
62                                                 window.setTimeout(function(){
63                                                         var ed = tinyMCE.activeEditor, n = ed.selection.getNode(), DL = ed.dom.getParent(n, 'DL');
64                                                 
65                                                         if ( n.width != (parseInt(ed.dom.getStyle(DL, 'width')) - 10) ) {
66                                                                 ed.dom.setStyle(DL, 'width', parseInt(n.width)+10);
67                                                                 ed.execCommand('mceRepaint');
68                                                         }
69                                                 }, 100);
70                                         }
71                                 }
72                         });
73
74                         ed.onMouseDown.add(function(ed, e) {
75                                 if ( tinymce.isOpera || e.target.nodeName != 'IMG' ) {
76                                         t.hideButtons();
77                                         return;
78                                 }
79                                 ed.plugins.wpeditimage.showButtons(e.target);
80                         });
81
82                         ed.onKeyPress.add(function(ed, e) {
83                                 var DL, DIV;
84
85                                 if ( e.keyCode == 13 && (DL = ed.dom.getParent(ed.selection.getNode(), 'DL')) ) {
86                                         var P = ed.dom.create('p', {}, '&nbsp;');
87                                         if ( (DIV = DL.parentNode) && DIV.nodeName == 'DIV' ) 
88                                                 ed.dom.insertAfter( P, DIV );
89                                         else ed.dom.insertAfter( P, DL );
90
91                                         tinymce.dom.Event.cancel(e);
92                                         ed.selection.select(P);
93                                         return false;
94                                 }
95                         });
96
97                         ed.onBeforeSetContent.add(function(ed, o) {
98                                 o.content = t._do_shcode(o.content);
99                         });
100
101                         ed.onPostProcess.add(function(ed, o) {
102                                 if (o.get)
103                                         o.content = t._get_shcode(o.content);
104                         });
105                 },
106
107                 _do_shcode : function(co) {
108                         return co.replace(/\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\][\s\u00a0]*/g, function(a,b,c){
109                                 b = b.replace(/\\'|\\&#39;|\\&#039;/g, '&#39;').replace(/\\"|\\&quot;/g, '&quot;');
110                                 c = c.replace(/\\&#39;|\\&#039;/g, '&#39;').replace(/\\&quot;/g, '&quot;');
111                                 var id = b.match(/id=['"]([^'"]+)/i), cls = b.match(/align=['"]([^'"]+)/i);
112                                 var w = b.match(/width=['"]([0-9]+)/), cap = b.match(/caption=['"]([^'"]+)/i);
113
114                                 id = ( id && id[1] ) ? id[1] : '';
115                                 cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
116                                 w = ( w && w[1] ) ? w[1] : '';
117                                 cap = ( cap && cap[1] ) ? cap[1] : '';
118                                 if ( ! w || ! cap ) return c;
119                                 
120                                 var div_cls = (cls == 'aligncenter') ? 'mceTemp mceIEcenter' : 'mceTemp';
121
122                                 return '<div class="'+div_cls+'"><dl id="'+id+'" class="wp-caption '+cls+'" style="width: '+(10+parseInt(w))+
123                                 'px"><dt class="wp-caption-dt">'+c+'</dt><dd class="wp-caption-dd">'+cap+'</dd></dl></div>';
124                         });
125                 },
126
127                 _get_shcode : function(co) {
128                         return co.replace(/<div class="mceTemp[^"]*">\s*<dl([^>]+)>\s*<dt[^>]+>([\s\S]+?)<\/dt>\s*<dd[^>]+>(.+?)<\/dd>\s*<\/dl>\s*<\/div>\s*/gi, function(a,b,c,cap){
129                                 var id = b.match(/id=['"]([^'"]+)/i), cls = b.match(/class=['"]([^'"]+)/i);
130                                 var w = c.match(/width=['"]([0-9]+)/);
131
132                                 id = ( id && id[1] ) ? id[1] : '';
133                                 cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
134                                 w = ( w && w[1] ) ? w[1] : '';
135
136                                 if ( ! w || ! cap ) return c;
137                                 cls = cls.match(/align[^ '"]+/) || 'alignnone';
138                                 cap = cap.replace(/<\S[^<>]*>/gi, '').replace(/'/g, '&#39;').replace(/"/g, '&quot;');
139
140                                 return '[caption id="'+id+'" align="'+cls+'" width="'+w+'" caption="'+cap+'"]'+c+'[/caption]';
141                         });
142                 },
143
144                 showButtons : function(n) {
145                         var t = this, ed = tinyMCE.activeEditor, p1, p2, vp, DOM = tinymce.DOM, X, Y;
146                         var cls = ed.dom.getAttrib(n, 'class');
147
148                         if ( cls.indexOf('mceItem') != -1 || cls.indexOf('wpGallery') != -1 )
149                                 return;
150
151                         vp = ed.dom.getViewPort(ed.getWin());
152                         p1 = DOM.getPos(ed.getContentAreaContainer());
153                         p2 = ed.dom.getPos(n);
154
155                         X = Math.max(p2.x - vp.x, 0) + p1.x;
156                         Y = Math.max(p2.y - vp.y, 0) + p1.y;
157
158                         DOM.setStyles('wp_editbtns', {
159                                 'top' : Y+5+'px',
160                                 'left' : X+5+'px',
161                                 'display' : 'block'
162                         });
163
164                         t.btnsTout = window.setTimeout( function(){ed.plugins.wpeditimage.hideButtons();}, 5000 );
165                 },
166
167                 hideButtons : function() {
168                         if ( tinymce.DOM.isHidden('wp_editbtns') ) return;
169
170                         tinymce.DOM.hide('wp_editbtns');
171                         window.clearTimeout(this.btnsTout);
172                 },
173
174                 _createButtons : function() {
175                         var t = this, ed = tinyMCE.activeEditor, DOM = tinymce.DOM;
176
177                         DOM.remove('wp_editbtns');
178
179                         var wp_editbtns = DOM.add(document.body, 'div', {
180                                 id : 'wp_editbtns',
181                                 style : 'display:none;'
182                         });
183
184                         var wp_editimgbtn = DOM.add('wp_editbtns', 'img', {
185                                 src : t.url+'/img/image.png',
186                                 id : 'wp_editimgbtn',
187                                 width : '24',
188                                 height : '24',
189                                 title : ed.getLang('wpeditimage.edit_img')
190                         });
191
192                         wp_editimgbtn.onmousedown = function(e) {
193                                 var ed = tinyMCE.activeEditor;
194                                 ed.windowManager.bookmark = ed.selection.getBookmark('simple');
195                                 ed.execCommand("WP_EditImage");
196                                 this.parentNode.style.display = 'none';
197                         };
198
199                         var wp_delimgbtn = DOM.add('wp_editbtns', 'img', {
200                                 src : t.url+'/img/delete.png',
201                                 id : 'wp_delimgbtn',
202                                 width : '24',
203                                 height : '24',
204                                 title : ed.getLang('wpeditimage.del_img')
205                         });
206
207                         wp_delimgbtn.onmousedown = function(e) {
208                                 var ed = tinyMCE.activeEditor, el = ed.selection.getNode(), p;
209
210                                 if ( el.nodeName == 'IMG' && ed.dom.getAttrib(el, 'class').indexOf('mceItem') == -1 ) {
211                                         if ( (p = ed.dom.getParent(el, 'div')) && ed.dom.hasClass(p, 'mceTemp') )
212                                                 ed.dom.remove(p);
213                                         else if ( (p = ed.dom.getParent(el, 'A')) && p.childNodes.length == 1 )
214                                                 ed.dom.remove(p);
215                                         else ed.dom.remove(el);
216
217                                         this.parentNode.style.display = 'none';
218                                         ed.execCommand('mceRepaint');
219                                         return false;
220                                 }
221                         };
222                 },
223
224                 getInfo : function() {
225                         return {
226                                 longname : 'Edit Image',
227                                 author : 'WordPress',
228                                 authorurl : 'http://wordpress.org',
229                                 infourl : '',
230                                 version : "1.0"
231                         };
232                 }
233         });
234
235         tinymce.PluginManager.add('wpeditimage', tinymce.plugins.wpEditImage);
236 })();