/
burger.js
84 lines (61 loc) · 1.81 KB
/
burger.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
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/front/blob/master/LICENSE
*/
// burger
// script for a burger menu component
Component.Burger = function(persistent)
{
// not empty
if(Vari.isEmpty(this))
return null;
// component
Component.ResizeChange.call(this,{persistent: persistent});
// nodes
const $nodes = this;
// handler
setHdlrs(this,'burger:',{
isOpen: function() {
return trigHdlr(document,'doc:getAttr','data-burger') === 'open';
},
toggle: function() {
trigEvt(this,trigHdlr(this,'burger:isOpen')? 'burger:close':'burger:open');
}
});
// event
ael(this,'click',function() {
trigHdlr(this,'burger:toggle');
});
ael(this,'burger:open',function() {
trigHdlr(document,'doc:setAttr','data-burger','open');
});
ael(this,'burger:close',function() {
trigHdlr(document,'doc:setAttr','data-burger','close');
});
const escapeHandler = ael(document,'keyboardEscape:catched',function() {
trigEvt($nodes,'burger:close');
});
// teardown
const teardown = function()
{
trigEvt(this,'burger:close');
rel(document,escapeHandler);
}
// setup
aelOnce(this,'component:setup',function() {
trigEvt(this,'burger:close');
})
// teardown
aelOnce(this,'component:teardown',function() {
teardown.call(this);
});
// persistent
if(persistent !== true)
{
aelOnce(document,'doc:unmountPage',function() {
teardown.call($nodes);
});
}
return this;
}