Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/ckeditor/js/views/ControllerView.js @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
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, Backbone, CKEDITOR, _) { | |
9 Drupal.ckeditor.ControllerView = Backbone.View.extend({ | |
10 events: {}, | |
11 | |
12 initialize: function initialize() { | |
13 this.getCKEditorFeatures(this.model.get('hiddenEditorConfig'), this.disableFeaturesDisallowedByFilters.bind(this)); | |
14 | |
15 this.model.listenTo(this.model, 'change:activeEditorConfig', this.model.sync); | |
16 this.listenTo(this.model, 'change:isDirty', this.parseEditorDOM); | |
17 }, | |
18 parseEditorDOM: function parseEditorDOM(model, isDirty, options) { | |
19 if (isDirty) { | |
20 var currentConfig = this.model.get('activeEditorConfig'); | |
21 | |
22 var rows = []; | |
23 this.$el.find('.ckeditor-active-toolbar-configuration').children('.ckeditor-row').each(function () { | |
24 var groups = []; | |
25 | |
26 $(this).find('.ckeditor-toolbar-group').each(function () { | |
27 var $group = $(this); | |
28 var $buttons = $group.find('.ckeditor-button'); | |
29 if ($buttons.length) { | |
30 var group = { | |
31 name: $group.attr('data-drupal-ckeditor-toolbar-group-name'), | |
32 items: [] | |
33 }; | |
34 $group.find('.ckeditor-button, .ckeditor-multiple-button').each(function () { | |
35 group.items.push($(this).attr('data-drupal-ckeditor-button-name')); | |
36 }); | |
37 groups.push(group); | |
38 } | |
39 }); | |
40 if (groups.length) { | |
41 rows.push(groups); | |
42 } | |
43 }); | |
44 this.model.set('activeEditorConfig', rows); | |
45 | |
46 this.model.set('isDirty', false); | |
47 | |
48 if (options.broadcast !== false) { | |
49 var prev = this.getButtonList(currentConfig); | |
50 var next = this.getButtonList(rows); | |
51 if (prev.length !== next.length) { | |
52 this.$el.find('.ckeditor-toolbar-active').trigger('CKEditorToolbarChanged', [prev.length < next.length ? 'added' : 'removed', _.difference(_.union(prev, next), _.intersection(prev, next))[0]]); | |
53 } | |
54 } | |
55 } | |
56 }, | |
57 getCKEditorFeatures: function getCKEditorFeatures(CKEditorConfig, callback) { | |
58 var getProperties = function getProperties(CKEPropertiesList) { | |
59 return _.isObject(CKEPropertiesList) ? _.keys(CKEPropertiesList) : []; | |
60 }; | |
61 | |
62 var convertCKERulesToEditorFeature = function convertCKERulesToEditorFeature(feature, CKEFeatureRules) { | |
63 for (var i = 0; i < CKEFeatureRules.length; i++) { | |
64 var CKERule = CKEFeatureRules[i]; | |
65 var rule = new Drupal.EditorFeatureHTMLRule(); | |
66 | |
67 var tags = getProperties(CKERule.elements); | |
68 rule.required.tags = CKERule.propertiesOnly ? [] : tags; | |
69 rule.allowed.tags = tags; | |
70 | |
71 rule.required.attributes = getProperties(CKERule.requiredAttributes); | |
72 rule.allowed.attributes = getProperties(CKERule.attributes); | |
73 | |
74 rule.required.styles = getProperties(CKERule.requiredStyles); | |
75 rule.allowed.styles = getProperties(CKERule.styles); | |
76 | |
77 rule.required.classes = getProperties(CKERule.requiredClasses); | |
78 rule.allowed.classes = getProperties(CKERule.classes); | |
79 | |
80 rule.raw = CKERule; | |
81 | |
82 feature.addHTMLRule(rule); | |
83 } | |
84 }; | |
85 | |
86 var hiddenCKEditorID = 'ckeditor-hidden'; | |
87 if (CKEDITOR.instances[hiddenCKEditorID]) { | |
88 CKEDITOR.instances[hiddenCKEditorID].destroy(true); | |
89 } | |
90 | |
91 var hiddenEditorConfig = this.model.get('hiddenEditorConfig'); | |
92 if (hiddenEditorConfig.drupalExternalPlugins) { | |
93 var externalPlugins = hiddenEditorConfig.drupalExternalPlugins; | |
94 Object.keys(externalPlugins || {}).forEach(function (pluginName) { | |
95 CKEDITOR.plugins.addExternal(pluginName, externalPlugins[pluginName], ''); | |
96 }); | |
97 } | |
98 CKEDITOR.inline($('#' + hiddenCKEditorID).get(0), CKEditorConfig); | |
99 | |
100 CKEDITOR.once('instanceReady', function (e) { | |
101 if (e.editor.name === hiddenCKEditorID) { | |
102 var CKEFeatureRulesMap = {}; | |
103 var rules = e.editor.filter.allowedContent; | |
104 var rule = void 0; | |
105 var name = void 0; | |
106 for (var i = 0; i < rules.length; i++) { | |
107 rule = rules[i]; | |
108 name = rule.featureName || ':('; | |
109 if (!CKEFeatureRulesMap[name]) { | |
110 CKEFeatureRulesMap[name] = []; | |
111 } | |
112 CKEFeatureRulesMap[name].push(rule); | |
113 } | |
114 | |
115 var features = {}; | |
116 var buttonsToFeatures = {}; | |
117 Object.keys(CKEFeatureRulesMap).forEach(function (featureName) { | |
118 var feature = new Drupal.EditorFeature(featureName); | |
119 convertCKERulesToEditorFeature(feature, CKEFeatureRulesMap[featureName]); | |
120 features[featureName] = feature; | |
121 var command = e.editor.getCommand(featureName); | |
122 if (command) { | |
123 buttonsToFeatures[command.uiItems[0].name] = featureName; | |
124 } | |
125 }); | |
126 | |
127 callback(features, buttonsToFeatures); | |
128 } | |
129 }); | |
130 }, | |
131 getFeatureForButton: function getFeatureForButton(button) { | |
132 if (button === '-') { | |
133 return false; | |
134 } | |
135 | |
136 var featureName = this.model.get('buttonsToFeatures')[button.toLowerCase()]; | |
137 | |
138 if (!featureName) { | |
139 featureName = button.toLowerCase(); | |
140 } | |
141 var featuresMetadata = this.model.get('featuresMetadata'); | |
142 if (!featuresMetadata[featureName]) { | |
143 featuresMetadata[featureName] = new Drupal.EditorFeature(featureName); | |
144 this.model.set('featuresMetadata', featuresMetadata); | |
145 } | |
146 return featuresMetadata[featureName]; | |
147 }, | |
148 disableFeaturesDisallowedByFilters: function disableFeaturesDisallowedByFilters(features, buttonsToFeatures) { | |
149 this.model.set('featuresMetadata', features); | |
150 | |
151 this.model.set('buttonsToFeatures', buttonsToFeatures); | |
152 | |
153 this.broadcastConfigurationChanges(this.$el); | |
154 | |
155 var existingButtons = []; | |
156 | |
157 var buttonGroups = _.flatten(this.model.get('activeEditorConfig')); | |
158 for (var i = 0; i < buttonGroups.length; i++) { | |
159 var buttons = buttonGroups[i].items; | |
160 for (var k = 0; k < buttons.length; k++) { | |
161 existingButtons.push(buttons[k]); | |
162 } | |
163 } | |
164 | |
165 existingButtons = _.unique(existingButtons); | |
166 | |
167 for (var n = 0; n < existingButtons.length; n++) { | |
168 var button = existingButtons[n]; | |
169 var feature = this.getFeatureForButton(button); | |
170 | |
171 if (feature === false) { | |
172 continue; | |
173 } | |
174 | |
175 if (Drupal.editorConfiguration.featureIsAllowedByFilters(feature)) { | |
176 this.$el.find('.ckeditor-toolbar-active').trigger('CKEditorToolbarChanged', ['added', existingButtons[n]]); | |
177 } else { | |
178 $('.ckeditor-toolbar-active li[data-drupal-ckeditor-button-name="' + button + '"]').detach().appendTo('.ckeditor-toolbar-disabled > .ckeditor-toolbar-available > ul'); | |
179 | |
180 this.model.set({ isDirty: true }, { broadcast: false }); | |
181 } | |
182 } | |
183 }, | |
184 broadcastConfigurationChanges: function broadcastConfigurationChanges($ckeditorToolbar) { | |
185 var view = this; | |
186 var hiddenEditorConfig = this.model.get('hiddenEditorConfig'); | |
187 var getFeatureForButton = this.getFeatureForButton.bind(this); | |
188 var getCKEditorFeatures = this.getCKEditorFeatures.bind(this); | |
189 $ckeditorToolbar.find('.ckeditor-toolbar-active').on('CKEditorToolbarChanged.ckeditorAdmin', function (event, action, button) { | |
190 var feature = getFeatureForButton(button); | |
191 | |
192 if (feature === false) { | |
193 return; | |
194 } | |
195 | |
196 var configEvent = action === 'added' ? 'addedFeature' : 'removedFeature'; | |
197 Drupal.editorConfiguration[configEvent](feature); | |
198 }).on('CKEditorPluginSettingsChanged.ckeditorAdmin', function (event, settingsChanges) { | |
199 Object.keys(settingsChanges || {}).forEach(function (key) { | |
200 hiddenEditorConfig[key] = settingsChanges[key]; | |
201 }); | |
202 | |
203 getCKEditorFeatures(hiddenEditorConfig, function (features) { | |
204 var featuresMetadata = view.model.get('featuresMetadata'); | |
205 Object.keys(features || {}).forEach(function (name) { | |
206 var feature = features[name]; | |
207 if (featuresMetadata.hasOwnProperty(name) && !_.isEqual(featuresMetadata[name], feature)) { | |
208 Drupal.editorConfiguration.modifiedFeature(feature); | |
209 } | |
210 }); | |
211 | |
212 view.model.set('featuresMetadata', features); | |
213 }); | |
214 }); | |
215 }, | |
216 getButtonList: function getButtonList(config) { | |
217 var buttons = []; | |
218 | |
219 config = _.flatten(config); | |
220 | |
221 config.forEach(function (group) { | |
222 group.items.forEach(function (button) { | |
223 buttons.push(button); | |
224 }); | |
225 }); | |
226 | |
227 return _.without(buttons, '-'); | |
228 } | |
229 }); | |
230 })(jQuery, Drupal, Backbone, CKEDITOR, _); |