-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
145 lines (100 loc) · 2.73 KB
/
utils.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
/**
* Returns the diff of two dates in milliseconds
**/
var diffDates = function(date1, date2) {
// Convert both dates to milliseconds
var date1_ms = date1.getTime()
var date2_ms = date2.getTime()
// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms)
// Convert back to days and return
return difference_ms;
};
/**
* Calculates the diff in days between two dates
**/
var daysBetweenDates = function(date1, date2) {
// get the diff
var diff = diffDates(date1, date2);
// The number of milliseconds in one day
var ONE_DAY = 1000 * 60 * 60 * 24
// Convert back to days and return
return diff/ONE_DAY;
};
/**
* Saves the options configured in chrome's storage that is synced
**/
var saveOptions = function(params, fn) {
// Save it using the Chrome extension storage API.
chrome.storage.sync.set(params, function() {
// done
fn();
});
};
/**
* Returns the saved options for the plugin from chrome's storage
**/
var getOptions = function(fn) {
// get from the synced storage
chrome.storage.sync.get(null, function(options) {
// return
fn(options);
});
};
/**
* Returns the value of the input by it's id
**/
var getInputValueByID = function(elementID, fn) {
var el = document.getElementsById(elementID);
return (el || {}).value || '';
};
/**
* Adds a event listener on the object id passed
**/
var bindToID = function(elementID, fn) {
var el = document.getElementsById(elementID);
if(!el) return;
el.addEventListener('click', fn);
};
/**
* Adds a event listener on the object class passed
**/
var bindToClass = function(className, fn) {
var els = document.getElementsByClassName(className);
for(var i = 0; i < (els || []).length; i++) {
els[i].addEventListener('click', fn);
}
};
// the current section
var currentSection = '';
/**
* Shows a certain section with a matching "data-section" tag
* the rest are hidden, really simple method to just hide/show content
* for now.
**/
var getSection = function() {
// just return it
return currentSection;
};
/**
* Shows a certain section with a matching "data-section" tag
* the rest are hidden, really simple method to just hide/show content
* for now.
**/
/* Edited to ulitize the class system for better looks */
var showSection = function (sectionClass, target) {
// set the current
currentSection = target;
// get all with the section
var els = document.getElementsByClassName(sectionClass);
// loop all matching elements
for (var i = 0; i < els.length; i++) {
if (els[i].getAttribute('data-section') == target) {
// show
els[i].classList.add('tab-section-active');
} else {
// hide
els[i].classList.remove('tab-section-active');
}
}
};