]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/wpeditimage/js/editimage.dev.js
Wordpress 3.0
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / wpeditimage / js / editimage.dev.js
1
2 var tinymce = null, tinyMCEPopup, tinyMCE, wpImage;
3
4 tinyMCEPopup = {
5         init: function() {
6                 var t = this, w, li, q, i, it;
7
8                 li = ('' + document.location.search).replace(/^\?/, '').split('&');
9                 q = {};
10                 for ( i = 0; i < li.length; i++ ) {
11                         it = li[i].split('=');
12                         q[unescape(it[0])] = unescape(it[1]);
13                 }
14
15                 if (q.mce_rdomain)
16                         document.domain = q.mce_rdomain;
17
18                 // Find window & API
19                 w = t.getWin();
20                 tinymce = w.tinymce;
21                 tinyMCE = w.tinyMCE;
22                 t.editor = tinymce.EditorManager.activeEditor;
23                 t.params = t.editor.windowManager.params;
24
25                 // Setup local DOM
26                 t.dom = t.editor.windowManager.createInstance('tinymce.dom.DOMUtils', document);
27                 t.editor.windowManager.onOpen.dispatch(t.editor.windowManager, window);
28         },
29
30         getWin : function() {
31                 return window.dialogArguments || opener || parent || top;
32         },
33
34         getParam : function(n, dv) {
35                 return this.editor.getParam(n, dv);
36         },
37
38         close : function() {
39                 var t = this, win = t.getWin();
40
41                 // To avoid domain relaxing issue in Opera
42                 function close() {
43                         win.tb_remove();
44                         tinymce = tinyMCE = t.editor = t.dom = t.dom.doc = null; // Cleanup
45                 };
46
47                 if (tinymce.isOpera)
48                         win.setTimeout(close, 0);
49                 else
50                         close();
51         },
52
53         execCommand : function(cmd, ui, val, a) {
54                 a = a || {};
55                 a.skip_focus = 1;
56
57                 this.restoreSelection();
58                 return this.editor.execCommand(cmd, ui, val, a);
59         },
60
61         storeSelection : function() {
62                 this.editor.windowManager.bookmark = tinyMCEPopup.editor.selection.getBookmark('simple');
63         },
64
65         restoreSelection : function() {
66                 var t = tinyMCEPopup;
67
68                 if (tinymce.isIE)
69                         t.editor.selection.moveToBookmark(t.editor.windowManager.bookmark);
70         }
71 }
72 tinyMCEPopup.init();
73
74 wpImage = {
75         preInit : function() {
76                 // import colors stylesheet from parent
77                 var win = tinyMCEPopup.getWin(), styles = win.document.styleSheets, url, i;
78
79                 for ( i = 0; i < styles.length; i++ ) {
80                         url = styles.item(i).href;
81                         if ( url && url.indexOf('colors') != -1 )
82                                 document.write( '<link rel="stylesheet" href="'+url+'" type="text/css" media="all" />' );
83                 }
84         },
85
86         I : function(e) {
87                 return document.getElementById(e);
88         },
89
90         current : '',
91         link : '',
92         link_rel : '',
93         target_value : '',
94         current_size_sel : 's100',
95         width : '',
96         height : '',
97         align : '',
98         img_alt : '',
99
100         setTabs : function(tab) {
101                 var t = this;
102
103                 if ( 'current' == tab.className ) return false;
104                 t.I('div_advanced').style.display = ( 'tab_advanced' == tab.id ) ? 'block' : 'none';
105                 t.I('div_basic').style.display = ( 'tab_basic' == tab.id ) ? 'block' : 'none';
106                 t.I('tab_basic').className = t.I('tab_advanced').className = '';
107                 tab.className = 'current';
108                 return false;
109         },
110
111         img_seturl : function(u) {
112                 var t = this, rel = t.I('link_rel').value;
113
114                 if ( 'current' == u ) {
115                         t.I('link_href').value = t.current;
116                         t.I('link_rel').value = t.link_rel;
117                 } else {
118                         t.I('link_href').value = t.link;
119                         if ( rel ) {
120                                 rel = rel.replace( /attachment|wp-att-[0-9]+/gi, '' );
121                                 t.I('link_rel').value = tinymce.trim(rel);
122                         }
123                 }
124         },
125
126         imgAlignCls : function(v) {
127                 var t = this, cls = t.I('img_classes').value;
128
129                 t.I('img_demo').className = t.align = v;
130
131                 cls = cls.replace( /align[^ "']+/gi, '' );
132                 cls += (' ' + v);
133                 cls = cls.replace( /\s+/g, ' ' ).replace( /^\s/, '' );
134
135                 if ( 'aligncenter' == v ) {
136                         t.I('hspace').value = '';
137                         t.updateStyle('hspace');
138                 }
139
140                 t.I('img_classes').value = cls;
141         },
142
143         showSize : function(el) {
144                 var t = this, demo = t.I('img_demo'), w = t.width, h = t.height, id = el.id || 's100', size;
145
146                 size = parseInt(id.substring(1)) / 200;
147                 demo.width = Math.round(w * size);
148                 demo.height = Math.round(h * size);
149
150                 t.showSizeClear();
151                 el.style.borderColor = '#A3A3A3';
152                 el.style.backgroundColor = '#E5E5E5';
153         },
154
155         showSizeSet : function() {
156                 var t = this, s130, s120, s110;
157
158                 if ( (t.width * 1.3) > parseInt(t.preloadImg.width) ) {
159                         s130 = t.I('s130'), s120 = t.I('s120'), s110 = t.I('s110');
160
161                         s130.onclick = s120.onclick = s110.onclick = null;
162                         s130.onmouseover = s120.onmouseover = s110.onmouseover = null;
163                         s130.style.color = s120.style.color = s110.style.color = '#aaa';
164                 }
165         },
166
167         showSizeRem : function() {
168                 var t = this, demo = t.I('img_demo'), f = document.forms[0];
169
170                 demo.width = Math.round(f.width.value * 0.5);
171                 demo.height = Math.round(f.height.value * 0.5);
172                 t.showSizeClear();
173                 t.I(t.current_size_sel).style.borderColor = '#A3A3A3';
174                 t.I(t.current_size_sel).style.backgroundColor = '#E5E5E5';
175
176                 return false;
177         },
178
179         showSizeClear : function() {
180                 var divs = this.I('img_size').getElementsByTagName('div'), i;
181
182                 for ( i = 0; i < divs.length; i++ ) {
183                         divs[i].style.borderColor = '#f1f1f1';
184                         divs[i].style.backgroundColor = '#f1f1f1';
185                 }
186         },
187
188         imgEditSize : function(el) {
189                 var t = this, f = document.forms[0], W, H, w, h, id;
190
191                 if ( ! t.preloadImg || ! t.preloadImg.width || ! t.preloadImg.height )
192                         return;
193                 
194                 W = parseInt(t.preloadImg.width), H = parseInt(t.preloadImg.height), w = t.width || W, h = t.height || H, id = el.id || 's100';
195
196                 size = parseInt(id.substring(1)) / 100;
197
198                 w = Math.round(w * size);
199                 h = Math.round(h * size);
200
201                 f.width.value = Math.min(W, w);
202                 f.height.value = Math.min(H, h);
203
204                 t.current_size_sel = id;
205                 t.demoSetSize();
206         },
207
208         demoSetSize : function(img) {
209                 var demo = this.I('img_demo'), f = document.forms[0];
210
211                 demo.width = f.width.value ? Math.round(f.width.value * 0.5) : '';
212                 demo.height = f.height.value ? Math.round(f.height.value * 0.5) : '';
213         },
214
215         demoSetStyle : function() {
216                 var f = document.forms[0], demo = this.I('img_demo'), dom = tinyMCEPopup.editor.dom;
217
218                 if (demo) {
219                         dom.setAttrib(demo, 'style', f.img_style.value);
220                         dom.setStyle(demo, 'width', '');
221                         dom.setStyle(demo, 'height', '');
222                 }
223         },
224
225         origSize : function() {
226                 var t = this, f = document.forms[0], el = t.I('s100');
227
228                 f.width.value = t.width = t.preloadImg.width;
229                 f.height.value = t.height = t.preloadImg.height;
230                 t.showSizeSet();
231                 t.demoSetSize();
232                 t.showSize(el);
233         },
234
235         init : function() {
236                 var ed = tinyMCEPopup.editor, h;
237
238                 h = document.body.innerHTML;
239                 document.body.innerHTML = ed.translate(h);
240                 window.setTimeout( function(){wpImage.setup();}, 500 );
241         },
242
243         setup : function() {
244                 var t = this, c, el, link, fname, f = document.forms[0], ed = tinyMCEPopup.editor,
245                         d = t.I('img_demo'), dom = tinyMCEPopup.dom, DL, caption = '', dlc, pa;
246
247                 document.dir = tinyMCEPopup.editor.getParam('directionality','');
248
249                 if ( tinyMCEPopup.editor.getParam('wpeditimage_disable_captions', false) )
250                         t.I('cap_field').style.display = 'none';
251
252                 tinyMCEPopup.restoreSelection();
253                 el = ed.selection.getNode();
254                 if (el.nodeName != 'IMG')
255                         return;
256
257                 f.img_src.value = d.src = link = ed.dom.getAttrib(el, 'src');
258                 ed.dom.setStyle(el, 'float', '');
259                 t.getImageData();
260                 c = ed.dom.getAttrib(el, 'class');
261
262                 if ( DL = dom.getParent(el, 'dl') ) {
263                         dlc = ed.dom.getAttrib(DL, 'class');
264                         dlc = dlc.match(/align[^ "']+/i);
265                         if ( dlc && ! dom.hasClass(el, dlc) ) {
266                                 c += ' '+dlc;
267                                 tinymce.trim(c);
268                         }
269
270                         tinymce.each(DL.childNodes, function(e) {
271                                 if ( e.nodeName == 'DD' && dom.hasClass(e, 'wp-caption-dd') ) {
272                                         caption = e.innerHTML;
273                                         return;
274                                 }
275                         });
276                 }
277
278                 f.img_cap.value = caption;
279                 f.img_title.value = ed.dom.getAttrib(el, 'title');
280                 f.img_alt.value = ed.dom.getAttrib(el, 'alt');
281                 f.border.value = ed.dom.getAttrib(el, 'border');
282                 f.vspace.value = ed.dom.getAttrib(el, 'vspace');
283                 f.hspace.value = ed.dom.getAttrib(el, 'hspace');
284                 f.align.value = ed.dom.getAttrib(el, 'align');
285                 f.width.value = t.width = ed.dom.getAttrib(el, 'width');
286                 f.height.value = t.height = ed.dom.getAttrib(el, 'height');
287                 f.img_classes.value = c;
288                 f.img_style.value = ed.dom.getAttrib(el, 'style');
289
290                 // Move attribs to styles
291                 if ( dom.getAttrib(el, 'hspace') )
292                         t.updateStyle('hspace');
293
294                 if ( dom.getAttrib(el, 'border') )
295                         t.updateStyle('border');
296
297                 if ( dom.getAttrib(el, 'vspace') )
298                         t.updateStyle('vspace');
299
300                 if ( pa = ed.dom.getParent(el, 'A') ) {
301                         f.link_href.value = t.current = ed.dom.getAttrib(pa, 'href');
302                         f.link_title.value = ed.dom.getAttrib(pa, 'title');
303                         f.link_rel.value = t.link_rel = ed.dom.getAttrib(pa, 'rel');
304                         f.link_style.value = ed.dom.getAttrib(pa, 'style');
305                         t.target_value = ed.dom.getAttrib(pa, 'target');
306                         f.link_classes.value = ed.dom.getAttrib(pa, 'class');
307                 }
308
309                 f.link_target.checked = ( t.target_value && t.target_value == '_blank' ) ? 'checked' : '';
310
311                 fname = link.substring( link.lastIndexOf('/') );
312                 fname = fname.replace(/-[0-9]{2,4}x[0-9]{2,4}/, '' );
313                 t.link = link.substring( 0, link.lastIndexOf('/') ) + fname;
314
315                 if ( c.indexOf('alignleft') != -1 ) {
316                         t.I('alignleft').checked = "checked";
317                         d.className = t.align = "alignleft";
318                 } else if ( c.indexOf('aligncenter') != -1 ) {
319                         t.I('aligncenter').checked = "checked";
320                         d.className = t.align = "aligncenter";
321                 } else if ( c.indexOf('alignright') != -1 ) {
322                         t.I('alignright').checked = "checked";
323                         d.className = t.align = "alignright";
324                 } else if ( c.indexOf('alignnone') != -1 ) {
325                         t.I('alignnone').checked = "checked";
326                         d.className = t.align = "alignnone";
327                 }
328
329                 if ( t.width && t.preloadImg.width ) t.showSizeSet();
330                 document.body.style.display = '';
331         },
332
333         remove : function() {
334                 var ed = tinyMCEPopup.editor, p, el;
335
336                 tinyMCEPopup.restoreSelection();
337                 el = ed.selection.getNode();
338                 if (el.nodeName != 'IMG') return;
339
340                 if ( (p = ed.dom.getParent(el, 'div')) && ed.dom.hasClass(p, 'mceTemp') )
341                         ed.dom.remove(p);
342                 else if ( (p = ed.dom.getParent(el, 'A')) && p.childNodes.length == 1 )
343                         ed.dom.remove(p);
344                 else ed.dom.remove(el);
345
346                 ed.execCommand('mceRepaint');
347                 tinyMCEPopup.close();
348                 return;
349         },
350
351         update : function() {
352                 var t = this, f = document.forms[0], ed = tinyMCEPopup.editor, el, b, fixSafari = null,
353                         DL, P, A, DIV, do_caption = null, img_class = f.img_classes.value, html,
354                         id, cap_id = '', cap, DT, DD, cap_width, div_cls, lnk = '', pa, aa;
355
356                 tinyMCEPopup.restoreSelection();
357                 el = ed.selection.getNode();
358
359                 if (el.nodeName != 'IMG') return;
360                 if (f.img_src.value === '') {
361                         t.remove();
362                         return;
363                 }
364
365                 if ( f.img_cap.value != '' && f.width.value != '' ) {
366                         do_caption = 1;
367                         img_class = img_class.replace( /align[^ "']+\s?/gi, '' );
368                 }
369
370                 A = ed.dom.getParent(el, 'a');
371                 P = ed.dom.getParent(el, 'p');
372                 DL = ed.dom.getParent(el, 'dl');
373                 DIV = ed.dom.getParent(el, 'div');
374
375                 tinyMCEPopup.execCommand("mceBeginUndoLevel");
376
377                 ed.dom.setAttribs(el, {
378                         src : f.img_src.value,
379                         title : f.img_title.value,
380                         alt : f.img_alt.value,
381                         width : f.width.value,
382                         height : f.height.value,
383                         style : f.img_style.value,
384                         'class' : img_class
385                 });
386
387                 if ( f.link_href.value ) {
388                         // Create new anchor elements
389                         if ( A == null ) {
390                                 if ( ! f.link_href.value.match(/https?:\/\//i) )
391                                         f.link_href.value = tinyMCEPopup.editor.documentBaseURI.toAbsolute(f.link_href.value);
392
393                                 if ( tinymce.isWebKit && ed.dom.hasClass(el, 'aligncenter') ) {
394                                         ed.dom.removeClass(el, 'aligncenter');
395                                         fixSafari = 1;
396                                 }
397
398                                 tinyMCEPopup.execCommand("CreateLink", false, "#mce_temp_url#", {skip_undo : 1});
399                                 if ( fixSafari ) ed.dom.addClass(el, 'aligncenter');
400
401                                 tinymce.each(ed.dom.select("a"), function(n) {
402                                         if (ed.dom.getAttrib(n, 'href') == '#mce_temp_url#') {
403
404                                                 ed.dom.setAttribs(n, {
405                                                         href : f.link_href.value,
406                                                         title : f.link_title.value,
407                                                         rel : f.link_rel.value,
408                                                         target : (f.link_target.checked == true) ? '_blank' : '',
409                                                         'class' : f.link_classes.value,
410                                                         style : f.link_style.value
411                                                 });
412                                         }
413                                 });
414                         } else {
415                                 ed.dom.setAttribs(A, {
416                                         href : f.link_href.value,
417                                         title : f.link_title.value,
418                                         rel : f.link_rel.value,
419                                         target : (f.link_target.checked == true) ? '_blank' : '',
420                                         'class' : f.link_classes.value,
421                                         style : f.link_style.value
422                                 });
423                         }
424                 }
425
426                 if ( do_caption ) {
427                         cap_width = 10 + parseInt(f.width.value);
428                         div_cls = (t.align == 'aligncenter') ? 'mceTemp mceIEcenter' : 'mceTemp';
429
430                         if ( DL ) {
431                                 ed.dom.setAttribs(DL, {
432                                         'class' : 'wp-caption '+t.align,
433                                         style : 'width: '+cap_width+'px;'
434                                 });
435
436                                 if ( DIV )
437                                         ed.dom.setAttrib(DIV, 'class', div_cls);
438
439                                 if ( (DT = ed.dom.getParent(el, 'dt')) && (DD = DT.nextSibling) && ed.dom.hasClass(DD, 'wp-caption-dd') )
440                                         ed.dom.setHTML(DD, f.img_cap.value);
441
442                         } else {
443                                 if ( (id = f.img_classes.value.match( /wp-image-([0-9]{1,6})/ )) && id[1] )
444                                         cap_id = 'attachment_'+id[1];
445
446                                 if ( f.link_href.value && (lnk = ed.dom.getParent(el, 'a')) ) {
447                                         if ( lnk.childNodes.length == 1 )
448                                                 html = ed.dom.getOuterHTML(lnk);
449                                         else {
450                                                 html = ed.dom.getOuterHTML(lnk);
451                                                 html = html.match(/<a[^>]+>/i);
452                                                 html = html+ed.dom.getOuterHTML(el)+'</a>';
453                                         }
454                                 } else html = ed.dom.getOuterHTML(el);
455
456                                 html = '<dl id="'+cap_id+'" class="wp-caption '+t.align+'" style="width: '+cap_width+
457                                 'px"><dt class="wp-caption-dt">'+html+'</dt><dd class="wp-caption-dd">'+f.img_cap.value+'</dd></dl>';
458
459                                 cap = ed.dom.create('div', {'class': div_cls}, html);
460
461                                 if ( P ) {
462                                         P.parentNode.insertBefore(cap, P);
463                                         if ( P.childNodes.length == 1 )
464                                                 ed.dom.remove(P);
465                                         else if ( lnk && lnk.childNodes.length == 1 )
466                                                 ed.dom.remove(lnk);
467                                         else ed.dom.remove(el);
468                                 } else if ( pa = ed.dom.getParent(el, 'TD,TH,LI') ) {
469                                         pa.appendChild(cap);
470                                         if ( lnk && lnk.childNodes.length == 1 )
471                                                 ed.dom.remove(lnk);
472                                         else ed.dom.remove(el);
473                                 }
474                         }
475
476                 } else {
477                         if ( DL && DIV ) {
478                                 if ( f.link_href.value && (aa = ed.dom.getParent(el, 'a')) ) html = ed.dom.getOuterHTML(aa);
479                                 else html = ed.dom.getOuterHTML(el);
480
481                                 P = ed.dom.create('p', {}, html);
482                                 DIV.parentNode.insertBefore(P, DIV);
483                                 ed.dom.remove(DIV);
484                         }
485                 }
486
487                 if ( f.img_classes.value.indexOf('aligncenter') != -1 ) {
488                         if ( P && ( ! P.style || P.style.textAlign != 'center' ) )
489                                 ed.dom.setStyle(P, 'textAlign', 'center');
490                 } else {
491                         if ( P && P.style && P.style.textAlign == 'center' )
492                                 ed.dom.setStyle(P, 'textAlign', '');
493                 }
494
495                 if ( ! f.link_href.value && A ) {
496                         b = ed.selection.getBookmark();
497                         ed.dom.remove(A, 1);
498                         ed.selection.moveToBookmark(b);
499                 }
500
501                 tinyMCEPopup.execCommand("mceEndUndoLevel");
502                 ed.execCommand('mceRepaint');
503                 tinyMCEPopup.close();
504         },
505
506         updateStyle : function(ty) {
507                 var dom = tinyMCEPopup.dom, v, f = document.forms[0], img = dom.create('img', {style : f.img_style.value});
508
509                 if (tinyMCEPopup.editor.settings.inline_styles) {
510                         // Handle align
511                         if (ty == 'align') {
512                                 dom.setStyle(img, 'float', '');
513                                 dom.setStyle(img, 'vertical-align', '');
514
515                                 v = f.align.value;
516                                 if (v) {
517                                         if (v == 'left' || v == 'right')
518                                                 dom.setStyle(img, 'float', v);
519                                         else
520                                                 img.style.verticalAlign = v;
521                                 }
522                         }
523
524                         // Handle border
525                         if (ty == 'border') {
526                                 dom.setStyle(img, 'border', '');
527
528                                 v = f.border.value;
529                                 if (v || v == '0') {
530                                         if (v == '0')
531                                                 img.style.border = '0';
532                                         else
533                                                 img.style.border = v + 'px solid black';
534                                 }
535                         }
536
537                         // Handle hspace
538                         if (ty == 'hspace') {
539                                 dom.setStyle(img, 'marginLeft', '');
540                                 dom.setStyle(img, 'marginRight', '');
541
542                                 v = f.hspace.value;
543                                 if (v) {
544                                         img.style.marginLeft = v + 'px';
545                                         img.style.marginRight = v + 'px';
546                                 }
547                         }
548
549                         // Handle vspace
550                         if (ty == 'vspace') {
551                                 dom.setStyle(img, 'marginTop', '');
552                                 dom.setStyle(img, 'marginBottom', '');
553
554                                 v = f.vspace.value;
555                                 if (v) {
556                                         img.style.marginTop = v + 'px';
557                                         img.style.marginBottom = v + 'px';
558                                 }
559                         }
560
561                         // Merge
562                         f.img_style.value = dom.serializeStyle(dom.parseStyle(img.style.cssText));
563                         this.demoSetStyle();
564                 }
565         },
566
567         checkVal : function(f) {
568
569                 if ( f.value == '' ) {
570         //              if ( f.id == 'width' ) f.value = this.width || this.preloadImg.width;
571         //              if ( f.id == 'height' ) f.value = this.height || this.preloadImg.height;
572                         if ( f.id == 'img_src' ) f.value = this.I('img_demo').src || this.preloadImg.src;
573                 }
574         },
575
576         resetImageData : function() {
577                 var f = document.forms[0];
578
579                 f.width.value = f.height.value = '';
580         },
581
582         updateImageData : function() {
583                 var f = document.forms[0], t = wpImage, w = f.width.value, h = f.height.value;
584
585                 if ( !w && h )
586                         w = f.width.value = t.width = Math.round( t.preloadImg.width / (t.preloadImg.height / h) );
587                 else if ( w && !h )
588                         h = f.height.value = t.height = Math.round( t.preloadImg.height / (t.preloadImg.width / w) );
589
590                 if ( !w )
591                         f.width.value = t.width = t.preloadImg.width;
592
593                 if ( !h )
594                         f.height.value = t.height = t.preloadImg.height;
595
596                 t.showSizeSet();
597                 t.demoSetSize();
598                 if ( f.img_style.value )
599                         t.demoSetStyle();
600         },
601
602         getImageData : function() {
603                 var t = wpImage, f = document.forms[0];
604
605                 t.preloadImg = new Image();
606                 t.preloadImg.onload = t.updateImageData;
607                 t.preloadImg.onerror = t.resetImageData;
608                 t.preloadImg.src = tinyMCEPopup.editor.documentBaseURI.toAbsolute(f.img_src.value);
609         }
610 };
611
612 window.onload = function(){wpImage.init();}
613 wpImage.preInit();