Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / ckeditor / js / plugins / drupalimagecaption / plugin.js
1 /**
2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
5 * @preserve
6 **/
7
8 (function (CKEDITOR) {
9   function findElementByName(element, name) {
10     if (element.name === name) {
11       return element;
12     }
13
14     var found = null;
15     element.forEach(function (el) {
16       if (el.name === name) {
17         found = el;
18
19         return false;
20       }
21     }, CKEDITOR.NODE_ELEMENT);
22     return found;
23   }
24
25   CKEDITOR.plugins.add('drupalimagecaption', {
26     requires: 'drupalimage',
27
28     beforeInit: function beforeInit(editor) {
29       editor.lang.image2.captionPlaceholder = '';
30
31       var placeholderText = editor.config.drupalImageCaption_captionPlaceholderText;
32
33       editor.on('widgetDefinition', function (event) {
34         var widgetDefinition = event.data;
35         if (widgetDefinition.name !== 'image') {
36           return;
37         }
38
39         var captionFilterEnabled = editor.config.drupalImageCaption_captionFilterEnabled;
40         var alignFilterEnabled = editor.config.drupalImageCaption_alignFilterEnabled;
41
42         CKEDITOR.tools.extend(widgetDefinition.features, {
43           caption: {
44             requiredContent: 'img[data-caption]'
45           },
46           align: {
47             requiredContent: 'img[data-align]'
48           }
49         }, true);
50
51         var requiredContent = widgetDefinition.requiredContent.getDefinition();
52         requiredContent.attributes['data-align'] = '';
53         requiredContent.attributes['data-caption'] = '';
54         widgetDefinition.requiredContent = new CKEDITOR.style(requiredContent);
55         widgetDefinition.allowedContent.img.attributes['!data-align'] = true;
56         widgetDefinition.allowedContent.img.attributes['!data-caption'] = true;
57
58         widgetDefinition.editables.caption.allowedContent = 'a[!href]; em strong cite code br';
59
60         var originalDowncast = widgetDefinition.downcast;
61         widgetDefinition.downcast = function (element) {
62           var img = findElementByName(element, 'img');
63           originalDowncast.call(this, img);
64
65           var caption = this.editables.caption;
66           var captionHtml = caption && caption.getData();
67           var attrs = img.attributes;
68
69           if (captionFilterEnabled) {
70             if (captionHtml) {
71               attrs['data-caption'] = captionHtml;
72             }
73           }
74           if (alignFilterEnabled) {
75             if (this.data.align !== 'none') {
76               attrs['data-align'] = this.data.align;
77             }
78           }
79
80           if (img.parent.name === 'a') {
81             return img.parent;
82           }
83
84           return img;
85         };
86
87         var originalUpcast = widgetDefinition.upcast;
88         widgetDefinition.upcast = function (element, data) {
89           if (element.name !== 'img' || !element.attributes['data-entity-type'] || !element.attributes['data-entity-uuid']) {
90             return;
91           }
92
93           if (element.attributes['data-cke-realelement']) {
94             return;
95           }
96
97           element = originalUpcast.call(this, element, data);
98           var attrs = element.attributes;
99
100           if (element.parent.name === 'a') {
101             element = element.parent;
102           }
103
104           var retElement = element;
105           var caption = void 0;
106
107           if (captionFilterEnabled) {
108             caption = attrs['data-caption'];
109             delete attrs['data-caption'];
110           }
111           if (alignFilterEnabled) {
112             data.align = attrs['data-align'];
113             delete attrs['data-align'];
114           }
115           data['data-entity-type'] = attrs['data-entity-type'];
116           delete attrs['data-entity-type'];
117           data['data-entity-uuid'] = attrs['data-entity-uuid'];
118           delete attrs['data-entity-uuid'];
119
120           if (captionFilterEnabled) {
121             if (element.parent.name === 'p' && caption) {
122               var index = element.getIndex();
123               var splitBefore = index > 0;
124               var splitAfter = index + 1 < element.parent.children.length;
125
126               if (splitBefore) {
127                 element.parent.split(index);
128               }
129               index = element.getIndex();
130               if (splitAfter) {
131                 element.parent.split(index + 1);
132               }
133
134               element.parent.replaceWith(element);
135               retElement = element;
136             }
137
138             if (caption) {
139               var figure = new CKEDITOR.htmlParser.element('figure');
140               caption = new CKEDITOR.htmlParser.fragment.fromHtml(caption, 'figcaption');
141
142               caption.attributes['data-placeholder'] = placeholderText;
143
144               element.replaceWith(figure);
145               figure.add(element);
146               figure.add(caption);
147               figure.attributes.class = editor.config.image2_captionedClass;
148               retElement = figure;
149             }
150           }
151
152           if (alignFilterEnabled) {
153             if (data.align === 'center' && (!captionFilterEnabled || !caption)) {
154               var p = new CKEDITOR.htmlParser.element('p');
155               element.replaceWith(p);
156               p.add(element);
157
158               p.addClass(editor.config.image2_alignClasses[1]);
159               retElement = p;
160             }
161           }
162
163           return retElement;
164         };
165
166         CKEDITOR.tools.extend(widgetDefinition._mapDataToDialog, {
167           align: 'data-align',
168           'data-caption': 'data-caption',
169           hasCaption: 'hasCaption'
170         });
171
172         var originalCreateDialogSaveCallback = widgetDefinition._createDialogSaveCallback;
173         widgetDefinition._createDialogSaveCallback = function (editor, widget) {
174           var saveCallback = originalCreateDialogSaveCallback.call(this, editor, widget);
175
176           return function (dialogReturnValues) {
177             dialogReturnValues.attributes.hasCaption = !!dialogReturnValues.attributes.hasCaption;
178
179             var actualWidget = saveCallback(dialogReturnValues);
180
181             if (dialogReturnValues.attributes.hasCaption) {
182               actualWidget.editables.caption.setAttribute('data-placeholder', placeholderText);
183
184               var captionElement = actualWidget.editables.caption.$;
185               if (captionElement.childNodes.length === 1 && captionElement.childNodes.item(0).nodeName === 'BR') {
186                 captionElement.removeChild(captionElement.childNodes.item(0));
187               }
188             }
189           };
190         };
191       }, null, null, 20);
192     },
193     afterInit: function afterInit(editor) {
194       var disableButtonIfOnWidget = function disableButtonIfOnWidget(evt) {
195         var widget = editor.widgets.focused;
196         if (widget && widget.name === 'image') {
197           this.setState(CKEDITOR.TRISTATE_DISABLED);
198           evt.cancel();
199         }
200       };
201
202       if (editor.plugins.justify && !editor.config.drupalImageCaption_alignFilterEnabled) {
203         var cmd = void 0;
204         var commands = ['justifyleft', 'justifycenter', 'justifyright', 'justifyblock'];
205         for (var n = 0; n < commands.length; n++) {
206           cmd = editor.getCommand(commands[n]);
207           cmd.contextSensitive = 1;
208           cmd.on('refresh', disableButtonIfOnWidget, null, null, 4);
209         }
210       }
211     }
212   });
213 })(CKEDITOR);