This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
forked from zuixjs/zkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu_overlay.js
149 lines (138 loc) · 5.4 KB
/
menu_overlay.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'use strict';
zuix.controller(function(cp) {
let menuOverlayShowing = false;
let menuButtonShowing = true;
let menuButton;
let menuButtonClose;
let menuOverlay;
let itemsWrapper;
let menuItems;
let scroller = null;
let currentOffset = 0;
cp.create = function() {
menuButton = cp.field('menu_button').hide()
.on('click', toggleMenu);
menuButtonClose = cp.field('menu_button_close').hide()
.on('click', toggleMenu);
menuOverlay = cp.field('menu_overlay').visibility('hidden')
.on('click', toggleMenu);
itemsWrapper = cp.field('items_wrapper');
const items = zuix.$(cp.model().items).children();
items.each(function(i, el) {
const wrapperDiv = zuix.$(document.createElement('div'))
.addClass('menu-item')
.attr('data-ui-transition-delay', (.3/(items.length()-i))+'s')
.append(el);
itemsWrapper.append(wrapperDiv.get());
});
menuItems = itemsWrapper.find('div[class*="menu-item"]');
// apply custom color to menu button
if (cp.view().attr('data-o-button-color') != null) {
cp.view().find('.circle-button').css('background', cp.view().attr('data-o-button-color'));
}
if (cp.view().attr('data-o-icon-color') != null) {
cp.view().find('.circle-button').css('fill', cp.view().attr('data-o-icon-color'));
}
const scrollerName = cp.view().attr('data-o-scroller');
if (scrollerName != null) {
scroller = zuix.field(scrollerName);
} else {
scroller = zuix.$(window);
}
if (scroller != null) {
scroller.on('scroll', function(e) {
const scrollTop = scroller.get() === window ? (document.documentElement.scrollTop || document.body.scrollTop) : scroller.get().scrollTop;
if (menuButtonShowing) {
if ((currentOffset - scrollTop) < -2) hideButton();
} else if (!menuButtonShowing) {
if ((currentOffset - scrollTop) > 2) showButton();
}
currentOffset = scrollTop;
if (menuOverlayShowing) {
toggleMenu();
}
});
}
// Animate CSS extension
zuix.using('component', '@lib/extensions/animate_css', function(res, ctx) {
// show floating action button
setTimeout(function() {
menuButton.animateCss('slideInUp').show();
}, 1000);
});
// Material Design Light integration - DOM upgrade
if (typeof componentHandler !== 'undefined') {
componentHandler.upgradeElements(cp.view().get());
}
cp.expose('show', function() {
cp.view().show();
});
cp.expose('hide', function() {
cp.view().hide();
});
cp.expose('toggleButton', toggleButton);
cp.expose('showButton', showButton);
cp.expose('hideButton', hideButton);
};
function toggleButton() {
if (menuButtonShowing) {
hideButton();
} else {
showButton();
}
}
function hideButton() {
menuButtonShowing = false;
menuButton.animateCss('fadeOutDown', {duration: '0.3s'}, function() {
this.hide();
});
cp.trigger('hide');
}
function showButton() {
menuButtonShowing = true;
menuButton.animateCss('fadeInUp').show();
cp.trigger('show');
}
function toggleMenu() {
if (!menuOverlayShowing) {
menuOverlayShowing = true;
cp.trigger('open');
menuButton.animateCss('rotateOut', {duration: '0.3s'});
menuButtonClose.animateCss('rotateIn', {duration: '0.3s'}, function() {
menuButton.hide();
}).show();
menuOverlay.animateCss('fadeIn', {duration: '0.5s'}).visibility('');
menuItems.each(function(p, el) {
let transitionDelay = '0';
if (this.attr('data-ui-transition-delay') != null) {
transitionDelay = this.attr('data-ui-transition-delay');
}
this.animateCss('bounceInRight', {duration: '0.5s', delay: transitionDelay});
});
} else if (menuOverlayShowing) {
menuOverlayShowing = false;
cp.trigger('close');
if (menuButtonShowing) {
menuButtonClose.animateCss('rotateOut', {duration: '0.3s'}, function() {
this.hide();
});
menuButton.animateCss('rotateIn', {duration: '0.3s'});
} else {
menuButtonClose.animateCss('fadeOutDown', {duration: '0.3s'}, function() {
this.hide();
});
}
menuOverlay.animateCss('fadeOut', {duration: '0.5s', delay: '0.2s'}, function() {
this.visibility('hidden');
});
menuItems.each(function(p, el) {
let transitionDelay = '0';
if (this.attr('data-ui-transition-delay') != null) {
transitionDelay = this.attr('data-ui-transition-delay');
}
this.animateCss('fadeOutRight', {duration: '0.5s', delay: transitionDelay});
});
menuButton.show();
}
}
});