client/app/books/books_controller.js
author rougeronj
Wed, 08 Apr 2015 00:36:37 +0200
changeset 77 ee963d1c409b
parent 74 44ebb0d0b836
child 91 f7a844a9079e
permissions -rw-r--r--
Add new serializer for the data send by Orpheo. We receive XML so we parse it to json. Then we parse some html to get the proper data of each attribute. Add xmlToJson parser module to requirements.txt
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){
74
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
     6
		$scope.books = ammicoApi.listBooks.query({format:'json'}, function(data){
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
     7
			data.sort(function (a, b) {
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
     8
				return a.date < b.date;
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
     9
			});
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    10
		});
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    11
		
24
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    12
		$scope.searchSubmit = function(){
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    13
			$location.path('/search/' + $scope.q);
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    14
		};
74
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    15
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    16
		$scope.addBook = function (idParent) {
53
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    17
			$modal.open({
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    18
				templateUrl: 'books/add_modal.html',
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    19
				controller: 'ModalAddBookCtrl'
74
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    20
			}).result.then(function (title) {
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    21
				ammicoApi.listBooks.save({idParent:idParent, title: title}, function(newBook){
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    22
					$scope.books.push(newBook);
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    23
				});
53
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    24
			});
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    25
		};
74
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    26
		$scope.deleteItem = function(book){
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    27
			if(window.confirm('Êtes-vous sûr(e) de vouloir effacer cet élément ? Cette action est irrémédiable.')){
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    28
				ammicoApi.book.delete({idBook: book.id}, function(){
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    29
					$scope.books.splice($scope.books.indexOf(book), 1);
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    30
				},
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    31
				function(error) {
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    32
					if (error.status === 404){
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    33
						$scope.books.splice($scope.books.indexOf(book), 1);
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    34
					}
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    35
				});
69
88f76bf93465 update list book view with accordeons and add delete fonction on a book
rougeronj
parents: 65
diff changeset
    36
			}
88f76bf93465 update list book view with accordeons and add delete fonction on a book
rougeronj
parents: 65
diff changeset
    37
		};
24
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    38
	})
55
10fd23382e76 split ammico $resource to a new .Service
rougeronj
parents: 53
diff changeset
    39
	.controller('bookCtrl', function($scope, ammicoApi, $routeParams) {
74
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    40
		$scope.slides = ammicoApi.booksSlides.query({idBook:$routeParams.idBook, format:'json'}, function(data){
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    41
			data.sort(function (a, b) {
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    42
				return a.orderIndex > b.orderIndex;
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    43
			});
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    44
		});
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
		$scope.sortableOptions = {
74
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    46
				stop: function() {
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    47
					var order = $scope.slides.map(function(i){
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    48
						return i.id;
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    49
					});
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    50
					ammicoApi.order.save({idBook:$routeParams.idBook}, {order: order});
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    51
				}
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
    52
		};
74
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    53
24
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    54
		$scope.deleteItem = function(i){
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    55
			if(0<=i && i<$scope.slides.length){
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    56
				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
    57
					ammicoApi.slide.delete({idSlide: $scope.slides[i].id}, function(){
42
edbfef527c66 remove unused success callback - remove unused css
rougeronj
parents: 32
diff changeset
    58
						$scope.slides.splice(i, 1);
65
8116c2b28414 add book to current scope on success add on server side
rougeronj
parents: 57
diff changeset
    59
					},
8116c2b28414 add book to current scope on success add on server side
rougeronj
parents: 57
diff changeset
    60
					function(error) {
8116c2b28414 add book to current scope on success add on server side
rougeronj
parents: 57
diff changeset
    61
						if (error.status === 404){
8116c2b28414 add book to current scope on success add on server side
rougeronj
parents: 57
diff changeset
    62
							$scope.slides.splice(i, 1);
8116c2b28414 add book to current scope on success add on server side
rougeronj
parents: 57
diff changeset
    63
						}
32
802ba9793507 add delete request to delete a slide - rename some .ressources fonctions
rougeronj
parents: 24
diff changeset
    64
					});
24
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    65
				}
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    66
			}
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    67
		};
53
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    68
	})
55
10fd23382e76 split ammico $resource to a new .Service
rougeronj
parents: 53
diff changeset
    69
	.controller('ModalAddBookCtrl', function ($scope, ammicoApi, $modalInstance) {
74
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    70
53
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    71
		$scope.add = function(){
74
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    72
			$modalInstance.close($scope.title);
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    73
//			ammicoApi.listBooks.save({title: $scope.title}, function(newBook){
44ebb0d0b836 get the book id of the book to delete from the template instead of $index (because we reorder the list so $index are not really clear anymore)
rougeronj
parents: 69
diff changeset
    74
//			});
53
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    75
		};
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    76
		$scope.cancel = function () {
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    77
			$modalInstance.dismiss('cancel');
dac1df90b49e fucntion and button to add a book
rougeronj
parents: 49
diff changeset
    78
		};
24
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    79
	});
21ecbde9e080 add books controller to handle list of books
rougeronj
parents:
diff changeset
    80
})();