Another attempt to fix the http blog redirects.
[WebKit-https.git] / Websites / webkit.org / blog / 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;
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 ( ed.dom.getParent(e.target, 'div.mceTemp') || ed.dom.is(e.target, 'div.mceTemp') ) {
52                                         window.setTimeout(function(){
53                                                 var ed = tinyMCE.activeEditor, n = ed.selection.getNode(), DL, width;
54
55                                                 if ( 'IMG' == n.nodeName ) {
56                                                         DL = ed.dom.getParent(n, 'dl.wp-caption');
57                                                         width = ed.dom.getAttrib(n, 'width') || n.width;
58                                                         width = parseInt(width, 10);
59
60                                                         if ( DL && width != ( parseInt(ed.dom.getStyle(DL, 'width'), 10) - 10 ) ) {
61                                                                 ed.dom.setStyle(DL, 'width', 10 + width);
62                                                                 ed.execCommand('mceRepaint');
63                                                         }
64                                                 }
65                                         }, 100);
66                                 }
67                         });
68
69                         // show editimage buttons
70                         ed.onMouseDown.add(function(ed, e) {
71                                 var p;
72
73                                 if ( e.target.nodeName == 'IMG' && ed.dom.getAttrib(e.target, 'class').indexOf('mceItem') == -1 ) {
74                                         ed.plugins.wordpress._showButtons(e.target, 'wp_editbtns');
75                                         if ( tinymce.isGecko && (p = ed.dom.getParent(e.target, 'dl.wp-caption')) && ed.dom.hasClass(p.parentNode, 'mceTemp') )
76                                                 ed.selection.select(p.parentNode);
77                                 }
78                         });
79
80                         // when pressing Return inside a caption move the cursor to a new parapraph under it
81                         ed.onKeyPress.add(function(ed, e) {
82                                 var n, DL, DIV, P;
83
84                                 if ( e.keyCode == 13 ) {
85                                         n = ed.selection.getNode();
86                                         DL = ed.dom.getParent(n, 'dl.wp-caption');
87                                         DIV = ed.dom.getParent(DL, 'div.mceTemp');
88
89                                         if ( DL && DIV ) {
90                                                 P = ed.dom.create('p', {}, '&nbsp;');
91                                                 ed.dom.insertAfter( P, DIV );
92                                                 
93                                                 if ( P.firstChild )
94                                                         ed.selection.select(P.firstChild);
95                                                 else
96                                                         ed.selection.select(P);
97                                                 
98                                                 tinymce.dom.Event.cancel(e);
99                                                 return false;
100                                         }
101                                 }
102                         });
103
104                         ed.onBeforeSetContent.add(function(ed, o) {
105                                 o.content = t._do_shcode(o.content);
106                         });
107
108                         ed.onPostProcess.add(function(ed, o) {
109                                 if (o.get)
110                                         o.content = t._get_shcode(o.content);
111                         });
112                 },
113
114                 _do_shcode : function(co) {
115                         return co.replace(/(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?[\s\u00a0]*/g, function(a,b,c){
116                                 var id, cls, w, cap, div_cls;
117                                 
118                                 b = b.replace(/\\'|\\&#39;|\\&#039;/g, '&#39;').replace(/\\"|\\&quot;/g, '&quot;');
119                                 c = c.replace(/\\&#39;|\\&#039;/g, '&#39;').replace(/\\&quot;/g, '&quot;');
120                                 id = b.match(/id=['"]([^'"]+)/i);
121                                 cls = b.match(/align=['"]([^'"]+)/i);
122                                 w = b.match(/width=['"]([0-9]+)/);
123                                 cap = b.match(/caption=['"]([^'"]+)/i);
124
125                                 id = ( id && id[1] ) ? id[1] : '';
126                                 cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
127                                 w = ( w && w[1] ) ? w[1] : '';
128                                 cap = ( cap && cap[1] ) ? cap[1] : '';
129                                 if ( ! w || ! cap ) return c;
130                                 
131                                 div_cls = (cls == 'aligncenter') ? 'mceTemp mceIEcenter' : 'mceTemp';
132
133                                 return '<div class="'+div_cls+'" draggable><dl id="'+id+'" class="wp-caption '+cls+'" style="width: '+(10+parseInt(w))+
134                                 'px"><dt class="wp-caption-dt">'+c+'</dt><dd class="wp-caption-dd">'+cap+'</dd></dl></div>';
135                         });
136                 },
137
138                 _get_shcode : function(co) {
139                         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){
140                                 var id, cls, w;
141                                 
142                                 id = b.match(/id=['"]([^'"]+)/i);
143                                 cls = b.match(/class=['"]([^'"]+)/i);
144                                 w = c.match(/width=['"]([0-9]+)/);
145
146                                 id = ( id && id[1] ) ? id[1] : '';
147                                 cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
148                                 w = ( w && w[1] ) ? w[1] : '';
149
150                                 if ( ! w || ! cap ) return c;
151                                 cls = cls.match(/align[^ '"]+/) || 'alignnone';
152                                 cap = cap.replace(/<\S[^<>]*>/gi, '').replace(/'/g, '&#39;').replace(/"/g, '&quot;');
153
154                                 return '[caption id="'+id+'" align="'+cls+'" width="'+w+'" caption="'+cap+'"]'+c+'[/caption]';
155                         });
156                 },
157
158                 _createButtons : function() {
159                         var t = this, ed = tinyMCE.activeEditor, DOM = tinymce.DOM, editButton, dellButton;
160
161                         DOM.remove('wp_editbtns');
162
163                         DOM.add(document.body, 'div', {
164                                 id : 'wp_editbtns',
165                                 style : 'display:none;'
166                         });
167
168                         editButton = DOM.add('wp_editbtns', 'img', {
169                                 src : t.url+'/img/image.png',
170                                 id : 'wp_editimgbtn',
171                                 width : '24',
172                                 height : '24',
173                                 title : ed.getLang('wpeditimage.edit_img')
174                         });
175
176                         tinymce.dom.Event.add(editButton, 'mousedown', function(e) {
177                                 var ed = tinyMCE.activeEditor;
178                                 ed.windowManager.bookmark = ed.selection.getBookmark('simple');
179                                 ed.execCommand("WP_EditImage");
180                         });
181
182                         dellButton = DOM.add('wp_editbtns', 'img', {
183                                 src : t.url+'/img/delete.png',
184                                 id : 'wp_delimgbtn',
185                                 width : '24',
186                                 height : '24',
187                                 title : ed.getLang('wpeditimage.del_img')
188                         });
189
190                         tinymce.dom.Event.add(dellButton, 'mousedown', function(e) {
191                                 var ed = tinyMCE.activeEditor, el = ed.selection.getNode(), p;
192
193                                 if ( el.nodeName == 'IMG' && ed.dom.getAttrib(el, 'class').indexOf('mceItem') == -1 ) {
194                                         if ( (p = ed.dom.getParent(el, 'div')) && ed.dom.hasClass(p, 'mceTemp') )
195                                                 ed.dom.remove(p);
196                                         else if ( (p = ed.dom.getParent(el, 'A')) && p.childNodes.length == 1 )
197                                                 ed.dom.remove(p);
198                                         else
199                                                 ed.dom.remove(el);
200
201                                         ed.execCommand('mceRepaint');
202                                         return false;
203                                 }
204                         });
205                 },
206
207                 getInfo : function() {
208                         return {
209                                 longname : 'Edit Image',
210                                 author : 'WordPress',
211                                 authorurl : 'http://wordpress.org',
212                                 infourl : '',
213                                 version : "1.0"
214                         };
215                 }
216         });
217
218         tinymce.PluginManager.add('wpeditimage', tinymce.plugins.wpEditImage);
219 })();