-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathibox.js
1597 lines (1411 loc) · 56.3 KB
/
ibox.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
991
992
993
994
995
996
997
998
999
1000
/*! iBox v2.0.6 ~ (c) 2013 Max Zhang, https://github.com/maxzhang/ibox2 */
(function() {
var global = this,
slice = Array.prototype.slice,
enumerables = ['hasOwnProperty', 'valueOf', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'constructor'],
noArgs = [],
TemplateClass = function() {},
chain = function(object) {
TemplateClass.prototype = object;
var result = new TemplateClass();
TemplateClass.prototype = null;
return result;
},
apply = function(object, config) {
if (object && config && typeof config === 'object') {
var i, j, k;
for (i in config) {
object[i] = config[i];
}
if (enumerables) {
for (j = enumerables.length; j--;) {
k = enumerables[j];
if (config.hasOwnProperty(k)) {
object[k] = config[k];
}
}
}
}
};
/**
* Class基类,使用Klass.define()方法声明类继承的顶级父类
*/
var Base = function() {};
apply(Base, {
$isClass: true,
extend: function(SuperClass) {
var superPrototype = SuperClass.prototype,
basePrototype, prototype, name;
prototype = this.prototype = chain(superPrototype);
this.superclass = prototype.superclass = superPrototype;
if (!SuperClass.$isClass) {
basePrototype = Base.prototype;
for (name in basePrototype) {
if (name in prototype) {
prototype[name] = basePrototype[name];
}
}
}
},
/**
* 新增或重写一个static属性
*
* var MyCls = Klass.define({
* ...
* });
*
* MyCls.addStatics({
* someProperty: 'someValue', // MyCls.someProperty = 'someValue'
* method1: function() { ... }, // MyCls.method1 = function() { ... };
* method2: function() { ... } // MyCls.method2 = function() { ... };
* });
*
* @param {Object} members
* @return {Base} this
* @static
*/
addStatics: function(members) {
var member, name;
for (name in members) {
if (members.hasOwnProperty(name)) {
member = members[name];
this[name] = member;
}
}
return this;
},
addMembers: function(members) {
var prototype = this.prototype,
names = [],
i, ln, name, member;
for (name in members) {
names.push(name);
}
if (enumerables) {
names.push.apply(names, enumerables);
}
for (i = 0,ln = names.length; i < ln; i++) {
name = names[i];
if (members.hasOwnProperty(name)) {
member = members[name];
if (typeof member == 'function' && !member.$isClass) {
member.$owner = this;
member.$name = name;
}
prototype[name] = member;
}
}
return this;
},
/**
* 重写类的属性或方法,例子:
* <code>
* var Cls1 = Klass.define({
* constructor: function(name) {
* this.name = name;
* },
*
* say: function() {
* alert(this.name + ' say: hello, world!');
* }
* });
*
* Cls1.implement({
* say: function() {
* alert(this.name + ' say: hello, I'm Max, nice to meet you!');
* },
*
* sayHello: function() {
* alert('hello, world!');
* }
* });
*
* var cls1 = new Cls1();
* cls1.say(); // 输出 'Max say: hello, I'm Max, nice to meet you!'
* cls1.sayHello(); // 输出 'hello world!'
* </code>
*
* 如果想为类的方法定义一个新的别名,应该使用下面的方式,不能使用override函数:
* <code>
* Cls1.prototype.speak = Cls1.prototype.say;
*
* var cls1 = new Cls1();
* cls1.speak(); // 输出 'Max say: hello, I'm Max, nice to meet you!'
* </code>
*
* @param {Object} overrides 被添加到类的属性或方法
* @static
*/
implement: function() {
this.addMembers.apply(this, arguments);
}
});
// Base类的prototype属性
apply(Base.prototype, {
$isInstance: true,
/**
* 调用当前方法的父类方法,例子:
* <code>
* var Cls1 = Klass.define({
* constructor: function(name) {
* this.name = name;
* },
*
* say: function() {
* alert(this.name + ' say: hello, world!');
* }
* });
*
* var Cls2 = Klass.define(Cls1, {
* constructor: function() {
* thia.callParent(['Max']); // 调用父类的构造函数
* }
* });
*
* var cls2 = new Cls2();
* cls2.say(); // 输出 'Max say: hello, world!'
* </code>
*
* @param {Array/Arguments} args 传递给父类方法的形参
* @return {Object} 返回父类方法的执行结果
*/
callParent: function(args) {
var method,
superMethod = (method = this.callParent.caller) &&
(method = method.$owner ? method : method.caller) &&
method.$owner.superclass[method.$name];
return superMethod.apply(this, args ? slice.call(args, 0) : noArgs);
},
// Default constructor, simply returns `this`
constructor: function() {
return this;
}
});
var makeCtor = function() {
function constructor() {
return this.constructor.apply(this, arguments) || null;
}
return constructor;
};
var extend = function(newClass, newClassExtend) {
var basePrototype = Base.prototype,
SuperClass, superPrototype, name;
if (newClassExtend && newClassExtend !== Object) {
SuperClass = newClassExtend;
} else {
SuperClass = Base;
}
superPrototype = SuperClass.prototype;
if (!SuperClass.$isClass) {
for (name in basePrototype) {
if (!superPrototype[name]) {
superPrototype[name] = basePrototype[name];
}
}
}
newClass.extend(SuperClass);
};
/**
* 声明类,类的继承,重写类方法
*/
var Klass = {
/**
* 声明一个类,或继承自一个父类,子类拥有父类的所有prototype定义的特性,
* 如未定义extend属性,默认继承BaseKlass类,例子:
* <code>
* var Cls1 = Klass.define({
* constructor: function(name) {
* this.name = name;
* },
*
* say: function() {
* alert(this.name + ' say: hello, world!');
* }
* });
*
* var Cls2 = Klass.define(Cls1, {
* constructor: function() {
* thia.callParent(['Max']); // 调用父类的构造函数
* }
* });
*
* var cls2 = new Cls2();
* cls2.say(); // 输出 'Max say: hello, world!'
* </code>
*
* @param {Object} newClassExtend 继承父类
* @param {Object} overrides 类的属性和方法
* @return {Klass} The new class
*/
define: function(newClassExtend, overrides) {
var newClass, name;
if (!newClassExtend && !overrides) {
newClassExtend = Base;
overrides = {};
} else if (!overrides) {
overrides = newClassExtend;
newClassExtend = Base;
}
newClass = makeCtor();
for (name in Base) {
newClass[name] = Base[name];
}
if (overrides.statics) {
newClass.addStatics(overrides.statics);
delete overrides.statics;
}
extend(newClass, newClassExtend);
newClass.addMembers(overrides);
return newClass;
}
};
if (typeof module === "object" && module && typeof module.exports === "object") {
// 声明 Node module
module.exports = Klass;
} else {
// 声明 AMD / SeaJS module
if (typeof define === "function" && (define.amd || seajs)) {
define('klass', [], function() {
return Klass;
});
}
}
if (typeof global === "object" && typeof global.document === "object") {
global.Klass = Klass;
}
})();
(function(window) {
var dummyStyle = document.createElement('div').style,
propPrefix = (function() {
var vendors = 't,webkitT,MozT,msT,OT'.split(','),
t,
i = 0,
l = vendors.length;
for (; i < l; i++) {
t = vendors[i] + 'ransform';
if (t in dummyStyle) {
return vendors[i].substr(0, vendors[i].length - 1);
}
}
return false;
}()),
cssPrefix = propPrefix ? '-' + propPrefix.toLowerCase() + '-' : '',
prefixStyle = function(style) {
if (propPrefix === '') return style;
style = style.charAt(0).toUpperCase() + style.substr(1);
return propPrefix + style;
},
transform = prefixStyle('transform'),
transition = prefixStyle('transition'),
transitionProperty = prefixStyle('transitionProperty'),
transitionDuration = prefixStyle('transitionDuration'),
transformOrigin = prefixStyle('transformOrigin'),
transitionTimingFunction = prefixStyle('transitionTimingFunction'),
transitionDelay = prefixStyle('transitionDelay'),
transitionEndEvent = (function() {
if (propPrefix == 'webkit' || propPrefix === 'O') {
return propPrefix.toLowerCase() + 'TransitionEnd';
}
return 'transitionend';
}());
dummyStyle = null;
window.vendor = {
propPrefix: propPrefix,
cssPrefix: cssPrefix,
transform: transform,
transition: transition,
transitionProperty: transitionProperty,
transitionDuration: transitionDuration,
transformOrigin: transformOrigin,
transitionTimingFunction: transitionTimingFunction,
transitionDelay: transitionDelay,
transitionEndEvent: transitionEndEvent
};
})(window);
(function(window) {
var vendor = window.vendor,
slice = Array.prototype.slice,
isAndroid = /Android/i.test(window.navigator.userAgent);
window.adapter = {
createOrientationChangeProxy: function(fn, scope) {
if (typeof scope === 'undefined') {
scope = fn;
}
return function() {
clearTimeout(scope.orientationChangedTimeout);
var args = slice.call(arguments, 0);
scope.orientationChangedTimeout = setTimeout(function() {
var ori = window.orientation;
if (ori != scope.lastOrientation) {
fn.apply(scope, args);
}
scope.lastOrientation = ori;
}, isAndroid ? 300 : 50);
};
},
listenTransition: function(target, duration, callbackFn) {
var me = this,
clear = function() {
if (target.transitionTimer) clearTimeout(target.transitionTimer);
target.transitionTimer = null;
target.removeEventListener(vendor.transitionEndEvent, handler, false);
},
handler = function() {
clear();
if (callbackFn) callbackFn.call(me);
};
clear();
target.addEventListener(vendor.transitionEndEvent, handler, false);
target.transitionTimer = setTimeout(handler, duration + 100);
}
};
})(window);
(function(window) {
var navigator = window.navigator,
userAgent = navigator.userAgent,
android = userAgent.match(/(Android)\s([\d\.]+)/i),
ios = userAgent.match(/(iPad|iPhone|iPod);[\w\s]+(?:iPhone|)\sOS\s([\d_\.]+)/i),
wp = userAgent.match(/(Windows\s+Phone)(?:\sOS)?\s([\d\.]+)/i),
isWebkit = /WebKit\/[\d.]+/i.test(userAgent),
isSafari = ios ? (navigator.standalone ? isWebkit : (/Safari/i.test(userAgent) && !/CriOS/i.test(userAgent) && !/MQQBrowser/i.test(userAgent))) : false,
os = {};
if (android) {
os.android = true;
os.version = android[2];
os.android4 = /^4/.test(os.version);
os.android3 = /^3/.test(os.version);
os.android2 = /^2/.test(os.version);
}
if (ios) {
os.ios = true;
os.version = ios[2].replace(/_/g, '.');
os['ios' + os.version.match(/^(\w+)/i)[1]] = true;
if (ios[1] === 'iPad') {
os.ipad = true;
} else if (ios[1] === 'iPhone') {
os.iphone = true;
} else if (ios[1] === 'iPod') {
os.ipod = true;
}
}
if (wp) {
os.wp = true;
os.version = wp[2];
os.wp8 = /^8/.test(os.version);
os.wp7 = /^7/.test(os.version);
}
window.supporter = {
/**
* 移动设备操作系统信息,可能会包含一下属性:
*
* Boolean : android
* Boolean : android4
* Boolean : android3
* Boolean : android2
* Boolean : ios
* Boolean : ios7
* Boolean : ios6
* Boolean : ios5
* Boolean : ipad
* Boolean : iphone
* Boolean : ipod
* Boolean : wp
* Boolean : wp8
* Boolean : wp7
* String : version 系统版本号
*
*/
os: os,
/**
* 是否智能设备
*/
isSmartDevice: (function() {
return !!(os.ios || os.android || os.wp);
}()),
/**
* 是否webkit内核浏览器
*/
isWebkit: isWebkit,
/**
* 是否safari浏览器
*/
isSafari: isSafari,
/**
* 低于iOS7
*/
isBelowIos7: !!(os.ios && os.version.match(/^(\w+)/i)[1] < 7)
};
})(window);
(function(window) {
var navigator = window.navigator,
adapter = window.adapter,
supporter = window.supporter,
result = function(val, defaultValue, scope) {
var type = typeof val;
return type === 'undefined' ? defaultValue : (type === 'function' ? val.call(scope || window) : val);
};
window.resizer = (function() {
var callbacks = [],
resizeTimer,
pub;
function resize() {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function() {
resizeTimer = null;
processResize();
}, 100);
}
function processResize() {
var innerWidth = window.innerWidth,
innerHeight = window.innerHeight,
screenWidth = window.screen.width,
screenHeight = window.screen.height,
width = innerWidth, height,
offsetLeft, offsetRight, offsetTop, offsetBottom,
fn, scope, options;
if (supporter.isSafari && supporter.isBelowIos7) { // 计算高度,收起 iOS6 顶部导航条
height = navigator.standalone ? innerHeight : (window.orientation === 0 ? screenHeight - 44 : screenWidth - 32) - 20;
height = height < innerHeight ? innerHeight : height;
} else {
height = innerHeight;
}
if (width != pub.width || height != pub.height) {
pub.width = width;
pub.height = height;
callbacks.forEach(function(o) {
fn = o.fn;
if (fn) {
scope = o.scope;
options = o.options || {};
offsetLeft = result(options.offsetLeft, 0, scope);
offsetRight = result(options.offsetRight, 0, scope);
offsetTop = result(options.offsetTop, 0, scope);
offsetBottom = result(options.offsetBottom, 0, scope);
fn.call(scope || window, width - offsetLeft - offsetRight, height - offsetTop - offsetBottom);
}
});
}
}
pub = {
on: function(callbackFn, scope, options) {
callbacks.push({
fn: callbackFn,
scope: scope,
options: options
});
return pub;
},
off: function(callbackFn, scope) {
callbacks.every(function(o, i) {
if (o.fn === callbackFn && o.scope === scope) {
callbacks.splice(i, 1);
return false;
}
});
return pub;
},
trigger: function() {
resize();
return pub;
}
};
window.addEventListener('resize', resize, false);
window.addEventListener('orientationchange', adapter.createOrientationChangeProxy(processResize), false);
resize();
return pub;
}());
})(window);
(function(window) {
var toString = Object.prototype.toString,
slice = Array.prototype.slice;
var Utils = {
BLANK_IMAGE: 'data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==',
noop: function() {},
isDefined: function(val) {
return typeof val !== 'undefined';
},
isString: function(val) {
return typeof val === 'string';
},
isBoolean: function(val) {
return typeof val === 'boolean';
},
isObject: (toString.call(null) === '[object Object]') ? function(value) {
// check ownerDocument here as well to exclude DOM nodes
return value !== null && value !== undefined && toString.call(value) === '[object Object]' && value.ownerDocument === undefined;
} : function(value) {
return toString.call(value) === '[object Object]';
},
isArray: ('isArray' in Array) ? Array.isArray : function(val) {
return toString.call(val) === '[object Array]';
},
isFunction: function(val) {
return toString.call(val) === '[object Function]';
},
result: function(val, defaultValue, scope) {
return !Utils.isDefined(val) ? defaultValue : (Utils.isFunction(val) ? val.call(scope || window) : val);
},
proxy:function(fn, scope) {
return function() {
return fn.apply(scope, arguments);
};
},
removeElement: function() {
var args = slice.call(arguments, 0);
args.forEach(function(el) {
if (el && el.parentNode) {
el.parentNode.removeChild(el);
}
});
},
addClass: function(elem, value) {
var classes, cur, clazz, i;
classes = (value || '').match(/\S+/g) || [];
cur = elem.nodeType === 1 && ( elem.className ? (' ' + elem.className + ' ').replace(/[\t\r\n]/g, ' ') : ' ');
if (cur) {
i = 0;
while ((clazz = classes[i++])) {
if (cur.indexOf(' ' + clazz + ' ') < 0) {
cur += clazz + ' ';
}
}
elem.className = cur.trim();
}
},
removeClass: function(elem, value) {
var classes, cur, clazz, i;
classes = (value || '').match(/\S+/g) || [];
cur = elem.nodeType === 1 && ( elem.className ? (' ' + elem.className + ' ').replace(/[\t\r\n]/g, ' ') : ' ');
if (cur) {
i = 0;
while ((clazz = classes[i++])) {
while (cur.indexOf(' ' + clazz + ' ') >= 0) {
cur = cur.replace(' ' + clazz + ' ', ' ');
}
}
elem.className = cur.trim();
}
},
parsePx: function(px) {
return px ? parseInt(px.replace(/[^\d]/g, ''), 10) : 0;
},
getComputedSize: function(el, outerSize) {
var style = window.getComputedStyle(el, null),
w, h, iw, ih, ow, oh;
if (!Utils.isDefined(outerSize)) {
w = el.offsetWidth;
h = el.offsetHeight;
iw = w - Utils.parsePx(style.paddingLeft) - Utils.parsePx(style.paddingRight) - Utils.parsePx(style.borderLeftWidth) - Utils.parsePx(style.borderRightWidth);
ih = h - Utils.parsePx(style.paddingTop) - Utils.parsePx(style.paddingBottom) - Utils.parsePx(style.borderTopWidth) - Utils.parsePx(style.borderBottomWidth);
ow = w + Utils.parsePx(style.marginLeft) + Utils.parsePx(style.marginRight);
oh = h + Utils.parsePx(style.marginTop) + Utils.parsePx(style.marginBottom);
} else {
if (!Utils.isObject(outerSize)) {
outerSize = { width: outerSize };
}
ow = outerSize.width;
oh = outerSize.height;
if (Utils.isDefined(ow)) {
w = ow - Utils.parsePx(style.marginLeft) - Utils.parsePx(style.marginRight);
iw = w - Utils.parsePx(style.paddingLeft) - Utils.parsePx(style.paddingRight) - Utils.parsePx(style.borderLeftWidth) - Utils.parsePx(style.borderRightWidth);
}
if (Utils.isDefined(oh)) {
h = oh - Utils.parsePx(style.marginTop) - Utils.parsePx(style.marginBottom);
ih = h - Utils.parsePx(style.paddingTop) - Utils.parsePx(style.paddingBottom) - Utils.parsePx(style.borderTopWidth) - Utils.parsePx(style.borderBottomWidth);
}
}
return {
width: w < 0 ? 0 : w,
height: h < 0 ? 0 : h,
innerWidth: iw < 0 ? 0 : iw,
innerHeight: ih < 0 ? 0 : ih,
outerWidth: ow < 0 ? 0 : ow,
outerHeight: oh < 0 ? 0 : oh
};
},
queryFunction: function(method) {
var fn;
if (method && Utils.isString(method)) {
fn = window;
method = method.split('.');
method.forEach(function(m) {
fn = fn[m];
});
}
return fn || Utils.noop;
},
dispatchClickEvent: function(e) {
var target = e.target,
ev;
if (target.tagName != 'SELECT' && target.tagName != 'INPUT' && target.tagName != 'TEXTAREA') {
ev = document.createEvent('MouseEvents');
ev.initMouseEvent('click', true, true, e.view, 1,
target.screenX, target.screenY, target.clientX, target.clientY,
e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
0, null);
ev._constructed = true;
target.dispatchEvent(ev);
}
}
};
window.iBoxUtils = Utils;
})(window);
(function(window) {
var slice = Array.prototype.slice,
supporter = window.supporter,
resizer = window.resizer,
iBoxUtils = window.iBoxUtils;
/**
* @class iBox
*/
var iBox = Klass.define({
statics: {
version: '2.0.6'
},
/**
* @cfg {String} baseCSSPrefix ibox页面元素样式前缀,默认'ibox'
*/
baseCSSPrefix: 'ibox',
/**
* @cfg {String} cls 添加到ibox主容器元素的扩展样式
*/
/**
* @cfg {String} headerCls 添加到header元素的扩展样式
*/
/**
* @cfg {String} bodyCls 添加到ibox body元素的扩展样式
*/
/**
* @cfg {Object} offsets ibox主容器的偏移值
*/
offsets: { top: 0, bottom: 0 },
/**
* 构造函数
*
* @param {Element/Object} target (optional) 目标对象 / iBox 主容器对象
* @param {Object} config (optional) 配置参数
*/
constructor: function(target, config) {
if (iBoxUtils.isObject(target)) {
config = target;
target = null;
}
target = target || document.body;
config = config || {};
for (var o in config) {
this[o] = config[o];
}
this.views = {};
this.render(target);
this.scrollTop();
resizer.on(this.resize, this).trigger();
var first = this.body.children[0];
if (first) {
this.slide({ el: first, silent: true });
}
},
// private
render: function(target) {
if (!this.rendered) {
this.rendered = true;
var elCls = this.baseCSSPrefix,
hdCls = this.baseCSSPrefix + '-header',
bdCls = this.baseCSSPrefix + '-body';
this.el = iBoxUtils.isString(target) ? document.querySelector(target) : target;
iBoxUtils.addClass(this.el, elCls);
if (this.cls) {
iBoxUtils.addClass(this.el, this.cls);
}
this.header = this.el.querySelector('.' + hdCls);
if (!this.header) {
this.header = document.createElement('div');
this.el.appendChild(this.header);
}
iBoxUtils.addClass(this.header, hdCls);
if (this.headerCls) {
iBoxUtils.addClass(this.header, this.headerCls);
}
this.body = this.el.querySelector('.' + bdCls);
if (!this.body) {
this.body = document.createElement('div');
this.el.appendChild(this.body);
}
iBoxUtils.addClass(this.body, bdCls);
if (this.bodyCls) {
iBoxUtils.addClass(this.body, this.bodyCls);
}
this.onRender();
}
},
/**
* @protected
* 当iBox渲染时调用,可以被子类实现或实例化时重写
*/
onRender: iBoxUtils.noop,
/**
* 重置iBox高宽
*/
resize: function() {
var width = resizer.width, height = resizer.height,
offsetTop = iBoxUtils.result(this.offsets.top, 0, this),
offsetBottom = iBoxUtils.result(this.offsets.bottom, 0, this),
elSize, headerSize, bodySize, headerHeight;
height = height - offsetTop - offsetBottom;
elSize = iBoxUtils.getComputedSize(this.el, { width: width, height: height });
headerHeight = iBoxUtils.getComputedSize(this.header).outerHeight;
headerSize = iBoxUtils.getComputedSize(this.header, { width: elSize.innerWidth, height: headerHeight });
bodySize = iBoxUtils.getComputedSize(this.body, { width: elSize.innerWidth, height: elSize.innerHeight - headerHeight });
this.scrollTop();
this.el.style.cssText = 'top:' + offsetTop + 'px;width:' + elSize.innerWidth + 'px;height:' + elSize.innerHeight + 'px;';
this.header.style.cssText = 'top:0px;width:' + headerSize.innerWidth + 'px;height:' + headerSize.innerHeight + 'px;';
this.body.style.cssText = 'top:' + headerHeight + 'px;width:' + bodySize.innerWidth + 'px;height:' + bodySize.innerHeight + 'px;';
if (this.lastView && !this.sliding) this.lastView.resize();
this.onResize(elSize, headerSize, bodySize);
},
/**
* @protected
* 当iBox重置高宽时调用,可以被子类实现或实例化时重写
*/
onResize: iBoxUtils.noop,
/**
* 切换视图,接口调用包含以下6种方式:
*
* slide(options);
*
* slide(options, callbacks);
*
* slide(options, reverse);
*
* slide(options, reverse, silent);
*
* slide(options, reverse, callbacks);
*
* slide(options, reverse, silent, callbacks);
*
* @param {Object/String} options 切换视图参数
* 新增视图,传入JSON参数,设置视图
* 已存在视图,直接传入String类型视图id
* @param {Boolean/Object} reverse (optional) true视图切换动画反向
* @param {Boolean/Object} silent (optional) true静默切换视图
* @param {Object} callbacks (optional) 回调函数,show回调对应下一视图,hide回调对应上一视图
* Function : beforeShow
* Function : onShow
* Function : beforeHide
* Function : onHide
*/
slide: function(options, reverse, silent, callbacks) {
var me = this,
args = slice.call(arguments, 0), argLen = args.length,
lastView = me.lastView, nextView;
me.scrollTop();
if (options) {
if (argLen == 2 && !iBoxUtils.isBoolean(reverse)) {
callbacks = reverse;
reverse = false;
} if (argLen == 3 && !iBoxUtils.isBoolean(silent)) {
callbacks = silent;
silent = false;
}
options = iBoxUtils.isString(options) ? { id: options } : options;
options.id = options.id && (iBoxUtils.isString(options.id) ? options.id : options.id.getAttribute('id'));
silent = silent === true;
reverse = reverse === true;
callbacks = callbacks || {};
nextView = me.views[options.id];
if (!nextView) {
nextView = new iBox.View(me.body, me.header, options);
me.views[nextView.id] = nextView;
}
if (lastView != nextView && !me.sliding) {
me.sliding = true;
if (lastView) {
if (callbacks.beforeHide) callbacks.beforeHide(lastView);
lastView.slide(reverse, 'out', silent, function() {
if (callbacks.onHide) callbacks.onHide(lastView);
if (lastView.single !== true) {
lastView.destroy();
delete me.views[lastView.id];
}
});
} else {
silent = true; // 没有前一视图时,直接跳过动画切换,这种情况一般会发生在初始化的时候
}
if (callbacks.beforeShow) callbacks.beforeShow(nextView);
nextView.slide(reverse, 'in', silent, function() {
me.lastView = nextView;
if (callbacks.onShow) callbacks.onShow(nextView);
me.sliding = false;
me.resize(); // 这里必须是滑动结束之后,才能重置iBox高宽
});
}
}
},
// private
scrollTop: function() {
if (supporter.isSafari) window.scrollTo(0, 1);
},
/**
* @protected
* 当iBox销毁之前调用,可以被子类实现或实例化时重写
*/
beforeDestroy: iBoxUtils.noop,
/**
* @protected
* 当iBox被销毁时调用,可以被子类实现或实例化时重写
*/
onDestroy: iBoxUtils.noop,
/**
* 销毁iBox对象
*/
destroy: function() {
if (!this.destroyed) {
this.destroyed = true;
this.beforeDestroy();
resizer.off(this.resize, this);
for (var o in this.view) {
this.views[o].destroy();
delete this.views[o];
}
this.views = this.lastView = null;
iBoxUtils.removeElement(this.header, this.body);
this.el = this.header = this.body = null;
this.onDestroy();
}
}
});
/*
* 实现 iphone5 分辨率 viewport 兼容,在 iphone5 下,WebApp 被添加到桌面后,从桌面启动时,
* 可视区域无法达到满屏,所以需要将页面上 viewport width 属性设置为320.1,如下:
* <meta name="viewport" content="width=320.1, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0" />
*
* 为了适配非 iphone5 设备,仍旧要将 viewport width 设置为 'device-width'
*
* Note: 这个问题已在iOS7下被修复。
*/