base64image.js 20 KB

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