PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/editors/tiny_mce_3_4_3_1/plugins/advimage/js/image.js

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