annotate core/modules/contextual/js/toolbar/views/VisualView.es6.js @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 /**
Chris@0 2 * @file
Chris@0 3 * A Backbone View that provides the visual view of the edit mode toggle.
Chris@0 4 */
Chris@0 5
Chris@17 6 (function(Drupal, Backbone) {
Chris@17 7 Drupal.contextualToolbar.VisualView = Backbone.View.extend(
Chris@17 8 /** @lends Drupal.contextualToolbar.VisualView# */ {
Chris@17 9 /**
Chris@17 10 * Events for the Backbone view.
Chris@17 11 *
Chris@17 12 * @return {object}
Chris@17 13 * A mapping of events to be used in the view.
Chris@17 14 */
Chris@17 15 events() {
Chris@17 16 // Prevents delay and simulated mouse events.
Chris@17 17 const touchEndToClick = function(event) {
Chris@17 18 event.preventDefault();
Chris@17 19 event.target.click();
Chris@17 20 };
Chris@0 21
Chris@17 22 return {
Chris@17 23 click() {
Chris@17 24 this.model.set('isViewing', !this.model.get('isViewing'));
Chris@17 25 },
Chris@17 26 touchend: touchEndToClick,
Chris@17 27 };
Chris@17 28 },
Chris@0 29
Chris@17 30 /**
Chris@17 31 * Renders the visual view of the edit mode toggle.
Chris@17 32 *
Chris@17 33 * Listens to mouse & touch and handles edit mode toggle interactions.
Chris@17 34 *
Chris@17 35 * @constructs
Chris@17 36 *
Chris@17 37 * @augments Backbone.View
Chris@17 38 */
Chris@17 39 initialize() {
Chris@17 40 this.listenTo(this.model, 'change', this.render);
Chris@17 41 this.listenTo(this.model, 'change:isViewing', this.persist);
Chris@17 42 },
Chris@17 43
Chris@17 44 /**
Chris@17 45 * @inheritdoc
Chris@17 46 *
Chris@17 47 * @return {Drupal.contextualToolbar.VisualView}
Chris@17 48 * The current contextual toolbar visual view.
Chris@17 49 */
Chris@17 50 render() {
Chris@17 51 // Render the visibility.
Chris@17 52 this.$el.toggleClass('hidden', !this.model.get('isVisible'));
Chris@17 53 // Render the state.
Chris@17 54 this.$el
Chris@17 55 .find('button')
Chris@17 56 .toggleClass('is-active', !this.model.get('isViewing'));
Chris@17 57
Chris@17 58 return this;
Chris@17 59 },
Chris@17 60
Chris@17 61 /**
Chris@17 62 * Model change handler; persists the isViewing value to localStorage.
Chris@17 63 *
Chris@17 64 * `isViewing === true` is the default, so only stores in localStorage when
Chris@17 65 * it's not the default value (i.e. false).
Chris@17 66 *
Chris@17 67 * @param {Drupal.contextualToolbar.StateModel} model
Chris@17 68 * A {@link Drupal.contextualToolbar.StateModel} model.
Chris@17 69 * @param {bool} isViewing
Chris@17 70 * The value of the isViewing attribute in the model.
Chris@17 71 */
Chris@17 72 persist(model, isViewing) {
Chris@17 73 if (!isViewing) {
Chris@17 74 localStorage.setItem('Drupal.contextualToolbar.isViewing', 'false');
Chris@17 75 } else {
Chris@17 76 localStorage.removeItem('Drupal.contextualToolbar.isViewing');
Chris@17 77 }
Chris@17 78 },
Chris@0 79 },
Chris@17 80 );
Chris@17 81 })(Drupal, Backbone);