client/app/components/app_service.js
author rougeronj
Wed, 27 May 2015 19:02:43 +0200
changeset 99 c7c424e9eed5
parent 93 4b167851ee7d
child 106 cb13a9009bf5
permissions -rw-r--r--
refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
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) {
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
     7
        var books = $resource(context.urls.ammicoUrl+'/books/:idBook', {idBook:'@id'}, {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
    })
4b167851ee7d Starting a better management of the 'models' and the requests
rougeronj
parents: 0
diff changeset
    10
    .factory('Slide', function($resource, context) {
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
    11
        var slides = $resource(context.urls.ammicoUrl+'/slides/:idSlide', {idSlide:'@id'}, {update: { method: 'PUT'}});
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
            },
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    19
            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
    20
                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
    21
                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
    22
                    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
    23
                        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
    24
                            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
    25
                        }
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    26
                    },
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    27
                    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
    28
                        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
    29
                                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
    30
                        }
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    31
                    });
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    32
                }
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    33
            }
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    34
        });
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    35
        
c7c424e9eed5 refactor the ammicoApi service and put it into a specific module (ammicoCommon). Use factory to serve the resources
rougeronj
parents: 93
diff changeset
    36
        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
    37
    })
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
    .factory('Order', 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
    39
        return $resource(context.urls.ammicoUrl+'/books/:idBook/order', {idBook:'@id'});
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
    })
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
    .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
    42
        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
    43
            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
    44
            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
    45
            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
    46
            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
    47
            console.log(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
    48
            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
    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
    .service('authApi', 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
    52
        this.login = $resource(context.urls.ammicoUrl+'/auth/api-token-auth');
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
        this.logout = $resource(context.urls.ammicoUrl+'/auth/logout');
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
        this.user = $resource(context.urls.ammicoUrl+'/auth/user');
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
        this.test = $resource(context.urls.ammicoUrl+'/auth/auth');
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
    .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
    58
        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
    59
            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
    60
                    {
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
                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
    62
                    },
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
                    {
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
                        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
    65
                            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
    66
                            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
    67
                            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
    68
                            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
    69
                                // 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
    70
                                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
    71
                                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
    72
                                    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
    73
                                    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
    74
                                    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
    75
                                        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
    76
                                            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
    77
                                        }
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
                                        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
    79
                                            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
    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
                                }
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
                                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
    84
                            }
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
                        }
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
                    });
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
        };
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
    })
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
    .directive('myCustomer', 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
    90
        return {
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
            link: function(scope, elem, attr) {
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
                elem.bind('error', 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
    93
                    elem.parents('li.item').addClass('no-img');
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
                    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
    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
                elem.on('load', 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
    97
                    var w = $(this).width(),
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
                        h = $(this).height();
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
                    if (w > h){
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
                        $(this).addClass('img-landscape');
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
                    }
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
        }
93
4b167851ee7d Starting a better management of the 'models' and the requests
rougeronj
parents: 0
diff changeset
   105
    });
4b167851ee7d Starting a better management of the 'models' and the requests
rougeronj
parents: 0
diff changeset
   106
    
4b167851ee7d Starting a better management of the 'models' and the requests
rougeronj
parents: 0
diff changeset
   107
})();