includes/clientside/tinymce/plugins/advimage/js/image.js
changeset 335 67bd3121a12e
child 395 fa4c5ecb7c9a
equal deleted inserted replaced
334:c72b545f1304 335:67bd3121a12e
       
     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(ed) {
       
    12 		var f = document.forms[0], nl = f.elements, ed = tinyMCEPopup.editor, dom = ed.dom, n = ed.selection.getNode();
       
    13 
       
    14 		tinyMCEPopup.resizeToInnerSize();
       
    15 		this.fillClassList('class_list');
       
    16 		this.fillFileList('src_list', 'tinyMCEImageList');
       
    17 		this.fillFileList('over_list', 'tinyMCEImageList');
       
    18 		this.fillFileList('out_list', 'tinyMCEImageList');
       
    19 
       
    20 		if (n.nodeName == 'IMG') {
       
    21 			nl.src.value = dom.getAttrib(n, 'src');
       
    22 			nl.width.value = dom.getAttrib(n, 'width');
       
    23 			nl.height.value = dom.getAttrib(n, 'height');
       
    24 			nl.alt.value = dom.getAttrib(n, 'alt');
       
    25 			nl.title.value = dom.getAttrib(n, 'title');
       
    26 			nl.vspace.value = this.getAttrib(n, 'vspace');
       
    27 			nl.hspace.value = this.getAttrib(n, 'hspace');
       
    28 			nl.border.value = this.getAttrib(n, 'border');
       
    29 			selectByValue(f, 'align', this.getAttrib(n, 'align'));
       
    30 			selectByValue(f, 'class_list', dom.getAttrib(n, 'class'));
       
    31 			nl.style.value = dom.getAttrib(n, 'style');
       
    32 			nl.id.value = dom.getAttrib(n, 'id');
       
    33 			nl.dir.value = dom.getAttrib(n, 'dir');
       
    34 			nl.lang.value = dom.getAttrib(n, 'lang');
       
    35 			nl.usemap.value = dom.getAttrib(n, 'usemap');
       
    36 			nl.longdesc.value = dom.getAttrib(n, 'longdesc');
       
    37 			nl.insert.value = ed.getLang('update');
       
    38 
       
    39 			if (/^\s*this.src\s*=\s*\'([^\']+)\';?\s*$/.test(dom.getAttrib(n, 'onmouseover')))
       
    40 				nl.onmouseoversrc.value = dom.getAttrib(n, 'onmouseover').replace(/^\s*this.src\s*=\s*\'([^\']+)\';?\s*$/, '$1');
       
    41 
       
    42 			if (/^\s*this.src\s*=\s*\'([^\']+)\';?\s*$/.test(dom.getAttrib(n, 'onmouseout')))
       
    43 				nl.onmouseoutsrc.value = dom.getAttrib(n, 'onmouseout').replace(/^\s*this.src\s*=\s*\'([^\']+)\';?\s*$/, '$1');
       
    44 		}
       
    45 
       
    46 		// Setup browse button
       
    47 		document.getElementById('srcbrowsercontainer').innerHTML = getBrowserHTML('srcbrowser','src','image','theme_advanced_image');
       
    48 		if (isVisible('srcbrowser'))
       
    49 			document.getElementById('src').style.width = '260px';
       
    50 
       
    51 		// Setup browse button
       
    52 		document.getElementById('onmouseoversrccontainer').innerHTML = getBrowserHTML('overbrowser','onmouseoversrc','image','theme_advanced_image');
       
    53 		if (isVisible('overbrowser'))
       
    54 			document.getElementById('onmouseoversrc').style.width = '260px';
       
    55 
       
    56 		// Setup browse button
       
    57 		document.getElementById('onmouseoutsrccontainer').innerHTML = getBrowserHTML('outbrowser','onmouseoutsrc','image','theme_advanced_image');
       
    58 		if (isVisible('outbrowser'))
       
    59 			document.getElementById('onmouseoutsrc').style.width = '260px';
       
    60 
       
    61 		// If option enabled default contrain proportions to checked
       
    62 		if (ed.getParam("advimage_constrain_proportions", true))
       
    63 			f.constrain.checked = true;
       
    64 
       
    65 		// Check swap image if valid data
       
    66 		if (nl.onmouseoversrc.value || nl.onmouseoutsrc.value)
       
    67 			this.setSwapImage(true);
       
    68 		else
       
    69 			this.setSwapImage(false);
       
    70 
       
    71 		this.changeAppearance();
       
    72 		this.updateStyle();
       
    73 		this.showPreviewImage(nl.src.value, 1);
       
    74 	},
       
    75 
       
    76 	insert : function(file, title) {
       
    77 		var t = this;
       
    78 
       
    79 		if (tinyMCEPopup.getParam("accessibility_warnings", 1)) {
       
    80 			if (!document.forms[0].alt.value) {
       
    81 				tinyMCEPopup.editor.windowManager.confirm(tinyMCEPopup.getLang('advimage_dlg.missing_alt'), function(s) {
       
    82 					if (s)
       
    83 						t.insertAndClose();
       
    84 				});
       
    85 
       
    86 				return;
       
    87 			}
       
    88 		}
       
    89 
       
    90 		t.insertAndClose();
       
    91 	},
       
    92 
       
    93 	insertAndClose : function() {
       
    94 		var ed = tinyMCEPopup.editor, f = document.forms[0], nl = f.elements, v, args = {}, el;
       
    95 
       
    96 		// Fixes crash in Safari
       
    97 		if (tinymce.isWebKit)
       
    98 			ed.getWin().focus();
       
    99 
       
   100 		if (!ed.settings.inline_styles) {
       
   101 			args = tinymce.extend(args, {
       
   102 				vspace : nl.vspace.value,
       
   103 				hspace : nl.hspace.value,
       
   104 				border : nl.border.value,
       
   105 				align : getSelectValue(f, 'align')
       
   106 			});
       
   107 		}
       
   108 
       
   109 		tinymce.extend(args, {
       
   110 			src : nl.src.value,
       
   111 			width : nl.width.value,
       
   112 			height : nl.height.value,
       
   113 			alt : nl.alt.value,
       
   114 			title : nl.title.value,
       
   115 			'class' : getSelectValue(f, 'class_list'),
       
   116 			style : nl.style.value,
       
   117 			id : nl.id.value,
       
   118 			dir : nl.dir.value,
       
   119 			lang : nl.lang.value,
       
   120 			usemap : nl.usemap.value,
       
   121 			longdesc : nl.longdesc.value
       
   122 		});
       
   123 
       
   124 		if (nl.onmouseoversrc.value)
       
   125 			args.onmouseover = "this.src='" + nl.onmouseoversrc.value + "';";
       
   126 
       
   127 		if (nl.onmouseoutsrc.value)
       
   128 			args.onmouseout = "this.src='" + nl.onmouseoutsrc.value + "';";
       
   129 
       
   130 		el = ed.selection.getNode();
       
   131 
       
   132 		if (el && el.nodeName == 'IMG') {
       
   133 			ed.dom.setAttribs(el, args);
       
   134 		} else {
       
   135 			ed.execCommand('mceInsertContent', false, '<img id="__mce_tmp" src="javascript:;" />');
       
   136 			ed.dom.setAttribs('__mce_tmp', args);
       
   137 			ed.dom.setAttrib('__mce_tmp', 'id', '');
       
   138 		}
       
   139 
       
   140 		tinyMCEPopup.close();
       
   141 	},
       
   142 
       
   143 	getAttrib : function(e, at) {
       
   144 		var ed = tinyMCEPopup.editor, dom = ed.dom, v, v2;
       
   145 
       
   146 		if (ed.settings.inline_styles) {
       
   147 			switch (at) {
       
   148 				case 'align':
       
   149 					if (v = dom.getStyle(e, 'float'))
       
   150 						return v;
       
   151 
       
   152 					if (v = dom.getStyle(e, 'vertical-align'))
       
   153 						return v;
       
   154 
       
   155 					break;
       
   156 
       
   157 				case 'hspace':
       
   158 					v = dom.getStyle(e, 'margin-left')
       
   159 					v2 = dom.getStyle(e, 'margin-right');
       
   160 					if (v && v == v2)
       
   161 						return parseInt(v.replace(/[^0-9]/g, ''));
       
   162 
       
   163 					break;
       
   164 
       
   165 				case 'vspace':
       
   166 					v = dom.getStyle(e, 'margin-top')
       
   167 					v2 = dom.getStyle(e, 'margin-bottom');
       
   168 					if (v && v == v2)
       
   169 						return parseInt(v.replace(/[^0-9]/g, ''));
       
   170 
       
   171 					break;
       
   172 
       
   173 				case 'border':
       
   174 					v = 0;
       
   175 
       
   176 					tinymce.each(['top', 'right', 'bottom', 'left'], function(sv) {
       
   177 						sv = dom.getStyle(e, 'border-' + sv + '-width');
       
   178 
       
   179 						// False or not the same as prev
       
   180 						if (!sv || (sv != v && v !== 0)) {
       
   181 							v = 0;
       
   182 							return false;
       
   183 						}
       
   184 
       
   185 						if (sv)
       
   186 							v = sv;
       
   187 					});
       
   188 
       
   189 					if (v)
       
   190 						return parseInt(v.replace(/[^0-9]/g, ''));
       
   191 
       
   192 					break;
       
   193 			}
       
   194 		}
       
   195 
       
   196 		if (v = dom.getAttrib(e, at))
       
   197 			return v;
       
   198 
       
   199 		return '';
       
   200 	},
       
   201 
       
   202 	setSwapImage : function(st) {
       
   203 		var f = document.forms[0];
       
   204 
       
   205 		f.onmousemovecheck.checked = st;
       
   206 		setBrowserDisabled('overbrowser', !st);
       
   207 		setBrowserDisabled('outbrowser', !st);
       
   208 
       
   209 		if (f.over_list)
       
   210 			f.over_list.disabled = !st;
       
   211 
       
   212 		if (f.out_list)
       
   213 			f.out_list.disabled = !st;
       
   214 
       
   215 		f.onmouseoversrc.disabled = !st;
       
   216 		f.onmouseoutsrc.disabled  = !st;
       
   217 	},
       
   218 
       
   219 	fillClassList : function(id) {
       
   220 		var dom = tinyMCEPopup.dom, lst = dom.get(id), v, cl;
       
   221 
       
   222 		if (v = tinyMCEPopup.getParam('theme_advanced_styles')) {
       
   223 			cl = [];
       
   224 
       
   225 			tinymce.each(v.split(';'), function(v) {
       
   226 				var p = v.split('=');
       
   227 
       
   228 				cl.push({'title' : p[0], 'class' : p[1]});
       
   229 			});
       
   230 		} else
       
   231 			cl = tinyMCEPopup.editor.dom.getClasses();
       
   232 
       
   233 		if (cl.length > 0) {
       
   234 			lst.options[lst.options.length] = new Option(tinyMCEPopup.getLang('not_set'), '');
       
   235 
       
   236 			tinymce.each(cl, function(o) {
       
   237 				lst.options[lst.options.length] = new Option(o.title || o['class'], o['class']);
       
   238 			});
       
   239 		} else
       
   240 			dom.remove(dom.getParent(id, 'tr'));
       
   241 	},
       
   242 
       
   243 	fillFileList : function(id, l) {
       
   244 		var dom = tinyMCEPopup.dom, lst = dom.get(id), v, cl;
       
   245 
       
   246 		l = window[l];
       
   247 
       
   248 		if (l && l.length > 0) {
       
   249 			lst.options[lst.options.length] = new Option('', '');
       
   250 
       
   251 			tinymce.each(l, function(o) {
       
   252 				lst.options[lst.options.length] = new Option(o[0], o[1]);
       
   253 			});
       
   254 		} else
       
   255 			dom.remove(dom.getParent(id, 'tr'));
       
   256 	},
       
   257 
       
   258 	resetImageData : function() {
       
   259 		var f = document.forms[0];
       
   260 
       
   261 		f.elements.width.value = f.elements.height.value = '';
       
   262 	},
       
   263 
       
   264 	updateImageData : function(img, st) {
       
   265 		var f = document.forms[0];
       
   266 
       
   267 		if (!st) {
       
   268 			f.elements.width.value = img.width;
       
   269 			f.elements.height.value = img.height;
       
   270 		}
       
   271 
       
   272 		this.preloadImg = img;
       
   273 	},
       
   274 
       
   275 	changeAppearance : function() {
       
   276 		var ed = tinyMCEPopup.editor, f = document.forms[0], img = document.getElementById('alignSampleImg');
       
   277 
       
   278 		if (img) {
       
   279 			if (ed.getParam('inline_styles')) {
       
   280 				ed.dom.setAttrib(img, 'style', f.style.value);
       
   281 			} else {
       
   282 				img.align = f.align.value;
       
   283 				img.border = f.border.value;
       
   284 				img.hspace = f.hspace.value;
       
   285 				img.vspace = f.vspace.value;
       
   286 			}
       
   287 		}
       
   288 	},
       
   289 
       
   290 	changeHeight : function() {
       
   291 		var f = document.forms[0], tp, t = this;
       
   292 
       
   293 		if (!f.constrain.checked || !t.preloadImg) {
       
   294 			t.updateStyle();
       
   295 			return;
       
   296 		}
       
   297 
       
   298 		if (f.width.value == "" || f.height.value == "")
       
   299 			return;
       
   300 
       
   301 		tp = (parseInt(f.width.value) / parseInt(t.preloadImg.width)) * t.preloadImg.height;
       
   302 		f.height.value = tp.toFixed(0);
       
   303 		t.updateStyle();
       
   304 	},
       
   305 
       
   306 	changeWidth : function() {
       
   307 		var f = document.forms[0], tp, t = this;
       
   308 
       
   309 		if (!f.constrain.checked || !t.preloadImg) {
       
   310 			t.updateStyle();
       
   311 			return;
       
   312 		}
       
   313 
       
   314 		if (f.width.value == "" || f.height.value == "")
       
   315 			return;
       
   316 
       
   317 		tp = (parseInt(f.height.value) / parseInt(t.preloadImg.height)) * t.preloadImg.width;
       
   318 		f.width.value = tp.toFixed(0);
       
   319 		t.updateStyle();
       
   320 	},
       
   321 
       
   322 	updateStyle : function() {
       
   323 		var dom = tinyMCEPopup.dom, st, v, f = document.forms[0];
       
   324 
       
   325 		if (tinyMCEPopup.editor.settings.inline_styles) {
       
   326 			st = tinyMCEPopup.dom.parseStyle(dom.get('style').value);
       
   327 
       
   328 			// Handle align
       
   329 			v = getSelectValue(f, 'align');
       
   330 			if (v) {
       
   331 				if (v == 'left' || v == 'right') {
       
   332 					st['float'] = v;
       
   333 					delete st['vertical-align'];
       
   334 				} else {
       
   335 					st['vertical-align'] = v;
       
   336 					delete st['float'];
       
   337 				}
       
   338 			} else {
       
   339 				delete st['float'];
       
   340 				delete st['vertical-align'];
       
   341 			}
       
   342 
       
   343 			// Handle border
       
   344 			v = f.border.value;
       
   345 			if (v || v == '0') {
       
   346 				if (v == '0')
       
   347 					st['border'] = '0';
       
   348 				else
       
   349 					st['border'] = v + 'px solid black';
       
   350 			} else
       
   351 				delete st['border'];
       
   352 
       
   353 			// Handle hspace
       
   354 			v = f.hspace.value;
       
   355 			if (v) {
       
   356 				delete st['margin'];
       
   357 				st['margin-left'] = v + 'px';
       
   358 				st['margin-right'] = v + 'px';
       
   359 			} else {
       
   360 				delete st['margin-left'];
       
   361 				delete st['margin-right'];
       
   362 			}
       
   363 
       
   364 			// Handle vspace
       
   365 			v = f.vspace.value;
       
   366 			if (v) {
       
   367 				delete st['margin'];
       
   368 				st['margin-top'] = v + 'px';
       
   369 				st['margin-bottom'] = v + 'px';
       
   370 			} else {
       
   371 				delete st['margin-top'];
       
   372 				delete st['margin-bottom'];
       
   373 			}
       
   374 
       
   375 			// Merge
       
   376 			st = tinyMCEPopup.dom.parseStyle(dom.serializeStyle(st));
       
   377 			dom.get('style').value = dom.serializeStyle(st);
       
   378 		}
       
   379 	},
       
   380 
       
   381 	changeMouseMove : function() {
       
   382 	},
       
   383 
       
   384 	showPreviewImage : function(u, st) {
       
   385 		if (!u) {
       
   386 			tinyMCEPopup.dom.setHTML('prev', '');
       
   387 			return;
       
   388 		}
       
   389 
       
   390 		if (!st && tinyMCEPopup.getParam("advimage_update_dimensions_onchange", true))
       
   391 			this.resetImageData();
       
   392 
       
   393 		u = tinyMCEPopup.editor.documentBaseURI.toAbsolute(u);
       
   394 
       
   395 		if (!st)
       
   396 			tinyMCEPopup.dom.setHTML('prev', '<img id="previewImg" src="' + u + '" border="0" onload="ImageDialog.updateImageData(this);" onerror="ImageDialog.resetImageData();" />');
       
   397 		else
       
   398 			tinyMCEPopup.dom.setHTML('prev', '<img id="previewImg" src="' + u + '" border="0" onload="ImageDialog.updateImageData(this, 1);" />');
       
   399 	}
       
   400 };
       
   401 
       
   402 ImageDialog.preInit();
       
   403 tinyMCEPopup.onInit.add(ImageDialog.init, ImageDialog);