client/app/books/books_controller.js
author rougeronj
Tue, 07 Apr 2015 11:55:11 +0200
changeset 72 ed2ee692ff6f
parent 69 88f76bf93465
child 74 44ebb0d0b836
permissions -rw-r--r--
Update PopulateVisit function to get only the new visits from Jamespot. In case the parameter "parrent_visit" is passed when creating a book, we copy all the slide corresponding to the visit being copied. Refactor PopulateVisite to PopulateVisit.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
     1
(function(){
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
     2
	'use strict';
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
     3
49
88cd0bb61c12 add ui-sortble lib to be able to sort the list of the slide in the book view - it sends a post request to the server to update the slides' order
rougeronj
parents: 42
diff changeset
     4
	angular.module('ammicoBooks',['ngRoute', 'ui.sortable'])
57
388b29d38b44 add authentication and controle of authentication before routing
rougeronj
parents: 55
diff changeset
     5
	.controller('booksCtrl', function($scope, $location, $modal, ammicoApi){
55
10fd23382e76 split ammico $resource to a new .Service
rougeronj
parents: 53
diff changeset
     6
		$scope.books = ammicoApi.listBooks.query({format:'json'});
24
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
     7
		$scope.searchSubmit = function(){
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
     8
			$location.path('/search/' + $scope.q);
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
     9
		};
53
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    10
		
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    11
		$scope.addBook = function () {
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    12
			$modal.open({
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    13
				templateUrl: 'books/add_modal.html',
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    14
				controller: 'ModalAddBookCtrl'
65
8116c2b28414 add book to current scope on success add on server side
rougeronj
parents: 57
diff changeset
    15
			}).result.then(function (newBook) {
8116c2b28414 add book to current scope on success add on server side
rougeronj
parents: 57
diff changeset
    16
				$scope.books.push(newBook);
53
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    17
			});
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    18
		};
69
88f76bf93465 update list book view with accordeons and add delete fonction on a book
rougeronj
parents: 65
diff changeset
    19
		$scope.deleteItem = function(i){
88f76bf93465 update list book view with accordeons and add delete fonction on a book
rougeronj
parents: 65
diff changeset
    20
			if(0<=i && i<$scope.books.length){
88f76bf93465 update list book view with accordeons and add delete fonction on a book
rougeronj
parents: 65
diff changeset
    21
				if(window.confirm('Êtes-vous sûr(e) de vouloir effacer cet élément ? Cette action est irrémédiable.')){
88f76bf93465 update list book view with accordeons and add delete fonction on a book
rougeronj
parents: 65
diff changeset
    22
					ammicoApi.book.delete({idBook: $scope.books[i].id}, function(){
88f76bf93465 update list book view with accordeons and add delete fonction on a book
rougeronj
parents: 65
diff changeset
    23
						$scope.books.splice(i, 1);
88f76bf93465 update list book view with accordeons and add delete fonction on a book
rougeronj
parents: 65
diff changeset
    24
					},
88f76bf93465 update list book view with accordeons and add delete fonction on a book
rougeronj
parents: 65
diff changeset
    25
					function(error) {
88f76bf93465 update list book view with accordeons and add delete fonction on a book
rougeronj
parents: 65
diff changeset
    26
						if (error.status === 404){
88f76bf93465 update list book view with accordeons and add delete fonction on a book
rougeronj
parents: 65
diff changeset
    27
							$scope.books.splice(i, 1);
88f76bf93465 update list book view with accordeons and add delete fonction on a book
rougeronj
parents: 65
diff changeset
    28
						}
88f76bf93465 update list book view with accordeons and add delete fonction on a book
rougeronj
parents: 65
diff changeset
    29
					});
88f76bf93465 update list book view with accordeons and add delete fonction on a book
rougeronj
parents: 65
diff changeset
    30
				}
88f76bf93465 update list book view with accordeons and add delete fonction on a book
rougeronj
parents: 65
diff changeset
    31
			}
88f76bf93465 update list book view with accordeons and add delete fonction on a book
rougeronj
parents: 65
diff changeset
    32
		};
24
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    33
	})
55
10fd23382e76 split ammico $resource to a new .Service
rougeronj
parents: 53
diff changeset
    34
	.controller('bookCtrl', function($scope, ammicoApi, $routeParams) {
10fd23382e76 split ammico $resource to a new .Service
rougeronj
parents: 53
diff changeset
    35
		$scope.slides = ammicoApi.booksSlides.query({idBook:$routeParams.idBook, format:'json'});
49
88cd0bb61c12 add ui-sortble lib to be able to sort the list of the slide in the book view - it sends a post request to the server to update the slides' order
rougeronj
parents: 42
diff changeset
    36
		$scope.slides.sort(function (a, b) {
88cd0bb61c12 add ui-sortble lib to be able to sort the list of the slide in the book view - it sends a post request to the server to update the slides' order
rougeronj
parents: 42
diff changeset
    37
		    return a.orderIndex > b.orderIndex;
88cd0bb61c12 add ui-sortble lib to be able to sort the list of the slide in the book view - it sends a post request to the server to update the slides' order
rougeronj
parents: 42
diff changeset
    38
		  });
88cd0bb61c12 add ui-sortble lib to be able to sort the list of the slide in the book view - it sends a post request to the server to update the slides' order
rougeronj
parents: 42
diff changeset
    39
		$scope.sortableOptions = {
88cd0bb61c12 add ui-sortble lib to be able to sort the list of the slide in the book view - it sends a post request to the server to update the slides' order
rougeronj
parents: 42
diff changeset
    40
		    stop: function() {
88cd0bb61c12 add ui-sortble lib to be able to sort the list of the slide in the book view - it sends a post request to the server to update the slides' order
rougeronj
parents: 42
diff changeset
    41
		    	var order = $scope.slides.map(function(i){
88cd0bb61c12 add ui-sortble lib to be able to sort the list of the slide in the book view - it sends a post request to the server to update the slides' order
rougeronj
parents: 42
diff changeset
    42
		            return i.id;
88cd0bb61c12 add ui-sortble lib to be able to sort the list of the slide in the book view - it sends a post request to the server to update the slides' order
rougeronj
parents: 42
diff changeset
    43
		    	});
55
10fd23382e76 split ammico $resource to a new .Service
rougeronj
parents: 53
diff changeset
    44
		    	ammicoApi.order.save({idBook:$routeParams.idBook}, {order: order});
49
88cd0bb61c12 add ui-sortble lib to be able to sort the list of the slide in the book view - it sends a post request to the server to update the slides' order
rougeronj
parents: 42
diff changeset
    45
		    }
88cd0bb61c12 add ui-sortble lib to be able to sort the list of the slide in the book view - it sends a post request to the server to update the slides' order
rougeronj
parents: 42
diff changeset
    46
		};
88cd0bb61c12 add ui-sortble lib to be able to sort the list of the slide in the book view - it sends a post request to the server to update the slides' order
rougeronj
parents: 42
diff changeset
    47
		
24
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    48
		$scope.deleteItem = function(i){
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    49
			if(0<=i && i<$scope.slides.length){
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    50
				if(window.confirm('Êtes-vous sûr(e) de vouloir effacer cet élément ? Cette action est irrémédiable.')){
55
10fd23382e76 split ammico $resource to a new .Service
rougeronj
parents: 53
diff changeset
    51
					ammicoApi.slide.delete({idSlide: $scope.slides[i].id}, function(){
42
edbfef527c66 remove unused success callback - remove unused css
rougeronj
parents: 32
diff changeset
    52
						$scope.slides.splice(i, 1);
65
8116c2b28414 add book to current scope on success add on server side
rougeronj
parents: 57
diff changeset
    53
					},
8116c2b28414 add book to current scope on success add on server side
rougeronj
parents: 57
diff changeset
    54
					function(error) {
8116c2b28414 add book to current scope on success add on server side
rougeronj
parents: 57
diff changeset
    55
						if (error.status === 404){
8116c2b28414 add book to current scope on success add on server side
rougeronj
parents: 57
diff changeset
    56
							$scope.slides.splice(i, 1);
8116c2b28414 add book to current scope on success add on server side
rougeronj
parents: 57
diff changeset
    57
						}
32
802ba9793507 add delete request to delete a slide - rename some .ressources fonctions
rougeronj
parents: 24
diff changeset
    58
					});
24
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    59
				}
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    60
			}
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    61
		};
53
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    62
	})
55
10fd23382e76 split ammico $resource to a new .Service
rougeronj
parents: 53
diff changeset
    63
	.controller('ModalAddBookCtrl', function ($scope, ammicoApi, $modalInstance) {
53
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    64
		
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    65
		$scope.add = function(){
65
8116c2b28414 add book to current scope on success add on server side
rougeronj
parents: 57
diff changeset
    66
			ammicoApi.listBooks.save({title: $scope.title}, function(newBook){
8116c2b28414 add book to current scope on success add on server side
rougeronj
parents: 57
diff changeset
    67
				$modalInstance.close(newBook);
8116c2b28414 add book to current scope on success add on server side
rougeronj
parents: 57
diff changeset
    68
			});
53
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    69
		};
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    70
		$scope.cancel = function () {
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    71
			$modalInstance.dismiss('cancel');
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    72
		};
24
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    73
	});
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    74
})();