3 * A Backbone View that provides an interactive toolbar (1 per in-place editor).
6 (function ($, _, Backbone, Drupal) {
7 Drupal.quickedit.FieldToolbarView = Backbone.View.extend(/** @lends Drupal.quickedit.FieldToolbarView# */{
10 * The edited element, as indicated by EditorView.getEditedElement.
17 * A reference to the in-place editor.
19 * @type {Drupal.quickedit.EditorView}
31 * @augments Backbone.View
33 * @param {object} options
34 * Options object to construct the field toolbar.
35 * @param {jQuery} options.$editedElement
36 * The element being edited.
37 * @param {Drupal.quickedit.EditorView} options.editorView
38 * The EditorView the toolbar belongs to.
41 this.$editedElement = options.$editedElement;
42 this.editorView = options.editorView;
47 this.$root = this.$el;
49 // Generate a DOM-compatible ID for the form container DOM element.
50 this._id = `quickedit-toolbar-for-${this.model.id.replace(/[\/\[\]]/g, '_')}`;
52 this.listenTo(this.model, 'change:state', this.stateChange);
58 * @return {Drupal.quickedit.FieldToolbarView}
59 * The current FieldToolbarView.
62 // Render toolbar and set it as the view's element.
63 this.setElement($(Drupal.theme('quickeditFieldToolbar', {
67 // Attach to the field toolbar $root element in the entity toolbar.
68 this.$el.prependTo(this.$root);
74 * Determines the actions to take given a change of state.
76 * @param {Drupal.quickedit.FieldModel} model
77 * The quickedit FieldModel
78 * @param {string} state
79 * The state of the associated field. One of
80 * {@link Drupal.quickedit.FieldModel.states}.
82 stateChange(model, state) {
83 const from = model.previous('state');
90 // Remove the view's existing element if we went to the 'activating'
91 // state or later, because it will be recreated. Not doing this would
92 // result in memory leaks.
93 if (from !== 'inactive' && from !== 'highlighted') {
105 if (this.editorView.getQuickEditUISettings().fullWidthToolbar) {
106 this.$el.addClass('quickedit-toolbar-fullwidth');
109 if (this.editorView.getQuickEditUISettings().unifiedToolbar) {
110 this.insertWYSIWYGToolGroups();
132 * Insert WYSIWYG markup into the associated toolbar.
134 insertWYSIWYGToolGroups() {
136 .append(Drupal.theme('quickeditToolgroup', {
137 id: this.getFloatedWysiwygToolgroupId(),
138 classes: ['wysiwyg-floated', 'quickedit-animate-slow', 'quickedit-animate-invisible', 'quickedit-animate-delay-veryfast'],
141 .append(Drupal.theme('quickeditToolgroup', {
142 id: this.getMainWysiwygToolgroupId(),
143 classes: ['wysiwyg-main', 'quickedit-animate-slow', 'quickedit-animate-invisible', 'quickedit-animate-delay-veryfast'],
147 // Animate the toolgroups into visibility.
148 this.show('wysiwyg-floated');
149 this.show('wysiwyg-main');
153 * Retrieves the ID for this toolbar's container.
155 * Only used to make sane hovering behavior possible.
158 * A string that can be used as the ID for this toolbar's container.
161 return `quickedit-toolbar-for-${this._id}`;
165 * Retrieves the ID for this toolbar's floating WYSIWYG toolgroup.
167 * Used to provide an abstraction for any WYSIWYG editor to plug in.
170 * A string that can be used as the ID.
172 getFloatedWysiwygToolgroupId() {
173 return `quickedit-wysiwyg-floated-toolgroup-for-${this._id}`;
177 * Retrieves the ID for this toolbar's main WYSIWYG toolgroup.
179 * Used to provide an abstraction for any WYSIWYG editor to plug in.
182 * A string that can be used as the ID.
184 getMainWysiwygToolgroupId() {
185 return `quickedit-wysiwyg-main-toolgroup-for-${this._id}`;
191 * @param {string} toolgroup
195 * The toolgroup element.
198 return this.$el.find(`.quickedit-toolgroup.${toolgroup}`);
204 * @param {string} toolgroup
208 const $group = this._find(toolgroup);
209 // Attach a transitionEnd event handler to the toolbar group so that
210 // update events can be triggered after the animations have ended.
211 $group.on(Drupal.quickedit.util.constants.transitionEnd, (event) => {
212 $group.off(Drupal.quickedit.util.constants.transitionEnd);
214 // The call to remove the class and start the animation must be started in
215 // the next animation frame or the event handler attached above won't be
217 window.setTimeout(() => {
218 $group.removeClass('quickedit-animate-invisible');
223 }(jQuery, _, Backbone, Drupal));