diff --git a/ng1/app/app.js b/ng1/app/app.js new file mode 100644 index 0000000..46d627b --- /dev/null +++ b/ng1/app/app.js @@ -0,0 +1,6 @@ +(function () { + 'use strict'; + + var app = angular.module('app', ['ngRoute']); + +})(); diff --git a/ng1/app/edit.ctrl.js b/ng1/app/edit.ctrl.js new file mode 100644 index 0000000..e6184f1 --- /dev/null +++ b/ng1/app/edit.ctrl.js @@ -0,0 +1,35 @@ +(function () { + 'use strict'; + + angular.module("app").controller("EditCtrl", Ctrl); + Ctrl.$inject = ['$http', '$routeParams', '$location'] + + function Ctrl($http, $routeParams, $location) { + var vm = this; + + vm.contact = {}; + vm.submitForm = submitForm; + + getContact($routeParams.id) + function getContact(id) { + if(!id) return + + $http.get(`/api/contacts/${id}`).then(resp => { + vm.contact = resp.data; + }); + }; + + function submitForm() { + console.log(vm.contact); + if(vm.contact._id) { + $http.put(`/api/contacts/${vm.contact._id}`, vm.contact).then(resp => { + $location.path("/search"); + }); + } else { + $http.post(`/api/contacts`, vm.contact).then(resp => { + $location.path("/search"); + }); + } + }; + }; +})(); diff --git a/ng1/app/edit.html b/ng1/app/edit.html new file mode 100644 index 0000000..96039cd --- /dev/null +++ b/ng1/app/edit.html @@ -0,0 +1,20 @@ +
+ + + + + + + + + + + + + + + +
Eesnimi:
Telefon:

+ Salvesta +
+
diff --git a/ng1/app/modal.html b/ng1/app/modal.html new file mode 100644 index 0000000..99f9925 --- /dev/null +++ b/ng1/app/modal.html @@ -0,0 +1,12 @@ +
+
+ + Oled kindel? + +

+ + Jah | + Ei + +
+
diff --git a/ng1/app/modal.srv.js b/ng1/app/modal.srv.js new file mode 100644 index 0000000..728883d --- /dev/null +++ b/ng1/app/modal.srv.js @@ -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); + } + } + +})(); diff --git a/ng1/app/routes.js b/ng1/app/routes.js new file mode 100644 index 0000000..fc00f53 --- /dev/null +++ b/ng1/app/routes.js @@ -0,0 +1,24 @@ +(function () { + 'use strict'; + + angular.module('app').config(RouteConfig); + RouteConfig.$inject = ["$routeProvider"] + + function RouteConfig($routeProvider) { + + $routeProvider.when('/search', { + templateUrl : 'app/search.html', + controller : 'SearchCtrl', + controllerAs : 'vm' + }).when("/new", { + templateUrl: "app/edit.html", + controller: "EditCtrl", + controllerAs: "vm" + }).when("/edit/:id", { + templateUrl: "app/edit.html", + controller: "EditCtrl", + controllerAs: "vm" + }).otherwise("/search") + } + +})(); diff --git a/ng1/app/search.ctrl.js b/ng1/app/search.ctrl.js new file mode 100644 index 0000000..f7f7b39 --- /dev/null +++ b/ng1/app/search.ctrl.js @@ -0,0 +1,47 @@ +(function () { + 'use strict'; + + angular.module("app").controller("SearchCtrl", Ctrl); + Ctrl.$inject = ['$http', 'modalService'] + + function Ctrl($http, modalService) { + var vm = this; + vm.contacts = []; + vm.searchString = ""; + vm.deleteContact = deleteContact; + vm.deleteSelected = deleteSelected; + vm.nameCodeFilter = nameCodeFilter; + + + getContacts(); + function getContacts() { + $http.get("/api/contacts").then(resp => { + vm.contacts = resp.data; + }).catch(e => { + console.error("Failed to get contacts", e); + }) + }; + + function deleteContact(id) { + modalService.confirm().then(resp => { + $http.delete(`/api/contacts/${id}`).then(resp => { + console.info(`Contact ${id}`) + getContacts(); + }) + }) + + }; + + function deleteSelected() { + return; + }; + + function matchSearchString(string) { + return void 0 !== string && string.toLowerCase().indexOf(vm.searchString.toLowerCase()) >= 0 + } + + function nameCodeFilter(contact) { + return 0 === vm.searchString.length || (matchSearchString(contact.name) || matchSearchString(contact.phone)); + }; + }; +})(); diff --git a/ng1/app/search.html b/ng1/app/search.html new file mode 100644 index 0000000..75699f1 --- /dev/null +++ b/ng1/app/search.html @@ -0,0 +1,34 @@ + + +

+ + + + + + + + + + + + + + + + + + +
NimiTelefon
+ {{ contact.name }} + + {{ contact.phone }} + + Kustuta + + +
+ +

+ diff --git a/ng1/index.html b/ng1/index.html index bb8b71a..5aad432 100644 --- a/ng1/index.html +++ b/ng1/index.html @@ -12,9 +12,22 @@ -Hello! + +

+
+ + + + + + + +