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