/
tick.js
93 lines (75 loc) · 2.65 KB
/
tick.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
!function(d,w){
var runningList = { items: [] };
d.getElementById('editable').addEventListener('blur',saveList,false)
/*
* @desc Method to place all tasks' text values in an array that we can use later for comparison.
*/
function setRunningList()
{
// in case it's not empty
runningList.items = [];
// We have to stash actual text because the NodeList is "live".
Array.prototype.slice.call(d.querySelectorAll('#editable > li')).forEach(function(el,i){
runningList.items.push(el.innerText)
});
}
/*
* @desc Method that grabs the text values of the <li> nodes and sends to the server if their are changes to the ticks.
*/
function saveList()
{
var allItems = Array.prototype.slice.call(d.querySelectorAll('#editable > li'))
, changes = false
, ws = /^\s|\t|\n$/
, postList = { items:[] }
// Sanitize the list first.
allItems.forEach(function(el,i){
if(!ws.exec(el.innerText))
{
tItem = {task: el.innerText, urgent: false};
postList.items.push(tItem);
}
})
// check to see if any items have changed.
postList.items.forEach(function(el,i){
if(el.task !== runningList.items[i]) changes = true;
var currentItem = allItems[i];
if( /^[^*]+[*]{1}[^*]*$/.test(el.task) )
{
currentItem.removeAttribute('data-urgent')
currentItem.innerText = currentItem.innerText.replace('*', '')
}
else if( /^[^*]+[*]{2}[^*]*$/.test(el.task) || (currentItem['data-urgent'] !== undefined))
{
var attr = d.createAttribute('data-urgent');
attr.nodeValue = 'true';
currentItem.setAttributeNode(attr);
currentItem.innerText = currentItem.innerText.replace('**', '')
el.urgent = true;
}
el.task = el.task.replace('**', '').replace('*', '');
});
// check to see if the lengths are different
if(postList.items.length != runningList.items.length) changes = true;
//console.log("Any changes? "+ changes)
// If nothing new, don't save.
if(!changes) return;
var options = {
url: '/save',
type: 'json',
isExpressJsonRequest: true, // this is my custom shit because Express/Connect freaks.
method: 'POST',
data: JSON.stringify(postList),
success: function (resp) {
//console.log(resp.message);
// Update the current list.
setRunningList();
},
failure: function (err) {console.error(err) },
headers: {}
}
options.headers['content-type'] = 'application/json'
reqwest(options);
}
setRunningList();
}(document,window)