-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapt-drop.js
93 lines (69 loc) · 2.22 KB
/
adapt-drop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
define([ 'core/js/adapt' ], function(Adapt) {
class DropView extends Backbone.View {
className() {
let classes = 'drop is-closed';
const modelClasses = this.model.get('_drop')._classes;
if (modelClasses) classes += ` ${modelClasses}`;
return classes;
}
initialize({ $content }) {
this.$content = $content;
this.listenTo(Adapt, 'remove', this.remove).render();
}
events() {
return { 'click .js-toggle-view': 'onClick' };
}
render() {
const template = Handlebars.templates.drop;
const data = this.model.get('_drop');
this.$el.html(template(data)).appendTo(this.$content.parent());
this.$content.addClass('drop__content').appendTo(this.$el).hide();
this.setChildrenVisibility(false);
}
onClick() {
this.$el.addClass('is-visited');
this.toggle();
return false;
}
setChildrenVisibility(isVisible) {
this.model.getChildren().each(child => {
if (child.setOnChildren) {
child.setOnChildren('_isVisible', isVisible, { pluginName: '_drop' });
}
});
}
toggle() {
const shouldOpen = !this.$el.hasClass('is-open');
this.$el.toggleClass('is-open is-closed');
this.$('.drop__btn').first()
.addClass('is-visited')
.attr('aria-expanded', shouldOpen);
this.$content.stop().slideToggle({
start: (shouldOpen ? this.onOpen : this.onClose).bind(this),
done: (shouldOpen ? this.onOpened : this.onClosed).bind(this)
});
}
onOpen() {
$(window).trigger('resize');
Adapt.trigger('device:resize');
this.setChildrenVisibility(true);
}
onOpened() {
Adapt.a11y.focus(this.$content);
}
onClose() {}
onClosed() {
this.setChildrenVisibility(false);
}
}
Adapt.once('app:dataReady', () => {
const views = [ 'menu', 'menuItem', 'page', 'article', 'block', 'component' ];
const eventList = views.map(view => `${view}View:postRender`).join(' ');
Adapt.on(eventList, ({ model, $el }) => {
const config = model.get('_drop');
if (config && config._isEnabled) {
new DropView({ model, $content: $el.children().first() });
}
});
});
});