/
jmui.js
1405 lines (1214 loc) · 44.4 KB
/
jmui.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
/**
* Copyright (c) 2014 Tencent AlloyTeam, All rights reserved.
* http://www.AlloyTeam.com/
* Code licensed under the BSD License:
* http://www.AlloyTeam.com/license.txt
*
* @file JMUI核心js
* @author Yussicahe yussicahe@gmail.com
*/
;(function ($) {
var DATA_PREFIX = 'jmu'; // data存储前缀
/**
* @func attributeData
* @desc 从DOM节点上获取所有"data-*"属性
* @param object DOM节点
* @returns {object} "data-*"属性的键值对
*/
function attributeData(node) {
var store = {};
$.each(node.attributes || [], function (i, attr) {
if (parseInt(attr.name.indexOf('data-'), 10) === 0) {
store[$.camelCase(attr.name.replace('data-', ''))] = $.zepto.deserializeValue(attr.value);
}
});
return store;
}
/**
* @function zeptolize
* @description 将组件挂到$.fn上,便于调用
* @param {string} 组件名
*/
function zeptolize(name) {
var key = $.camelCase(name.substring(0, 1).toLowerCase() + name.substring(1)),
old = $.fn[key];
/**
* @function
* @description 调用组件
* @param {object} 组件配置选项
* @param {string} 组件方法名
* @returns {*} 组件实例或者相应方法的返回值
*/
$.fn[key] = function (options, func) {
var args,
ret,
component,
dataName = DATA_PREFIX + '-' + key;
if (typeof options === 'string') {
args = [].slice.call(arguments, 1);
func = options;
options = {};
} else {
args = [].slice.call(arguments, 2);
}
$.each(this, function (i, el) {
// 尝试从data中取组件实例
component = $(el).data(dataName);
if (typeof component === 'undefined') {
var attrOptions = attributeData(el);
component = new JMU[name](el, $.extend({}, options, attrOptions)); // 无实例则创建一个
$(el).data(dataName, component);
} else if (options) { // 更新组件的配置选项
component.setOptions(options);
}
if (typeof func === 'undefined') { // 未指定方法,返回组件实例
ret = component;
return false; // 断开循环
} else { // 调用组件指定方法
func = component[func];
// 如果组件无指定方法,抛出错误信息
if (!$.isFunction(func)) {
throw new Error(name + ' has no such method "' + func + '".');
}
ret = func.apply(component, args);
// 如果是get性质的方法,断开循环
if (typeof ret !== 'undefined') {
return false;
}
}
});
return typeof ret !== 'undefined'? ret : this;
};
// NO CONFLICT
$.fn[key].noConflict = function () {
$.fn[key] = old;
return this;
};
}
window.JMU = {
/**
* @function emptyFunction
* @description 空函数
*/
emptyFunction: function () {},
/**
* @function preventDefault
* @description 阻止事件的默认行为
* @param {object} 事件对象e
*/
preventDefault: function (e) {
e = e || window.event;
e.preventDefault();
},
/**
* @function defineComponent
* @description 定义组件
* @param {string} 组件名
* @param {object} 组件原型
* @param {class} 组件的父类
* @returns {class} 组件类
*/
defineComponent: function (name, object, superClass) {
if (!$.isFunction(superClass)) {
superClass = JMU.Component; // 默认基类为Component
}
/**
* @class
* @description 组件类
* @param object 组件的容器节点
* @param {object} 组件配置选项
*/
function subClass(container, options) {
// 初始化组件
this._init(container, options);
}
subClass.superClass = superClass;
subClass.prototype = Object.create(superClass.prototype);
/**
* @function $super
* @description 调用父类的方法
* @param {string} 方法名
* @returns {*} 方法的返回值
*/
object.$super = function (name) {
var func = superClass.prototype[name];
return $.isFunction(func) && func.apply(this, [].slice.call(arguments, 1));
};
$.extend(subClass.prototype, object);
JMU[name] = subClass;
zeptolize(name);
return subClass;
}
};
})(Zepto);
;/**
* Copyright (c) 2014 Tencent AlloyTeam, All rights reserved.
* http://www.AlloyTeam.com/
* Code licensed under the BSD License:
* http://www.AlloyTeam.com/license.txt
*
* @author Yussicahe
* @description 定义组件基类Component
*/
;(function ($, JMU) {
/*
* @class Component
* @description 组件的基类Component
*/
function Component() {}
Component.prototype = {
/**
* @property {object} 组件默认配置选项
*/
options: {},
/**
* @property {string|object} 组件模板
*/
template: '',
/**
* @property {boolean} 组件是否显示
*/
isShow: false,
/*
* @method setOptions
* @description 设置组件配置选项
* @param {object} 配置选项
*/
setOptions: function (options) {
$.extend(this.options, options);
},
/**
* @method _init
* @description 组件初始化
* @param object 组件的容器节点
*/
_init: function (container, options) {
this.$container = $(container || document.body);
// 设置组件配置选项
this.setOptions(options);
this._create();
},
/*
* @method _create
* @description 创建组件的DOM节点
*/
_create: function () {
var options = this.options || {},
$el = options.$el,
template = this.template || '';
if ($el) { // 组件DOM已经存在的情况
this.$el = typeof $el === 'string'? $($el, this.$container) : $el; // $el可以是selector
} else { // 不存在则通过模板创建一个
this.$el = $(typeof template === 'string'? template : template.main); // 模板如果是对象,则以main来创建组件
this.$container.append(this.$el); // 将组件添加到DOM中
}
this._bindEvents();
},
/*
* @method _render
* @description 渲染组件
*/
_render: JMU.emptyFunction,
/*
* @method _bindEvents
* @desc 给组件绑定相应的事件
*/
_bindEvents: function () {
// 需要的情况下阻止掉页面滚动
if (this.options.preventScroll) {
this.$el.on('touchmove', JMU.preventDefault);
}
},
/*
* @method show
* @description 显示组件
*/
show: function (options) {
var self = this;
this.setOptions(options);
this._render();
if (this.isShow) {
return;
}
this.isShow = true;
options = this.options;
// 是否有遮罩
if (options.mask && JMU.Mask) {
$('body').mask('show', {
tapHide: options.tapHide,
relatedComponent: this // tapHide为true时点击遮罩同时会隐藏组件
});
}
// 显示之前加的class
this.$el.addClass('jmu-before-show');
// 是否有动画
if (options.animation) {
this.$el.show().addClass('jmu-effect');
this.$el[0].offsetWidth; // repaint
} else {
this.$el.removeClass('jmu-effect').show();
}
this.$el.off('webkitTransitionEnd');
this.$el.addClass('jmu-show');
// 是否显示一段时间后自动隐藏
if(options.duration){
clearTimeout(this.hideTimer);
this.hideTimer = setTimeout(function(){
self.hide();
}, options.duration);
}
},
/*
* @method hide
* @description 隐藏组件
*/
hide: function(options){
var self = this;
this.setOptions(options);
if (!this.isShow) {
return;
}
this.isShow = false;
options = this.options;
this.$el.removeClass('jmu-before-show');
// 是否有动画
if (options.animation) {
this.$el.one('webkitTransitionEnd', function () {
self.$el.hide();
});
} else {
this.$el.hide();
}
this.$el.removeClass('jmu-show');
// 是否有遮罩
if (options.mask && JMU.Mask) {
$('body').mask('hide');
}
}
};
JMU.Component = Component;
})(Zepto, JMU);;;(function($){
function bounceFix() {
$.isBounceFix = true;
// fix by jdochen
// 为兼容后期生成的节点,使用委托方式绑定
$(document)
.on('touchmove', '[data-bouncefix],[data-scrollable]', function (e) {
var el = e.currentTarget,
clientHeight = el.clientHeight,
scrollHeight = el.scrollHeight;
if(scrollHeight <= clientHeight){
e.preventDefault();
}
})
.on('touchstart', '[data-scrollable]', function (e) {
var el = e.currentTarget,
scrollTop = el.scrollTop,
clientHeight = el.clientHeight,
scrollHeight = el.scrollHeight;
if(scrollHeight > clientHeight){
if ( scrollTop <= 0 ) {
el.scrollTop = 1;
}
if ( scrollTop >= scrollHeight - clientHeight ){
el.scrollTop = scrollHeight - clientHeight - 1;
}
}
});
}
// only for ios6+
if( $.os.ios && $.os.version >= '6' ) {
$('body').addClass('jmu-bounce-fix');
bounceFix();
}
})(Zepto);
;;(function($){
var canvas = $('<canvas></canvas>')[0],
context = canvas.getContext('2d'),
imgs = $('img[data-holder]');
for (var i = 0, len = imgs.length; i < len; i++) {
var size = $(imgs[i]).data('holder'),
text = $(imgs[i]).data('holder-text') || $(imgs[i]).data('holder');
textWidth = context.measureText(text).width,
width = size.split('x')[0],
height = size.split('x')[1],
textX = (width-textWidth) / 2,
textY = height / 2;
canvas.width = +width;
canvas.height = +height;
context.fillStyle = getRandomColor(0, 255);
context.fillRect(0,0,width,height);
context.fillStyle = '#fff';
context.fillText(text, textX, textY);
$(imgs[i]).attr('src', canvas.toDataURL());
$(imgs[i]).attr('alt', text);
}
function getRandomNumber(min, max) {
return Math.round(Math.random() * (max - min)) +1;
}
function getRandomColor(min, max) {
var r = getRandomNumber(min, max),
g = getRandomNumber(min, max),
b = getRandomNumber(min, max),
a = getRandomNumber(0.1, 0.5),
color = 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
return color;
}
})(Zepto);;(function( $, JMU ) {
JMU.Component( 'Lazyload', {
options: {
attribute: 'data-lazy',
viewWidth: window.innerWidth*2,
viewHeight: window.innerHeight*2, // 默认懒加载为两屏
handler: function($el, src){
var img;
if(src){
if($el[0].tagName.toLowerCase() == 'img'){
img = $el[0];
}else{
img = $('img', $el)[0];
}
if(img){
img.onload = function(){
$el.addClass('jmu-loaded');
};
img.src = src;
}
}
},
defer: 200
},
_init: function(el, opts){
this.$super('_init', el, opts);
this.$scrollEl = this.$container;
this.$container = $.isWindow(this.$scrollEl[0])? $('body') : this.$scrollEl;
this._bindEvents();
},
_bindEvents: function(){
this._debounceLoad = $.debounce($.proxy(this.startLoad, this), this.options.defer, false);
this.$scrollEl.on('scroll', this._debounceLoad);
$(window).on('resize', this._debounceLoad);
},
startLoad: function(){
var $container = this.$container,
attribute = this.options.attribute,
handler = this.options.handler;
var $els = $('[' + attribute + ']', $container);
var flag = false;
for(var i = 0, l = $els.length; i < l; i++){
var el = $els[i],
$el = $(el);
if(!$el.attr('skip-load') && $el.isInView(this.options.viewWidth, this.options.viewHeight)){
handler($el, $el.attr(attribute));
$el.removeAttr(attribute);
flag = true;
}else if(flag){
break;
}
}
}
});
})(Zepto, JMU);
;(function($){
var domainPrefix = window.location.domain;
$.extend($, {
emptyFunction: function(){},
preventDefault: function(e){
e.preventDefault();
},
template: (function(){
var cache = {};
return function(str, data){
// Figure out if we're getting a template, or if we need to
// load the template - and be sure to cache the result.
var fn = cache[str] ||
// Generate a reusable function that will serve as a template
// generator (and which will be cached).
new Function("obj",
"var p=[],print=function(){p.push.apply(p,arguments);};" +
// Introduce the data as local variables using with(){}
"with(obj){p.push('" +
// Convert the template into pure JavaScript
str
.replace(/[\r\t\n]/g, " ")
.split("<%").join("\t")
.replace(/((^|%>)[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)%>/g, "',$1,'")
.split("\t").join("');")
.split("%>").join("p.push('")
.split("\r").join("\\'") + "');}return p.join('');");
// Provide some basic currying to the user
return data ? fn( data ) : fn;
};
})(),
insertStyleSheet: function() {
var $el = $('<style type="text/css"></style>').appendTo('head');
return $el[0].sheet;
},
debounce: function(func, wait, immediate){
var timeout;
return function() {
var context = this,
args = arguments,
callNow = immediate && !timeout;
var later = function() {
timeout = null;
if (!immediate) {
func.apply(context, args);
}
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) {
func.apply(context, args);
}
};
}
});
$.extend($.fn, {
isInView: function(viewWidth, viewHeight){
var el = $(this)[0];
if(!el){
return false;
}
var rect = el.getBoundingClientRect();
if((rect.left > -rect.width && rect.right < (viewWidth || window.innerWidth) + rect.width) &&
(rect.top > -rect.height && rect.bottom < (viewHeight || window.innerHeight) + rect.height)){
return true;
}else{
return false;
}
}
});
})(Zepto);;;(function( $, JMU ) {
/*底部浮层组件
*
* 调用示例:by gctang
* pro.actionSheet.show({
* content: ['value1','value2'], // 或以'<li>value1</li><li>value2</li>>'的字符串形式传入
* binHandle: [function(){}, function(){}]//需与content一一对应
*})
* or 高级配置用法:
* pro.actionSheet.show({
* content: [
* {
* id: 'testId',//给节点添加自定义id
* className: 'jmu-clor-red',//添加自己的样式
* value: 'value1' //展示的值
* cmd: 'customEvent1' //自定义事件名
* },
* {
* value: 'value2',
* cmd: 'customEvent2' //自定义事件名
* }
* ],
* customEvent1: function(){
* //自定义事件 do something
* }
* })
*/
JMU.defineComponent( 'ActionShare', {
options: {
mask: true,
animation: true,
tapHide: true,
preventScroll: true,
content: '',
btnHandle: []
},
template: {
main: '<div class="jmu-action-sheet">\
<div class="sheet-title jmu-border-1px border-bottom">分享到:</div>\
<ul class="content"></ul>\
<div class="jmu-color-blue btn btn-cancel" data-cmd="as-cancel" data-dismiss="true">取消</div>\
</div>',
ul: '<% for(var i = 0, l = list.length; i < l; i++){\
if(typeof(list[i]) === "string"){\
%>\
<li class="jmu-color-blue btn" data-dismiss="true"><%=list[i]%></li>\
<% }else{ %>\
<li <%=list[i].id ? "id="+list[i].id : ""%> class="jmu-color-blue btn <%=list[i].className ? list[i].className : ""%>" data-dismiss="true" <%=list[i].cmd ? "data-cmd="+list[i].cmd : ""%> ><%=list[i].value ? list[i].value : ""%></li>\
<% }\
} %>'
},
_render: function(){
var options = this.options;
if($.isArray(options.content)){
this.$el.find('.content').html($.template(this.template.ul, { list : options.content }));
}else{
this.$el.find('.content').html(options.content);
}
},
_bindEvents: function(){
var self = this;
this.$el.on('tap', '.btn', function(e){
var $btn = $(e.currentTarget);
var command = $btn.data('cmd');
$btn.active(function(){
var fn = null;
var index = -1;
if(command === 'as-cancel'){
index = self.$el.find('.content').children().length;
}else{
index = $btn.index();
}
if($btn.data('dismiss')){
self.hide(); // 点击后隐藏action-sheet
}
if(self.options.btnHandle.length > 0){
fn = self.options.btnHandle[index];
$.isFunction( fn ) && fn();
} else if($.isFunction( self.options[command] )){
self.options[command]();
}
});
});
}
});
})(Zepto, JMU);
;(function( $, JMU ) {
/*底部浮层组件
*
* 调用示例:by gctang
* pro.actionSheet.show({
* content: ['value1','value2'], // 或以'<li>value1</li><li>value2</li>>'的字符串形式传入
* binHandle: [function(){}, function(){}]//需与content一一对应
*})
* or 高级配置用法:
* pro.actionSheet.show({
* content: [
* {
* id: 'testId',//给节点添加自定义id
* className: 'jmu-clor-red',//添加自己的样式
* value: 'value1' //展示的值
* cmd: 'customEvent1' //自定义事件名
* },
* {
* value: 'value2',
* cmd: 'customEvent2' //自定义事件名
* }
* ],
* customEvent1: function(){
* //自定义事件 do something
* }
* })
*/
JMU.defineComponent( 'ActionSheet', {
options: {
mask: true,
animation: true,
tapHide: true,
preventScroll: true,
content: '',
btnHandle: []
},
template: {
main: '<div class="jmu-action-sheet">\
<ul class="content"></ul>\
<div class="jmu-color-blue btn btn-cancel" data-cmd="as-cancel" data-dismiss="true">取消</div>\
</div>',
ul: '<% for(var i = 0, l = list.length; i < l; i++){\
if(typeof(list[i]) === "string"){\
%>\
<li class="jmu-color-blue jmu-border-1px btn" data-dismiss="true"><%=list[i]%></li>\
<% }else{ %>\
<li <%=list[i].id ? "id="+list[i].id : ""%> class="jmu-color-blue jmu-border-1px btn <%=list[i].className ? list[i].className : ""%>" data-dismiss="true" <%=list[i].cmd ? "data-cmd="+list[i].cmd : ""%> ><%=list[i].value ? list[i].value : ""%></li>\
<% }\
} %>'
},
_render: function(){
var options = this.options;
if($.isArray(options.content)){
this.$el.find('.content').html($.template(this.template.ul, { list : options.content }));
}else{
this.$el.find('.content').html(options.content);
}
},
_bindEvents: function(){
var self = this;
this.$el.on('tap', '.btn', function(e){
var $btn = $(e.currentTarget);
var command = $btn.data('cmd');
$btn.active(function(){
var fn = null;
var index = -1;
if(command === 'as-cancel'){
index = self.$el.find('.content').children().length;
}else{
index = $btn.index();
}
if($btn.data('dismiss')){
self.hide(); // 点击后隐藏action-sheet
}
if(self.options.btnHandle.length > 0){
fn = self.options.btnHandle[index];
$.isFunction( fn ) && fn();
} else if($.isFunction( self.options[command] )){
self.options[command]();
}
});
});
}
});
})(Zepto, JMU);
;/**
* Copyright (c) 2014 Tencent AlloyTeam, All rights reserved.
* http://www.AlloyTeam.com/
* Code licensed under the BSD License:
* http://www.AlloyTeam.com/license.txt
*
* @author Yussicahe
* @description dialog组件
*/
;(function ($, JMU) {
JMU.defineComponent('Dialog', {
options: {
mask: true,
animation: true,
tapHide: true,
preventScroll: true,
showCloseBtn: false,
title: '提示',
content: '',
btnText: ['取消', '确定'],
btnHandle: []
},
template: '<div class="jmu-dialog">\
<div class="body jmu-color-black">\
<div class="title"></div>\
<div class="content"></div>\
</div>\
<div class="btns jmu-border-1px jmu-color-blue">\
<div class="btn"></div>\
<div class="btn jmu-border-1px"></div>\
</div>\
</div>',
_render: function(){
var options = this.options;
if(options.title){
this.$el.find('.title').text(options.title).css('display', 'block');
}else{
this.$el.find('.title').css('display', 'none');
}
this.$el.find('.content').html(options.content);
this.$el.find('.btn:nth-child(1)').text(options.btnText[0]);
if(options.btnText.length > 1){
this.$el.find('.btn:nth-child(2)').text(options.btnText[1]).css('display', 'block');
}else{
this.$el.find('.btn:nth-child(2)').css('display', 'none');
}
},
_bindEvents: function(){
var self = this;
this.$el.on('tap', '.btn', function(e){
var $btn = $(e.currentTarget);
$btn.active(function(){
self.hide(); // 点击后隐藏dialog
// 防止dialog隐藏动画和其他渲染一起出现卡顿
setTimeout(function(){
var fn = self.options.btnHandle[$btn.index()];
$.isFunction( fn ) && fn();
}, 0);
});
});
}
});
})(Zepto, JMU);
;(function( $, JMU ) {
JMU.defineComponent( 'Dot', {
options: {
type: 'normal', // normal, new, num
color: 'red', // red 或 blue
content: '', // New红点和数字红点需要指定content
css: null // 样式,可以自由控制红点的位置和大小,默认红点在容器的右上角
},
template: '<div></div>',
_render: function(){
var options = this.options;
this.$el.removeClass().addClass('jmu-dot-' + options.type + ' ' + 'jmu-dot-' + options.color).text(options.content);
if(options.css){
this.$el.css(options.css);
}else{
this.$el.addClass('jmu-dot-tr'); // 位置默认在容器的右上角
}
}
});
})(Zepto, JMU);
;;(function( $, JMU ) {
var cssPrefix = '-webkit-';
var ratio = window.devicePixelRatio == 1? 1 : 2;
var sheet = $.insertStyleSheet();
var cache = {}; /* Cache animation rules */
JMU.defineComponent( 'Loading', {
options: {
size: 30, // The width/ height of the spinner
lineWidth : 2, // The the width of each lines in px
lines: 12, // The number of lines to draw
color : '158,158,158', // Must be an RGB string
roundDuration: 1.6 // Seconds per round
},
template: '<canvas></canvas>',
_addAnimation: function(lines){
var name = 'jmu-loading-' + lines;
if (!cache[name] && lines > 0) {
var rule = '',
percentage;
rule += '@' + cssPrefix + 'keyframes ' + name + '{';
for (var i = 0; i <= lines; i++) {
percentage = i/lines;
rule += percentage*100 + '%{' + cssPrefix + 'transform:rotate(' + percentage*360 + 'deg)}';
}
rule += '}';
sheet.insertRule(rule, sheet.cssRules.length);
cache[name] = true; //缓存
}
return name;
},
_render: function(){
if($.env && $.env.isPoorDevice){
ratio = 1;
}
var options = this.options;
var size = options.size * ratio,
halfSize = size/2,
inner = halfSize * (1/3) ,
outer = halfSize * (2/3) ,
lineWidth = options.lineWidth * ratio,
lines = options.lines;
this.$el.attr({ width: size, height: size });
var ctx = this.$el[0].getContext('2d');
ctx.lineWidth = lineWidth;
ctx.lineCap = 'round';
ctx.clearRect(0, 0, size, size);
ctx.translate(halfSize, halfSize);
for (var i = 0, l = lines; i < l; i++) {
ctx.rotate(Math.PI * 2 / lines);
ctx.strokeStyle = 'rgba(' + options.color + ',' + ( i < (1/4 * l) ? 1/4 : i/l ) + ')';
ctx.beginPath();
ctx.moveTo(0, inner);
ctx.lineTo(0, outer);
ctx.stroke();
}
this.$el.css(cssPrefix + 'animation', this._addAnimation(lines) + ' ' + options.roundDuration + 's step-start infinite');
var style = { width: halfSize, height: halfSize };
style[cssPrefix + 'transform-origin'] = '0 0';
style[cssPrefix + 'transform'] = 'scale(0.5)';
this.$container.css(style);
// this.$el.appendTo(this.$container);
}
});
})(Zepto, JMU);
(function( $, JMU ) {
JMU.defineComponent( 'TextLoading', {
options: {
preventScroll: true,
content: ''
},
template: '<div class="jmu-text-loading">\
<div class="loading" data-color="255,255,255" data-size="36"></div>\
<div class="content jmu-color-white"></div>\
</div>',
_render: function(){
var options = this.options;
this.$el.find('.loading').loading('show');
this.$el.find('.content').html(options.content);
}
});
})(Zepto, JMU);
(function( $, JMU ) {
JMU.defineComponent( 'PageLoading', {
options: {
content: ''
},
template: '<div class="jmu-page-loading">\
<div class="loading"></div>\
<div class="content"></div>\
</div>',
_render: function(){
var options = this.options;
this.$el.find('.loading').loading('show');
this.$el.find('.content').html(options.content);
}
});
})(Zepto, JMU);
(function( $, JMU ) {
JMU.defineComponent( 'RichLoading', {
options: {
preventScroll: true,
content: '',
onClose: $.emptyFunction
},
template: '<div class="jmu-rich-loading">\
<div class="loading" data-color="255,255,255" data-size="36"></div>\
<div class="jmu-no-wrap content"></div>\
<div class="jmu-icon-close btn"></div>\
</div>',
_render: function(){
var options = this.options;
this.$el.find('.loading').loading('show');
this.$el.find('.content').html(options.content);
},
_bindEvents: function(){
var self = this;
this.$el.on('tap', '.btn', function(e){
var $btn = $(e.currentTarget);
$btn.active(function(){
self.options.onClose();
self.hide(); // 点击后隐藏dialog