Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(CKEDITOR) {
  2.   CKEDITOR.plugins.add('carouselka', {
  3.     requires: 'widget',
  4.     icons: 'carouselka',
  5.     hidpi: true,
  6.     init: function(editor) {
  7.       editor.widgets.add('carouselka', {
  8.         button: 'Добавить галерею',
  9.         template: '<figure class="carouselka"><div class="carouselka-meta">Фотогалерея (0 фото)</div></figure>',
  10.         editables: {},
  11.         allowedContent: {
  12.           div: {
  13.             attributes: 'data-images',
  14.             classes: { 'carouselka-meta': true },
  15.             requiredClasses: { 'carouselka-meta': true }
  16.           },
  17.           figure: {
  18.             classes: { carouselka: true },
  19.             requiredClasses: { carouselka: true }
  20.           }
  21.         },
  22.         requiredContent: 'figure(carouselka)',
  23.         defaults: {
  24.           images: []
  25.         },
  26.         upcast: function(el) {
  27.           return el.name === 'figure' && el.hasClass('carouselka');
  28.         },
  29.         onModalSubmit: function(images) {
  30.           if (!images.length) {
  31.             this.destroySelf();
  32.           } else {
  33.             this.setData({
  34.               images: images
  35.             });
  36.  
  37.             editor.fire('change');
  38.           }
  39.         },
  40.         onModalCancel: function() {
  41.           var images = this.data.images;
  42.  
  43.           if (!images.length) {
  44.             this.destroySelf();
  45.           }
  46.         },
  47.         edit: function() {
  48.           var images = this.data.images;
  49.  
  50.           window.CKEDITOR.EventHub.trigger('ckeditor-carouselka', {
  51.             images: images,
  52.             onSubmit: this.onModalSubmit.bind(this),
  53.             onCancel: this.onModalCancel.bind(this)
  54.           });
  55.         },
  56.         init: function() {
  57.           var content = this.element.$.children[0];
  58.           var images = (content && JSON.parse(content.getAttribute('data-images'))) || [];
  59.  
  60.           var metaElem = new CKEDITOR.htmlParser.element('div', { class: 'carouselka-meta' });
  61.  
  62.           this.element.$.children[0] = metaElem;
  63.  
  64.           this.setData({ images: images });
  65.         },
  66.         data: function() {
  67.           var images = this.data.images;
  68.           var imagesCount = images.length;
  69.  
  70.           this.element.$.children[0].innerText = 'Фотогалерея (' + imagesCount + ' фото)';
  71.         },
  72.         destroySelf: function() {
  73.           var self = this;
  74.           var el = this.element.$;
  75.  
  76.           self.destroy();
  77.  
  78.           setTimeout(function() {
  79.             el.remove();
  80.             editor.fire('change');
  81.           }, 200);
  82.         },
  83.         downcast: function(el) {
  84.           el.children = [
  85.             new CKEDITOR.htmlParser.element('div', {
  86.               class: 'carouselka-meta',
  87.               'data-images': JSON.stringify(this.data.images)
  88.             })
  89.           ];
  90.  
  91.           return el;
  92.         }
  93.       });
  94.     }
  95.   });
  96. })(CKEDITOR);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement