PageRenderTime 28ms CodeModel.GetById 5ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/editors/tiny_mce_3_4_3_1/themes/advanced/js/image.js

#
JavaScript | 247 lines | 189 code | 51 blank | 7 comment | 52 complexity | 1304e7b82cff8a21118f0e857b17e05c 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() {
  9. var f = document.forms[0], ed = tinyMCEPopup.editor;
  10. // Setup browse button
  11. document.getElementById('srcbrowsercontainer').innerHTML = getBrowserHTML('srcbrowser','src','image','theme_advanced_image');
  12. if (isVisible('srcbrowser'))
  13. document.getElementById('src').style.width = '180px';
  14. e = ed.selection.getNode();
  15. this.fillFileList('image_list', tinyMCEPopup.getParam('external_image_list', 'tinyMCEImageList'));
  16. if (e.nodeName == 'IMG') {
  17. f.src.value = ed.dom.getAttrib(e, 'src');
  18. f.alt.value = ed.dom.getAttrib(e, 'alt');
  19. f.border.value = this.getAttrib(e, 'border');
  20. f.vspace.value = this.getAttrib(e, 'vspace');
  21. f.hspace.value = this.getAttrib(e, 'hspace');
  22. f.width.value = ed.dom.getAttrib(e, 'width');
  23. f.height.value = ed.dom.getAttrib(e, 'height');
  24. f.insert.value = ed.getLang('update');
  25. this.styleVal = ed.dom.getAttrib(e, 'style');
  26. selectByValue(f, 'image_list', f.src.value);
  27. selectByValue(f, 'align', this.getAttrib(e, 'align'));
  28. this.updateStyle();
  29. }
  30. },
  31. fillFileList : function(id, l) {
  32. var dom = tinyMCEPopup.dom, lst = dom.get(id), v, cl;
  33. l = typeof(l) === 'function' ? l() : window[l];
  34. if (l && l.length > 0) {
  35. lst.options[lst.options.length] = new Option('', '');
  36. tinymce.each(l, function(o) {
  37. lst.options[lst.options.length] = new Option(o[0], o[1]);
  38. });
  39. } else
  40. dom.remove(dom.getParent(id, 'tr'));
  41. },
  42. update : function() {
  43. var f = document.forms[0], nl = f.elements, ed = tinyMCEPopup.editor, args = {}, el;
  44. tinyMCEPopup.restoreSelection();
  45. if (f.src.value === '') {
  46. if (ed.selection.getNode().nodeName == 'IMG') {
  47. ed.dom.remove(ed.selection.getNode());
  48. ed.execCommand('mceRepaint');
  49. }
  50. tinyMCEPopup.close();
  51. return;
  52. }
  53. if (!ed.settings.inline_styles) {
  54. args = tinymce.extend(args, {
  55. vspace : nl.vspace.value,
  56. hspace : nl.hspace.value,
  57. border : nl.border.value,
  58. align : getSelectValue(f, 'align')
  59. });
  60. } else
  61. args.style = this.styleVal;
  62. tinymce.extend(args, {
  63. src : f.src.value.replace(/ /g, '%20'),
  64. alt : f.alt.value,
  65. width : f.width.value,
  66. height : f.height.value
  67. });
  68. el = ed.selection.getNode();
  69. if (el && el.nodeName == 'IMG') {
  70. ed.dom.setAttribs(el, args);
  71. tinyMCEPopup.editor.execCommand('mceRepaint');
  72. tinyMCEPopup.editor.focus();
  73. } else {
  74. ed.execCommand('mceInsertContent', false, '<img id="__mce_tmp" />', {skip_undo : 1});
  75. ed.dom.setAttribs('__mce_tmp', args);
  76. ed.dom.setAttrib('__mce_tmp', 'id', '');
  77. ed.undoManager.add();
  78. }
  79. tinyMCEPopup.close();
  80. },
  81. updateStyle : function() {
  82. var dom = tinyMCEPopup.dom, st, v, f = document.forms[0];
  83. if (tinyMCEPopup.editor.settings.inline_styles) {
  84. st = tinyMCEPopup.dom.parseStyle(this.styleVal);
  85. // Handle align
  86. v = getSelectValue(f, 'align');
  87. if (v) {
  88. if (v == 'left' || v == 'right') {
  89. st['float'] = v;
  90. delete st['vertical-align'];
  91. } else {
  92. st['vertical-align'] = v;
  93. delete st['float'];
  94. }
  95. } else {
  96. delete st['float'];
  97. delete st['vertical-align'];
  98. }
  99. // Handle border
  100. v = f.border.value;
  101. if (v || v == '0') {
  102. if (v == '0')
  103. st['border'] = '0';
  104. else
  105. st['border'] = v + 'px solid black';
  106. } else
  107. delete st['border'];
  108. // Handle hspace
  109. v = f.hspace.value;
  110. if (v) {
  111. delete st['margin'];
  112. st['margin-left'] = v + 'px';
  113. st['margin-right'] = v + 'px';
  114. } else {
  115. delete st['margin-left'];
  116. delete st['margin-right'];
  117. }
  118. // Handle vspace
  119. v = f.vspace.value;
  120. if (v) {
  121. delete st['margin'];
  122. st['margin-top'] = v + 'px';
  123. st['margin-bottom'] = v + 'px';
  124. } else {
  125. delete st['margin-top'];
  126. delete st['margin-bottom'];
  127. }
  128. // Merge
  129. st = tinyMCEPopup.dom.parseStyle(dom.serializeStyle(st), 'img');
  130. this.styleVal = dom.serializeStyle(st, 'img');
  131. }
  132. },
  133. getAttrib : function(e, at) {
  134. var ed = tinyMCEPopup.editor, dom = ed.dom, v, v2;
  135. if (ed.settings.inline_styles) {
  136. switch (at) {
  137. case 'align':
  138. if (v = dom.getStyle(e, 'float'))
  139. return v;
  140. if (v = dom.getStyle(e, 'vertical-align'))
  141. return v;
  142. break;
  143. case 'hspace':
  144. v = dom.getStyle(e, 'margin-left')
  145. v2 = dom.getStyle(e, 'margin-right');
  146. if (v && v == v2)
  147. return parseInt(v.replace(/[^0-9]/g, ''));
  148. break;
  149. case 'vspace':
  150. v = dom.getStyle(e, 'margin-top')
  151. v2 = dom.getStyle(e, 'margin-bottom');
  152. if (v && v == v2)
  153. return parseInt(v.replace(/[^0-9]/g, ''));
  154. break;
  155. case 'border':
  156. v = 0;
  157. tinymce.each(['top', 'right', 'bottom', 'left'], function(sv) {
  158. sv = dom.getStyle(e, 'border-' + sv + '-width');
  159. // False or not the same as prev
  160. if (!sv || (sv != v && v !== 0)) {
  161. v = 0;
  162. return false;
  163. }
  164. if (sv)
  165. v = sv;
  166. });
  167. if (v)
  168. return parseInt(v.replace(/[^0-9]/g, ''));
  169. break;
  170. }
  171. }
  172. if (v = dom.getAttrib(e, at))
  173. return v;
  174. return '';
  175. },
  176. resetImageData : function() {
  177. var f = document.forms[0];
  178. f.width.value = f.height.value = "";
  179. },
  180. updateImageData : function() {
  181. var f = document.forms[0], t = ImageDialog;
  182. if (f.width.value == "")
  183. f.width.value = t.preloadImg.width;
  184. if (f.height.value == "")
  185. f.height.value = t.preloadImg.height;
  186. },
  187. getImageData : function() {
  188. var f = document.forms[0];
  189. this.preloadImg = new Image();
  190. this.preloadImg.onload = this.updateImageData;
  191. this.preloadImg.onerror = this.resetImageData;
  192. this.preloadImg.src = tinyMCEPopup.editor.documentBaseURI.toAbsolute(f.src.value);
  193. }
  194. };
  195. ImageDialog.preInit();
  196. tinyMCEPopup.onInit.add(ImageDialog.init, ImageDialog);