]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/themes/advanced/js/image.js
Wordpress 2.6.2
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / themes / advanced / js / image.js
1 var ImageDialog = {
2         preInit : function() {
3                 var url;
4
5                 tinyMCEPopup.requireLangPack();
6
7                 if (url = tinyMCEPopup.getParam("external_image_list_url"))
8                         document.write('<script language="javascript" type="text/javascript" src="' + tinyMCEPopup.editor.documentBaseURI.toAbsolute(url) + '"></script>');
9         },
10
11         init : function() {
12                 var f = document.forms[0], ed = tinyMCEPopup.editor;
13
14                 // Setup browse button
15                 document.getElementById('srcbrowsercontainer').innerHTML = getBrowserHTML('srcbrowser','src','image','theme_advanced_image');
16                 if (isVisible('srcbrowser'))
17                         document.getElementById('src').style.width = '180px';
18
19                 e = ed.selection.getNode();
20
21                 this.fillFileList('image_list', 'tinyMCEImageList');
22
23                 if (e.nodeName == 'IMG') {
24                         f.src.value = ed.dom.getAttrib(e, 'src');
25                         f.alt.value = ed.dom.getAttrib(e, 'alt');
26                         f.border.value = this.getAttrib(e, 'border');
27                         f.vspace.value = this.getAttrib(e, 'vspace');
28                         f.hspace.value = this.getAttrib(e, 'hspace');
29                         f.width.value = ed.dom.getAttrib(e, 'width');
30                         f.height.value = ed.dom.getAttrib(e, 'height');
31                         f.insert.value = ed.getLang('update');
32                         f.class_name.value = ed.dom.getAttrib(e, 'class');
33                         this.styleVal = ed.dom.getAttrib(e, 'style');
34                         selectByValue(f, 'image_list', f.src.value);
35                         selectByValue(f, 'align', this.getAttrib(e, 'align'));
36                         this.updateStyle();
37                 }
38         },
39
40         fillFileList : function(id, l) {
41                 var dom = tinyMCEPopup.dom, lst = dom.get(id), v, cl;
42
43                 l = window[l];
44
45                 if (l && l.length > 0) {
46                         lst.options[lst.options.length] = new Option('', '');
47
48                         tinymce.each(l, function(o) {
49                                 lst.options[lst.options.length] = new Option(o[0], o[1]);
50                         });
51                 } else
52                         dom.remove(dom.getParent(id, 'tr'));
53         },
54
55         update : function() {
56                 var f = document.forms[0], nl = f.elements, ed = tinyMCEPopup.editor, args = {}, el;
57
58                 tinyMCEPopup.restoreSelection();
59
60                 if (f.src.value === '') {
61                         if (ed.selection.getNode().nodeName == 'IMG') {
62                                 ed.dom.remove(ed.selection.getNode());
63                                 ed.execCommand('mceRepaint');
64                         }
65
66                         tinyMCEPopup.close();
67                         return;
68                 }
69
70                 if (!ed.settings.inline_styles) {
71                         args = tinymce.extend(args, {
72                                 vspace : nl.vspace.value,
73                                 hspace : nl.hspace.value,
74                                 border : nl.border.value,
75                                 align : getSelectValue(f, 'align')
76                         });
77                 } else
78                         args.style = this.styleVal;
79                 
80                 tinymce.extend(args, {
81                         src : f.src.value,
82                         alt : f.alt.value,
83                         width : f.width.value,
84                         height : f.height.value,
85                         'class' : f.class_name.value
86                 });
87
88                 el = ed.selection.getNode();
89
90                 if (el && el.nodeName == 'IMG') {
91                         ed.dom.setAttribs(el, args);
92                 } else {
93                         ed.execCommand('mceInsertContent', false, '<img id="__mce_tmp" />', {skip_undo : 1});
94                         ed.dom.setAttribs('__mce_tmp', args);
95                         ed.dom.setAttrib('__mce_tmp', 'id', '');
96                         ed.undoManager.add();
97                 }
98
99                 tinyMCEPopup.close();
100         },
101
102         updateStyle : function() {
103                 var dom = tinyMCEPopup.dom, st, v, cls, oldcls, rep, f = document.forms[0];
104
105                 if (tinyMCEPopup.editor.settings.inline_styles) {
106                         st = tinyMCEPopup.dom.parseStyle(this.styleVal);
107
108                         // Handle align
109                         v = getSelectValue(f, 'align');
110                         cls = f.class_name.value || '';
111                         cls = cls ? cls.replace(/alignright\s*|alignleft\s*|aligncenter\s*/g, '') : '';
112                         cls = cls ? cls.replace(/^\s*(.+?)\s*$/, '$1') : '';
113                         if (v) {
114                                 if (v == 'left' || v == 'right') {
115                                         st['float'] = v;
116                                         delete st['vertical-align'];
117                                         oldcls = cls ? ' '+cls : '';
118                                         f.class_name.value = 'align' + v + oldcls;
119                                 } else {
120                                         st['vertical-align'] = v;
121                                         delete st['float'];
122                                         f.class_name.value = cls;
123                                 }
124                         } else {
125                                 delete st['float'];
126                                 delete st['vertical-align'];
127                                 f.class_name.value = cls;
128                         }
129
130                         // Handle border
131                         v = f.border.value;
132                         if (v || v == '0') {
133                                 if (v == '0')
134                                         st['border'] = '0';
135                                 else
136                                         st['border'] = v + 'px solid black';
137                         } else
138                                 delete st['border'];
139
140                         // Handle hspace
141                         v = f.hspace.value;
142                         if (v) {
143                                 delete st['margin'];
144                                 st['margin-left'] = v + 'px';
145                                 st['margin-right'] = v + 'px';
146                         } else {
147                                 delete st['margin-left'];
148                                 delete st['margin-right'];
149                         }
150
151                         // Handle vspace
152                         v = f.vspace.value;
153                         if (v) {
154                                 delete st['margin'];
155                                 st['margin-top'] = v + 'px';
156                                 st['margin-bottom'] = v + 'px';
157                         } else {
158                                 delete st['margin-top'];
159                                 delete st['margin-bottom'];
160                         }
161
162                         // Merge
163                         st = tinyMCEPopup.dom.parseStyle(dom.serializeStyle(st));
164                         this.styleVal = dom.serializeStyle(st);
165                 }
166         },
167
168         getAttrib : function(e, at) {
169                 var ed = tinyMCEPopup.editor, dom = ed.dom, v, v2;
170
171                 if (ed.settings.inline_styles) {
172                         switch (at) {
173                                 case 'align':
174                                         if (v = dom.getStyle(e, 'float'))
175                                                 return v;
176
177                                         if (v = dom.getStyle(e, 'vertical-align'))
178                                                 return v;
179
180                                         break;
181
182                                 case 'hspace':
183                                         v = dom.getStyle(e, 'margin-left')
184                                         v2 = dom.getStyle(e, 'margin-right');
185                                         if (v && v == v2)
186                                                 return parseInt(v.replace(/[^0-9]/g, ''));
187
188                                         break;
189
190                                 case 'vspace':
191                                         v = dom.getStyle(e, 'margin-top')
192                                         v2 = dom.getStyle(e, 'margin-bottom');
193                                         if (v && v == v2)
194                                                 return parseInt(v.replace(/[^0-9]/g, ''));
195
196                                         break;
197
198                                 case 'border':
199                                         v = 0;
200
201                                         tinymce.each(['top', 'right', 'bottom', 'left'], function(sv) {
202                                                 sv = dom.getStyle(e, 'border-' + sv + '-width');
203
204                                                 // False or not the same as prev
205                                                 if (!sv || (sv != v && v !== 0)) {
206                                                         v = 0;
207                                                         return false;
208                                                 }
209
210                                                 if (sv)
211                                                         v = sv;
212                                         });
213
214                                         if (v)
215                                                 return parseInt(v.replace(/[^0-9]/g, ''));
216
217                                         break;
218                         }
219                 }
220
221                 if (v = dom.getAttrib(e, at))
222                         return v;
223
224                 return '';
225         },
226
227         resetImageData : function() {
228                 var f = document.forms[0];
229
230                 f.width.value = f.height.value = "";    
231         },
232
233         updateImageData : function() {
234                 var f = document.forms[0], t = ImageDialog;
235
236                 if (f.width.value == "")
237                         f.width.value = t.preloadImg.width;
238
239                 if (f.height.value == "")
240                         f.height.value = t.preloadImg.height;
241         },
242
243         getImageData : function() {
244                 var f = document.forms[0];
245
246                 this.preloadImg = new Image();
247                 this.preloadImg.onload = this.updateImageData;
248                 this.preloadImg.onerror = this.resetImageData;
249                 this.preloadImg.src = tinyMCEPopup.editor.documentBaseURI.toAbsolute(f.src.value);
250         }
251 };
252
253 ImageDialog.preInit();
254 tinyMCEPopup.onInit.add(ImageDialog.init, ImageDialog);