X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fcontextual%2Fjs%2Ftoolbar%2Fviews%2FVisualView.js;fp=web%2Fcore%2Fmodules%2Fcontextual%2Fjs%2Ftoolbar%2Fviews%2FVisualView.js;h=d1d413502d32467658e5c0ec53b2c6599c4942a5;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/contextual/js/toolbar/views/VisualView.js b/web/core/modules/contextual/js/toolbar/views/VisualView.js new file mode 100644 index 000000000..d1d413502 --- /dev/null +++ b/web/core/modules/contextual/js/toolbar/views/VisualView.js @@ -0,0 +1,84 @@ +/** + * @file + * A Backbone View that provides the visual view of the edit mode toggle. + */ + +(function (Drupal, Backbone) { + + 'use strict'; + + Drupal.contextualToolbar.VisualView = Backbone.View.extend(/** @lends Drupal.contextualToolbar.VisualView# */{ + + /** + * Events for the Backbone view. + * + * @return {object} + * A mapping of events to be used in the view. + */ + events: function () { + // Prevents delay and simulated mouse events. + var touchEndToClick = function (event) { + event.preventDefault(); + event.target.click(); + }; + + return { + click: function () { + this.model.set('isViewing', !this.model.get('isViewing')); + }, + touchend: touchEndToClick + }; + }, + + /** + * Renders the visual view of the edit mode toggle. + * + * Listens to mouse & touch and handles edit mode toggle interactions. + * + * @constructs + * + * @augments Backbone.View + */ + initialize: function () { + this.listenTo(this.model, 'change', this.render); + this.listenTo(this.model, 'change:isViewing', this.persist); + }, + + /** + * @inheritdoc + * + * @return {Drupal.contextualToolbar.VisualView} + * The current contextual toolbar visual view. + */ + render: function () { + // Render the visibility. + this.$el.toggleClass('hidden', !this.model.get('isVisible')); + // Render the state. + this.$el.find('button').toggleClass('is-active', !this.model.get('isViewing')); + + return this; + }, + + /** + * Model change handler; persists the isViewing value to localStorage. + * + * `isViewing === true` is the default, so only stores in localStorage when + * it's not the default value (i.e. false). + * + * @param {Drupal.contextualToolbar.StateModel} model + * A {@link Drupal.contextualToolbar.StateModel} model. + * @param {bool} isViewing + * The value of the isViewing attribute in the model. + */ + persist: function (model, isViewing) { + if (!isViewing) { + localStorage.setItem('Drupal.contextualToolbar.isViewing', 'false'); + } + else { + localStorage.removeItem('Drupal.contextualToolbar.isViewing'); + } + } + + }); + +})(Drupal, Backbone);