-
Notifications
You must be signed in to change notification settings - Fork 4
/
ui.js
1389 lines (1186 loc) · 38.2 KB
/
ui.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
/*!
* Z.js v0.1.0
* @snandy 2014-06-01 23:39:57
*
*/
/**
* 拖拽插件
*
* 使用 Use
* Z.ui.Dragable(option)
*
* 配置 Option
* elem: // DOM元素
* handle: // @string 鼠标按下开始拖动的元素
* canDrag: // @boolean 默认: true
* axis: // @string 拖拽方向,默认: "xy"。x: 仅水平方向,y: 仅垂直方向
* area: // @array [minX,maxX,minY,maxY] 拖拽范围 默认任意拖动
* inwin: // @boolean 仅在浏览器窗口内拖动
* cursor: // @string 鼠标状态
* zIndex: // @number 拖拽时zIndex值
* fixed: // @boolean 出现滚动条时保持fixed 默认true
*
* 方法 Method
* stopDrag // 停止拖拽
* startDrag // 开启拖拽
*
* 事件 Event
* start 开始拖拽
* drag 拖拽中
* end 拖拽结束
*
*/
Z.declareUI('Dragdrop', function() {
var winObj = Z(window)
var docObj = Z(document)
var doc = docObj[0]
var ZF = Z.Function
var axisReg = /^xy$/
this.init = function(elem, option) {
// 相关属性数据
this.elem = Z.isElement(elem) ? elem : Z(elem)[0]
this.reset(option)
// 暂存配置对象
this.dragObj.data('originData', Z.extend({}, option))
// 鼠标mousedown
var mousedown = ZF.bind(this.onMousedown, this)
this.downObj.mousedown(function(ev) {
mousedown(ev)
})
}
this.reset = function(option) {
this.handle = option.handle
this.canDrag = option.canDrag !== false
this.axis = option.axis || 'xy'
this.area = option.area || []
this.inwin = option.inwin || false
this.cursor = option.cursor || 'move'
this.zIndex = option.zIndex || ''
this.dragObj = Z(this.elem)
this.downObj = this.handle ? this.dragObj.find(this.handle) : this.dragObj
// 设置鼠标状态
this.downObj.css('cursor', this.cursor)
}
this.onMousedown = function(ev) {
var dragElem = this.dragObj[0]
// 模拟拖拽,需要设置为绝对定位
this.dragObj.css('position', 'absolute')
// 鼠标按下的时候计算下window的宽高,拖拽元素尺寸,不要再mousemove内计算
var viewSize = Z.viewSize()
this.dragElemWidth = Math.max(dragElem.offsetWidth, dragElem.clientWidth)
this.dragElemHeight = Math.max(dragElem.offsetHeight, dragElem.clientHeight)
this.dragElemMarginTop = parseInt(dragElem.style.marginTop, 10) || 0
this.dragElemMarginLeft = parseInt(dragElem.style.marginLeft, 10) || 0
// 仅在窗口可视范围内移动
if (this.inwin) {
var winX = viewSize.width - this.dragElemWidth
var winY = viewSize.height - this.dragElemHeight
this.area = [0, winX, 0, winY]
}
var mousemove = ZF.bind(this.onMousemove, this)
this.mouseup = ZF.bind(this.onMouseup, this)
this.mousemove = function(ev) {
mousemove(ev)
}
if (window.captureEvents) { //标准DOM
ev.stopPropagation()
ev.preventDefault()
winObj.blur(this.mouseup)
} else if(dragElem.setCapture) { //IE
dragElem.setCapture()
ev.cancelBubble = true
this.dragObj.on('losecapture', this.mouseup)
}
this.diffX = ev.clientX - dragElem.offsetLeft
this.diffY = ev.clientY - dragElem.offsetTop
// 开始拖拽
docObj.mousemove(this.mousemove)
docObj.mouseup(this.mouseup)
// starg 事件
this.fire('start')
}
this.onMousemove = function(ev) {
var minX, maxX, minY, maxY
var moveX = ev.clientX - this.diffX
var moveY = ev.clientY - this.diffY
var dragObj = this.dragObj
var dragElem = dragObj[0]
var area = this.area
// 设置拖拽范围
if (this.area) {
minX = area[0]
maxX = area[1]
minY = area[2]
maxY = area[3]
moveX < minX && (moveX = minX) // left 最小值
moveX > maxX && (moveX = maxX) // left 最大值
moveY < minY && (moveY = minY) // top 最小值
moveY > maxY && (moveY = maxY) // top 最大值
}
// 设置是否可拖拽,有时可能有取消元素拖拽行为的需求
if (this.canDrag) {
var axis = this.axis
//设置位置,并修正margin
moveX = moveX - this.dragElemMarginTop
moveY = moveY - this.dragElemMarginLeft
if (axis === 'x' || axisReg.test(axis)) {
dragElem.style.left = moveX + 'px'
}
if (axis === 'y' || axisReg.test(axis)) {
dragElem.style.top = moveY + 'px'
}
}
// drag 事件
this.fire('drag', moveX, moveY)
}
this.onMouseup = function(ev) {
var self = this
var dragElem = this.dragObj[0]
// 删除事件注册
docObj.off('mousemove', this.mousemove)
docObj.off('mouseup', this.mouseup)
if (window.releaseEvents) { //标准DOM
winObj.off('blur', this.mouseup)
} else if (dargElem.releaseCapture) { //IE
dragObj.off('losecapture', this.mouseup)
dragElem.releaseCapture()
}
// end 事件
this.fire('end')
}
this.stopDrag = function() {
this.canDrag = false
}
this.startDrag = function() {
this.canDrag = true
}
this.setAxis = function(xy) {
this.axis = xy
}
})
/**
* 页签组件
*
* 使用 Use
* Z.ui.Tab(elem, option)
* Z.ui.Tab('.single', {eventType: 'click'})
*
* 配置 Option
* elem // DOM元素或CSS选择器
* eventType: // 默认 "mouseover",鼠标移动到上面时切换,可选 "click"
* currClass: // 默认 "curr"
* autoPlay: // 是否自动播放,默认false
* interval: // 自动播放时间间隔
* attrName: // tab的css属性选择器的key,默认为 data-ui
* tabNavVal: // tab的css属性选择器的key,默认为 tab-nav
* tabConVal: // tab content的css属性选择器的key,默认为 tab-content
* index: // 指定当前的tab, autoPlay必须为true
*
* 方法 Method
* paly 播放
* stop 停止播放
*
* 事件 Event
* change 页签切换事件
*
*/
Z.declareUI('Tab', function() {
var ZF = Z.Function
this.init = function(elem, option) {
var option = option || {}
// 内部状态变量
this.elem = Z.isElement(elem) ? elem : Z(elem)[0]
this.eventType = option.eventType || 'mouseover'
this.currClass = option.currClass || 'curr'
this.autoPlay = option.autoPlay || false
this.interval = option.interval || 5000
this.currIndex = option.currIndex || 0
this.attrName = option.attrName || 'data-ui'
this.tabNavVal = option.tabNavVal || 'tab-nav'
this.tabConVal = option.tabConVal || 'tab-content'
// DOM element
var attrName = this.attrName
var tabNavVal = this.tabNavVal
var tabConVal = this.tabConVal
var elemObj = this.elemObj = Z(this.elem)
var navObj = this.navObj = elemObj.find('[' + attrName + '=' + tabNavVal + ']')
var contentObj = this.contentObj = elemObj.find('[' + attrName + '=' + tabConVal + ']')
if (navObj.length != contentObj.length) {
throw new Error('navObj and contentObj length must be equal')
}
// tab长度
this.length = navObj.length
// 设置当前tab,默认为第一个
if (this.currIndex) {
navObj.removeClass(this.currClass)
contentObj.hide()
this.change(this.currIndex)
}
// 自动播放
if (this.autoPlay) {
this.play()
}
// 事件
var self = this
elemObj.delegate('[' + attrName + '=' + tabNavVal + ']', this.eventType, function(ev) {
var index = navObj.index(this)
self.change(index)
})
}
this.change = function(index) {
var navObj = this.navObj
var contentObj = this.contentObj
var currClass = this.currClass
var nav = navObj.eq(index)
var content = contentObj.eq(index)
this._preNav = this._preNav ? this._preNav : nav
this._preCon = this._preCon ? this._preCon : content
// nav
this._preNav.removeClass(currClass)
nav.addClass(currClass)
this._preNav = nav
// content
this._preCon.hide()
content.show()
this._preCon = content
// 充值当前的索引
this.currIndex = index
// change事件
this.fire('change', index)
}
this.play = function() {
var self = this
this.timer = setInterval(function() {
// 递增顺序播放
var index = self.currIndex + 1
// 到达最后一个tab后,从第一个开始
if (index === self.length) {
index = 0
}
self.change(index)
}, this.interval)
}
this.stop = function() {
clearInterval(this.timer)
}
})
Z.declareUI('Suggest', function() {
function create(tag, cls) {
var el = document.createElement(tag)
if (cls) {
el.className = cls
}
return Z(el)
}
this.init = function(input, option) {
this.input = Z(input)
this.ulCls = option.ulCls || ''
this.liCls = option.liCls || 'suggest-li'
this.currCls = option.currCls || 'curr'
this.url = option.url || ''
this.data = option.data || []
this.processData = option.processData || function(data) {return data}
this.processLi = option.processLi || function(v1, v2) { return v1 }
this.processVal = option.processVal || function(v1, v2) { return v1 }
this.currLi = null
var posi = this.input.offset()
this.div = create('div', 'suggest-div').hide()
this.div.css({
position: 'absolute',
top: posi.top + this.input[0].offsetHeight - 1,
left: posi.left,
width: this.input[0].offsetWidth - 2
})
this.ul = create('ul', option.ulCls)
this.div.append(this.ul)
Z(document.body).append(this.div)
this.events()
this.fire('init')
}
this.events = function() {
this.input.on('keyup', {
context: this,
handler: function(ev) {
if (this.input.value === '') {
this.hide()
} else {
this.onKeyup(ev)
}
}
}).on('blur', {
context: this,
handler: function() {
this.hide()
}
})
var self = this
var currCls = self.currCls
this.ul.delegate('li', 'mousedown', function() {
var li = Z(this)
self.input.val( li.attr('data-val') )
}).delegate('li', 'mouseover', function() {
var li = Z(this)
li.addClass(currCls)
self.currLi = li
}).delegate('li', 'mouseout', function() {
var li = Z(this)
li.removeClass(currCls)
})
}
this.hide = function() {
this.div.hide()
this.visible = false
this.fire('hide')
}
this.show = function() {
this.div.show()
this.visible = true
this.fire('show')
}
this.render = function() {
var iVal = this.input.val()
if (iVal === '') {
this.hide()
return
}
this.ul.empty()
Z.each(this.data, function(it, i) {
var li = create('li', this.liCls)
var fVal = this.processVal(it, iVal)
li.attr('data-val', fVal)
li.html( this.processLi(it, iVal) )
this.ul.append(li)
}, this)
this.visible = true
this.show()
this.fire('render')
}
this.specKeys = function(keyCode) {
var ul = this.ul
var lis = ul.find('li')
var input = this.input
var currLi = this.currLi
var liCls = this.liCls
var currCls = this.currCls
switch (keyCode) {
case 13: // Enter
if (currLi) {
input.val( currLi.attr('data-val') )
this.hide()
}
return
case 38: // 方向键上
if (currLi == null) {
this.currLi = lis.last()
this.currLi.addClass(currCls)
input.val( this.currLi.attr('data-val') )
} else {
if (this.currLi.prev().length) {
this.currLi.removeClass(currCls)
this.currLi = this.currLi.prev()
this.currLi.addClass(currCls)
input.val( this.currLi.attr('data-val') )
} else {
this.currLi.removeClass(currCls)
this.currLi = null
input[0].focus()
input.val(this.finalValue)
}
}
return
case 40: // 方向键下
if (currLi == null) {
this.currLi = lis.first()
this.currLi.addClass(currCls)
input.val( this.currLi.attr('data-val') )
} else {
if (this.currLi.next().length) {
this.currLi.removeClass(currCls)
this.currLi = this.currLi.next()
this.currLi.addClass(currCls)
input.val( this.currLi.attr('data-val') )
} else {
this.currLi.removeClass(currCls)
this.currLi = null
input[0].focus()
input.val(this.finalValue)
}
}
return
case 27: // ESC键
this.hide();
input.val(this.finalValue)
return
}
}
this.onKeyup = function(ev) {
var self = this
var input = this.input
var keyCode = ev.keyCode
if ( (keyCode === 13 || keyCode === 27 || keyCode ===38 || keyCode === 40) && this.visible) {
this.specKeys(keyCode)
} else {
this.lis = []
var val = input.val()
if (this.finalValue !== val) {
if (this.url) {
Z.get(this.url, function(data) {
self.data = self.processData(data)
self.render()
})
} else {
this.render()
}
this.finalValue = val
}
}
}
})
Z.declareUI('Calendar', function() {
var reDate = /^\d{4}\-\d{1,2}\-\d{1,2}$/
var week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
var months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
// 是否是闰年
function isLeapYear(year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}
/*
* 根据年月计算天数
*/
function calculateDays(year, month) {
var days = months[month]
// 2月比较特殊,非闰年28天,闰年29天,如2008年2月为29天
if ( 1 == month && isLeapYear(year) ) {
days = 29
}
return days
}
function format(date, hasDay) {
var arr, m, d, day
if (Z.isString(date)) {
arr = date.split('-')
date = new Date(arr[0], arr[1]-1, arr[2])
}
var mm = date.getMonth()
var dd = date.getDate()
if (mm < 9) {
m = '0' + (mm + 1)
} else {
m = mm + 1
}
if (dd < 10) {
d = '0' + dd
} else {
d = dd
}
var str = date.getFullYear() + '-' + m + '-' + d
if (hasDay) {
day = week[date.getDay()]
str += ' ' + day
}
return str
}
this.init = function(input, option) {
option = option || {}
this.x = option.x || 0
this.y = option.y || 0
this.hasDay = option.hasDay
this.startDate = option.startDate
this.endDate = option.endDate
this.chosenDate = option.chosenDate
this.dateCls = option.dateCls || 'day'
this.chosenCls = option.chosenCls || 'chosen'
this.dateOverCls = option.dateOverCls || 'over'
this.prevHook = option.prevHook || '.prev'
this.nextHook = option.nextHook || '.next'
this.closeHook = option.closeHook || '.close'
this.todayHook = option.todayHook || '.today'
this.yearHook = option.yearHook || '[data-cal=year]'
this.monthHook = option.monthHook || '[data-cal=month]'
this.currDate = new Date()
this.input = Z(input)
var self = this
this.input.click(function(ev) {
var input = Z(this)
// 已经初始化过直接返回
if ( input.data('hasDatepicker') ) return
self.render()
})
}
this.setPosi = function() {
var posi = this.input.offset()
var outerHeight = this.input.outerHeight()
var left = (this.x ? this.x-0 : 0) + posi.left
var top = (this.y ? this.y-0 : 0) + posi.top + outerHeight
this.div.css({
position: 'absolute',
left: left,
top: top
})
}
this.onBodyClick = function(ev) {
var target = Z(ev.target)
var datePicker = target.closest('.o-datepicker')
if (!datePicker.length && target[0] != this.input[0]) {
this.remove()
}
}
this.template = function() {
var div = Z.dom('<div class="o-datepicker ui-calendar"></div>')
div = Z(div)
var templ = '<table cellpadding="0" cellpadding="0" class="ui-calendar-table">' +
'<thead>' +
'<tr><th class="prev"><i></i></th><th colspan="5" class="switch"><span data-cal="year"></span>年<span data-cal="month"></span>月</th><th class="next"><i></i></th></tr>' +
'<tr><th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr>' +
'</thead>' +
'<tbody></tbody>' +
'<tfoot></tfoot>' +
'</table>'
div.append(templ)
table = div.find('table')
table.find('tfoot').append('<tr><td colspan="2"><span class="today">今天</span></td><td></td><td></td><td></td><td colspan="2"><span class="close">关闭</span></td></tr>')
var tr = '<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>'
var arr = []
for (var i = 0; i < 6; i++) {
arr[i] = tr
}
table.find('tbody').append(arr.join(''))
return div.append(table)
}
this.render = function() {
var input = this.input
var chosenDate = this.chosenDate
var val = input.val()
if ( val && reDate.test(val) ) {
val = val.split('-')
this.currDate = new Date(val[0], val[1] - 1, val[2])
} else {
if ( Z.isDate(chosenDate) ) {
this.currDate = chosenDate
} else {
if ( reDate.test(chosenDate) ) {
var arr = chosenDate.split('-')
this.currDate = new Date(arr[0], arr[1]-1, arr[2])
}
}
}
currDate = this.currDate
this.div = this.template()
var table = this.table = this.div.find('table')
var yearSpan = table.find(this.yearHook)
var monthSpan = table.find(this.monthHook)
var cMonth = currDate.getMonth()
var cYear = currDate.getFullYear()
if (cMonth == 11) {
yearSpan.text(cYear)
monthSpan.text(cMonth + 1)
} else {
yearSpan.text(cYear)
monthSpan.text(cMonth + 1)
}
// 回填 '天'
this.fillDate()
// 添加事件
this.events()
// 标记input已经弹出了日期组件
input.data('hasDatepicker', true)
// 设置日期组件的位置
this.setPosi()
// 添加到body
Z('body').append(this.div)
}
this.fillDate = function() {
var currDate = this.currDate
var startDate = this.startDate
var endDate = this.endDate
// fill td
var table = this.table
var tds = table.find('tbody').find('td').empty().removeClass()
var cYear = table.find(this.yearHook).text() - 0
var cMonth = table.find(this.monthHook).text() - 1
var qDate = new Date(cYear, cMonth, 1)
var rDate = new Date()
var aDay = qDate.getDay()
var days = calculateDays(cYear, cMonth)
// 填充数字,并高亮当前天
for (var i = 0; i < days; i++) {
var td = tds.eq(i + aDay)
td.text(i + 1)
// 年月日都一样就高亮显示
if (i + 1 == currDate.getDate() && cMonth == currDate.getMonth() && cYear == currDate.getFullYear()) {
td.addClass(this.chosenCls)
}
}
var start = 0
var end = days
var hasDate = true
if ( startDate && reDate.test(startDate) ) {
var arr = startDate.split('-')
var year = arr[0] - 0
var month = arr[1] - 1
var day = arr[2] - 1
if (cYear == year && cMonth == month) {
start = day
}
if (cYear < year || cMonth < month && cYear <= year) {
hasDate = false
}
}
if ( endDate && reDate.test(endDate) ) {
var arr = endDate.split('-')
var year = arr[0] - 0
var month = arr[1] - 1
var day = arr[2] - 1
if (cYear == year && cMonth == month) {
end = day
}
if (cYear > year || cMonth > month && cYear == year) {
hasDate = false
}
}
if (hasDate) {
for (var u = start; u < end; u++) {
var td = tds.eq(u + aDay)
td.addClass('day')
}
}
}
this.events = function() {
var self = this
var dateCls = '.' + this.dateCls
var dateOverCls = this.dateOverCls
this.div.delegate(dateCls, 'click', function(ev) {
var td = Z(this)
var table = td.closest('table')
var year = table.find(self.yearHook).text()
var month = table.find(self.monthHook).text() - 1
var date = td.text()
self.currDate = new Date(year, month, date)
self.remove()
self.fire('select')
}).delegate(dateCls, 'mouseover', function() {
Z(this).addClass(dateOverCls)
}).delegate(dateCls, 'mouseout', function() {
Z(this).removeClass(dateOverCls)
}).delegate(this.todayHook, 'click', function() {
self.currDate = new Date()
self.remove()
self.fire('select')
}).delegate(this.closeHook, 'click', function() {
self.remove()
self.fire('close')
}).delegate(this.prevHook, 'click', function() {
var span = Z(this)
if (span.hasClass('disabled')) return false
self.prevMonth()
}).delegate(this.nextHook, 'click', function() {
var span = Z(this)
if (span.hasClass('disabled')) return false
self.nextMonth()
})
Z(window).on('resize', {
context: this,
handler: this.setPosi
})
Z(document).on('click', {
context: this,
handler: this.onBodyClick
})
}
this.nextMonth = function() {
var year = this.table.find(this.yearHook)
var month = this.table.find(this.monthHook)
var y = year.text() - 0
var m = month.text() - 1
switch (m) {
case 11:
year.text(y+1)
month.text(1)
break
case 0:
year.text(y)
month.text(2)
break
default:
m += 1
year.text(y)
month.text(m + 1)
break
}
this.fillDate()
}
this.prevMonth = function() {
var year = this.table.find(this.yearHook)
var month = this.table.find(this.monthHook)
var y = year.text() - 0
var m = month.text() - 1
if (m == 0) {
year.text(y-1)
month.text(12)
} else {
year.text(y)
month.text(m)
}
this.fillDate()
}
this.remove = function() {
this.div.remove()
this.input.val( format(this.currDate, this.hasDay) )
this.input.data('hasDatepicker', false)
Z(window).off('resize', this.setPosi)
Z(document).off('click', this.onBodyClick)
}
})
Z.declareUI('CalendarTwo', function() {
//
var week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
var months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
var reDate = /^\d{4}\-\d{1,2}\-\d{1,2}$/
function format(date, hasDay) {
var arr, m, d, day
if (Z.isString(date)) {
arr = date.split('-')
date = new Date(arr[0], arr[1]-1, arr[2])
}
var mm = date.getMonth()
var dd = date.getDate()
if (mm < 9) {
m = '0' + (mm + 1)
} else {
m = mm + 1
}
if (dd < 10) {
d = '0' + dd
} else {
d = dd
}
var str = date.getFullYear() + '-' + m + '-' + d
if (hasDay) {
day = week[date.getDay()]
str += ' ' + day
}
return str
}
function template() {
var templ = '<table cellpadding="0" cellpadding="0" class="datepicker">' +
'<thead>' +
'<tr class="controls"><th colspan="7"><span class="prevMonth"><s></s></span><span class="currDate"><span class="currYs"></span>年<span class="currMo"></span>月</span></th></tr>' +
'<tr class="days"><th class="org sun">日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th class="org sat">六</th></tr>' +
'</thead>' +
'<tbody></tbody>' +
'<tfoot><tr><td colspan="7"></td></tr></tfoot>' +
'</table>'
var table1 = Z.dom(templ)[0]
table1 = Z(table1)
table1.find('tfoot').find('td').append('<span class="today">今天</span>')
var table2 = Z.dom(templ)[0]
table2 = Z(table2)
table2.find('.prevMonth').replaceClass('prevMonth', 'nextMonth')
table2.find('tfoot').find('td').append('<span class="close">关闭</span>')
var tr = '<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>'
var arr = []
for (var i = 0; i < 6; i++) {
arr[i] = tr
}
table1.find('tbody').append(arr.join(''))
table2.find('tbody').append(arr.join(''))
var div = Z.dom('<div class="o-datepicker"></div>')
div = Z(div)
return div.append(table1).append(table2)
}
this.init = function(input, option) {
option = option || {}
this.x = option.x || 0
this.y = option.y || 0
this.hasDay = option.hasDay
this.startDate = option.startDate
this.endDate = option.endDate
this.chosenDate = option.chosenDate
this.currDate = new Date()
this.input = Z(input)
var self = this
this.input.click(function(ev) {
var input = Z(this)
// 已经初始化过直接返回
if ( input.data('hasDatepicker') ) return
self.render()
})
}
this.setPosi = function() {
var posi = this.input.offset()
var outerHeight = this.input.outerHeight()
var left = (this.x ? this.x-0 : 0) + posi.left
var top = (this.y ? this.y-0 : 0) + posi.top + outerHeight
this.div.css({
position: 'absolute',
left: left,
top: top
})
}
this.onBodyClick = function(ev) {
var target = Z(ev.target)
if (!target.closest('.datepicker').length && target[0] != this.input[0]) {
this.remove()
}
}
this.remove = function() {
this.div.remove()
this.input.val( format(this.currDate, this.hasDay) )
this.input.data('hasDatepicker', false)
Z(window).off('resize', this.setPosi)
Z(document).off('click', this.onBodyClick)
}
this.events = function() {
var self = this
this.div.delegate('.date', 'click', function(ev) {
var td = Z(this)
var table = td.closest('table')
var year = table.find('.currYs').text()
var month = table.find('.currMo').text() - 1
var date = td.text()
self.currDate = new Date(year, month, date)
self.remove()
self.fire('select')
}).delegate('.date', 'mouseover', function() {
Z(this).addClass('over')
}).delegate('.date', 'mouseout', function() {
Z(this).removeClass('over')
}).delegate('.today', 'click', function() {
self.currDate = new Date()
self.remove()
self.fire('select')
}).delegate('.close', 'click', function() {
self.remove()
self.fire('close')
}).delegate('.prevMonth', 'click', function() {
var span = Z(this)
if (span.hasClass('disabled')) return false
self.prevMonth()