forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseFrame.js
More file actions
executable file
·100 lines (81 loc) · 2.55 KB
/
baseFrame.js
File metadata and controls
executable file
·100 lines (81 loc) · 2.55 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
const Config = require('../include/config');
const moment = require('moment-timezone');
const Hashids = require('hashids');
const hashids = new Hashids();
class baseFrame {
/**
* constructor.
* @param config
* @param id
* @param type
*/
constructor(config, id, type) {
this.config = config;
this.project = config.get('project');
this.resource = {id, type};
if(typeof id === 'string' || id instanceof String)
this.resource.new = true;
this.id = baseFrame.generateId();
this._start = false;
this._stop = false;
this.timezone = config.get('timezone');
this.notes = [];
}
static fromJson(config, json) {
let frame = new this(config, json.resource.id, json.resource.type);
frame.project = json.project;
frame.id = json.id;
frame._start = json.start;
frame._stop = json.stop;
frame.notes = json.notes;
frame.timezone = json.timezone;
frame.modified = json.modified;
frame.validate();
return frame;
}
validate() {
moment.suppressDeprecationWarnings = true;
if(!moment(this._start).isValid())
throw `Error: Start date is not in a valid ISO date format!`;
if(this._stop && !moment(this._stop).isValid())
throw `Error: Stop date is not in a valid ISO date format!`;
moment.suppressDeprecationWarnings = false;
}
_getCurrentDate() {
if(this.timezone)
return moment().tz(this.timezone).format();
return moment();
}
static copy(frame) {
return this.fromJson(Object.assign(new Config, frame.config), {
id: frame.id,
project: frame.project,
resource: frame.resource,
notes: frame.notes,
start: frame._start,
stop: frame._stop,
timezone: frame.timezone,
modified: frame.modified
});
}
get duration() {
return moment(this.stop).diff(this.start) / 1000;
}
get date() {
return this.start;
}
get start() {
return this.timezone ? moment(this._start).tz(this.timezone) : moment(this._start);
}
get stop() {
return this.timezone ? this._stop ? moment(this._stop).tz(this.timezone) : false : (this._stop ? moment(this._stop) : false );
}
/**
* generate a unique id
* @returns {number}
*/
static generateId() {
return hashids.encode(new Date().getTime());
}
}
module.exports = baseFrame;