]> scripts.mit.edu Git - autoinstallsdev/wordpress.git/blob - wp-includes/js/tinymce/plugins/wpeditimage/js/editimage.dev.js
2d7e38e49aaf062af424ce1425771772f6ab2b6b
[autoinstallsdev/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                 if ( f.width.value != el.width || f.height.value != el.height )
378                         img_class = img_class.replace(/size-[^ "']+/, '');
379
380                 ed.dom.setAttribs(el, {
381                         src : f.img_src.value,
382                         title : f.img_title.value,
383                         alt : f.img_alt.value,
384                         width : f.width.value,
385                         height : f.height.value,
386                         style : f.img_style.value,
387                         'class' : img_class
388                 });
389
390                 if ( f.link_href.value ) {
391                         // Create new anchor elements
392                         if ( A == null ) {
393                                 if ( ! f.link_href.value.match(/https?:\/\//i) )
394                                         f.link_href.value = tinyMCEPopup.editor.documentBaseURI.toAbsolute(f.link_href.value);
395
396                                 if ( tinymce.isWebKit && ed.dom.hasClass(el, 'aligncenter') ) {
397                                         ed.dom.removeClass(el, 'aligncenter');
398                                         fixSafari = 1;
399                                 }
400
401                                 tinyMCEPopup.execCommand("CreateLink", false, "#mce_temp_url#", {skip_undo : 1});
402                                 if ( fixSafari ) ed.dom.addClass(el, 'aligncenter');
403
404                                 tinymce.each(ed.dom.select("a"), function(n) {
405                                         if (ed.dom.getAttrib(n, 'href') == '#mce_temp_url#') {
406
407                                                 ed.dom.setAttribs(n, {
408                                                         href : f.link_href.value,
409                                                         title : f.link_title.value,
410                                                         rel : f.link_rel.value,
411                                                         target : (f.link_target.checked == true) ? '_blank' : '',
412                                                         'class' : f.link_classes.value,
413                                                         style : f.link_style.value
414                                                 });
415                                         }
416                                 });
417                         } else {
418                                 ed.dom.setAttribs(A, {
419                                         href : f.link_href.value,
420                                         title : f.link_title.value,
421                                         rel : f.link_rel.value,
422                                         target : (f.link_target.checked == true) ? '_blank' : '',
423                                         'class' : f.link_classes.value,
424                                         style : f.link_style.value
425                                 });
426                         }
427                 }
428
429                 if ( do_caption ) {
430                         cap_width = 10 + parseInt(f.width.value);
431                         div_cls = (t.align == 'aligncenter') ? 'mceTemp mceIEcenter' : 'mceTemp';
432
433                         if ( DL ) {
434                                 ed.dom.setAttribs(DL, {
435                                         'class' : 'wp-caption '+t.align,
436                                         style : 'width: '+cap_width+'px;'
437                                 });
438
439                                 if ( DIV )
440                                         ed.dom.setAttrib(DIV, 'class', div_cls);
441
442                                 if ( (DT = ed.dom.getParent(el, 'dt')) && (DD = DT.nextSibling) && ed.dom.hasClass(DD, 'wp-caption-dd') )
443                                         ed.dom.setHTML(DD, f.img_cap.value);
444
445                         } else {
446                                 if ( (id = f.img_classes.value.match( /wp-image-([0-9]{1,6})/ )) && id[1] )
447                                         cap_id = 'attachment_'+id[1];
448
449                                 if ( f.link_href.value && (lnk = ed.dom.getParent(el, 'a')) ) {
450                                         if ( lnk.childNodes.length == 1 )
451                                                 html = ed.dom.getOuterHTML(lnk);
452                                         else {
453                                                 html = ed.dom.getOuterHTML(lnk);
454                                                 html = html.match(/<a[^>]+>/i);
455                                                 html = html+ed.dom.getOuterHTML(el)+'</a>';
456                                         }
457                                 } else html = ed.dom.getOuterHTML(el);
458
459                                 html = '<dl id="'+cap_id+'" class="wp-caption '+t.align+'" style="width: '+cap_width+
460                                 'px"><dt class="wp-caption-dt">'+html+'</dt><dd class="wp-caption-dd">'+f.img_cap.value+'</dd></dl>';
461
462                                 cap = ed.dom.create('div', {'class': div_cls}, html);
463
464                                 if ( P ) {
465                                         P.parentNode.insertBefore(cap, P);
466                                         if ( P.childNodes.length == 1 )
467                                                 ed.dom.remove(P);
468                                         else if ( lnk && lnk.childNodes.length == 1 )
469                                                 ed.dom.remove(lnk);
470                                         else ed.dom.remove(el);
471                                 } else if ( pa = ed.dom.getParent(el, 'TD,TH,LI') ) {
472                                         pa.appendChild(cap);
473                                         if ( lnk && lnk.childNodes.length == 1 )
474                                                 ed.dom.remove(lnk);
475                                         else ed.dom.remove(el);
476                                 }
477                         }
478
479                 } else {
480                         if ( DL && DIV ) {
481                                 if ( f.link_href.value && (aa = ed.dom.getParent(el, 'a')) ) html = ed.dom.getOuterHTML(aa);
482                                 else html = ed.dom.getOuterHTML(el);
483
484                                 P = ed.dom.create('p', {}, html);
485                                 DIV.parentNode.insertBefore(P, DIV);
486                                 ed.dom.remove(DIV);
487                         }
488                 }
489
490                 if ( f.img_classes.value.indexOf('aligncenter') != -1 ) {
491                         if ( P && ( ! P.style || P.style.textAlign != 'center' ) )
492                                 ed.dom.setStyle(P, 'textAlign', 'center');
493                 } else {
494                         if ( P && P.style && P.style.textAlign == 'center' )
495                                 ed.dom.setStyle(P, 'textAlign', '');
496                 }
497
498                 if ( ! f.link_href.value && A ) {
499                         b = ed.selection.getBookmark();
500                         ed.dom.remove(A, 1);
501                         ed.selection.moveToBookmark(b);
502                 }
503
504                 tinyMCEPopup.execCommand("mceEndUndoLevel");
505                 ed.execCommand('mceRepaint');
506                 tinyMCEPopup.close();
507         },
508
509         updateStyle : function(ty) {
510                 var dom = tinyMCEPopup.dom, v, f = document.forms[0], img = dom.create('img', {style : f.img_style.value});
511
512                 if (tinyMCEPopup.editor.settings.inline_styles) {
513                         // Handle align
514                         if (ty == 'align') {
515                                 dom.setStyle(img, 'float', '');
516                                 dom.setStyle(img, 'vertical-align', '');
517
518                                 v = f.align.value;
519                                 if (v) {
520                                         if (v == 'left' || v == 'right')
521                                                 dom.setStyle(img, 'float', v);
522                                         else
523                                                 img.style.verticalAlign = v;
524                                 }
525                         }
526
527                         // Handle border
528                         if (ty == 'border') {
529                                 dom.setStyle(img, 'border', '');
530
531                                 v = f.border.value;
532                                 if (v || v == '0') {
533                                         if (v == '0')
534                                                 img.style.border = '0';
535                                         else
536                                                 img.style.border = v + 'px solid black';
537                                 }
538                         }
539
540                         // Handle hspace
541                         if (ty == 'hspace') {
542                                 dom.setStyle(img, 'marginLeft', '');
543                                 dom.setStyle(img, 'marginRight', '');
544
545                                 v = f.hspace.value;
546                                 if (v) {
547                                         img.style.marginLeft = v + 'px';
548                                         img.style.marginRight = v + 'px';
549                                 }
550                         }
551
552                         // Handle vspace
553                         if (ty == 'vspace') {
554                                 dom.setStyle(img, 'marginTop', '');
555                                 dom.setStyle(img, 'marginBottom', '');
556
557                                 v = f.vspace.value;
558                                 if (v) {
559                                         img.style.marginTop = v + 'px';
560                                         img.style.marginBottom = v + 'px';
561                                 }
562                         }
563
564                         // Merge
565                         f.img_style.value = dom.serializeStyle(dom.parseStyle(img.style.cssText));
566                         this.demoSetStyle();
567                 }
568         },
569
570         checkVal : function(f) {
571
572                 if ( f.value == '' ) {
573         //              if ( f.id == 'width' ) f.value = this.width || this.preloadImg.width;
574         //              if ( f.id == 'height' ) f.value = this.height || this.preloadImg.height;
575                         if ( f.id == 'img_src' ) f.value = this.I('img_demo').src || this.preloadImg.src;
576                 }
577         },
578
579         resetImageData : function() {
580                 var f = document.forms[0];
581
582                 f.width.value = f.height.value = '';
583         },
584
585         updateImageData : function() {
586                 var f = document.forms[0], t = wpImage, w = f.width.value, h = f.height.value;
587
588                 if ( !w && h )
589                         w = f.width.value = t.width = Math.round( t.preloadImg.width / (t.preloadImg.height / h) );
590                 else if ( w && !h )
591                         h = f.height.value = t.height = Math.round( t.preloadImg.height / (t.preloadImg.width / w) );
592
593                 if ( !w )
594                         f.width.value = t.width = t.preloadImg.width;
595
596                 if ( !h )
597                         f.height.value = t.height = t.preloadImg.height;
598
599                 t.showSizeSet();
600                 t.demoSetSize();
601                 if ( f.img_style.value )
602                         t.demoSetStyle();
603         },
604
605         getImageData : function() {
606                 var t = wpImage, f = document.forms[0];
607
608                 t.preloadImg = new Image();
609                 t.preloadImg.onload = t.updateImageData;
610                 t.preloadImg.onerror = t.resetImageData;
611                 t.preloadImg.src = tinyMCEPopup.editor.documentBaseURI.toAbsolute(f.img_src.value);
612         }
613 };
614
615 window.onload = function(){wpImage.init();}
616 wpImage.preInit();