annotate core/modules/quickedit/js/views/FieldToolbarView.es6.js @ 0:c75dbcec494b

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