base64image.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * Created by ALL-INKL.COM - Neue Medien Muennich - 04. Feb 2014
  3. * Licensed under the terms of GPL, LGPL and MPL licenses.
  4. */
  5. CKEDITOR.dialog.add("base64imageDialog", function(editor){
  6. var t = null,
  7. selectedImg = null,
  8. orgWidth = null, orgHeight = null,
  9. imgPreview = null, urlCB = null, urlI = null, fileCB = null, imgScal = 1, lock = true;
  10. /* Check File Reader Support */
  11. function fileSupport() {
  12. var r = false, n = null;
  13. try {
  14. if(FileReader) {
  15. var n = document.createElement("input");
  16. if(n && "files" in n) r = true;
  17. }
  18. } catch(e) { r = false; }
  19. n = null;
  20. return r;
  21. }
  22. var fsupport = fileSupport();
  23. /* Load preview image */
  24. function imagePreviewLoad(s) {
  25. /* no preview */
  26. if(typeof(s) != "string" || !s) {
  27. imgPreview.getElement().setHtml("");
  28. return;
  29. }
  30. /* Create image */
  31. var i = new Image();
  32. /* Display loading text in preview element */
  33. imgPreview.getElement().setHtml("Loading...");
  34. /* When image is loaded */
  35. i.onload = function() {
  36. /* Remove preview */
  37. imgPreview.getElement().setHtml("");
  38. /* Set attributes */
  39. if(orgWidth == null || orgHeight == null) {
  40. t.setValueOf("tab-properties", "width", this.width);
  41. t.setValueOf("tab-properties", "height", this.height);
  42. imgScal = 1;
  43. if(this.height > 0 && this.width > 0) imgScal = this.width / this.height;
  44. if(imgScal <= 0) imgScal = 1;
  45. } else {
  46. orgWidth = null;
  47. orgHeight = null;
  48. }
  49. this.id = editor.id+"previewimage";
  50. this.setAttribute("style", "max-width:400px;max-height:100px;");
  51. this.setAttribute("alt", "");
  52. /* Insert preview image */
  53. try {
  54. var p = imgPreview.getElement().$;
  55. if(p) p.appendChild(this);
  56. } catch(e) {}
  57. };
  58. /* Error Function */
  59. i.onerror = function(){ imgPreview.getElement().setHtml(""); };
  60. i.onabort = function(){ imgPreview.getElement().setHtml(""); };
  61. /* Load image */
  62. i.src = s;
  63. }
  64. /* Change input values and preview image */
  65. function imagePreview(src){
  66. /* Remove preview */
  67. imgPreview.getElement().setHtml("");
  68. if(src == "base64") {
  69. /* Disable Checkboxes */
  70. if(urlCB) urlCB.setValue(false, true);
  71. if(fileCB) fileCB.setValue(false, true);
  72. } else if(src == "url") {
  73. /* Ensable Image URL Checkbox */
  74. if(urlCB) urlCB.setValue(true, true);
  75. if(fileCB) fileCB.setValue(false, true);
  76. /* Load preview image */
  77. if(urlI) imagePreviewLoad(urlI.getValue());
  78. } else if(fsupport) {
  79. /* Ensable Image File Checkbox */
  80. if(urlCB) urlCB.setValue(false, true);
  81. if(fileCB) fileCB.setValue(true, true);
  82. /* Read file and load preview */
  83. var fileI = t.getContentElement("tab-source", "file");
  84. var n = null;
  85. try { n = fileI.getInputElement().$; } catch(e) { n = null; }
  86. if(n && "files" in n && n.files && n.files.length > 0 && n.files[0]) {
  87. if("type" in n.files[0] && !n.files[0].type.match("image.*")) return;
  88. if(!FileReader) return;
  89. imgPreview.getElement().setHtml("Loading...");
  90. var fr = new FileReader();
  91. fr.onload = (function(f) { return function(e) {
  92. imgPreview.getElement().setHtml("");
  93. imagePreviewLoad(e.target.result);
  94. }; })(n.files[0]);
  95. fr.onerror = function(){ imgPreview.getElement().setHtml(""); };
  96. fr.onabort = function(){ imgPreview.getElement().setHtml(""); };
  97. fr.readAsDataURL(n.files[0]);
  98. }
  99. }
  100. };
  101. /* Calculate image dimensions */
  102. function getImageDimensions() {
  103. var o = {
  104. "w" : t.getContentElement("tab-properties", "width").getValue(),
  105. "h" : t.getContentElement("tab-properties", "height").getValue(),
  106. "uw" : "px",
  107. "uh" : "px"
  108. };
  109. if(o.w.indexOf("%") >= 0) o.uw = "%";
  110. if(o.h.indexOf("%") >= 0) o.uh = "%";
  111. o.w = parseInt(o.w, 10);
  112. o.h = parseInt(o.h, 10);
  113. if(isNaN(o.w)) o.w = 0;
  114. if(isNaN(o.h)) o.h = 0;
  115. return o;
  116. }
  117. /* Set image dimensions */
  118. function imageDimensions(src) {
  119. var o = getImageDimensions();
  120. var u = "px";
  121. if(src == "width") {
  122. if(o.uw == "%") u = "%";
  123. o.h = Math.round(o.w / imgScal);
  124. } else {
  125. if(o.uh == "%") u = "%";
  126. o.w = Math.round(o.h * imgScal);
  127. }
  128. if(u == "%") {
  129. o.w += "%";
  130. o.h += "%";
  131. }
  132. t.getContentElement("tab-properties", "width").setValue(o.w),
  133. t.getContentElement("tab-properties", "height").setValue(o.h)
  134. }
  135. /* Set integer Value */
  136. function integerValue(elem) {
  137. var v = elem.getValue(), u = "";
  138. if(v.indexOf("%") >= 0) u = "%";
  139. v = parseInt(v, 10);
  140. if(isNaN(v)) v = 0;
  141. elem.setValue(v+u);
  142. }
  143. if(fsupport) {
  144. /* Dialog with file and url image source */
  145. var sourceElements = [
  146. {
  147. type: "hbox",
  148. widths: ["70px"],
  149. children: [
  150. {
  151. type: "checkbox",
  152. id: "urlcheckbox",
  153. style: "margin-top:5px",
  154. label: editor.lang.common.url+":"
  155. },
  156. {
  157. type: "text",
  158. id: "url",
  159. label: "",
  160. onChange: function(){ imagePreview("url"); }
  161. }
  162. ]
  163. },
  164. {
  165. type: "hbox",
  166. widths: ["70px"],
  167. children: [
  168. {
  169. type: "checkbox",
  170. id: "filecheckbox",
  171. style: "margin-top:5px",
  172. label: editor.lang.common.upload+":"
  173. },
  174. {
  175. type: "file",
  176. id: "file",
  177. label: "",
  178. onChange: function(){ imagePreview("file"); }
  179. }
  180. ]
  181. },
  182. {
  183. type: "html",
  184. id: "preview",
  185. html: new CKEDITOR.template("<div style=\"text-align:center;\"></div>").output()
  186. }
  187. ];
  188. } else {
  189. /* Dialog with url image source */
  190. var sourceElements = [
  191. {
  192. type: "text",
  193. id: "url",
  194. label: editor.lang.common.url,
  195. onChange: function(){ imagePreview("url"); }
  196. },
  197. {
  198. type: "html",
  199. id: "preview",
  200. html: new CKEDITOR.template("<div style=\"text-align:center;\"></div>").output()
  201. }
  202. ];
  203. }
  204. /* Dialog */
  205. return {
  206. title: editor.lang.common.image,
  207. minWidth: 450,
  208. minHeight: 180,
  209. onLoad: function(){
  210. if(fsupport) {
  211. /* Get checkboxes */
  212. urlCB = this.getContentElement("tab-source", "urlcheckbox");
  213. fileCB = this.getContentElement("tab-source", "filecheckbox");
  214. /* Checkbox Events */
  215. urlCB.getInputElement().on("click", function(){ imagePreview("url"); });
  216. fileCB.getInputElement().on("click", function(){ imagePreview("file"); });
  217. }
  218. /* Get url input element */
  219. urlI = this.getContentElement("tab-source", "url");
  220. /* Get image preview element */
  221. imgPreview = this.getContentElement("tab-source", "preview");
  222. /* Constrain proportions or not */
  223. this.getContentElement("tab-properties", "lock").getInputElement().on("click", function(){
  224. if(this.getValue()) lock = true; else lock = false;
  225. if(lock) imageDimensions("width");
  226. }, this.getContentElement("tab-properties", "lock"));
  227. /* Change Attributes Events */
  228. this.getContentElement("tab-properties", "width").getInputElement().on("keyup", function(){ if(lock) imageDimensions("width"); });
  229. this.getContentElement("tab-properties", "height").getInputElement().on("keyup", function(){ if(lock) imageDimensions("height"); });
  230. this.getContentElement("tab-properties", "vmargin").getInputElement().on("keyup", function(){ integerValue(this); }, this.getContentElement("tab-properties", "vmargin"));
  231. this.getContentElement("tab-properties", "hmargin").getInputElement().on("keyup", function(){ integerValue(this); }, this.getContentElement("tab-properties", "hmargin"));
  232. this.getContentElement("tab-properties", "border").getInputElement().on("keyup", function(){ integerValue(this); }, this.getContentElement("tab-properties", "border"));
  233. },
  234. onShow: function(){
  235. /* Remove preview */
  236. imgPreview.getElement().setHtml("");
  237. t = this, orgWidth = null, orgHeight = null, imgScal = 1, lock = true;
  238. /* selected image or null */
  239. selectedImg = editor.getSelection();
  240. if(selectedImg) selectedImg = selectedImg.getSelectedElement();
  241. if(!selectedImg || selectedImg.getName() !== "img") selectedImg = null;
  242. /* Set input values */
  243. t.setValueOf("tab-properties", "lock", lock);
  244. t.setValueOf("tab-properties", "vmargin", "0");
  245. t.setValueOf("tab-properties", "hmargin", "0");
  246. t.setValueOf("tab-properties", "border", "0");
  247. t.setValueOf("tab-properties", "align", "none");
  248. if(selectedImg) {
  249. /* Set input values from selected image */
  250. if(typeof(selectedImg.getAttribute("width")) == "string") orgWidth = selectedImg.getAttribute("width");
  251. if(typeof(selectedImg.getAttribute("height")) == "string") orgHeight = selectedImg.getAttribute("height");
  252. if((orgWidth == null || orgHeight == null) && selectedImg.$) {
  253. orgWidth = selectedImg.$.width;
  254. orgHeight = selectedImg.$.height;
  255. }
  256. if(orgWidth != null && orgHeight != null) {
  257. t.setValueOf("tab-properties", "width", orgWidth);
  258. t.setValueOf("tab-properties", "height", orgHeight);
  259. orgWidth = parseInt(orgWidth, 10);
  260. orgHeight = parseInt(orgHeight, 10);
  261. imgScal = 1;
  262. if(!isNaN(orgWidth) && !isNaN(orgHeight) && orgHeight > 0 && orgWidth > 0) imgScal = orgWidth / orgHeight;
  263. if(imgScal <= 0) imgScal = 1;
  264. }
  265. if(typeof(selectedImg.getAttribute("src")) == "string") {
  266. if(selectedImg.getAttribute("src").indexOf("data:") === 0) {
  267. imagePreview("base64");
  268. imagePreviewLoad(selectedImg.getAttribute("src"));
  269. } else {
  270. t.setValueOf("tab-source", "url", selectedImg.getAttribute("src"));
  271. }
  272. }
  273. if(typeof(selectedImg.getAttribute("alt")) == "string") t.setValueOf("tab-properties", "alt", selectedImg.getAttribute("alt"));
  274. if(typeof(selectedImg.getAttribute("hspace")) == "string") t.setValueOf("tab-properties", "hmargin", selectedImg.getAttribute("hspace"));
  275. if(typeof(selectedImg.getAttribute("vspace")) == "string") t.setValueOf("tab-properties", "vmargin", selectedImg.getAttribute("vspace"));
  276. if(typeof(selectedImg.getAttribute("border")) == "string") t.setValueOf("tab-properties", "border", selectedImg.getAttribute("border"));
  277. if(typeof(selectedImg.getAttribute("align")) == "string") {
  278. switch(selectedImg.getAttribute("align")) {
  279. case "top":
  280. case "text-top":
  281. t.setValueOf("tab-properties", "align", "top");
  282. break;
  283. case "baseline":
  284. case "bottom":
  285. case "text-bottom":
  286. t.setValueOf("tab-properties", "align", "bottom");
  287. break;
  288. case "left":
  289. t.setValueOf("tab-properties", "align", "left");
  290. break;
  291. case "right":
  292. t.setValueOf("tab-properties", "align", "right");
  293. break;
  294. }
  295. }
  296. t.selectPage("tab-properties");
  297. }
  298. },
  299. onOk : function(){
  300. /* Get image source */
  301. var src = "";
  302. try { src = CKEDITOR.document.getById(editor.id+"previewimage").$.src; } catch(e) { src = ""; }
  303. if(typeof(src) != "string" || src == null || src === "") return;
  304. /* selected image or new image */
  305. if(selectedImg) var newImg = selectedImg; else var newImg = editor.document.createElement("img");
  306. newImg.setAttribute("src", src);
  307. src = null;
  308. /* Set attributes */
  309. newImg.setAttribute("alt", t.getValueOf("tab-properties", "alt").replace(/^\s+/, "").replace(/\s+$/, ""));
  310. var attr = {
  311. "width" : ["width", "width:#;", "integer", 1],
  312. "height" : ["height", "height:#;", "integer", 1],
  313. "vmargin" : ["vspace", "margin-top:#;margin-bottom:#;", "integer", 0],
  314. "hmargin" : ["hspace", "margin-left:#;margin-right:#;", "integer", 0],
  315. "align" : ["align", ""],
  316. "border" : ["border", "border:# solid black;", "integer", 0]
  317. }, css = [], value, cssvalue, attrvalue, k;
  318. for(k in attr) {
  319. value = t.getValueOf("tab-properties", k);
  320. attrvalue = value;
  321. cssvalue = value;
  322. unit = "px";
  323. if(k == "align") {
  324. switch(value) {
  325. case "top":
  326. case "bottom":
  327. attr[k][1] = "vertical-align:#;";
  328. break;
  329. case "left":
  330. case "right":
  331. attr[k][1] = "float:#;";
  332. break;
  333. default:
  334. value = null;
  335. break;
  336. }
  337. }
  338. if(attr[k][2] == "integer") {
  339. if(value.indexOf("%") >= 0) unit = "%";
  340. value = parseInt(value, 10);
  341. if(isNaN(value)) value = null; else if(value < attr[k][3]) value = null;
  342. if(value != null) {
  343. if(unit == "%") {
  344. attrvalue = value+"%";
  345. cssvalue = value+"%";
  346. } else {
  347. attrvalue = value;
  348. cssvalue = value+"px";
  349. }
  350. }
  351. }
  352. if(value != null) {
  353. newImg.setAttribute(attr[k][0], attrvalue);
  354. css.push(attr[k][1].replace(/#/g, cssvalue));
  355. }
  356. }
  357. if(css.length > 0) newImg.setAttribute("style", css.join(""));
  358. /* Insert new image */
  359. if(!selectedImg) editor.insertElement(newImg);
  360. /* Resize image */
  361. if(editor.plugins.imageresize) editor.plugins.imageresize.resize(editor, newImg, 800, 800);
  362. },
  363. /* Dialog form */
  364. contents: [
  365. {
  366. id: "tab-source",
  367. label: editor.lang.common.generalTab,
  368. elements: sourceElements
  369. },
  370. {
  371. id: "tab-properties",
  372. label: editor.lang.common.advancedTab,
  373. elements: [
  374. {
  375. type: "text",
  376. id: "alt",
  377. label: editor.lang.base64image.alt
  378. },
  379. {
  380. type: 'hbox',
  381. widths: ["15%", "15%", "70%"],
  382. children: [
  383. {
  384. type: "text",
  385. width: "45px",
  386. id: "width",
  387. label: editor.lang.common.width
  388. },
  389. {
  390. type: "text",
  391. width: "45px",
  392. id: "height",
  393. label: editor.lang.common.height
  394. },
  395. {
  396. type: "checkbox",
  397. id: "lock",
  398. label: editor.lang.base64image.lockRatio,
  399. style: "margin-top:18px;"
  400. }
  401. ]
  402. },
  403. {
  404. type: 'hbox',
  405. widths: ["23%", "30%", "30%", "17%"],
  406. style: "margin-top:10px;",
  407. children: [
  408. {
  409. type: "select",
  410. id: "align",
  411. label: editor.lang.common.align,
  412. items: [
  413. [editor.lang.common.notSet, "none"],
  414. [editor.lang.common.alignTop, "top"],
  415. [editor.lang.common.alignBottom, "bottom"],
  416. [editor.lang.common.alignLeft, "left"],
  417. [editor.lang.common.alignRight, "right"]
  418. ]
  419. },
  420. {
  421. type: "text",
  422. width: "45px",
  423. id: "vmargin",
  424. label: editor.lang.base64image.vSpace
  425. },
  426. {
  427. type: "text",
  428. width: "45px",
  429. id: "hmargin",
  430. label: editor.lang.base64image.hSpace
  431. },
  432. {
  433. type: "text",
  434. width: "45px",
  435. id: "border",
  436. label: editor.lang.base64image.border
  437. }
  438. ]
  439. }
  440. ]
  441. }
  442. ]
  443. };
  444. });