Chris@0
|
1 /**
|
Chris@0
|
2 * @file
|
Chris@0
|
3 * A Backbone View that provides an interactive toolbar (1 per in-place editor).
|
Chris@0
|
4 */
|
Chris@0
|
5
|
Chris@17
|
6 (function($, _, Backbone, Drupal) {
|
Chris@17
|
7 Drupal.quickedit.FieldToolbarView = Backbone.View.extend(
|
Chris@17
|
8 /** @lends Drupal.quickedit.FieldToolbarView# */ {
|
Chris@17
|
9 /**
|
Chris@17
|
10 * The edited element, as indicated by EditorView.getEditedElement.
|
Chris@17
|
11 *
|
Chris@17
|
12 * @type {jQuery}
|
Chris@17
|
13 */
|
Chris@17
|
14 $editedElement: null,
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@17
|
17 * A reference to the in-place editor.
|
Chris@17
|
18 *
|
Chris@17
|
19 * @type {Drupal.quickedit.EditorView}
|
Chris@0
|
20 */
|
Chris@17
|
21 editorView: null,
|
Chris@0
|
22
|
Chris@17
|
23 /**
|
Chris@17
|
24 * @type {string}
|
Chris@17
|
25 */
|
Chris@17
|
26 _id: null,
|
Chris@0
|
27
|
Chris@17
|
28 /**
|
Chris@17
|
29 * @constructs
|
Chris@17
|
30 *
|
Chris@17
|
31 * @augments Backbone.View
|
Chris@17
|
32 *
|
Chris@17
|
33 * @param {object} options
|
Chris@17
|
34 * Options object to construct the field toolbar.
|
Chris@17
|
35 * @param {jQuery} options.$editedElement
|
Chris@17
|
36 * The element being edited.
|
Chris@17
|
37 * @param {Drupal.quickedit.EditorView} options.editorView
|
Chris@17
|
38 * The EditorView the toolbar belongs to.
|
Chris@17
|
39 */
|
Chris@17
|
40 initialize(options) {
|
Chris@17
|
41 this.$editedElement = options.$editedElement;
|
Chris@17
|
42 this.editorView = options.editorView;
|
Chris@17
|
43
|
Chris@17
|
44 /**
|
Chris@17
|
45 * @type {jQuery}
|
Chris@17
|
46 */
|
Chris@17
|
47 this.$root = this.$el;
|
Chris@17
|
48
|
Chris@17
|
49 // Generate a DOM-compatible ID for the form container DOM element.
|
Chris@17
|
50 this._id = `quickedit-toolbar-for-${this.model.id.replace(
|
Chris@17
|
51 /[/[\]]/g,
|
Chris@17
|
52 '_',
|
Chris@17
|
53 )}`;
|
Chris@17
|
54
|
Chris@17
|
55 this.listenTo(this.model, 'change:state', this.stateChange);
|
Chris@17
|
56 },
|
Chris@17
|
57
|
Chris@17
|
58 /**
|
Chris@17
|
59 * @inheritdoc
|
Chris@17
|
60 *
|
Chris@17
|
61 * @return {Drupal.quickedit.FieldToolbarView}
|
Chris@17
|
62 * The current FieldToolbarView.
|
Chris@17
|
63 */
|
Chris@17
|
64 render() {
|
Chris@17
|
65 // Render toolbar and set it as the view's element.
|
Chris@17
|
66 this.setElement(
|
Chris@17
|
67 $(
|
Chris@17
|
68 Drupal.theme('quickeditFieldToolbar', {
|
Chris@17
|
69 id: this._id,
|
Chris@17
|
70 }),
|
Chris@17
|
71 ),
|
Chris@17
|
72 );
|
Chris@17
|
73
|
Chris@17
|
74 // Attach to the field toolbar $root element in the entity toolbar.
|
Chris@17
|
75 this.$el.prependTo(this.$root);
|
Chris@17
|
76
|
Chris@17
|
77 return this;
|
Chris@17
|
78 },
|
Chris@17
|
79
|
Chris@17
|
80 /**
|
Chris@17
|
81 * Determines the actions to take given a change of state.
|
Chris@17
|
82 *
|
Chris@17
|
83 * @param {Drupal.quickedit.FieldModel} model
|
Chris@17
|
84 * The quickedit FieldModel
|
Chris@17
|
85 * @param {string} state
|
Chris@17
|
86 * The state of the associated field. One of
|
Chris@17
|
87 * {@link Drupal.quickedit.FieldModel.states}.
|
Chris@17
|
88 */
|
Chris@17
|
89 stateChange(model, state) {
|
Chris@17
|
90 const from = model.previous('state');
|
Chris@17
|
91 const to = state;
|
Chris@17
|
92 switch (to) {
|
Chris@17
|
93 case 'inactive':
|
Chris@17
|
94 break;
|
Chris@17
|
95
|
Chris@17
|
96 case 'candidate':
|
Chris@17
|
97 // Remove the view's existing element if we went to the 'activating'
|
Chris@17
|
98 // state or later, because it will be recreated. Not doing this would
|
Chris@17
|
99 // result in memory leaks.
|
Chris@17
|
100 if (from !== 'inactive' && from !== 'highlighted') {
|
Chris@17
|
101 this.$el.remove();
|
Chris@17
|
102 this.setElement();
|
Chris@17
|
103 }
|
Chris@17
|
104 break;
|
Chris@17
|
105
|
Chris@17
|
106 case 'highlighted':
|
Chris@17
|
107 break;
|
Chris@17
|
108
|
Chris@17
|
109 case 'activating':
|
Chris@17
|
110 this.render();
|
Chris@17
|
111
|
Chris@17
|
112 if (this.editorView.getQuickEditUISettings().fullWidthToolbar) {
|
Chris@17
|
113 this.$el.addClass('quickedit-toolbar-fullwidth');
|
Chris@17
|
114 }
|
Chris@17
|
115
|
Chris@17
|
116 if (this.editorView.getQuickEditUISettings().unifiedToolbar) {
|
Chris@17
|
117 this.insertWYSIWYGToolGroups();
|
Chris@17
|
118 }
|
Chris@17
|
119 break;
|
Chris@17
|
120
|
Chris@17
|
121 case 'active':
|
Chris@17
|
122 break;
|
Chris@17
|
123
|
Chris@17
|
124 case 'changed':
|
Chris@17
|
125 break;
|
Chris@17
|
126
|
Chris@17
|
127 case 'saving':
|
Chris@17
|
128 break;
|
Chris@17
|
129
|
Chris@17
|
130 case 'saved':
|
Chris@17
|
131 break;
|
Chris@17
|
132
|
Chris@17
|
133 case 'invalid':
|
Chris@17
|
134 break;
|
Chris@17
|
135 }
|
Chris@17
|
136 },
|
Chris@17
|
137
|
Chris@17
|
138 /**
|
Chris@17
|
139 * Insert WYSIWYG markup into the associated toolbar.
|
Chris@17
|
140 */
|
Chris@17
|
141 insertWYSIWYGToolGroups() {
|
Chris@17
|
142 this.$el
|
Chris@17
|
143 .append(
|
Chris@17
|
144 Drupal.theme('quickeditToolgroup', {
|
Chris@17
|
145 id: this.getFloatedWysiwygToolgroupId(),
|
Chris@17
|
146 classes: [
|
Chris@17
|
147 'wysiwyg-floated',
|
Chris@17
|
148 'quickedit-animate-slow',
|
Chris@17
|
149 'quickedit-animate-invisible',
|
Chris@17
|
150 'quickedit-animate-delay-veryfast',
|
Chris@17
|
151 ],
|
Chris@17
|
152 buttons: [],
|
Chris@17
|
153 }),
|
Chris@17
|
154 )
|
Chris@17
|
155 .append(
|
Chris@17
|
156 Drupal.theme('quickeditToolgroup', {
|
Chris@17
|
157 id: this.getMainWysiwygToolgroupId(),
|
Chris@17
|
158 classes: [
|
Chris@17
|
159 'wysiwyg-main',
|
Chris@17
|
160 'quickedit-animate-slow',
|
Chris@17
|
161 'quickedit-animate-invisible',
|
Chris@17
|
162 'quickedit-animate-delay-veryfast',
|
Chris@17
|
163 ],
|
Chris@17
|
164 buttons: [],
|
Chris@17
|
165 }),
|
Chris@17
|
166 );
|
Chris@17
|
167
|
Chris@17
|
168 // Animate the toolgroups into visibility.
|
Chris@17
|
169 this.show('wysiwyg-floated');
|
Chris@17
|
170 this.show('wysiwyg-main');
|
Chris@17
|
171 },
|
Chris@17
|
172
|
Chris@17
|
173 /**
|
Chris@17
|
174 * Retrieves the ID for this toolbar's container.
|
Chris@17
|
175 *
|
Chris@17
|
176 * Only used to make sane hovering behavior possible.
|
Chris@17
|
177 *
|
Chris@17
|
178 * @return {string}
|
Chris@17
|
179 * A string that can be used as the ID for this toolbar's container.
|
Chris@17
|
180 */
|
Chris@17
|
181 getId() {
|
Chris@17
|
182 return `quickedit-toolbar-for-${this._id}`;
|
Chris@17
|
183 },
|
Chris@17
|
184
|
Chris@17
|
185 /**
|
Chris@17
|
186 * Retrieves the ID for this toolbar's floating WYSIWYG toolgroup.
|
Chris@17
|
187 *
|
Chris@17
|
188 * Used to provide an abstraction for any WYSIWYG editor to plug in.
|
Chris@17
|
189 *
|
Chris@17
|
190 * @return {string}
|
Chris@17
|
191 * A string that can be used as the ID.
|
Chris@17
|
192 */
|
Chris@17
|
193 getFloatedWysiwygToolgroupId() {
|
Chris@17
|
194 return `quickedit-wysiwyg-floated-toolgroup-for-${this._id}`;
|
Chris@17
|
195 },
|
Chris@17
|
196
|
Chris@17
|
197 /**
|
Chris@17
|
198 * Retrieves the ID for this toolbar's main WYSIWYG toolgroup.
|
Chris@17
|
199 *
|
Chris@17
|
200 * Used to provide an abstraction for any WYSIWYG editor to plug in.
|
Chris@17
|
201 *
|
Chris@17
|
202 * @return {string}
|
Chris@17
|
203 * A string that can be used as the ID.
|
Chris@17
|
204 */
|
Chris@17
|
205 getMainWysiwygToolgroupId() {
|
Chris@17
|
206 return `quickedit-wysiwyg-main-toolgroup-for-${this._id}`;
|
Chris@17
|
207 },
|
Chris@17
|
208
|
Chris@17
|
209 /**
|
Chris@17
|
210 * Finds a toolgroup.
|
Chris@17
|
211 *
|
Chris@17
|
212 * @param {string} toolgroup
|
Chris@17
|
213 * A toolgroup name.
|
Chris@17
|
214 *
|
Chris@17
|
215 * @return {jQuery}
|
Chris@17
|
216 * The toolgroup element.
|
Chris@17
|
217 */
|
Chris@17
|
218 _find(toolgroup) {
|
Chris@17
|
219 return this.$el.find(`.quickedit-toolgroup.${toolgroup}`);
|
Chris@17
|
220 },
|
Chris@17
|
221
|
Chris@17
|
222 /**
|
Chris@17
|
223 * Shows a toolgroup.
|
Chris@17
|
224 *
|
Chris@17
|
225 * @param {string} toolgroup
|
Chris@17
|
226 * A toolgroup name.
|
Chris@17
|
227 */
|
Chris@17
|
228 show(toolgroup) {
|
Chris@17
|
229 const $group = this._find(toolgroup);
|
Chris@17
|
230 // Attach a transitionEnd event handler to the toolbar group so that
|
Chris@17
|
231 // update events can be triggered after the animations have ended.
|
Chris@17
|
232 $group.on(Drupal.quickedit.util.constants.transitionEnd, event => {
|
Chris@17
|
233 $group.off(Drupal.quickedit.util.constants.transitionEnd);
|
Chris@17
|
234 });
|
Chris@17
|
235 // The call to remove the class and start the animation must be started in
|
Chris@17
|
236 // the next animation frame or the event handler attached above won't be
|
Chris@17
|
237 // triggered.
|
Chris@17
|
238 window.setTimeout(() => {
|
Chris@17
|
239 $group.removeClass('quickedit-animate-invisible');
|
Chris@17
|
240 }, 0);
|
Chris@17
|
241 },
|
Chris@0
|
242 },
|
Chris@17
|
243 );
|
Chris@17
|
244 })(jQuery, _, Backbone, Drupal);
|