/media/jscripts/tiny_mce/plugins/advimage/js/image.js

https://bitbucket.org/chuns/cms · JavaScript · 443 lines · 332 code · 97 blank · 14 comment · 97 complexity · 0d2cef4a6fd316c58a85b54a932d8120 MD5 · raw file

  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();
  10. tinyMCEPopup.resizeToInnerSize();
  11. this.fillClassList('class_list');
  12. this.fillFileList('src_list', 'tinyMCEImageList');
  13. this.fillFileList('over_list', 'tinyMCEImageList');
  14. this.fillFileList('out_list', 'tinyMCEImageList');
  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,
  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.close();
  147. },
  148. getAttrib : function(e, at) {
  149. var ed = tinyMCEPopup.editor, dom = ed.dom, v, v2;
  150. if (ed.settings.inline_styles) {
  151. switch (at) {
  152. case 'align':
  153. if (v = dom.getStyle(e, 'float'))
  154. return v;
  155. if (v = dom.getStyle(e, 'vertical-align'))
  156. return v;
  157. break;
  158. case 'hspace':
  159. v = dom.getStyle(e, 'margin-left')
  160. v2 = dom.getStyle(e, 'margin-right');
  161. if (v && v == v2)
  162. return parseInt(v.replace(/[^0-9]/g, ''));
  163. break;
  164. case 'vspace':
  165. v = dom.getStyle(e, 'margin-top')
  166. v2 = dom.getStyle(e, 'margin-bottom');
  167. if (v && v == v2)
  168. return parseInt(v.replace(/[^0-9]/g, ''));
  169. break;
  170. case 'border':
  171. v = 0;
  172. tinymce.each(['top', 'right', 'bottom', 'left'], function(sv) {
  173. sv = dom.getStyle(e, 'border-' + sv + '-width');
  174. // False or not the same as prev
  175. if (!sv || (sv != v && v !== 0)) {
  176. v = 0;
  177. return false;
  178. }
  179. if (sv)
  180. v = sv;
  181. });
  182. if (v)
  183. return parseInt(v.replace(/[^0-9]/g, ''));
  184. break;
  185. }
  186. }
  187. if (v = dom.getAttrib(e, at))
  188. return v;
  189. return '';
  190. },
  191. setSwapImage : function(st) {
  192. var f = document.forms[0];
  193. f.onmousemovecheck.checked = st;
  194. setBrowserDisabled('overbrowser', !st);
  195. setBrowserDisabled('outbrowser', !st);
  196. if (f.over_list)
  197. f.over_list.disabled = !st;
  198. if (f.out_list)
  199. f.out_list.disabled = !st;
  200. f.onmouseoversrc.disabled = !st;
  201. f.onmouseoutsrc.disabled = !st;
  202. },
  203. fillClassList : function(id) {
  204. var dom = tinyMCEPopup.dom, lst = dom.get(id), v, cl;
  205. if (v = tinyMCEPopup.getParam('theme_advanced_styles')) {
  206. cl = [];
  207. tinymce.each(v.split(';'), function(v) {
  208. var p = v.split('=');
  209. cl.push({'title' : p[0], 'class' : p[1]});
  210. });
  211. } else
  212. cl = tinyMCEPopup.editor.dom.getClasses();
  213. if (cl.length > 0) {
  214. lst.options.length = 0;
  215. lst.options[lst.options.length] = new Option(tinyMCEPopup.getLang('not_set'), '');
  216. tinymce.each(cl, function(o) {
  217. lst.options[lst.options.length] = new Option(o.title || o['class'], o['class']);
  218. });
  219. } else
  220. dom.remove(dom.getParent(id, 'tr'));
  221. },
  222. fillFileList : function(id, l) {
  223. var dom = tinyMCEPopup.dom, lst = dom.get(id), v, cl;
  224. l = window[l];
  225. lst.options.length = 0;
  226. if (l && l.length > 0) {
  227. lst.options[lst.options.length] = new Option('', '');
  228. tinymce.each(l, function(o) {
  229. lst.options[lst.options.length] = new Option(o[0], o[1]);
  230. });
  231. } else
  232. dom.remove(dom.getParent(id, 'tr'));
  233. },
  234. resetImageData : function() {
  235. var f = document.forms[0];
  236. f.elements.width.value = f.elements.height.value = '';
  237. },
  238. updateImageData : function(img, st) {
  239. var f = document.forms[0];
  240. if (!st) {
  241. f.elements.width.value = img.width;
  242. f.elements.height.value = img.height;
  243. }
  244. this.preloadImg = img;
  245. },
  246. changeAppearance : function() {
  247. var ed = tinyMCEPopup.editor, f = document.forms[0], img = document.getElementById('alignSampleImg');
  248. if (img) {
  249. if (ed.getParam('inline_styles')) {
  250. ed.dom.setAttrib(img, 'style', f.style.value);
  251. } else {
  252. img.align = f.align.value;
  253. img.border = f.border.value;
  254. img.hspace = f.hspace.value;
  255. img.vspace = f.vspace.value;
  256. }
  257. }
  258. },
  259. changeHeight : function() {
  260. var f = document.forms[0], tp, t = this;
  261. if (!f.constrain.checked || !t.preloadImg) {
  262. return;
  263. }
  264. if (f.width.value == "" || f.height.value == "")
  265. return;
  266. tp = (parseInt(f.width.value) / parseInt(t.preloadImg.width)) * t.preloadImg.height;
  267. f.height.value = tp.toFixed(0);
  268. },
  269. changeWidth : function() {
  270. var f = document.forms[0], tp, t = this;
  271. if (!f.constrain.checked || !t.preloadImg) {
  272. return;
  273. }
  274. if (f.width.value == "" || f.height.value == "")
  275. return;
  276. tp = (parseInt(f.height.value) / parseInt(t.preloadImg.height)) * t.preloadImg.width;
  277. f.width.value = tp.toFixed(0);
  278. },
  279. updateStyle : function(ty) {
  280. var dom = tinyMCEPopup.dom, st, v, f = document.forms[0], img = dom.create('img', {style : dom.get('style').value});
  281. if (tinyMCEPopup.editor.settings.inline_styles) {
  282. // Handle align
  283. if (ty == 'align') {
  284. dom.setStyle(img, 'float', '');
  285. dom.setStyle(img, 'vertical-align', '');
  286. v = getSelectValue(f, 'align');
  287. if (v) {
  288. if (v == 'left' || v == 'right')
  289. dom.setStyle(img, 'float', v);
  290. else
  291. img.style.verticalAlign = v;
  292. }
  293. }
  294. // Handle border
  295. if (ty == 'border') {
  296. dom.setStyle(img, 'border', '');
  297. v = f.border.value;
  298. if (v || v == '0') {
  299. if (v == '0')
  300. img.style.border = '0';
  301. else
  302. img.style.border = v + 'px solid black';
  303. }
  304. }
  305. // Handle hspace
  306. if (ty == 'hspace') {
  307. dom.setStyle(img, 'marginLeft', '');
  308. dom.setStyle(img, 'marginRight', '');
  309. v = f.hspace.value;
  310. if (v) {
  311. img.style.marginLeft = v + 'px';
  312. img.style.marginRight = v + 'px';
  313. }
  314. }
  315. // Handle vspace
  316. if (ty == 'vspace') {
  317. dom.setStyle(img, 'marginTop', '');
  318. dom.setStyle(img, 'marginBottom', '');
  319. v = f.vspace.value;
  320. if (v) {
  321. img.style.marginTop = v + 'px';
  322. img.style.marginBottom = v + 'px';
  323. }
  324. }
  325. // Merge
  326. dom.get('style').value = dom.serializeStyle(dom.parseStyle(img.style.cssText));
  327. }
  328. },
  329. changeMouseMove : function() {
  330. },
  331. showPreviewImage : function(u, st) {
  332. if (!u) {
  333. tinyMCEPopup.dom.setHTML('prev', '');
  334. return;
  335. }
  336. if (!st && tinyMCEPopup.getParam("advimage_update_dimensions_onchange", true))
  337. this.resetImageData();
  338. u = tinyMCEPopup.editor.documentBaseURI.toAbsolute(u);
  339. if (!st)
  340. tinyMCEPopup.dom.setHTML('prev', '<img id="previewImg" src="' + u + '" border="0" onload="ImageDialog.updateImageData(this);" onerror="ImageDialog.resetImageData();" />');
  341. else
  342. tinyMCEPopup.dom.setHTML('prev', '<img id="previewImg" src="' + u + '" border="0" onload="ImageDialog.updateImageData(this, 1);" />');
  343. }
  344. };
  345. ImageDialog.preInit();
  346. tinyMCEPopup.onInit.add(ImageDialog.init, ImageDialog);