-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.js
145 lines (131 loc) · 3.59 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
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
var taskTemplate = `
<div class="task">
<div class='task-info' style="display:inline-block; vertical-align:middle">
<span class='title'>{{task.name}}</span> {{sumTime(task)}} min
<input type="button" class={{task.name}} id="addNew" value="+" ng-click="Show(task.name)" style="font-family: CovesBold; font-size: 25px;"/>
</div>
<div class='subtasks'>
<div ng-repeat="subtask in task.subtasks" class="subtask-container">
<sub-task subtask="subtask" parent="task" />
</div>
<div id={{task.name}} style="display:none;">
<form>
Title: <input type='text' ng-model='task.newSubtaskName'>
Time: <input type='text' class='time-input' ng-model='task.newSubtaskTime'>
<button type='submit' ng-click='addSubTask(task)'>Add</button>
</form>
</div>
</div>
</div>
`;
var subTaskTemplate = `
<div class="subtask">
<div class="task-info">
<div class="task-icon"></div>
<span class="title">{{subtask.name}}</span> {{subtask.time}} min
<input type="checkbox" ng-model='subtask.complete' ng-change='updateParent()'>
</div>
</div>
`;
angular.module('todo-app', [])
.controller('mainController', mainController)
.directive('subTask', subTaskDirective)
.directive('task', taskDirective);
function mainController($scope) {
$scope.tasks = [
{
name: 'Clean Room',
subtasks: [
{
name: 'Make Bed',
time: 5,
complete: false
},
{
name: 'Clean Closet',
time: 10,
complete: true
}
],
time: 15,
complete: false
},
{
name: 'Clean Kitchen',
subtasks: [],
time: 0,
complete: false
}
];
$scope.sumTime = function(task) {
var time = 0;
for (var i in task.subtasks) {
if (!task.subtasks[i].complete) {
time += task.subtasks[i].time;
}
}
return time;
}
$scope.addTask = function() {
$scope.tasks.push({name: $scope.newTaskName, time: 0, subtasks: [], complete: false});
$scope.newTaskName = '';
}
$scope.addSubTask = function(task) {
debugger;
task.subtasks.push({name: task.newSubtaskName, time: parseInt(task.newSubtaskTime), complete: false});
task.newSubtaskName = '';
task.newSubtaskTime = '';
}
$scope.IsVisible = false;
$scope.ShowHide = function () {
//If DIV is visible it will be hidden and vice versa.
$scope.IsVisible = $scope.IsVisible ? false : true;
}
$scope.Show = function (task) {
console.log(task)
var x = document.getElementById(task);
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
}
function taskDirective() {
return {
template: taskTemplate,
restrict: 'E',
link: function(scope, element, attrs) {
}
}
}
function subTaskDirective() {
return {
template: subTaskTemplate,
scope: { subtask: '=', parent: '=' },
link: function(scope, element, attrs) {
scope.updateParent = function() {
var allDone = true;
for (var i in scope.parent.subtasks) {
allDone = scope.parent.subtasks[i].complete ? allDone : false;
}
scope.parent.complete = allDone;
if (allDone == true) {
console.log("All Done")
console.log(scope.parent.name)
var x = document.getElementsByClassName(scope.parent.name)["addNew"];
console.log(x)
x.style.display = "none";
}
}
}
}
}
function myFunction(task) {
var x = document.getElementById(task);
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}