forked from xzhhbx/jsbox_script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SemesterCalendar.js
185 lines (171 loc) · 5.96 KB
/
SemesterCalendar.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
const FILENAME = "学期日历.js"
function save(start_date, weeks) {
let data = JSON.stringify({
start_time: {
year: start_date.getFullYear(),
month: start_date.getMonth(),
day: start_date.getDate()
},
weeks: weeks
})
$console.info(data)
$file.write({
data: $data({ string: data }),
path: FILENAME
})
}
function refreshUI() {
let env = $app.env
let data = null
let start_date = null
let today = new Date()
let weeks = null
if ($file.exists(FILENAME)) {
$console.info("Got file")
data = JSON.parse($file.read(FILENAME).string)
let dd = data['start_time']
start_date = new Date(parseInt(dd["year"]), parseInt(dd["month"]), parseInt(dd["day"]))
weeks = data['weeks']
} else {
$console.info("No file")
}
let day_gap = null
let firstday = null
let weekidx = null
if (start_date && weeks) {
day_gap = (today.getTime() - start_date.getTime()) / 86400000
firstday = start_date.getDay() || 7
weekidx = (day_gap - (8 - firstday)) / 7 + 2
}
if (env == 1) {
$ui.render({
type: "view",
props: {
id: "main_view",
title: "学期日历",
},
layout: $layout.center,
views: [{
type: "label",
props: {
type: $kbType.number,
id: "start_date",
text: start_date ? `${start_date.getFullYear()}-${(start_date.getMonth() + 1)}-${start_date.getDate()}` : '点击设置起始时间',
align: $align.center,
},
layout: function (make, view) {
make.left.equalTo(30)
make.width.equalTo(150)
make.height.equalTo(50)
},
events: {
tapped: sender => {
$input.text({
type: $kbType.text,
placeholder: "起始日期,xxxx-xx-xx",
handler: function (text) {
let date = text.split('-').map(x => parseInt(x))
start_date = new Date(date[0], date[1] - 1, date[2])
save(start_date, weeks)
refreshUI()
}
})
}
}
}, {
type: "label",
props: {
id: "weeks",
text: weeks ? `${weeks}` : '点击设置周数',
align: $align.center,
},
layout: function (make, view) {
make.left.equalTo($("start_date").right).offset(-10)
make.width.equalTo(120)
make.height.equalTo(50)
},
events: {
tapped: sender => {
$input.text({
type: $kbType.number,
placeholder: "周数",
handler: function (text) {
weeks = parseInt(text)
save(start_date, weeks)
refreshUI()
}
})
}
}
}, {
type: "progress",
props: {
id: "progress",
radius: 10,
value: weekidx ? weekidx / weeks : 0
},
layout: function (make, view) {
make.top.equalTo($("weeks").bottom).offset(0)
make.centerX.equalTo(view.super)
make.height.equalTo(40)
make.width.equalTo(view.super.width).offset(-40)
},
events: {
tapped: sender => {
$share.sheet($("main_view").snapshot)
}
}
}, {
type: "label",
props: {
text: weekidx ? "当前为第" + parseInt(weekidx) + "周" : '请先设置起始日期和周数',
textColor: $color("#fff")
},
layout: function (make, view) {
make.center.equalTo($("progress"))
}
}]
})
} else if (env == 2) {
$ui.render({
props: {
title: ""
},
views: [{
type: "view",
props: {
id: "main_view"
},
layout: $layout.fill,
views: [{
type: "progress",
props: {
id: "progress",
radius: 10,
value: weekidx ? weekidx / weeks : 0
},
layout: function (make, view) {
make.center.equalTo(view.super)
make.height.equalTo(40)
make.width.equalTo(view.super.width).offset(-40)
},
events: {
tapped: sender => {
$share.sheet($("main_view").snapshot)
}
}
}, {
type: "label",
props: {
text: weekidx ? "当前为第" + parseInt(weekidx) + "周" : '请先设置起始日期和周数',
textColor: $color("#fff")
},
layout: function (make, view) {
make.center.equalTo($("progress"))
}
}]
}]
})
}
}
refreshUI()