/static/scripts/tiny_mce/plugins/advimage/js/image.js

http://n23.googlecode.com/ · JavaScript · 441 lines · 330 code · 97 blank · 14 comment · 97 complexity · 374c1c91d63b44121a0400ce27fa099d 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.editor.windowManager.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[lst.options.length] = new Option(tinyMCEPopup.getLang('not_set'), '');
  215. tinymce.each(cl, function(o) {
  216. lst.options[lst.options.length] = new Option(o.title || o['class'], o['class']);
  217. });
  218. } else
  219. dom.remove(dom.getParent(id, 'tr'));
  220. },
  221. fillFileList : function(id, l) {
  222. var dom = tinyMCEPopup.dom, lst = dom.get(id), v, cl;
  223. l = window[l];
  224. if (l && l.length > 0) {
  225. lst.options[lst.options.length] = new Option('', '');
  226. tinymce.each(l, function(o) {
  227. lst.options[lst.options.length] = new Option(o[0], o[1]);
  228. });
  229. } else
  230. dom.remove(dom.getParent(id, 'tr'));
  231. },
  232. resetImageData : function() {
  233. var f = document.forms[0];
  234. f.elements.width.value = f.elements.height.value = '';
  235. },
  236. updateImageData : function(img, st) {
  237. var f = document.forms[0];
  238. if (!st) {
  239. f.elements.width.value = img.width;
  240. f.elements.height.value = img.height;
  241. }
  242. this.preloadImg = img;
  243. },
  244. changeAppearance : function() {
  245. var ed = tinyMCEPopup.editor, f = document.forms[0], img = document.getElementById('alignSampleImg');
  246. if (img) {
  247. if (ed.getParam('inline_styles')) {
  248. ed.dom.setAttrib(img, 'style', f.style.value);
  249. } else {
  250. img.align = f.align.value;
  251. img.border = f.border.value;
  252. img.hspace = f.hspace.value;
  253. img.vspace = f.vspace.value;
  254. }
  255. }
  256. },
  257. changeHeight : function() {
  258. var f = document.forms[0], tp, t = this;
  259. if (!f.constrain.checked || !t.preloadImg) {
  260. return;
  261. }
  262. if (f.width.value == "" || f.height.value == "")
  263. return;
  264. tp = (parseInt(f.width.value) / parseInt(t.preloadImg.width)) * t.preloadImg.height;
  265. f.height.value = tp.toFixed(0);
  266. },
  267. changeWidth : function() {
  268. var f = document.forms[0], tp, t = this;
  269. if (!f.constrain.checked || !t.preloadImg) {
  270. return;
  271. }
  272. if (f.width.value == "" || f.height.value == "")
  273. return;
  274. tp = (parseInt(f.height.value) / parseInt(t.preloadImg.height)) * t.preloadImg.width;
  275. f.width.value = tp.toFixed(0);
  276. },
  277. updateStyle : function(ty) {
  278. var dom = tinyMCEPopup.dom, st, v, f = document.forms[0], img = dom.create('img', {style : dom.get('style').value});
  279. if (tinyMCEPopup.editor.settings.inline_styles) {
  280. // Handle align
  281. if (ty == 'align') {
  282. dom.setStyle(img, 'float', '');
  283. dom.setStyle(img, 'vertical-align', '');
  284. v = getSelectValue(f, 'align');
  285. if (v) {
  286. if (v == 'left' || v == 'right')
  287. dom.setStyle(img, 'float', v);
  288. else
  289. img.style.verticalAlign = v;
  290. }
  291. }
  292. // Handle border
  293. if (ty == 'border') {
  294. dom.setStyle(img, 'border', '');
  295. v = f.border.value;
  296. if (v || v == '0') {
  297. if (v == '0')
  298. img.style.border = '';
  299. else
  300. img.style.border = v + 'px solid black';
  301. }
  302. }
  303. // Handle hspace
  304. if (ty == 'hspace') {
  305. dom.setStyle(img, 'marginLeft', '');
  306. dom.setStyle(img, 'marginRight', '');
  307. v = f.hspace.value;
  308. if (v) {
  309. img.style.marginLeft = v + 'px';
  310. img.style.marginRight = v + 'px';
  311. }
  312. }
  313. // Handle vspace
  314. if (ty == 'vspace') {
  315. dom.setStyle(img, 'marginTop', '');
  316. dom.setStyle(img, 'marginBottom', '');
  317. v = f.vspace.value;
  318. if (v) {
  319. img.style.marginTop = v + 'px';
  320. img.style.marginBottom = v + 'px';
  321. }
  322. }
  323. // Merge
  324. dom.get('style').value = dom.serializeStyle(dom.parseStyle(img.style.cssText));
  325. }
  326. },
  327. changeMouseMove : function() {
  328. },
  329. showPreviewImage : function(u, st) {
  330. if (!u) {
  331. tinyMCEPopup.dom.setHTML('prev', '');
  332. return;
  333. }
  334. if (!st && tinyMCEPopup.getParam("advimage_update_dimensions_onchange", true))
  335. this.resetImageData();
  336. u = tinyMCEPopup.editor.documentBaseURI.toAbsolute(u);
  337. if (!st)
  338. tinyMCEPopup.dom.setHTML('prev', '<img id="previewImg" src="' + u + '" border="0" onload="ImageDialog.updateImageData(this);" onerror="ImageDialog.resetImageData();" />');
  339. else
  340. tinyMCEPopup.dom.setHTML('prev', '<img id="previewImg" src="' + u + '" border="0" onload="ImageDialog.updateImageData(this, 1);" />');
  341. }
  342. };
  343. ImageDialog.preInit();
  344. tinyMCEPopup.onInit.add(ImageDialog.init, ImageDialog);