client/app/components/app_service.js
author rougeronj
Wed, 08 Jul 2015 17:45:44 +0200
changeset 178 a3e1106b6f96
parent 172 ffdfe491869c
child 184 4d01c2d04359
permissions -rw-r--r--
update interface of the slideshow
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
93
4b167851ee7d Starting a better management of the 'models' and the requests
rougeronj
parents: 0
diff changeset
     1
4b167851ee7d Starting a better management of the 'models' and the requests
rougeronj
parents: 0
diff changeset
     2
(function(){
4b167851ee7d Starting a better management of the 'models' and the requests
rougeronj
parents: 0
diff changeset
     3
    'use strict';
4b167851ee7d Starting a better management of the 'models' and the requests
rougeronj
parents: 0
diff changeset
     4
    
99
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
     5
    angular.module('ammicoCommon', ['ngRoute'])
93
4b167851ee7d Starting a better management of the 'models' and the requests
rougeronj
parents: 0
diff changeset
     6
    .factory('Book', function($resource, context) {
146
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
     7
        var books = $resource(context.urls.ammicoUrl+'books/:idBook', {idBook:'@id', idExpo: context.idExpo}, {update: { method: 'PUT'}});
93
4b167851ee7d Starting a better management of the 'models' and the requests
rougeronj
parents: 0
diff changeset
     8
        return books;
4b167851ee7d Starting a better management of the 'models' and the requests
rougeronj
parents: 0
diff changeset
     9
    })
146
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    10
    .factory('Slide', function($resource, context, $modal) {
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    11
        var slides = $resource(context.urls.ammicoUrl+'slides/:idSlide', {idSlide:'@id'}, {update: { method: 'PUT'}});
99
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    12
        
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    13
        angular.extend(slides.prototype, {
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    14
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    15
            toggleFavorite: function () {
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    16
                this.favorite = !this.favorite;
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    17
                this.$update();
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    18
            },
146
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    19
            addToBook: function () {
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    20
                var _this = this;
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    21
                var modalInstance = $modal.open({
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    22
                    templateUrl: 'books/add_book_modal.html',
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    23
                    controller: 'ModalAddBookCtrl',
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    24
                    size: 'sm'
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    25
                });
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    26
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    27
                modalInstance.result.then(function (idBook) {
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    28
                    var newSlide = _this;
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    29
                    newSlide.book = idBook;
172
ffdfe491869c uncomment jshint and fix minor bug
rougeronj
parents: 146
diff changeset
    30
                    delete newSlide.id;
146
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    31
                    slides.save(newSlide);
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    32
                });
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    33
            },
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    34
            update: function(){
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    35
                console.log('test');
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    36
            },
99
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    37
            remove: function (array) {
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    38
                var _this = this;
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    39
                if(window.confirm('Êtes-vous sûr(e) de vouloir effacer cet élément ? Cette action est irrémédiable.')){
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    40
                    this.$delete(function(){
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    41
                        if (array){
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    42
                            array.splice(array.indexOf(_this), 1);
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    43
                        }
172
ffdfe491869c uncomment jshint and fix minor bug
rougeronj
parents: 146
diff changeset
    44
                        //$scope.$emit('slideDeleted', array);
99
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    45
                    },
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    46
                    function(error) {
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    47
                        if (error.status === 404 && array){
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    48
                                array.splice(array.indexOf(_this), 1);
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    49
                        }
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    50
                    });
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    51
                }
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    52
            }
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    53
        });
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    54
        
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    55
        return slides;
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    56
    })
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    57
    .factory('Order', function($resource, context) {
146
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    58
        return $resource(context.urls.ammicoUrl+'books/:idBook/order', {idBook:'@id'});
99
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    59
    })
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    60
    .service('Utils', function($resource, context, $sce) {
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    61
        this.sanitizeUrls = function(data){
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    62
            data.details.audio = $sce.trustAsResourceUrl(data.details.audio);
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    63
            data.details.video = $sce.trustAsResourceUrl(data.details.video);
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    64
            data.audio = $sce.trustAsResourceUrl(data.audio);
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    65
            data.video = $sce.trustAsResourceUrl(data.video);
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    66
            return data;
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    67
        };
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    68
    })
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    69
    .service('authApi', function($resource, context) {
146
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    70
        this.login = $resource(context.urls.ammicoUrl+'auth/api-token-auth', {idExpo: context.idExpo});
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    71
        this.logout = $resource(context.urls.ammicoUrl+'auth/logout');
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    72
        this.user = $resource(context.urls.ammicoUrl+'auth/user');
c6fa8ee3f562 refactor idArticle to id_article and idInventory to id_inventory
rougeronj
parents: 106
diff changeset
    73
        this.test = $resource(context.urls.ammicoUrl+'auth/auth');
99
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    74
    })
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    75
    .service('searchApi', function($resource, context) {
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    76
        this.searchResource = function(params){
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    77
            return $resource(context.urls.searchUrl,  
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    78
                    {
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    79
                callback: 'JSON_CALLBACK'
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    80
                    },
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    81
                    {
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    82
                        getJsonp: {
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    83
                            method: 'JSONP',
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    84
                            params: params,
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    85
                            isArray: false,
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    86
                            transformResponse: function(data){
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    87
                                // Transform meta list into meta dict
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    88
                                var nb = data.hits.length;
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    89
                                for(var i=0;i<nb;i++){
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    90
                                    var nb_metas = data.hits[i].metas.length;
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    91
                                    data.hits[i].metas_dict = {};
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    92
                                    for(var j=0;j<nb_metas;j++){
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    93
                                        if(typeof data.hits[i].metas[j].images==='undefined'){
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    94
                                            data.hits[i].metas_dict[data.hits[i].metas[j].name] = data.hits[i].metas[j].value;
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    95
                                        }
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    96
                                        else{
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    97
                                            data.hits[i].metas_dict.images = data.hits[i].metas[j].images[0].value;
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    98
                                        }
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    99
                                    }
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
   100
                                }
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
   101
                                return data;
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
   102
                            }
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
   103
                        }
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
   104
                    });
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
   105
        };
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
   106
    })
172
ffdfe491869c uncomment jshint and fix minor bug
rougeronj
parents: 146
diff changeset
   107
    .directive('imgType', function(){
99
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
   108
        return {
172
ffdfe491869c uncomment jshint and fix minor bug
rougeronj
parents: 146
diff changeset
   109
            restrict: 'AE',
ffdfe491869c uncomment jshint and fix minor bug
rougeronj
parents: 146
diff changeset
   110
            link: function(scope, elem) {
99
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
   111
                elem.bind('error', function() {
178
a3e1106b6f96 update interface of the slideshow
rougeronj
parents: 172
diff changeset
   112
                    elem.parent().addClass('no-img');
99
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
   113
                    elem.remove();
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
   114
                });
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
   115
                elem.on('load', function() {
172
ffdfe491869c uncomment jshint and fix minor bug
rougeronj
parents: 146
diff changeset
   116
                    var w = elem.width(),
ffdfe491869c uncomment jshint and fix minor bug
rougeronj
parents: 146
diff changeset
   117
                        h = elem.height();
99
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
   118
                    if (w > h){
172
ffdfe491869c uncomment jshint and fix minor bug
rougeronj
parents: 146
diff changeset
   119
                        elem.addClass('img-landscape');
99
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
   120
                    }
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
   121
                });
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
   122
            }
172
ffdfe491869c uncomment jshint and fix minor bug
rougeronj
parents: 146
diff changeset
   123
        };
93
4b167851ee7d Starting a better management of the 'models' and the requests
rougeronj
parents: 0
diff changeset
   124
    });
4b167851ee7d Starting a better management of the 'models' and the requests
rougeronj
parents: 0
diff changeset
   125
    
4b167851ee7d Starting a better management of the 'models' and the requests
rougeronj
parents: 0
diff changeset
   126
})();