done state

This commit is contained in:
Märt Kalmo 2017-03-13 12:29:29 +02:00
parent f29acaf34f
commit a894f852a6
10 changed files with 194 additions and 3 deletions

6
app/app.js Normal file
View File

@ -0,0 +1,6 @@
(function () {
'use strict';
angular.module('app', ['ngRoute']);
})();

27
app/details.ctrl.js Normal file
View File

@ -0,0 +1,27 @@
(function () {
'use strict';
angular.module('app').controller('DetailsCtrl', Ctrl);
function Ctrl($http, $routeParams, $location) {
var vm = this;
vm.task = {};
vm.back = back;
init();
function init() {
$http.get('api/tasks/' + $routeParams.id).then(function (result) {
vm.task = result.data;
});
}
function back() {
$location.path('/list');
}
}
})();

4
app/details.html Normal file
View File

@ -0,0 +1,4 @@
Title: {{ vm.task.title }}<br>
Added: {{ vm.task.added | date : 'HH:mm' }}
<br><br>
<button ng-click="vm.back()">Back</button>

41
app/list.ctrl.js Normal file
View File

@ -0,0 +1,41 @@
(function () {
'use strict';
angular.module('app').controller('ListCtrl', Ctrl);
function Ctrl($http, modalService) {
var vm = this;
vm.tasks = [];
vm.newTitle = '';
vm.addTask = addTask;
vm.removeTask = removeTask;
init();
function init() {
$http.get('api/tasks').then(function (result) {
vm.tasks = result.data;
});
}
function addTask() {
var newTask = {
title: vm.newTitle,
added: new Date()
};
$http.post('api/tasks', newTask).then(init);
vm.newTitle = '';
}
function removeTask(id) {
modalService.confirm()
.then(function () {
return $http.delete('api/tasks/' + id);
}).then(init);
}
}
})();

9
app/list.html Normal file
View File

@ -0,0 +1,9 @@
<div data-ng-repeat="task in vm.tasks">
<span ng-class="{ done: task.done }" ><a href="#/details/{{ task._id }}" >{{ task.title }}</a></span>
<input type="checkbox" ng-model="task.done"/><a href ng-click="vm.removeTask(task._id)">X</a>
</div><br>
<input ng-model="vm.newTitle"/>
<button ng-click="vm.addTask(newTask)">Add task</button>

13
app/modal.html Normal file
View File

@ -0,0 +1,13 @@
<div id="simple-modal">
<div class="simple-modal-content">
<b>Oled kindel?</b>
<br><br>
<a href ng-click="ok()" id="simple-modal-ok">Jah</a> |
<a href ng-click="cancel()" id="simple-modal-cancel">Ei</a>
</div>
</div>

53
app/modal.srv.js Normal file
View File

@ -0,0 +1,53 @@
(function () {
'use strict';
angular.module('app').service('modalService', Srv);
Srv.$inject = ['$q', '$document', '$http', '$compile', '$rootScope'];
function Srv($q, $document, $http, $compile, $rootScope) {
this.confirm = confirm;
var modal;
var scope;
function confirm() {
if (modal) {
return showModal();
}
return $http.get('app/modal.html').then(function (result) {
createModalElement(result.data);
return showModal();
});
}
function showModal() {
var deferred = $q.defer();
scope.ok = function () {
modal.addClass('hide');
deferred.resolve();
};
scope.cancel = function () {
modal.addClass('hide');
deferred.reject();
};
modal.removeClass('hide');
return deferred.promise;
}
function createModalElement(html) {
var body = angular.element($document[0].body);
var containerElement = angular.element(html);
scope = $rootScope.$new();
modal = $compile(containerElement[0])(scope);
body.append(modal);
}
}
})();

20
app/routes.js Normal file
View File

@ -0,0 +1,20 @@
(function () {
'use strict';
angular.module('app').config(RouteConfig);
function RouteConfig($routeProvider) {
$routeProvider.when('/list', {
templateUrl : 'app/list.html',
controller : 'ListCtrl',
controllerAs : 'vm'
}).when('/details/:id', {
templateUrl : 'app/details.html',
controller : 'DetailsCtrl',
controllerAs : 'vm'
}).otherwise('/list');
}
})();

View File

@ -1,3 +1,14 @@
{
"tasks": []
"tasks": [
{
"title": "Call Jill",
"added": "2017-03-13T09:46:46.127Z",
"_id": 1
},
{
"title": "Write to John",
"added": "2017-03-13T10:09:38.867Z",
"_id": 4
}
]
}

View File

@ -8,13 +8,20 @@
<style type="text/css">
.ng-cloak { display: none }
.done { text-decoration: line-through }
</style>
</head>
<body>
Hello!
<div ng-view></div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-route.min.js"></script>
<script src="app/app.js"></script>
<script src="app/routes.js"></script>
<script src="app/list.ctrl.js"></script>
<script src="app/details.ctrl.js"></script>
<script src="app/modal.srv.js"></script>
</body>
</html>