Chris@0
|
1 /**
|
Chris@0
|
2 * @file
|
Chris@0
|
3 * A Backbone View that provides the visual view of a contextual link.
|
Chris@0
|
4 */
|
Chris@0
|
5
|
Chris@17
|
6 (function(Drupal, Backbone, Modernizr) {
|
Chris@17
|
7 Drupal.contextual.VisualView = Backbone.View.extend(
|
Chris@17
|
8 /** @lends Drupal.contextual.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@17
|
21 const mapping = {
|
Chris@17
|
22 'click .trigger': function() {
|
Chris@17
|
23 this.model.toggleOpen();
|
Chris@17
|
24 },
|
Chris@17
|
25 'touchend .trigger': touchEndToClick,
|
Chris@17
|
26 'click .contextual-links a': function() {
|
Chris@17
|
27 this.model.close().blur();
|
Chris@17
|
28 },
|
Chris@17
|
29 'touchend .contextual-links a': touchEndToClick,
|
Chris@17
|
30 };
|
Chris@17
|
31 // We only want mouse hover events on non-touch.
|
Chris@17
|
32 if (!Modernizr.touchevents) {
|
Chris@17
|
33 mapping.mouseenter = function() {
|
Chris@17
|
34 this.model.focus();
|
Chris@17
|
35 };
|
Chris@17
|
36 }
|
Chris@17
|
37 return mapping;
|
Chris@17
|
38 },
|
Chris@0
|
39
|
Chris@17
|
40 /**
|
Chris@17
|
41 * Renders the visual view of a contextual link. Listens to mouse & touch.
|
Chris@17
|
42 *
|
Chris@17
|
43 * @constructs
|
Chris@17
|
44 *
|
Chris@17
|
45 * @augments Backbone.View
|
Chris@17
|
46 */
|
Chris@17
|
47 initialize() {
|
Chris@17
|
48 this.listenTo(this.model, 'change', this.render);
|
Chris@17
|
49 },
|
Chris@17
|
50
|
Chris@17
|
51 /**
|
Chris@17
|
52 * @inheritdoc
|
Chris@17
|
53 *
|
Chris@17
|
54 * @return {Drupal.contextual.VisualView}
|
Chris@17
|
55 * The current contextual visual view.
|
Chris@17
|
56 */
|
Chris@17
|
57 render() {
|
Chris@17
|
58 const isOpen = this.model.get('isOpen');
|
Chris@17
|
59 // The trigger should be visible when:
|
Chris@17
|
60 // - the mouse hovered over the region,
|
Chris@17
|
61 // - the trigger is locked,
|
Chris@17
|
62 // - and for as long as the contextual menu is open.
|
Chris@17
|
63 const isVisible =
|
Chris@17
|
64 this.model.get('isLocked') ||
|
Chris@17
|
65 this.model.get('regionIsHovered') ||
|
Chris@17
|
66 isOpen;
|
Chris@17
|
67
|
Chris@17
|
68 this.$el
|
Chris@17
|
69 // The open state determines if the links are visible.
|
Chris@17
|
70 .toggleClass('open', isOpen)
|
Chris@17
|
71 // Update the visibility of the trigger.
|
Chris@17
|
72 .find('.trigger')
|
Chris@17
|
73 .toggleClass('visually-hidden', !isVisible);
|
Chris@17
|
74
|
Chris@17
|
75 // Nested contextual region handling: hide any nested contextual triggers.
|
Chris@17
|
76 if ('isOpen' in this.model.changed) {
|
Chris@17
|
77 this.$el
|
Chris@17
|
78 .closest('.contextual-region')
|
Chris@17
|
79 .find('.contextual .trigger:not(:first)')
|
Chris@17
|
80 .toggle(!isOpen);
|
Chris@17
|
81 }
|
Chris@17
|
82
|
Chris@17
|
83 return this;
|
Chris@17
|
84 },
|
Chris@0
|
85 },
|
Chris@17
|
86 );
|
Chris@17
|
87 })(Drupal, Backbone, Modernizr);
|