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