This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjst-timeline.js
309 lines (273 loc) · 8.8 KB
/
jst-timeline.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
let timelinesData = {};
/**
* @description Load calendar with provided data
* @param {{label: string, date: string, id: any, iconClass: string, customClass: string}[]} data
* @param {showEmptyDates: boolean, iconClasses: any, momentFormat: string, dataShowFormat: string} options
*/
$.fn.loadTimeline = function(data, options) {
const { showEmptyDates, iconClasses, reverse } = options;
let { momentFormat, dataShowFormat } = options;
momentFormat = momentFormat ? momentFormat : "DD/MM/YYYY";
dataShowFormat = dataShowFormat ? dataShowFormat : "DD/MM/YYYY";
const dataToOrder = data;
let orderedData = [];
let arr = [];
if (showEmptyDates) {
// Verify wich months and years have data
dataToOrder.forEach(elem => {
const monthAndYear = moment(elem.date, momentFormat).format("MM/YYYY");
if (!arr.some(elem => monthAndYear === elem)) arr.push(monthAndYear);
});
// Add everydays of months and years with data
arr.forEach(monthAndYear => {
const momentD = moment(`01/${monthAndYear}`, "DD/MM/YYYY");
const days = momentD.daysInMonth();
let day = 1;
Array.from({ length: days }, () => {
const rightDate = momentD.format(dataShowFormat);
orderedData.push({ date: rightDate });
day++;
momentD.date(day);
});
});
}
if (dataToOrder.length > 1) {
orderedData = showEmptyDates ? getDate(data) : quickSort(data);
if (reverse) orderedData = orderedData.reverse();
orderedData.forEach(elem => {
if (elem.label) {
elem.date = toBrasilianDate(elem.date);
if (iconClasses)
elem.iconClass = elem.iconClass
? elem.iconClass
: getIcon(elem.iconId);
}
});
} else {
orderedData = dataToOrder;
orderedData[0].date = toBrasilianDate(orderedData[0].date);
// Check if there's a specific icon to the data or search for a icon if not
if (iconClasses) {
orderedData[0].iconClass = orderedData[0].iconClass
? orderedData[0].iconClass
: getIcon(orderedData[0].iconId);
}
}
const id = $(this).attr("id");
// Clears the box before add another
$(this).destroyTimeine();
timelinesData[id] = orderedData;
$(this).attr("class", "timeline-box");
$(this).append(
'<div class="line-timeline"></div><ul class="father-box" style="transform: translateX(0px);"></ul>'
);
// Add incoming array data to the timeline
for (let item of orderedData) {
const { label, date, iconClass, customClass, id } = item;
if (label) {
$(this).find(".father-box").append(`
<li class="data-box${customClass ? ` ${customClass}` : ""}" ${
iconClass ? "" : 'style="margin-top: 15px;"'
}${id ? ` id="${id}"` : ""}>
<div class="data"${id ? ` id="${id}"` : ""}>
${
iconClass
? `<span class="${iconClass}" aria-hidden="true"></span><br />`
: ""
}
<b>${label}</b>
</div>
<div class="vertical-line"></div>
<div class="ball"></div>
<p class="find-date">${date}</p>
</li>
`);
} else if (!label && showEmptyDates) {
$(this)
.find(".father-box")
.append(
`<li class="empty-day-box""><span class="empty-day${
isFirstDay(date) ? " empty-first-day" : ""
}"></span>
<span class="find-date date hide-date">${date}</span></li>`
);
}
}
// Handle events
$(this).turnoffEvents();
dragEvents(orderedData, this);
if (showEmptyDates) {
showHideDatesOnHover(this);
}
function isFirstDay(date) {
const momentDate = moment(date, momentFormat);
return momentDate.date() === 1;
}
/**
* @description Order array with an quicksort
* @param {any[]} array
*/
function quickSort(array) {
let smaller = [];
let larger = [];
if (array.length <= 1) return array;
const momentDate1 = moment(array[0].date, momentFormat);
for (let i = 1; i < array.length; i++) {
const momentDate2 = moment(array[i].date, momentFormat);
if (momentDate2.isAfter(momentDate1)) {
larger.push(array[i]);
}
if (momentDate2.isSameOrBefore(momentDate1)) {
smaller.push(array[i]);
}
}
return quickSort(smaller).concat(array[0], quickSort(larger));
}
/**
* @description get date array if this is to show emptyDates
* @param {any[]} array
*/
function getDate(array) {
array.forEach(elem => {
const find = orderedData.findIndex(searchElem => {
const momentDate = moment(elem.date, momentFormat);
const formatedDate = momentDate.format(dataShowFormat);
return searchElem.date === formatedDate;
});
orderedData[find] = elem;
});
return orderedData;
}
/**
* @description Change the current date to brasilian format
* @param {string} date
*/
function toBrasilianDate(date) {
return moment(date, momentFormat).format(dataShowFormat);
}
/**
* @description Get correspondent icon to an item
* @param {string} label
* @returns class of icon
*/
function getIcon(iconId) {
return iconClasses[iconId];
}
/** @description Start Drag events */
function dragEvents(orderedData, context) {
// Total width of scroll box
let scrollContentWidth = 0;
orderedData.forEach((_, i) => {
const width = context.find(".father-box li").get(i).scrollWidth;
scrollContentWidth += width;
});
// handlers of events
/**@type {boolean} */
let canDrag;
/**@type {number} */
let lastX = 0;
context.on({
mousemove: e => {
if (canDrag) {
// get actual width of the box if it's resized and decrement in the max drag width
const canMove =
scrollContentWidth - Math.floor($(context).width() * 0.65);
const maxDragWidth = canMove >= 0 ? canMove : 0;
const style = $(context)
.find(".father-box")
.attr("style");
const findTransform = style.indexOf("translate");
const translate = style.substring(findTransform);
const actualPositionX = Number(translate.replace(/\D/g, ""));
if (!lastX) lastX = e.pageX;
const movedValue = Math.abs(lastX) - e.pageX;
let finalPositionX = Math.abs(actualPositionX + movedValue);
lastX = e.pageX;
if (actualPositionX + movedValue <= 0) finalPositionX = 0;
if (actualPositionX + movedValue >= maxDragWidth)
finalPositionX = maxDragWidth;
context
.find(".father-box")
.attr("style", `transform: translate(-${finalPositionX}px)`);
}
},
mousedown: e => {
lastX = e.pageX;
canDrag = true;
$(".timeline-box").attr("style", "cursor: grabbing");
},
mouseup: _ => {
canDrag = false;
$(".timeline-box").attr("style", "cursor: grab");
},
mouseleave: _ => {
canDrag = false;
}
});
}
/** @description Start show and hide data animation events */
function showHideDatesOnHover(context) {
context.find(".empty-day-box").mouseover(function() {
$(this)
.find(".date")
.attr("class", "date show-date");
});
context.find(".empty-day-box").mouseleave(function() {
$(this)
.find(".date")
.attr("class", "date hide-date");
});
}
};
/**@description Turnoff all events of the timeline */
$.fn.turnoffEvents = function() {
$(this).off();
};
/**@description Destroys the timeline */
$.fn.destroyTimeine = function() {
$(this).empty();
const id = $(this).attr("id");
delete timelinesData[id];
};
/**@description Go to a passed date */
$.fn.goToDate = function(date) {
const widthBox = $(this).width();
const timelineId = $(this).attr("id");
const timelineData = timelinesData[timelineId];
let fullScrollWidth = 0;
let widthUntilDate = 0;
if (timelineData) {
timelineData.forEach((_, i) => {
const width = $(this)
.find(".father-box li")
.get(i).scrollWidth;
fullScrollWidth += width;
});
// If can scroll
if (fullScrollWidth > widthBox) {
let maxScroll = Math.floor(fullScrollWidth - widthBox);
$(this)
.find("li")
.find(".find-date")
.each(i => {
const { innerText, scrollWidth } = $(this)
.find("li")
.find(".find-date")
.get(i);
if (innerText !== date) {
widthUntilDate += scrollWidth + 18;
maxScroll += 54;
} else {
if (widthUntilDate > maxScroll) widthUntilDate = maxScroll;
$(this)
.find(".father-box")
.attr(
"style",
`transition: transform 0.5s;transform: translate(-${widthUntilDate}px)`
);
return;
}
});
}
}
};