-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
90 lines (75 loc) · 2.57 KB
/
main.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
var app = angular.module('todoApp', ['ngMaterial']);
app.controller('AppCtrl', ['$scope', '$mdDialog', '$mdToast', 'todoService', function($scope, $mdDialog, $mdToast, todoService){
// Init Todo List - adding an item to localStorage in case it is empty.
if (!localStorage.getItem("taskName")) {
todoService.initTodoList();
$scope.locStorTasks = todoService.getTodoList();
}
else {
// Get all todo-items from local storage
$scope.locStorTasks = todoService.getTodoList();
}
/* Application options
* name - name of the application
* show - default tab, which is active on start
*/
$scope.options = {
name: 'My Todo List',
show: 'All tasks'
};
// Menu items description with icon-id of material icon
$scope.navigationBarItems = [
{'title': 'All tasks', 'icon': 'list'},
{'title': 'Todo', 'icon': 'assignment'},
{'title': 'Done', 'icon': 'done'}
];
// Set task as done
$scope.updateLocalStorage = function () {
// Workaround: waiting for model update in $scope
setTimeout(function () {
localStorage.setItem("taskName", JSON.stringify($scope.locStorTasks));
}, 150);
};
// Delete task from the list
$scope.objectDelete = function (item) {
// remove from view model
delete $scope.locStorTasks[item];
// remove from localstorage
todoService.updateTodoList($scope.locStorTasks);
};
$scope.setContent = function (content) {
$scope.options.show = content;
};
// Add item to the local storage
$scope.addToLocalStorage = function (task) {
// Prevent adding of emty tasks
if (task === undefined) return;
// Add item to the local storage
todoService.addTask(task);
// show notification
$mdToast.showSimple('Task "' + task + '" was added!');
// clear view model
$scope.taskName = '';
};
// Show form for new task on fab-click
$scope.showAddDialog = function(ev) {
$mdDialog.show({
controller: 'AddDialogCtrl',
templateUrl: 'add-task.html',
targetEvent: ev,
clickOutsideToClose: true
})
};
}]);
app.controller('AddDialogCtrl', ['$scope', '$mdDialog', 'todoService', function($scope, $mdDialog, todoService){
$scope.hide = function() {
$mdDialog.hide();
};
$scope.cancel = function() {
$mdDialog.cancel();
};
$scope.add = function(task) {
todoService.addTask(task);
$mdDialog.hide();
};
}]);