]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/themes/advanced/js/image.js
Wordpress 3.5
[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', tinyMCEPopup.getParam('external_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 = typeof(l) === 'function' ? 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                 });
85
86                 el = ed.selection.getNode();
87
88                 if (el && el.nodeName == 'IMG') {
89                         ed.dom.setAttribs(el, args);
90                         tinyMCEPopup.editor.execCommand('mceRepaint');
91                         tinyMCEPopup.editor.focus();
92                 } else {
93                         tinymce.each(args, function(value, name) {
94                                 if (value === "") {
95                                         delete args[name];
96                                 }
97                         });
98
99                         ed.execCommand('mceInsertContent', false, tinyMCEPopup.editor.dom.createHTML('img', args), {skip_undo : 1});
100                         ed.undoManager.add();
101                 }
102
103                 tinyMCEPopup.close();
104         },
105
106         updateStyle : function() {
107                 var dom = tinyMCEPopup.dom, st = {}, v, f = document.forms[0];
108
109                 if (tinyMCEPopup.editor.settings.inline_styles) {
110                         tinymce.each(tinyMCEPopup.dom.parseStyle(this.styleVal), function(value, key) {
111                                 st[key] = value;
112                         });
113
114                         // Handle align
115                         v = getSelectValue(f, 'align');
116                         if (v) {
117                                 if (v == 'left' || v == 'right') {
118                                         st['float'] = v;
119                                         delete st['vertical-align'];
120                                 } else {
121                                         st['vertical-align'] = v;
122                                         delete st['float'];
123                                 }
124                         } else {
125                                 delete st['float'];
126                                 delete st['vertical-align'];
127                         }
128
129                         // Handle border
130                         v = f.border.value;
131                         if (v || v == '0') {
132                                 if (v == '0')
133                                         st['border'] = '0';
134                                 else
135                                         st['border'] = v + 'px solid black';
136                         } else
137                                 delete st['border'];
138
139                         // Handle hspace
140                         v = f.hspace.value;
141                         if (v) {
142                                 delete st['margin'];
143                                 st['margin-left'] = v + 'px';
144                                 st['margin-right'] = v + 'px';
145                         } else {
146                                 delete st['margin-left'];
147                                 delete st['margin-right'];
148                         }
149
150                         // Handle vspace
151                         v = f.vspace.value;
152                         if (v) {
153                                 delete st['margin'];
154                                 st['margin-top'] = v + 'px';
155                                 st['margin-bottom'] = v + 'px';
156                         } else {
157                                 delete st['margin-top'];
158                                 delete st['margin-bottom'];
159                         }
160
161                         // Merge
162                         st = tinyMCEPopup.dom.parseStyle(dom.serializeStyle(st), 'img');
163                         this.styleVal = dom.serializeStyle(st, 'img');
164                 }
165         },
166
167         getAttrib : function(e, at) {
168                 var ed = tinyMCEPopup.editor, dom = ed.dom, v, v2;
169
170                 if (ed.settings.inline_styles) {
171                         switch (at) {
172                                 case 'align':
173                                         if (v = dom.getStyle(e, 'float'))
174                                                 return v;
175
176                                         if (v = dom.getStyle(e, 'vertical-align'))
177                                                 return v;
178
179                                         break;
180
181                                 case 'hspace':
182                                         v = dom.getStyle(e, 'margin-left')
183                                         v2 = dom.getStyle(e, 'margin-right');
184                                         if (v && v == v2)
185                                                 return parseInt(v.replace(/[^0-9]/g, ''));
186
187                                         break;
188
189                                 case 'vspace':
190                                         v = dom.getStyle(e, 'margin-top')
191                                         v2 = dom.getStyle(e, 'margin-bottom');
192                                         if (v && v == v2)
193                                                 return parseInt(v.replace(/[^0-9]/g, ''));
194
195                                         break;
196
197                                 case 'border':
198                                         v = 0;
199
200                                         tinymce.each(['top', 'right', 'bottom', 'left'], function(sv) {
201                                                 sv = dom.getStyle(e, 'border-' + sv + '-width');
202
203                                                 // False or not the same as prev
204                                                 if (!sv || (sv != v && v !== 0)) {
205                                                         v = 0;
206                                                         return false;
207                                                 }
208
209                                                 if (sv)
210                                                         v = sv;
211                                         });
212
213                                         if (v)
214                                                 return parseInt(v.replace(/[^0-9]/g, ''));
215
216                                         break;
217                         }
218                 }
219
220                 if (v = dom.getAttrib(e, at))
221                         return v;
222
223                 return '';
224         },
225
226         resetImageData : function() {
227                 var f = document.forms[0];
228
229                 f.width.value = f.height.value = "";    
230         },
231
232         updateImageData : function() {
233                 var f = document.forms[0], t = ImageDialog;
234
235                 if (f.width.value == "")
236                         f.width.value = t.preloadImg.width;
237
238                 if (f.height.value == "")
239                         f.height.value = t.preloadImg.height;
240         },
241
242         getImageData : function() {
243                 var f = document.forms[0];
244
245                 this.preloadImg = new Image();
246                 this.preloadImg.onload = this.updateImageData;
247                 this.preloadImg.onerror = this.resetImageData;
248                 this.preloadImg.src = tinyMCEPopup.editor.documentBaseURI.toAbsolute(f.src.value);
249         }
250 };
251
252 ImageDialog.preInit();
253 tinyMCEPopup.onInit.add(ImageDialog.init, ImageDialog);