Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / ckeditor / js / plugins / drupallink / 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 ($, Drupal, drupalSettings, CKEDITOR) {
9   function parseAttributes(editor, element) {
10     var parsedAttributes = {};
11
12     var domElement = element.$;
13     var attribute = void 0;
14     var attributeName = void 0;
15     for (var attrIndex = 0; attrIndex < domElement.attributes.length; attrIndex++) {
16       attribute = domElement.attributes.item(attrIndex);
17       attributeName = attribute.nodeName.toLowerCase();
18
19       if (attributeName.indexOf('data-cke-') === 0) {
20         continue;
21       }
22
23       parsedAttributes[attributeName] = element.data('cke-saved-' + attributeName) || attribute.nodeValue;
24     }
25
26     if (parsedAttributes.class) {
27       parsedAttributes.class = CKEDITOR.tools.trim(parsedAttributes.class.replace(/cke_\S+/, ''));
28     }
29
30     return parsedAttributes;
31   }
32
33   function getAttributes(editor, data) {
34     var set = {};
35     Object.keys(data || {}).forEach(function (attributeName) {
36       set[attributeName] = data[attributeName];
37     });
38
39     set['data-cke-saved-href'] = set.href;
40
41     var removed = {};
42     Object.keys(set).forEach(function (s) {
43       delete removed[s];
44     });
45
46     return {
47       set: set,
48       removed: CKEDITOR.tools.objectKeys(removed)
49     };
50   }
51
52   function getSelectedLink(editor) {
53     var selection = editor.getSelection();
54     var selectedElement = selection.getSelectedElement();
55     if (selectedElement && selectedElement.is('a')) {
56       return selectedElement;
57     }
58
59     var range = selection.getRanges(true)[0];
60
61     if (range) {
62       range.shrink(CKEDITOR.SHRINK_TEXT);
63       return editor.elementPath(range.getCommonAncestor()).contains('a', 1);
64     }
65     return null;
66   }
67
68   CKEDITOR.plugins.add('drupallink', {
69     icons: 'drupallink,drupalunlink',
70     hidpi: true,
71
72     init: function init(editor) {
73       editor.addCommand('drupallink', {
74         allowedContent: {
75           a: {
76             attributes: {
77               '!href': true
78             },
79             classes: {}
80           }
81         },
82         requiredContent: new CKEDITOR.style({
83           element: 'a',
84           attributes: {
85             href: ''
86           }
87         }),
88         modes: { wysiwyg: 1 },
89         canUndo: true,
90         exec: function exec(editor) {
91           var drupalImageUtils = CKEDITOR.plugins.drupalimage;
92           var focusedImageWidget = drupalImageUtils && drupalImageUtils.getFocusedWidget(editor);
93           var linkElement = getSelectedLink(editor);
94
95           var existingValues = {};
96           if (linkElement && linkElement.$) {
97             existingValues = parseAttributes(editor, linkElement);
98           } else if (focusedImageWidget && focusedImageWidget.data.link) {
99               existingValues = CKEDITOR.tools.clone(focusedImageWidget.data.link);
100             }
101
102           var saveCallback = function saveCallback(returnValues) {
103             if (focusedImageWidget) {
104               focusedImageWidget.setData('link', CKEDITOR.tools.extend(returnValues.attributes, focusedImageWidget.data.link));
105               editor.fire('saveSnapshot');
106               return;
107             }
108
109             editor.fire('saveSnapshot');
110
111             if (!linkElement && returnValues.attributes.href) {
112               var selection = editor.getSelection();
113               var range = selection.getRanges(1)[0];
114
115               if (range.collapsed) {
116                 var text = new CKEDITOR.dom.text(returnValues.attributes.href.replace(/^mailto:/, ''), editor.document);
117                 range.insertNode(text);
118                 range.selectNodeContents(text);
119               }
120
121               var style = new CKEDITOR.style({
122                 element: 'a',
123                 attributes: returnValues.attributes
124               });
125               style.type = CKEDITOR.STYLE_INLINE;
126               style.applyToRange(range);
127               range.select();
128
129               linkElement = getSelectedLink(editor);
130             } else if (linkElement) {
131                 Object.keys(returnValues.attributes || {}).forEach(function (attrName) {
132                   if (returnValues.attributes[attrName].length > 0) {
133                     var value = returnValues.attributes[attrName];
134                     linkElement.data('cke-saved-' + attrName, value);
135                     linkElement.setAttribute(attrName, value);
136                   } else {
137                       linkElement.removeAttribute(attrName);
138                     }
139                 });
140               }
141
142             editor.fire('saveSnapshot');
143           };
144
145           var dialogSettings = {
146             title: linkElement ? editor.config.drupalLink_dialogTitleEdit : editor.config.drupalLink_dialogTitleAdd,
147             dialogClass: 'editor-link-dialog'
148           };
149
150           Drupal.ckeditor.openDialog(editor, Drupal.url('editor/dialog/link/' + editor.config.drupal.format), existingValues, saveCallback, dialogSettings);
151         }
152       });
153       editor.addCommand('drupalunlink', {
154         contextSensitive: 1,
155         startDisabled: 1,
156         requiredContent: new CKEDITOR.style({
157           element: 'a',
158           attributes: {
159             href: ''
160           }
161         }),
162         exec: function exec(editor) {
163           var style = new CKEDITOR.style({
164             element: 'a',
165             type: CKEDITOR.STYLE_INLINE,
166             alwaysRemoveElement: 1
167           });
168           editor.removeStyle(style);
169         },
170         refresh: function refresh(editor, path) {
171           var element = path.lastElement && path.lastElement.getAscendant('a', true);
172           if (element && element.getName() === 'a' && element.getAttribute('href') && element.getChildCount()) {
173             this.setState(CKEDITOR.TRISTATE_OFF);
174           } else {
175             this.setState(CKEDITOR.TRISTATE_DISABLED);
176           }
177         }
178       });
179
180       editor.setKeystroke(CKEDITOR.CTRL + 75, 'drupallink');
181
182       if (editor.ui.addButton) {
183         editor.ui.addButton('DrupalLink', {
184           label: Drupal.t('Link'),
185           command: 'drupallink'
186         });
187         editor.ui.addButton('DrupalUnlink', {
188           label: Drupal.t('Unlink'),
189           command: 'drupalunlink'
190         });
191       }
192
193       editor.on('doubleclick', function (evt) {
194         var element = getSelectedLink(editor) || evt.data.element;
195
196         if (!element.isReadOnly()) {
197           if (element.is('a')) {
198             editor.getSelection().selectElement(element);
199             editor.getCommand('drupallink').exec();
200           }
201         }
202       });
203
204       if (editor.addMenuItems) {
205         editor.addMenuItems({
206           link: {
207             label: Drupal.t('Edit Link'),
208             command: 'drupallink',
209             group: 'link',
210             order: 1
211           },
212
213           unlink: {
214             label: Drupal.t('Unlink'),
215             command: 'drupalunlink',
216             group: 'link',
217             order: 5
218           }
219         });
220       }
221
222       if (editor.contextMenu) {
223         editor.contextMenu.addListener(function (element, selection) {
224           if (!element || element.isReadOnly()) {
225             return null;
226           }
227           var anchor = getSelectedLink(editor);
228           if (!anchor) {
229             return null;
230           }
231
232           var menu = {};
233           if (anchor.getAttribute('href') && anchor.getChildCount()) {
234             menu = {
235               link: CKEDITOR.TRISTATE_OFF,
236               unlink: CKEDITOR.TRISTATE_OFF
237             };
238           }
239           return menu;
240         });
241       }
242     }
243   });
244
245   CKEDITOR.plugins.drupallink = {
246     parseLinkAttributes: parseAttributes,
247     getLinkAttributes: getAttributes
248   };
249 })(jQuery, Drupal, drupalSettings, CKEDITOR);