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