ContextualLinkView.es6.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * @file
  3. * A Backbone View that provides a dynamic contextual link.
  4. */
  5. (function ($, Backbone, Drupal) {
  6. Drupal.quickedit.ContextualLinkView = Backbone.View.extend(/** @lends Drupal.quickedit.ContextualLinkView# */{
  7. /**
  8. * Define all events to listen to.
  9. *
  10. * @return {object}
  11. * A map of events.
  12. */
  13. events() {
  14. // Prevents delay and simulated mouse events.
  15. function touchEndToClick(event) {
  16. event.preventDefault();
  17. event.target.click();
  18. }
  19. return {
  20. 'click a': function (event) {
  21. event.preventDefault();
  22. this.model.set('state', 'launching');
  23. },
  24. 'touchEnd a': touchEndToClick,
  25. };
  26. },
  27. /**
  28. * Create a new contextual link view.
  29. *
  30. * @constructs
  31. *
  32. * @augments Backbone.View
  33. *
  34. * @param {object} options
  35. * An object with the following keys:
  36. * @param {Drupal.quickedit.EntityModel} options.model
  37. * The associated entity's model.
  38. * @param {Drupal.quickedit.AppModel} options.appModel
  39. * The application state model.
  40. * @param {object} options.strings
  41. * The strings for the "Quick edit" link.
  42. */
  43. initialize(options) {
  44. // Insert the text of the quick edit toggle.
  45. this.$el.find('a').text(options.strings.quickEdit);
  46. // Initial render.
  47. this.render();
  48. // Re-render whenever this entity's isActive attribute changes.
  49. this.listenTo(this.model, 'change:isActive', this.render);
  50. },
  51. /**
  52. * Render function for the contextual link view.
  53. *
  54. * @param {Drupal.quickedit.EntityModel} entityModel
  55. * The associated `EntityModel`.
  56. * @param {bool} isActive
  57. * Whether the in-place editor is active or not.
  58. *
  59. * @return {Drupal.quickedit.ContextualLinkView}
  60. * The `ContextualLinkView` in question.
  61. */
  62. render(entityModel, isActive) {
  63. this.$el.find('a').attr('aria-pressed', isActive);
  64. // Hides the contextual links if an in-place editor is active.
  65. this.$el.closest('.contextual').toggle(!isActive);
  66. return this;
  67. },
  68. });
  69. }(jQuery, Backbone, Drupal));