client/app/app.js
author cavaliet
Thu, 09 Oct 2014 14:34:14 +0200
changeset 5 90a7c431b979
parent 1 1cdebfeb77f9
child 6 c53e9b24f93f
permissions -rw-r--r--
first step of working annotation real annotation client
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
     1
(function(){
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
     2
    'use strict';
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
     3
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
     4
    angular.module('mons', [ 'ngResource', 'ngRoute', 'autocomplete' ])
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
     5
        .config(function($routeProvider) {
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
     6
            $routeProvider.
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
     7
                when('/', {
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
     8
                    controller: 'homeCtrl'
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
     9
               }).
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    10
               otherwise({
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    11
                   redirectTo: '/'
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    12
               });
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    13
        })
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    14
        .config(function($logProvider){
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    15
            $logProvider.debugEnabled(true);
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    16
        })
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    17
        .service('dataApi', function($resource, context) {
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    18
            console.log('dataApi',$resource, context);
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    19
            this.dataResource = $resource(context.urls.dataUrl);
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    20
        })
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    21
        .service('dataModel', function(dataApi) {
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    22
            //console.log('dataModel',this,dataApi);
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    23
            this.data = dataApi.dataResource.get();
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    24
        })
5
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    25
        .controller('homeCtrl', function($scope, $location, dataModel, context){
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    26
            //console.log('homeCtrl 1', $scope, $location, context);
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    27
            console.log('homeCtrl 2', context);
1
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    28
            $scope.data = dataModel.data;
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    29
            
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    30
            dataModel.data.$promise.then(function(data) {
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    31
                if(typeof data.categories!=='undefined' && data.categories.length>0){
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    32
                    console.log('    JE RENTRE');
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    33
                    var cats = [];
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    34
                    var nbCat = data.categories.length;
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    35
                    for(var i=0;i<nbCat;i++){
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    36
                        cats.push(data.categories[i].label);
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    37
                        if(typeof data.categories[i].subcategories!=='undefined' && data.categories[i].subcategories.length>0){
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    38
                            var nbSubCat = data.categories[i].subcategories.length;
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    39
                            for(var j=0;j<nbSubCat;j++){
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    40
                                cats.push(data.categories[i].subcategories[j].label);
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    41
                            }
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    42
                        }
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    43
                    }
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    44
                    $scope.allCatLabels = cats;
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    45
                }
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    46
            });
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    47
            
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    48
            $scope.selectedlevel = false;
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    49
            
5
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    50
            
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    51
            
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    52
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    53
            // Socket management
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    54
            var sock = null;
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    55
            var ellog = null;
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    56
            
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    57
            ellog = document.getElementById('log');
1
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    58
            
5
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    59
            function log(m) {
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    60
                ellog.innerHTML += m + '\n';
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    61
                ellog.scrollTop = ellog.scrollHeight;
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    62
            }
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    63
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    64
            var wsuri;
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    65
            if (window.location.protocol === 'file:') {
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    66
                wsuri = 'ws://127.0.0.1:8090/annot';
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    67
            } else {
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    68
                wsuri = 'ws://' + window.location.hostname + ':8090/annot';
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    69
            }
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    70
            wsuri = wsuri + '?event=test';
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    71
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    72
            if ('WebSocket' in window) {
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    73
                sock = new WebSocket(wsuri);
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    74
            } else if ('MozWebSocket' in window) {
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    75
                sock = new window.MozWebSocket(wsuri);
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    76
            } else {
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    77
                log('Browser does not support WebSocket!');
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    78
                window.location = 'http://autobahn.ws/unsupportedbrowser';
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    79
            }
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    80
            console.log('hi 1', sock);
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    81
            if (sock) {
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    82
                sock.onopen = function() {
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    83
                    log('Connected to ' + wsuri);
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    84
                };
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    85
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    86
                sock.onclose = function(e) {
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    87
                    log('Connection closed (wasClean = ' + e.wasClean + ', code = ' + e.code + ', reason = "' + e.reason + '")');
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    88
                    sock = null;
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    89
                };
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    90
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    91
                sock.onmessage = function(e) {
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    92
                    log('Got message: ' + e.data);
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    93
                };
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    94
            }
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
    95
1
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    96
            $scope.sendAnnotation = function(eventCode){
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    97
                if($scope.username==='' || typeof $scope.username==='undefined'){
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    98
                    alert('Vous devez indiquer un nom d\'utilisateur.');
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
    99
                    return;
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   100
                }
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   101
                if(eventCode==='' || typeof eventCode==='undefined'){
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   102
                    alert('Vous devez indiquer un nom de catégorie.');
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   103
                    return;
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   104
                }
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   105
                // Send query
5
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
   106
                if (sock) {
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
   107
                    var new_annot = {
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
   108
                            categories : eventCode,
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
   109
                            user : 'admin'
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
   110
                    };
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
   111
                    sock.send(JSON.stringify(new_annot));
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
   112
                    log('Sent: ' + JSON.stringify(new_annot));
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
   113
                } else {
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
   114
                    log('Not connected.');
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
   115
                }
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
   116
                //alert('annotation envoyée ! eventCode = ' + eventCode);
1
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   117
            };
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   118
            
5
90a7c431b979 first step of working annotation real annotation client
cavaliet
parents: 1
diff changeset
   119
            // Interface management
1
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   120
            $scope.selectLevel = function(i, eventCode){
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   121
                if(i===false){
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   122
                    $scope.returnVisStyle = {visibility:'hidden'};
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   123
                    $scope.selectedlevel = false;
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   124
                    return;
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   125
                }
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   126
                if(typeof $scope.data.categories[i].subcategories!=='undefined' && $scope.data.categories[i].subcategories.length>0){
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   127
                    $scope.selectedlevel = $scope.data.categories[i].subcategories;
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   128
                    $scope.returnVisStyle = {visibility:'show'};
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   129
                }
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   130
                else{
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   131
                    // Send query
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   132
                    $scope.sendAnnotation(eventCode);
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   133
                }
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   134
            };
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   135
            
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   136
        });
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   137
1cdebfeb77f9 add client folder and update hgignore
cavaliet
parents:
diff changeset
   138
})();