/
navIndex.js
219 lines (167 loc) · 6.15 KB
/
navIndex.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* 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
*/
// navIndex
// base component that manages index navigation for many targets
Component.NavIndex = function(option)
{
// not empty
if(Vari.isEmpty(this))
return null;
// option
const $option = Pojo.replace({
type: null,
child: null,
childActive: null,
target: [],
go: null,
loop: false,
attr: null
},option);
// check
Str.typecheck($option.type);
Str.typecheck($option.child);
Str.typecheck($option.childActive);
const type = $option.type;
const child = $option.child;
// defaultChange
const defaultChange = function(target,context)
{
let r = false;
const $this = this;
const targets = trigHdlr(this,type+':getTargets');
const current = trigHdlr(this,type+':getCurrent');
if(trigHdlr(target,child+':canOpen') && (current == null || trigHdlr(current,child+':canClose')))
{
r = true;
trigEvt(this,type+':beforeChange',target,current,context,targets);
Arr.each(targets,function(ele) {
if(ele !== target)
trigEvt(ele,child+':close',context);
});
const promise = trigHdlr(this,type+':getPromise',target,current,context,targets);
const callback = function() {
trigEvt(target,child+':open');
trigEvt($this,type+':afterChange',target,current,context,targets);
};
if(promise != null)
promise.then(callback);
else
callback();
}
return r;
}
// go true
if($option.go == null)
$option.go = defaultChange;
Func.typecheck($option.go);
// handler
setHdlrs(this,type+':',{
canChange: function() {
return true;
},
getTargets: function() {
let r = $option.target;
if(Str.isNotEmpty(r))
r = qsa(this,r);
Arr.typecheck(r);
trigEvt(this,type+':bindChilds',r);
return r;
},
getTargetsLength: function() {
return Arr.length(trigHdlr(this,type+':getTargets'));
},
getCurrent: function() {
const targets = trigHdlr(this,type+':getTargets');
return Arr.find(targets,function(ele) {
return trigHdlr(ele,child+':'+$option.childActive);
});
},
getCurrentIndex: function() {
const current = trigHdlr(this,type+':getCurrent');
const targets = trigHdlr(this,type+':getTargets');
return Arr.search(current,targets);
},
get: function(value) {
value = trigHdlr(this,type+':readyValue',value);
const current = trigHdlr(this,type+':getCurrent');
const targets = trigHdlr(this,type+':getTargets');
return Nav.indexObj(value,current,targets,$option.loop);
},
go: function(value,context) {
let r = false;
if(trigHdlr(this,type+':canChange') === true)
{
const target = trigHdlr(this,type+':get',value);
const current = trigHdlr(this,type+':getCurrent');
if(target != null && target != current)
r = $option.go.call(this,target,context);
}
return r;
},
isIndex: function(value) {
return Nav.isIndex(trigHdlr(this,type+':getCurrentIndex'),trigHdlr(this,type+':getTargetsLength'));
},
isFirst: function() {
return Nav.isFirst(trigHdlr(this,type+':getCurrentIndex'),trigHdlr(this,type+':getTargetsLength'));
},
hasPrev: function() {
return Nav.hasPrev(trigHdlr(this,type+':getCurrentIndex'),trigHdlr(this,type+':getTargetsLength'),$option.loop);
},
hasNext: function() {
return Nav.hasNext(trigHdlr(this,type+':getCurrentIndex'),trigHdlr(this,type+':getTargetsLength'),$option.loop);
},
isLast: function() {
return Nav.isLast(trigHdlr(this,type+':getCurrentIndex'),trigHdlr(this,type+':getTargetsLength'));
},
goFirst: function(context) {
return trigHdlr(this,type+':go','first',context);
},
goPrev: function(context) {
return trigHdlr(this,type+':go','prev',context);
},
goNext: function(context) {
return trigHdlr(this,type+':go','next',context);
},
goLast: function(context) {
return trigHdlr(this,type+':go','last',context);
},
readyValue: function(value) {
return (value == null)? 'first':value;
},
});
// event
ael(this,type+':bindChilds',function(event,value) {
bindChilds.call(this,value);
});
// setup
aelOnce(this,'component:ready',function(event,value) {
trigHdlr(this,type+':go',value,'ready');
});
// bindChilds
const bindChilds = function(value)
{
const $this = this;
value = Ele.toArray(value,false);
const handlers = {
isEmpty: function() {
return Ele.match(this,":empty");
},
getParent: function() {
return $this;
}
};
Arr.each(value,function(ele) {
if(!trigHdlr(ele,child+':isBinded'))
{
// components
Component.InitOpenClose.call(ele,Pojo.replace($option,{type: child}));
// handler
setHdlrs(ele,child+':',handlers);
}
});
}
return this;
}