twinimg.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. (function() {
  2. CKEDITOR.dialog.add("twinimg", function(g) {
  3. var l = CKEDITOR.plugins.link,
  4. u1 = '',
  5. u2 = '',
  6. //a = this.getDialog(),
  7. h = function(a) {
  8. a && this.setValue(a[this.id] || "")
  9. // console.log(a[this.id]);
  10. },
  11. j = function(a) {
  12. a.advanced || (a.advanced = {});
  13. a.advanced[this.id] = this.getValue() || ""
  14. },
  15. rand = function()
  16. {
  17. var r = "";
  18. var ch = "abcdefghijklmnopqrstuvwxyz0123456789";
  19. for(var i=0; i<10; i++)r += ch.charAt(Math.floor(Math.random() * ch.length));
  20. return r;
  21. },
  22. gal_id = function(b){
  23. // gal = b.id
  24. return gal!="" ? gal : 'x'+rand();
  25. },
  26. e = false,
  27. imageExists = function(url){
  28. icon = CKEDITOR.getUrl(CKEDITOR.plugins.get("twinimg").path + "images/loading.gif");
  29. document.getElementById('cke_twinimg_image_preview').style.backgroundSize = "auto auto";
  30. document.getElementById('cke_twinimg_image_preview').style.backgroundImage = "url('"+icon+"')";
  31. var img = new Image();
  32. img.onload = function(){
  33. e=true;
  34. document.getElementById('cke_twinimg_image_preview').style.backgroundSize = "contain";
  35. document.getElementById('cke_twinimg_image_preview').style.backgroundImage = "url('"+url+"')";
  36. };
  37. img.onerror = function(){e=false;};
  38. img.src = url;
  39. },
  40. imageExists2 = function(url){
  41. icon = CKEDITOR.getUrl(CKEDITOR.plugins.get("twinimg").path + "images/loading.gif");
  42. document.getElementById('cke_twinimg_image_preview2').style.backgroundSize = "auto auto";
  43. document.getElementById('cke_twinimg_image_preview2').style.backgroundImage = "url('"+icon+"')";
  44. var img = new Image();
  45. img.onload = function(){
  46. e=true;
  47. document.getElementById('cke_twinimg_image_preview2').style.backgroundSize = "contain";
  48. document.getElementById('cke_twinimg_image_preview2').style.backgroundImage = "url('"+url+"')";
  49. };
  50. img.onerror = function(){e=false;};
  51. img.src = url;
  52. },
  53. c = g.lang.common,
  54. b = g.lang.link,
  55. d;
  56. return {
  57. title: 'TwinImg',
  58. minWidth: 350,
  59. minHeight: 230,
  60. contents: [{
  61. id: "info",
  62. label: b.info,
  63. title: b.info,
  64. elements: [{
  65. type: "vbox",
  66. id: "urlOptions",
  67. children: [{
  68. type: "hbox",
  69. widths: ["25%", "75%"],
  70. children: [{
  71. type: "text",
  72. id: "url",
  73. label: g.lang.twinimg.url,
  74. required: !0,
  75. onLoad: function() {
  76. this.allowOnChange = !0
  77. },
  78. onKeyUp: function() {
  79. this.allowOnChange = !1;
  80. var b = this.getValue(),
  81. k = /^((javascript:)|[#\/\.\?])/i;
  82. k.test(b);
  83. document.getElementById('cke_twinimg_image_preview').style.backgroundImage = "url('"+b+"')";
  84. imageExists(b);
  85. this.allowOnChange = !0
  86. },
  87. onChange: function() {
  88. if (this.allowOnChange) this.onKeyUp()
  89. },
  90. validate: function() {
  91. var a = this.getDialog();
  92. return !e ? (alert(c.invalidValue), !1) : !g.config.linkJavaScriptLinksAllowed && /javascript\:/.test(this.getValue()) ? (alert(c.invalidValue), !1) : this.getDialog().fakeObj ? !0 : CKEDITOR.dialog.validate.notEmpty(b.noUrl).apply(this);
  93. },
  94. setup: function(a) {
  95. // console.log(a);
  96. this.allowOnChange = !1;
  97. a.url1 && this.setValue(a.url1);
  98. imageExists(a.url1);
  99. this.allowOnChange = !0
  100. },
  101. commit: function(a) {
  102. this.onChange();
  103. a.type = "url";
  104. a.url || (a.url = {});
  105. a.url.protocol = "";
  106. a.url.url = this.getValue();
  107. this.allowOnChange = !1
  108. }
  109. }],
  110. setup: function() {
  111. this.getDialog().getContentElement("info", "linkType") || this.getElement().show()
  112. }
  113. },
  114. {
  115. type: "button",
  116. id: "browse",
  117. hidden: "true",
  118. // filebrowser: "info:url", // amitel fixed
  119. style : 'display:inline-block;margin-top:0px;',
  120. filebrowser :
  121. {
  122. action : 'Browse',
  123. target: 'info:url',
  124. url: g.config.filebrowserImageBrowseUrl || g.config.filebrowserBrowseUrl
  125. },
  126. label: c.browseServer,
  127. }]
  128. },
  129. {
  130. id: "prev",
  131. type: "html",
  132. html : g.lang.twinimg.preview+'<div id="cke_twinimg_image_preview" class="ImagePreview" style="border:2px solid #000; height:100px; text-align:center; background-size:contain; background-position:center center; background-repeat:no-repeat;"></div>'
  133. },
  134. {
  135. type: "vbox",
  136. id: "urlOptions2",
  137. children: [{
  138. type: "hbox",
  139. widths: ["25%", "75%"],
  140. children: [{
  141. type: "text",
  142. id: "url2",
  143. label: g.lang.twinimg.url,
  144. required: !0,
  145. onLoad: function() {
  146. this.allowOnChange = !0
  147. },
  148. onKeyUp: function() {
  149. this.allowOnChange = !1;
  150. var b = this.getValue(),
  151. k = /^((javascript:)|[#\/\.\?])/i;
  152. k.test(b);
  153. document.getElementById('cke_twinimg_image_preview2').style.backgroundImage = "url('"+b+"')";
  154. imageExists2(b);
  155. this.allowOnChange = !0
  156. },
  157. onChange: function() {
  158. if (this.allowOnChange) this.onKeyUp()
  159. },
  160. validate: function() {
  161. var a2 = this.getDialog();
  162. return !e ? (alert(c.invalidValue), !1) : !g.config.linkJavaScriptLinksAllowed && /javascript\:/.test(this.getValue()) ? (alert(c.invalidValue), !1) : this.getDialog().fakeObj ? !0 : CKEDITOR.dialog.validate.notEmpty(b.noUrl).apply(this);
  163. },
  164. setup: function(a) {
  165. this.allowOnChange = !1;
  166. a.url2 && this.setValue(a.url2);
  167. imageExists2(a.url2);
  168. this.allowOnChange = !0
  169. },
  170. commit: function(a) {
  171. this.onChange();
  172. a.type = "url";
  173. a.url || (a.url = {});
  174. a.url.protocol = "";
  175. a.url.url2 = this.getValue();
  176. this.allowOnChange = !1
  177. }
  178. }],
  179. setup: function() {
  180. this.getDialog().getContentElement("info", "linkType") || this.getElement().show()
  181. }
  182. },
  183. {
  184. type: "button",
  185. id: "browse",
  186. hidden: "true",
  187. // filebrowser: "info:url", // amitel fixed
  188. style : 'display:inline-block;margin-top:0px;',
  189. filebrowser :
  190. {
  191. action : 'Browse',
  192. target: 'info:url2',
  193. url: g.config.filebrowserImageBrowseUrl || g.config.filebrowserBrowseUrl
  194. },
  195. label: c.browseServer,
  196. }]
  197. },
  198. {
  199. id: "prev2",
  200. type: "html",
  201. html : g.lang.twinimg.preview+'<div id="cke_twinimg_image_preview2" class="ImagePreview" style="border:2px solid #000; height:100px; text-align:center; background-size:contain; background-position:center center; background-repeat:no-repeat;"></div>'
  202. },
  203. {
  204. type: "text",
  205. label: g.lang.twinimg.gallery,
  206. "default": "",
  207. id: "advRel",
  208. setup: h,
  209. commit: j
  210. },
  211. {
  212. type: "text",
  213. label: g.lang.twinimg.gallery2,
  214. "default": "",
  215. id: "advRel2",
  216. setup: h,
  217. commit: j
  218. },
  219. {
  220. type: "text",
  221. label: g.lang.twinimg.title,
  222. "a[title]": "",
  223. id: "advTitle",
  224. setup: h,
  225. commit: j
  226. }]
  227. }],
  228. onShow: function(data) {
  229. console.log('onShow');
  230. var selection = g.getSelection(),
  231. ranges = selection.getRanges();
  232. el = ranges[0].getTouchedStartNode();
  233. document.getElementById('cke_twinimg_image_preview').style.backgroundSize = "auto auto";
  234. document.getElementById('cke_twinimg_image_preview').style.backgroundSize = "contain";
  235. // document.getElementById('cke_twinimg_image_preview').style.backgroundImage = "url('"+CKEDITOR.getUrl(CKEDITOR.plugins.get("twinimg").path + "images/noimage.png")+"')";
  236. id = $(el).attr('id');
  237. id = (id != undefined) ? id : 'x'+rand();
  238. console.log(id);
  239. var a = {
  240. id: id,
  241. element: el,
  242. url1: el.getAttribute('rel-url1'),
  243. url2: el.getAttribute('rel-url2'),
  244. advRel: el.getAttribute('rel-label1'),
  245. advRel2: el.getAttribute('rel-label2'),
  246. advTitle: el.getAttribute('rel-title')
  247. };
  248. // a = l.parseLinkAttributes(this, el);
  249. console.log(a);
  250. // this._.selectedElement = c;
  251. //console.log(a);
  252. // p = '';//(typeof a.url['protocol'] !== 'undefined') ? a.url.protocol : "";
  253. //console.log(data);
  254. // document.getElementById('cke_twinimg_image_preview').style.backgroundImage = "url('"+p+a.url.url+"')";
  255. //a.url.url = p+a.url.url;
  256. // e = true;
  257. // gal = c.getAttribute('data-twinimg-saved');
  258. gal=id;
  259. this.setupContent(a)
  260. },
  261. onOk: function() {
  262. console.log('ok');
  263. console.log(this.a);
  264. var a = {};
  265. this.commitContent(a);
  266. if (this._.selectedElement) {
  267. var e = this._.selectedElement,
  268. d = e.data("cke-saved-href"),
  269. f = e.getHtml();
  270. e.setAttributes(c.set);
  271. e.removeAttributes(c.removed);
  272. if (d == f) e.setHtml(c.set["data-cke-saved-href"]), b.selectElement(e);
  273. delete this._.selectedElement
  274. // } else b = b.getRanges()[0], b.collapsed && (a = new CKEDITOR.dom.text(c.set["data-cke-saved-href"], g.document), b.insertNode(a), b.selectNodeContents(a)), c = new CKEDITOR.style({
  275. // element: "a",
  276. // attributes: c.set
  277. // }), c.type = CKEDITOR.STYLE_INLINE, c.applyToRange(b, g), b.select()
  278. } else {
  279. /*
  280. b = b.getRanges()[0];
  281. if (b.collapsed) {
  282. a = new CKEDITOR.dom.element('img', g.document);
  283. a.setAttribute('src', c.set["data-cke-saved-href"]);
  284. b.insertNode(a);
  285. b.selectNodeContents(a);
  286. }
  287. c.set['data-cke-saved-href'] = c.set['data-cke-saved-href'].replace( /\/100|\/50/g,'' );
  288. c = new CKEDITOR.style({
  289. element: "a",
  290. attributes: c.set
  291. });
  292. //c.set['data-cke-saved-href'] = 'ooo';
  293. c.type = CKEDITOR.STYLE_INLINE;
  294. // console.log(c);
  295. c.applyToRange(b, g);
  296. b.select();
  297. */
  298. id = gal_id(a);
  299. $.each($("[rel-id='"+id+"']"),
  300. function(e){
  301. console.log(el);
  302. delete el;
  303. }
  304. );
  305. delete $("#"+id);
  306. delete $("#twinimg");
  307. var imgdiv = new CKEDITOR.dom.element( 'div' );
  308. //class="juxtapose" data-animate="true" data-showcredits="true" data-showlabels="true" data-startingposition="35%"
  309. imgdiv.setAttribute('class', 'juxtapose');
  310. imgdiv.setAttribute('id', id );
  311. // imgdiv.setAttribute('style', "width:100%;min-height:200px;background-image: url('"+a.url.url+"');background-size:50%;background-position:50%;background-repeat: no-repeat;" );
  312. imgdiv.setAttribute('data-animate','true');
  313. imgdiv.setAttribute('data-showcredits','false');
  314. var img1 = new CKEDITOR.dom.element( 'img' );
  315. img1.setAttribute('data-label', a.advanced.advRel || "" );
  316. // img1.setAttribute('data-credit', a.advanced.advTitle || "" );
  317. img1.setAttribute('src', a.url.url );
  318. var img2 = new CKEDITOR.dom.element( 'img' );
  319. img2.setAttribute('data-label', a.advanced.advRel2 || "" );
  320. // img2.setAttribute('data-credit', a.advanced.advTitle || "" );
  321. img2.setAttribute('src', a.url.url2 );
  322. var script = new CKEDITOR.dom.element( 'script' );
  323. script.setAttribute('src', 'https://lib.amic.ru/js/juxtapose/juxtapose.min.js' );
  324. script.setAttribute('rel-id', 'twinimg' );
  325. script.setAttribute('defer', 'true' );
  326. var pt = new CKEDITOR.dom.element( 'p' );
  327. pt.setAttribute('class', 'style3');
  328. pt.appendText(a.advanced.advTitle || "");
  329. g.insertElement( imgdiv );
  330. imgdiv.append( img1 );
  331. imgdiv.append( img2 );
  332. g.insertElement( pt );
  333. // g.insertElement( script );
  334. // g.insertElement( script2 );
  335. }
  336. }
  337. }
  338. })
  339. })();