-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.js
77 lines (66 loc) · 2.16 KB
/
todo.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
angular.module("myApp", []).service('uuid4', function() {
/**! http://stackoverflow.com/a/2117523/377392 */
var fmt = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
this.generate = function() {
return fmt.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
};
}).controller("TreeController", ['$scope', 'uuid4', function($scope, uuid4) {
$scope.tree = [];
$scope.init = function () {
$scope.tree = JSON.parse(window.localStorage.getItem("bullets"));
if ($scope.tree === null) {
$scope.tree = [];
}
}
$scope.delete = function(data) {
data.nodes = [];
$scope.save();
};
$scope.add = function(data) {
var post = data.nodes.length + 1;
var newName = data.name + '-' + post;
data.nodes.push({name: newName, nodes: [], done:false, hide: false, guid: uuid4.generate()});
$scope.save();
};
$scope.flipEdit = function (data) {
data.edit = !data.edit;
$scope.save();
};
$scope.editSave = function (data, $event, isBlurred) {
//Enter or blurred
if ($event.which == 13 || isBlurred) {
data.name = $event.target.value;
this.flipEdit(data);
}
$scope.save();
};
$scope.hide = function (data, hideIt) {
data.hide = hideIt;
$scope.save();
}
$scope.addTodo = function(tree, $event) {
//Enter
if ($event.which == 13) {
tree.push({name:$event.target.value, nodes: [], done:false, hide: false, guid: uuid4.generate()});
$scope.save();
$event.target.value = "";
$event.preventDefault();
//Tab
} else if ($event.which == 9) {
$event.preventDefault();
//Up
} else if ($event.which == 38) {
console.log(tree.length);
$event.preventDefault();
//Down
} else if ($event.which == 40) {
$event.preventDefault();
}
};
$scope.save = function () {
window.localStorage.setItem("bullets", JSON.stringify($scope.tree));
}
}]);