Chris@0: /** Chris@0: * @file Chris@0: * A Backbone View that provides a dynamic contextual link. Chris@0: */ Chris@0: Chris@0: (function ($, Backbone, Drupal) { Chris@0: Drupal.quickedit.ContextualLinkView = Backbone.View.extend(/** @lends Drupal.quickedit.ContextualLinkView# */{ Chris@0: Chris@0: /** Chris@0: * Define all events to listen to. Chris@0: * Chris@0: * @return {object} Chris@0: * A map of events. Chris@0: */ Chris@0: events() { Chris@0: // Prevents delay and simulated mouse events. Chris@0: function touchEndToClick(event) { Chris@0: event.preventDefault(); Chris@0: event.target.click(); Chris@0: } Chris@0: Chris@0: return { Chris@0: 'click a': function (event) { Chris@0: event.preventDefault(); Chris@0: this.model.set('state', 'launching'); Chris@0: }, Chris@0: 'touchEnd a': touchEndToClick, Chris@0: }; Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Create a new contextual link view. Chris@0: * Chris@0: * @constructs Chris@0: * Chris@0: * @augments Backbone.View Chris@0: * Chris@0: * @param {object} options Chris@0: * An object with the following keys: Chris@0: * @param {Drupal.quickedit.EntityModel} options.model Chris@0: * The associated entity's model. Chris@0: * @param {Drupal.quickedit.AppModel} options.appModel Chris@0: * The application state model. Chris@0: * @param {object} options.strings Chris@0: * The strings for the "Quick edit" link. Chris@0: */ Chris@0: initialize(options) { Chris@0: // Insert the text of the quick edit toggle. Chris@0: this.$el.find('a').text(options.strings.quickEdit); Chris@0: // Initial render. Chris@0: this.render(); Chris@0: // Re-render whenever this entity's isActive attribute changes. Chris@0: this.listenTo(this.model, 'change:isActive', this.render); Chris@0: }, Chris@0: Chris@0: /** Chris@0: * Render function for the contextual link view. Chris@0: * Chris@0: * @param {Drupal.quickedit.EntityModel} entityModel Chris@0: * The associated `EntityModel`. Chris@0: * @param {bool} isActive Chris@0: * Whether the in-place editor is active or not. Chris@0: * Chris@0: * @return {Drupal.quickedit.ContextualLinkView} Chris@0: * The `ContextualLinkView` in question. Chris@0: */ Chris@0: render(entityModel, isActive) { Chris@0: this.$el.find('a').attr('aria-pressed', isActive); Chris@0: Chris@0: // Hides the contextual links if an in-place editor is active. Chris@0: this.$el.closest('.contextual').toggle(!isActive); Chris@0: Chris@0: return this; Chris@0: }, Chris@0: Chris@0: }); Chris@0: }(jQuery, Backbone, Drupal));