client/app/components/app_service.js
author rougeronj
Tue, 02 Jun 2015 09:46:30 +0200
changeset 106 cb13a9009bf5
parent 99 c7c424e9eed5
child 146 c6fa8ee3f562
permissions -rw-r--r--
improve slideshow interface and add ngTouch to handle swipe on mobile
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
            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
    48
        };
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
    .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
    51
        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
    52
        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
    53
        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
    54
        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
    55
    })
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
    .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
    57
        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
    58
            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
    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
                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
    61
                    },
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
                        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
    64
                            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
    65
                            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
    66
                            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
    67
                            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
    68
                                // 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
    69
                                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
    70
                                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
    71
                                    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
    72
                                    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
    73
                                    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
    74
                                        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
    75
                                            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
    76
                                        }
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
                                        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
    78
                                            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
    79
                                        }
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
                                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
    83
                            }
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
    .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
    89
        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
    90
            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
    91
                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
    92
                    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
    93
                    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
    94
                });
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
                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
    96
                    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
    97
                        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
    98
                    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
    99
                        $(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
   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
                });
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
        }
93
4b167851ee7d Starting a better management of the 'models' and the requests
rougeronj
parents: 0
diff changeset
   104
    });
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
})();