-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy patht.js
More file actions
162 lines (159 loc) · 5.29 KB
/
Copy patht.js
File metadata and controls
162 lines (159 loc) · 5.29 KB
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
// 抓到的JS,不是自己写的,不用管里面内容
function t() {
let now = new Date(),
today = {
year: now.getFullYear(),
month: now.getMonth() + 1,
day: now.getDate(),
},
gHolidayDropdownCtrl,
defaultHolidayOpt = {
value: 'default',
text: '假期安排',
selected: 1,
},
Utils = {
doubleDate: function (str) {
return ('0' + str).slice(-2)
},
formatDate: function (date) {
if (date.year && date.month && date.day)
date = [date.year, date.month, date.day].join('-')
return date.replace(/-(\d[^\d])/, '-0$1').replace(/-(\d)$/, '-0$1')
},
formatTpl: function (str, obj) {
return str.replace(/#{([^}]+)}/g, function (match, ret) {
return obj[ret] || ''
})
},
cutWord: function (str, len) {
for (var count = 0, code, i = 0; str.charCodeAt(i); ) {
if (((code = str.charCodeAt(i) > 255 ? 2 : 1), code + count > len))
return str.slice(0, --i) + '...'
;(count += code), i++
}
return str.slice(0, i)
},
filterListdByStrLen: function (list, len) {
for (var i = 0; i < list.length; i++)
if (list[i] && list[i].length <= len) return list[i]
},
},
DataCache = {
almanacs: {},
holidays: {},
classifyAlmanacsByYearMonth: function (almanacs) {
for (var i = 0, item, newcome = {}; (item = almanacs[i++]); ) {
if (1 === +item.status) item.holiday = 1
else if (2 === +item.status) item.work = 1
else if ('六' === item.cnDay || '日' === item.cnDay) item.weekend = 1
var ymkey = Utils.formatDate([item.year, item.month].join('-'))
if (!this.almanacs[ymkey])
(this.almanacs[ymkey] = [item]), (newcome[ymkey] = true)
else if (newcome[ymkey]) this.almanacs[ymkey].push(item)
}
},
setHolidays: function (holidays) {
for (var i = 0, item; (item = holidays[i++]); )
this.holidays[item.year] = item.list
},
getAlmanacsByDate: function (date) {
if ('object' === typeof date) date = [date.year, date.month].join('-')
else date = date.split('-').slice(0, 2).join('-')
var ymkey = Utils.formatDate(date),
ret = null
if (this.almanacs[ymkey]) {
ret = []
for (var i = 0; i < this.almanacs[ymkey].length; i++)
ret.push($.extend({}, this.almanacs[ymkey][i]))
}
return ret
},
getHolidaysByYear: function (year) {
return this.holidays[year]
},
getSingleDate: function (date) {
for (
var ymkey = Utils.formatDate([date.year, date.month].join('-')),
list = this.almanacs[ymkey],
i = 0;
i < list.length;
i++
)
if (
+list[i].year === date.year &&
+list[i].month === date.month &&
+list[i].day === date.day
)
return list[i]
},
},
CalendarService = {
fetchAlmanacs: function (date, cb) {
gthis.ajax(date.year + '年' + date.month + '月', 39043, {
params: {
tn: 'wisetpl',
format: 'json',
},
success: function (res) {
try {
DataCache.classifyAlmanacsByYearMonth(res[0].almanac), cb()
} catch (e) {}
},
})
},
build: function (date) {
var cndayMap = {
一: 1,
二: 2,
三: 3,
四: 4,
五: 5,
六: 6,
日: 7,
},
currMonthDays = DataCache.getAlmanacsByDate(date)
if (!currMonthDays) return null
var prevMonthDate = this.getAdjacentMonthDate(date, -1),
prevMonthDays = DataCache.getAlmanacsByDate(prevMonthDate),
headLost = cndayMap[currMonthDays[0].cnDay] - 1
if (prevMonthDays)
for (var ds = prevMonthDays.slice(-headLost); headLost-- > 0; ) {
var d = ds.pop()
;(d.othermonth = true), currMonthDays.unshift(d)
}
else return null
var nextMonthDate = this.getAdjacentMonthDate(date, 1),
nextMonthDays = DataCache.getAlmanacsByDate(nextMonthDate),
tailLost = 42 - currMonthDays.length
if (nextMonthDays)
for (var ds = nextMonthDays.slice(0, tailLost); tailLost-- > 0; ) {
var d = ds.shift()
;(d.othermonth = true), currMonthDays.push(d)
}
else return null
for (var cn = currMonthDays.length, matrix = []; cn > 0; )
matrix.unshift(currMonthDays.slice(cn - 7, cn)), (cn -= 7)
return matrix
},
getLastDay: function (y, m) {
return new Date(y, m, 0).getDate()
},
getAdjacentMonthDate: function (date, direction) {
var year = +date.year,
month = +date.month + direction,
day = +date.day
if (-1 === direction && month < 1) (month = 12), year--
else if (1 === direction && month > 12) (month = 1), year++
var lastDay = this.getLastDay(year, month)
if (day > lastDay) day = lastDay
return {
year: year,
month: month,
day: day,
}
},
}
return DataCache
}
module.exports = t