-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.js
More file actions
executable file
·179 lines (158 loc) · 4.22 KB
/
utils.js
File metadata and controls
executable file
·179 lines (158 loc) · 4.22 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// utils.js
var util_funcs = {
methods: {
isSlotAvailable: function(dom) {
return dom.classList.contains('ds-avail');
},
// counting from top-left corner
getCoords: function(slot_dom) {
let x = slot_dom.cellIndex - 1;
let y = slot_dom.parentElement.rowIndex - 1;
return [x, y];
},
// generate time slot matrix (column major)
create2DMatrix(x_dim, y_dim, val=false) {
var mat = new Array(x_dim);
for (var i=0; i<x_dim; i++) {
mat[i] = new Array(y_dim).fill(val);
}
return mat;
},
// fill a rectangular area with the 2d @matrix with @val.
updateMatrix(mat, rect, val) {
var [[x0, y0], [x1, y1]] = rect;
if (x1<x0) { x0 = [x1, x1=x0][0]; } // swap
if (y1<y0) { y0 = [y1, y1=y0][0]; } // swap
if (x0<0 || y0<0 ||
x1>mat.length-1 || y1>mat[0].length-1) {
return false; // out of bound coordinates
}
for (var i=x0; i<=x1; i++) {
mat[i].fill(val, y0, y1+1);
}
return true;
},
// set all values in a matrix
resetMatrix(mat, val) {
mat.forEach((col) => col.fill(val));
},
// merge mat2 to mat1
mergeMatrices(mat1, mat2) {
mat1.forEach(function(col1, coli) {
let col2 = mat2[coli];
for (let rowi=0; rowi<col1.length; rowi++) {
if (col2[rowi] != null) {
col1[rowi] = col2[rowi];
}
}
});
},
// generate span data from matrix of current week
matrix2span(mat, drop_null=false) {
if (!this.cur_week) { return; } // exit if not initialized
var self = this;
var data = {}; // key is day, value is array of time spans
mat.forEach(function(col, coli) {
let spans = [];
let span = null;
for (var rowi=0; rowi<col.length; rowi++) {
if (col[rowi]) {
if (!span) {
// span starts at green
span = self.time_labels[rowi]
}
} else {
if (span) {
// span ends at white
span += " - "+self.time_labels[rowi];
spans.push(span);
span = null;
}
}
}
if (span) {
// span to very last row
spans.push(span+" - 12am");
}
let date = self.cur_week[coli].format('YYYY-MM-DD');
if (spans.length > 0) {
// day has time spans
data[date] = spans;
} else if (!drop_null) {
data[date] = null;
}
});
return data;
},
// generate matrix from span data (for current week)
span2matrix(data) {
if (!this.cur_week) { return; } // exit if not initialized
var self = this;
var mat = self.create2DMatrix(self.x_dim, self.y_dim, false);
self.cur_week.forEach(function(m, mi) {
let key = m.format('YYYY-MM-DD');
if (data[key]) {
data[key].forEach(function(span, si) {
let idx_pair = self.parseSpan(span); // [starti, stopi]
if (idx_pair) {
mat[mi].fill(true, idx_pair[0], idx_pair[1]);
}
});
}
});
return mat;
},
// generate matrix coordinates[start(x,y), end(x,y)]
// from span data (for current week)
span2coords(spans) {
if (!this.cur_week) { return; } // exit if not initialized
var self = this;
var coords = [];
self.cur_week.forEach(function(m, x) {
let key = m.format('YYYY-MM-DD');
if (spans[key]) {
spans[key].forEach(function(span, si) {
let ys = self.parseSpan(span); // [starti, stopi]
coords.push({
start: [x, ys[0]],
end: [x, ys[1]-1],
});
}); // end of spans[key].forEach()
}
}); // end of cur_week.forEach()
return coords;
},
// text of time span to matrix index
parseSpan(span) {
var time_pair = span.split(' - ');
var idx_pair = time_pair.map(this.time2index);
if (idx_pair[1] == 0) {
idx_pair[1] = 48;
}
if ((typeof idx_pair[0] != 'number') ||
(typeof idx_pair[1] != 'number') ||
(idx_pair[0] > idx_pair[1]) ||
(idx_pair[0] < 0) ||
(idx_pair[1] > 48)) {
return;
}
return idx_pair;
},
// time text to matrix index
time2index(time) {
var match = time.match(/(\d+)(?::(\d+))?(am|pm)/i);
if (match) {
let hr = parseInt(match[1])
let min = parseInt((match[2] == undefined) ? 0 : match[2]);
let ampm = match[3];
if (hr == 12 && ampm == "am") { hr = 0; }
let idx = (hr * 2)
+ (min / 30 >> 0)
+ ((ampm == "am") ? 0 : 24);
return idx;
}
return;
},
}
}
export default util_funcs;