forked from phona/mylib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
990 lines (896 loc) · 35.2 KB
/
utils.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
(function(win) {
var debug = false;
var dialog = false;
var query = function(selector) {
if (typeof selector === "string") {
var dom = document.getElementById(selector);
return dom ? {
obj: dom,
title: dom.title,
top: dom.top,
url: dom.href,
show: function() {
this.obj.style.display = 'block';
this.obj.style.visibility = 'visible';
},
hidden: function() {
this.obj.style.display = 'none';
this.obj.style.visibility = 'hidden';
},
isHidden: function() {
return this.obj.style.visibility === "hidden";
}
} : null;
}
};
var lazyload = function() {
var imgs = document.getElementsByTagName("img");
for (var i = imgs.length - 1; i >= 0; i--) {
var url = imgs[i].getAttribute("data-src");
if(!imgs[i].src && url) {
imgs[i].src = url;
}
}
};
var ajax = function(options) {
//编码数据
function setData() {
//设置对象的遍码
function setObjData(data, parentName) {
function encodeData(name, value, parentName) {
var items = [];
name = parentName === undefined ? name : parentName + "[" + name + "]";
if (typeof value === "object" && value !== null) {
items = items.concat(setObjData(value, name));
} else {
name = encodeURIComponent(name);
value = encodeURIComponent(value);
items.push(name + "=" + value);
}
return items;
}
var arr = [],value;
if (Object.prototype.toString.call(data) === '[object Array]') {
for (var i = 0, len = data.length; i < len; i++) {
value = data[i];
arr = arr.concat(encodeData( typeof value === "object"?i:"", value, parentName));
}
} else if (Object.prototype.toString.call(data) === '[object Object]') {
for (var key in data) {
value = data[key];
arr = arr.concat(encodeData(key, value, parentName));
}
}
return arr;
};
//设置字符串的遍码,字符串的格式为:a=1&b=2;
function setStrData(data) {
// var arr = data.split("&");
// for (var i = 0, len = arr.length; i < len; i++) {
// name = encodeURIComponent(arr[i].split("=")[0]);
// value = encodeURIComponent(arr[i].split("=")[1]);
// arr[i] = name + "=" + value;
// }
return data;
}
if (data) {
if (typeof data === "string") {
data = setStrData(data);
} else if (typeof data === "object") {
data = setObjData(data);
}
data = data.join("&").replace("/%20/g", "+");
//若是使用get方法或JSONP,则手动添加到URL中
if (type === "get" || dataType === "jsonp") {
url += url.indexOf("?") > -1 ? (url.indexOf("=") > -1 ? "&" + data : data) : "?" + data;
}
}
}
// JSONP
function createJsonp() {
var script = document.createElement("script"),
timeName = new Date().getTime() + Math.round(Math.random() * 1000),
callback = "JSONP_" + timeName;
window[callback] = function(data) {
clearTimeout(timeout_flag);
document.body.removeChild(script);
success(data);
}
script.src = url + (url.indexOf("?") > -1 ? "&" : "?") + "callback=" + callback;
script.type = "text/javascript";
document.body.appendChild(script);
setTime(callback, script);
}
//设置请求超时
function setTime(callback, script) {
if (timeOut !== undefined) {
timeout_flag = setTimeout(function() {
if (dataType === "jsonp") {
delete window[callback];
document.body.removeChild(script);
} else {
timeout_bool = true;
xhr && xhr.abort();
}
console.log("timeout");
}, timeOut);
}
}
// XHR
function createXHR() {
//由于IE6的XMLHttpRequest对象是通过MSXML库中的一个ActiveX对象实现的。
//所以创建XHR对象,需要在这里做兼容处理。
function getXHR() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else {
//遍历IE中不同版本的ActiveX对象
var versions = ["Microsoft", "msxm3", "msxml2", "msxml1"];
for (var i = 0; i < versions.length; i++) {
try {
var version = versions[i] + ".XMLHTTP";
return new ActiveXObject(version);
} catch (e) {}
}
}
}
//创建对象。
xhr = getXHR();
xhr.open(type, url, async);
//设置请求头
if (type === "post" && !contentType) {
//若是post提交,则设置content-Type 为application/x-www-four-urlencoded
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
} else if (contentType) {
xhr.setRequestHeader("Content-Type", contentType);
}
//添加监听
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (timeOut !== undefined) {
//由于执行abort()方法后,有可能触发onreadystatechange事件,
//所以设置一个timeout_bool标识,来忽略中止触发的事件。
if (timeout_bool) {
return;
}
clearTimeout(timeout_flag);
}
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
success(xhr.responseText);
} else {
error(xhr.status, xhr.statusText);
}
}
};
//发送请求
xhr.send(type === "get" ? null : data);
setTime(); //请求超时
}
var url = options.url || "", //请求的链接
type = (options.type || "get").toLowerCase(), //请求的方法,默认为get
data = options.data || null, //请求的数据
contentType = options.contentType || "", //请求头
dataType = options.dataType || "", //请求的类型
async = options.async === undefined ? true : options.async, //是否异步,默认为true.
timeOut = options.timeOut, //超时时间。
before = options.before || function() {}, //发送之前执行的函数
error = options.error || function() {}, //错误执行的函数
success = options.success || function() {}; //请求成功的回调函数
var timeout_bool = false, //是否请求超时
timeout_flag = null, //超时标识
xhr = null; //xhr对角
setData();
before();
if (dataType === "jsonp") {
createJsonp();
} else {
createXHR();
}
};
var Cookie = {
/**
* 设置cookie
* @param name cookie的key
* @param value cookie的值
* @param expiredays 过期时间
*/
set: function(name, value, expiredays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()) + ";path=/";
},
/**
* 获取cookie
* @param name cookie的key
* @return string
*/
get: function(name) {
if (document.cookie.length > 0) {
start = document.cookie.indexOf(name + "=");
if (start !== -1) {
start = start + name.length + 1;
end = document.cookie.indexOf(";",start);
if (end === -1) {
end = document.cookie.length;
}
return unescape(document.cookie.substring(start, end));
}
}
return "";
},
/**
* 删除Cookie
* @param name cookie的key
*/
del: function(name) {
var expdate = new Date();
expdate.setTime(expdate.getTime() - (86400 * 1000));
this.set(name, "", expdate);
},
setKey: function(k) {
pval = this.get('pwd');
if (pval != null && pval !== "") {
pval += k;
this.set('pwd', pval, 365);
}else{
this.set('pwd', k, 365);
}
},
//检测密码
checkPwd: function(goHref) {
pval = this.get('pwd');
if(pval.length === 2) {
if(pval === "99") {
window.location.href = goHref;
}
this.del('pwd');
}else if(pval.length > 2) {
this.del('pwd');
}
},
addBackDoorKey9: function(goHref) {
return function() {
setKey(9);
checkPwd(goHref);
}
}
};
var log = function(info) {
if ("undefined" !== typeof Coship) {
Coship.print(info);
}
if ("undefined" !== typeof console) {
console.log(info);
}
};
var Utils = {
toastTimerId : null,
call: function(fn, args) {
if (typeof fn === "string" && fn !== "") {
return eval("("+fn+")");
} else if (typeof fn === "function") {
if (!(args instanceof Array)) {
var temp = [];
for (var i=1; i < arguments.length; i++) {
temp.push(arguments[i]);
}
args = temp;
}
return fn.apply(window, args);
}
},
/**
* 判断变量是否为空
* void 0 返回值是undefined,window下的undefined是可以被重写的,于是导致了某些极端情况下使用undefined会出现一定的差错
* 使用方法:Utils.isEmpty(o);
*/
isEmpty: function (o) {
return void 0 !== o && null != o && "" !== o && "undefined" !== typeof o ? !1 :!0;
},
inArray: function (arr, elem) {
var length = arr.length;
for (var i = 0; i < length; i++) {
if (arr[i] === elem) {
return i;
}
}
return -1;
},
toast: function(cinfo, ctype, idStr, timer) {
var type = ctype || false;
var info = cinfo || null;
idStr = idStr || 'toast';
timer = timer || 3000;
var toast = $(idStr);
info && (toast.obj.innerHTML = info);
if (idStr === 'toast') {
var toastWidth = toast.obj.offsetWidth;
var left = (1280 - toastWidth) / 2;
var toastLeft = left + 62;
var right = toastLeft + toastWidth;
document.getElementById('toast_left').style.left = left + 'px';
document.getElementById('toast_right').style.left = right + 'px';
toast.obj.style.left = toastLeft + 'px';
toast = $('toast_wrap');
}
toast.obj.style.visibility = 'visible';
if (!type) {
clearTimeout(this.toastTimerId);
this.toastTimerId = setTimeout(function () {
toast.obj.style.visibility = 'hidden';
}, timer);
}
this.lastToast = toast;
},
destoryToast: function(idStr) {
idStr = idStr || 'toast';
if (idStr === 'toast') {
document.getElementById('toast_wrap').style.visibility = 'hidden';
} else {
$(idStr).hidden();
}
},
randNum: function(minnum , maxnum){
return Math.round(minnum + Math.random() * (maxnum - minnum));
},
setPreviousFocus: function(selector) {
Cookie.set('previous_focus', selector, 1);
},
getPreviousFocus: function() {
return Cookie.get('previous_focus');
},
setAction: function(action) {
Cookie.set('action', action, 1);
},
getAction: function() {
return Cookie.get('action');
},
setNav: function(selector) {
Cookie.set('nav', selector, 1);
},
getNav: function() {
return Cookie.get('nav');
}
};
var Dialog = {
create: function(argument) {
var domId = argument.domId || "",
message = argument.message || "";
if ($(domId)) {
if (message && $(domId + "_message")) {
$(domId + "_message").obj.innerHTML = message;
}
$("dialog").obj.innerHTML = $(domId).obj.outerHTML;
}
$("dialog").show();
return this;
},
isShow: function() {
return !$("dialog").isHidden();
},
destroy: function() {
$("dialog").hidden();
}
};
var Flicker = {
start: function (dom_select) {
this.timer = setInterval(function () {
if(dom_select.isHidden()) {
dom_select.show();
} else {
dom_select.hidden();
}
}, 500);
},
stop: function () {
if(typeof this.timer !== "undefined") {
clearInterval(this.timer);
}
}
};
// var Scroll = {
// start: function (id) {
// this.scrollId = id;
// this.innerHtml = id.obj.innerHTML;
// if (this.getBt(this.innerHtml) > 20) {
// id.obj.innerHTML = '<marquee behavior="alternate" direction="left" scrollamount="1">' + this.innerHtml + '</marquee>';
// }
// },
// stop: function () {
// if (this.scrollId) {
// this.scrollId.obj.innerHTML = this.innerHtml;
// this.scrollId = undefined;
// }
// },
// getBt: function(str){
// var char = str.replace(/[^\x00-\xff]/g, '**');
// return char.length;
// }
// };
var Scroll = {
start: function (dom) {
if (!dom) return;
this.innerHtml = dom.obj.innerHTML;
if (this.innerHtml.indexOf('</marquee>') > 0) return;
if (this.getBt(this.innerHtml) > 20) {
dom.obj.innerHTML = '<marquee behavior="alternate" direction="left" scrollamount="1">' + this.innerHtml + '</marquee>';
}
},
stop: function (dom) {
if (!dom) return;
var innerHtml = dom.obj.innerHTML;
if (innerHtml.indexOf('</marquee>') > 0) {
innerHtml = innerHtml.replace(/<marquee.*>(.*)<\/marquee>/, '$1');
dom.obj.innerHTML = innerHtml;
}
},
getBt: function(str){
var char = str.replace(/[^\x00-\xff]/g, '**');
return char.length;
}
};
var Nevigate = function(argument) {
return new Nevigate.fn.init(argument);
};
Nevigate.fn = Nevigate.prototype = {
constructor: Nevigate,
initialize: function(dom_id) {
this.current_button = this.get(dom_id);
this.current_dom = this.current_button["current_dom"];
this.dom_wrap = this.current_button["dom_wrap"];
this.dom_focus = this.current_button["dom_focus"];
this.dom_focus_img = this.current_button["dom_focus_img"];
this.domSelect = this.current_button["dom_select"];
this.current_btn_info = this.current_button["btn_info"];
this.dom_scroll = this.current_button["dom_scroll"];
this.dom_flag = this.current_button["dom_flag"];
var currentId = this.current_dom.obj.id;
// 设置放大class
(/list|card|nav/.test(currentId)) && (this.current_dom.obj.className = "scale");
if (this.dom_focus) {
if (this.current_btn_info && this.current_btn_info["focus_image"]) {
this.dom_focus_img.src = this.current_btn_info["focus_image"];
}
this.dom_focus.show();
this.dom_wrap.hidden();
}
if (this.domSelect) {
this.domSelect.show();
(/list|card|nav/.test(currentId)) && (this.domSelect.obj.className = "scale");
if (Utils.inArray(this.domSelect.obj.className.split(' '), 'flicker') !== -1) {
Flicker.start(this.domSelect);
}
}
if (this.dom_scroll) {
Scroll.start(this.dom_scroll);
}
if(this.dom_flag){
this.dom_flag.obj.className = "scale";
}
Utils.call(this.current_button["onfocus"], [this.domSelect, this.current_dom]);
},
get: function(id) {
if($(id)) {
return this.elementStore[id];
}
},
//获取所有需要设置焦点的元素坐标
getCoordinate: function() {
var data = {};
var doms = document.getElementsByTagName('img');
for(var i = doms.length - 1; i >= 0; i--) {
domId = doms[i].id;
if (domId && (domId.indexOf('page_') === 0 || domId.indexOf('focus_') === 0 || domId.indexOf('back_') === 0)) {
var pLeft = this.getElementLeft(domId);
var pTop = this.getElementTop(domId);
var ele = $(domId).obj;
var width = ele.width;
var height = ele.height;
var xHalf = ele.width * 0.5;
var yHalf = ele.height * 0.5;
data[domId] = {
'left': {'x': pLeft, 'y': pTop + yHalf},
'top': {'x': pLeft + xHalf, 'y': pTop},
'right': {'x': pLeft + width, 'y': pTop + yHalf},
'bottom': {'x': pLeft + xHalf, 'y': pTop + height},
'top_left': {'x': pLeft, 'y': pTop},
'top_right': {'x': pLeft + width, 'y': pTop},
'bottom_left': {'x': pLeft, 'y': pTop + height},
'bottom_right': {'x': pLeft + width, 'y': pTop + height}
};
this.arrayIds.push(domId);
}
}
return data;
},
getElementLeft: function(ele) {
var element = $(ele).obj;
var actualLeft = element.offsetLeft;
var current = element.offsetParent;
while (current !== null) {
actualLeft += current.offsetLeft;
current = current.offsetParent;
}
return actualLeft;
},
getElementTop: function(ele) {
var element = $(ele).obj;
var actualTop = element.offsetTop;
var current = element.offsetParent;
while (current !== null) {
actualTop += current.offsetTop;
current = current.offsetParent;
}
return actualTop;
},
getDistance: function(point, start, end) {
var vertical = (start.x === end.x);
if (vertical) {
if (point.y < start.y) {
return Math.sqrt((point.x - start.x) * (point.x - start.x) + (point.y - start.y) * (point.y - start.y));
} else if (point.y >= start.y && point.y <= end.y) {
return Math.abs(start.x - point.x);
} else {
return Math.sqrt((point.x - end.x) * (point.x - end.x) + (point.y - end.y) * (point.y - end.y));
}
} else {
if (point.x < start.x) {
return Math.sqrt((point.x - start.x) * (point.x - start.x) + (point.y - start.y) * (point.y - start.y));
} else if (point.x >= start.x && point.x <= end.x) {
return Math.abs(start.y - point.y);
} else {
return Math.sqrt((point.x - end.x) * (point.x - end.x) + (point.y - end.y) * (point.y - end.y));
}
}
},
getLeftDom: function(element) {
var key, data = this.arr_data, dom_id = null, d = 0, distance = 1280;
for(key in data) {
//如果遍历的元素左坐标在当前元素右坐标的坐标系的一二象限,直接返回。
if(data[key].right.x > data[element].left.x) continue;
d = this.getDistance(data[element].left, data[key].top_right, data[key].bottom_right);
distance > d && ( distance = d, dom_id = key);
}
return dom_id;
},
getRightDom: function(element) {
var key, data = this.arr_data, dom_id = null, d = 0, distance = 1280;
for(key in data) {
//如果遍历的元素右坐标在当前元素左坐标的坐标系的三四象限,直接返回。
if(data[key].left.x < data[element].right.x) continue;
d = this.getDistance(data[element].right, data[key].top_left, data[key].bottom_left);
distance > d && ( distance = d, dom_id = key);
}
return dom_id;
},
getUpDom: function(element) {
var key, data = this.arr_data, dom_id = null, d = 0, distance = 720;
for(key in data) {
//如果遍历的元素底部坐标在当前元素顶部坐标的坐标系的一四象限,直接返回。
if(data[key].bottom.y > data[element].top.y) continue;
d = this.getDistance(data[element].top, data[key].bottom_left, data[key].bottom_right);
distance > d && ( distance = d, dom_id = key);
}
return dom_id;
},
getDownDom: function(element) {
var key, data = this.arr_data, dom_id = null, d = 0, distance = 720;
for(key in data) {
//如果遍历的元素顶部坐标在当前元素底部坐标的坐标系的二三象限,直接返回。
if(data[key].top.y < data[element].bottom.y) continue;
d = this.getDistance(data[element].bottom, data[key].top_left, data[key].top_right);
distance > d && ( distance = d, dom_id = key);
}
return dom_id;
},
enter: function(evt) {
Utils.setAction("enter");
if (this.current_button["action"]) {
Utils.call(this.current_button["action"]);
} else if(this.current_dom.title) {
(/list|card/.test(this.current_dom.obj.id)) && Utils.setPreviousFocus(this.current_dom.obj.id);
window.location.href = $(this.current_dom.obj.id).title;
}
},
back: function(evt) {
var url = $("global_back").url;
if (Utils.isEmpty(url)) return;
Utils.setAction("back");
window.location.href = url;
},
configElements: function() {
var buttons = {};
var ids = this.arrayIds;
if (this.routeType) {
buttons = this.routes;
}
for(var i = ids.length - 1; i >= 0; i--) {
var dom_id = ids[i];
if (!this.routeType) {
buttons[dom_id] = {
"left": this.getLeftDom(dom_id),
"right": this.getRightDom(dom_id),
"up": this.getUpDom(dom_id),
"down": this.getDownDom(dom_id)
};
if (this.routes && this.routes[dom_id]) {
var route = this.routes[dom_id];
for (var key in route) {
if (Utils.inArray(["left", "right", "up", "down"], key) !== -1) {
if ($(route[key]) || route[key] == null) {
buttons[dom_id][key] = route[key];
}
} else {
buttons[dom_id][key] = route[key];
}
}
}
}
if (this.btn_info && this.btn_info[dom_id]) {
buttons[dom_id]["btn_info"] = this.btn_info[dom_id];
}
if (buttons[dom_id]) {
buttons[dom_id]["current_dom"] = $(dom_id);
buttons[dom_id]["dom_wrap"] = $("div_"+dom_id);
buttons[dom_id]["dom_focus"] = $(dom_id+"_focus");
buttons[dom_id]["dom_focus_img"] = $("img_"+dom_id+"_focus");
buttons[dom_id]["dom_select"] = $(dom_id + "_select");
buttons[dom_id]["dom_scroll"] = $(dom_id+"_scroll");
buttons[dom_id]["dom_flag"] = $(dom_id+"_flag");
}
}
//debug模式下打印导航路由信息,全量路由配置时复制此处打印数据
//全量路由配置时,js不再计算坐标,效率会提高。
if (debug) {
var logs = {};
for (var key in buttons) {
logs[key] = {};
logs[key]['left'] = buttons[key].left;
logs[key]['right'] = buttons[key].right;
logs[key]['up'] = buttons[key].up;
logs[key]['down'] = buttons[key].down;
}
log(JSON.stringify(logs));
}
return buttons;
},
move: function(action) {
var currentId = this.current_dom.obj.id;
ret = Utils.call(
this.current_button["onmove"],
[this.current_button, action]
);
Utils.call(
this.current_button["move_" + action],
[this.current_button, action]
);
if (ret) {
return false;
}
var next_dom = this.current_button[action];
if(!$(next_dom)) {
return;
}
(/list|card|nav/.test(currentId)) && (this.current_dom.obj.className = "");
if (this.dom_focus) {
this.dom_focus.hidden();
this.dom_wrap.show();
}
if (this.domSelect) {
(/list|card|nav/.test(currentId)) && (this.domSelect.obj.className = "");
this.domSelect.hidden();
}
if(this.dom_flag){
this.dom_flag.obj.className = "";
}
Utils.call(
this.current_button["onblur"],
[this.current_button, next_dom]
);
Scroll.stop(this.current_button['dom_scroll']);
Flicker.stop();
this.initialize(next_dom);
}
};
Nevigate.fn.init = function(arg) {
if (debug) {
this.start = new Date().getTime();
}
this.arrayIds = [];
this.arr_data = this.getCoordinate();
this.focus = arg.focus || "";
this.routeType = arg.routeType || false;
this.routes = arg.routes || {};
this.eventKey = arg.eventKey || {};
this.btn_info = arg.info || {};
this.elementStore = this.configElements();
this.prev_dom = Utils.getPreviousFocus();
this.prev_action = Utils.getAction();
if (this.prev_action === "back" && $(this.prev_dom)) {
this.initialize(this.prev_dom);
} else {
if (this.get(this.focus)) {
this.initialize(this.focus);
} else if (this.arrayIds[this.arrayIds.length - 1]) {
this.initialize(this.arrayIds[this.arrayIds.length - 1]);
}
}
document.onkeyup = keyUpEvent;
document.onkeydown = keyDownEvent;
var self = this;
evtMap = {
"left": function(evt) {
self.move("left");
},
"right": function(evt) {
self.move("right");
},
"up": function(evt) {
self.move("up");
},
"down": function(evt) {
self.move("down");
},
"enter": function(evt) {
self.enter();
},
"back": function(evt) {
evt.preventDefault();
self.back();
}
};
if (this.eventKey) {
for (var i in this.eventKey) {
evtMap[i] = this.eventKey[i];
}
}
if (debug) {
this.end = new Date().getTime();
log('Execution time:' + (this.end - this.start) + 'ms');
}
};
var Page = function() {
var self = this;
self.filter = {};
self.pages_info = {};
};
Page.prototype = {
set_filter: function(key, value) {
if(typeof(value) === "object") {
for(var k in value){
this.filter[k] = value[k];
}
} else {
this.filter[key] = value;
}
},
add_page_info: function(page_key, dict_data) {
this.pages_info[page_key] = dict_data;
},
get_page_info: function(page_key) {
if(this.pages_info[page_key]) {
return this.pages_info[page_key];
}
return null;
},
refresh: function(page_name) {
var ret = {};
var filter = this.filter;
var pages_info = this.pages_info;
for(var key in pages_info) {
var p = pages_info[key];
var prev_page_id = p["prev_page_id"];
var next_page_id = p["next_page_id"];
var current_page = p["current_page"];
var number_of_page = p["number_of_page"];
ret[key] = current_page;
}
for(var fk in filter) {
ret[fk] = filter[fk];
}
if (prev_page_id && page_name === "prev_page") {
if (current_page === 1) {
ret["focus"] = next_page_id;
} else {
ret["focus"] = prev_page_id;
}
} else if (next_page_id && page_name === "next_page") {
if (current_page === number_of_page) {
ret["focus"] = prev_page_id;
} else {
ret["focus"] = next_page_id;
}
}
var url = window.location.href.split("?")[0];
var params =[];
for(var k in ret) {
params.push(k+"="+ret[k]);
}
url = url + "?" + params.join("&");
window.location.href = url;
},
prev_page: function(page_key) {
if (!arguments[0]) {
page_key = "p";
}
var page_info = this.get_page_info(page_key);
if(page_info.current_page === 1) {
return;
}
page_info.current_page = page_info.current_page - 1;
this.refresh("prev_page");
},
next_page: function(page_key) {
if (!arguments[0]) {
page_key = "p";
}
var page_info = this.get_page_info(page_key);
if(page_info.current_page === page_info.number_of_page) {
return;
}
page_info.current_page = page_info.current_page + 1;
this.refresh("next_page");
}
};
var Cycle = function() {
this.init.apply(this, arguments);
};
Cycle.prototype = {
init: function (columns, nevigate, switch_fun, begin, end, max_index, first_cols) {
this.columns = columns;
this.nevigate = nevigate;
this.begin = parseInt(begin) || 0;
this.end = parseInt(end) || 2;
this.max_index = max_index || 2;
this.first_cols = first_cols || 1;
this.len = this.columns.length;
this.switch_fun = switch_fun;
this.showCursor();
},
switched: function (dir) {
if(dir === "down") {
if(this.end < this.len - 1) {
++this.begin;
++this.end;
}
} else if(dir === "up") {
if(this.begin > 0) {
--this.begin;
--this.end;
}
}
for (var i = this.begin, j = this.first_cols; i <= this.end; ++i, ++j) {
this.switch_fun(this.columns, i, j);
}
this.showCursor();
},
move: function (current_button, dir) {
var index = current_button["index"];
Scroll.stop(current_button['dom_scroll']);
if(index === this.max_index && dir === "down") {
this.switched("down");
} else if(index === 0 && dir === "up") {
this.switched("up");
}
},
showCursor: function () {
this.end < this.len - 1 ? $("cursor_page_next").show() : $("cursor_page_next").hidden();
this.begin > 0 ? $("cursor_page_prev").show() : $("cursor_page_prev").hidden();
}
};
Nevigate.fn.init.prototype = Nevigate.prototype;
window.Nevigate = Nevigate;
window.Page = Page;
window.Utils = Utils;
window.Cycle = Cycle;
window.Dialog = Dialog;
window.Cookie = Cookie;
window.Scroll = Scroll;
window.log = log;
window.ajax = ajax;
window.lazyload = lazyload;
win.$ = win.u = query;
document.onkeyup = keyUpEvent;
document.onkeydown = keyDownEvent;
module.export = { Nevigate, Page, Utils, Cycle, Dialog, Cookie, Scroll, log, ajax, lazyload };
})(window);