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