annot-server/static/js/app.js
changeset 84 d7c5bffdd2d8
child 107 6d41506f9482
equal deleted inserted replaced
83:9be99c2fb279 84:d7c5bffdd2d8
       
     1 (function(){
       
     2     'use strict';
       
     3 
       
     4     angular.module('mons', [ 'ngResource', 'ngRoute', 'autocomplete' ])
       
     5         .config(function($routeProvider) {
       
     6             $routeProvider.
       
     7                 when('/', {
       
     8                     controller: 'homeCtrl'
       
     9                }).
       
    10                otherwise({
       
    11                    redirectTo: '/'
       
    12                });
       
    13         })
       
    14         .config(function($logProvider){
       
    15             $logProvider.debugEnabled(true);
       
    16         })
       
    17         .service('dataApi', function($resource, context) {
       
    18             //console.log('dataApi',$resource, context);
       
    19             this.dataResource = $resource(context.urls.dataUrl);
       
    20         })
       
    21         .service('dataModel', function(dataApi, context) {
       
    22             //console.log('dataModel',this,dataApi);
       
    23             if(typeof context.categories_json !== 'undefined' && context.categories_json) {
       
    24                 this.data = JSON.parse(context.categories_json);
       
    25             }
       
    26             else {
       
    27                 this.data = dataApi.dataResource.get();
       
    28             }
       
    29         })
       
    30         .controller('homeCtrl', function($scope, $location, dataModel, context, $interval){
       
    31 
       
    32             function getURLParameter(name) {
       
    33                 return decodeURI(
       
    34                     (new RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
       
    35                 );
       
    36             }
       
    37 
       
    38             $scope.data = dataModel.data;
       
    39 
       
    40             var process_categories = function(data) {
       
    41                 if(typeof data.categories!=='undefined' && data.categories.length>0){
       
    42                     var cats = [];
       
    43                     var nbCat = data.categories.length;
       
    44                     for(var i=0;i<nbCat;i++){
       
    45                         cats.push(data.categories[i].label);
       
    46                         if(typeof data.categories[i].subcategories!=='undefined' && data.categories[i].subcategories.length>0){
       
    47                             var nbSubCat = data.categories[i].subcategories.length;
       
    48                             for(var j=0;j<nbSubCat;j++){
       
    49                                 cats.push(data.categories[i].subcategories[j].label);
       
    50                             }
       
    51                         }
       
    52                     }
       
    53                     $scope.allCatLabels = cats;
       
    54                 }
       
    55             };
       
    56 
       
    57             if (typeof dataModel.data.$promise !== 'undefined') {
       
    58                 dataModel.data.$promise.then(process_categories);
       
    59             }
       
    60             else {
       
    61                 process_categories(dataModel.data);
       
    62             }
       
    63 
       
    64             $scope.selectedlevel = false;
       
    65 
       
    66             $scope.currentInterval = false;
       
    67             $scope.showSuccessAlert = false;
       
    68             $scope.showAlertDiv = false;
       
    69             function showAlert(m, success){
       
    70                 $scope.alertMessage = m;
       
    71                 $scope.showSuccessAlert = success;
       
    72                 $scope.showAlertDiv = true;
       
    73                 if(!$scope.$$phase) {
       
    74                     $scope.$apply();
       
    75                 }
       
    76                 if($scope.currentInterval){
       
    77                     $interval.cancel($scope.currentInterval);
       
    78                     $scope.currentInterval = false;
       
    79                 }
       
    80                 $scope.currentInterval = $interval(function(){ $interval.cancel($scope.currentInterval); $scope.showAlertDiv = false; }, 2000, 1);
       
    81             }
       
    82             $scope.annotPile = [];
       
    83 
       
    84 
       
    85 
       
    86             // Socket management
       
    87             var sock = null;
       
    88             var ellog = null;
       
    89 
       
    90             ellog = document.getElementById('log');
       
    91             function log(m) {
       
    92                 if(ellog){
       
    93                     ellog.innerHTML += m + '\n';
       
    94                     ellog.scrollTop = ellog.scrollHeight;
       
    95                 }
       
    96             }
       
    97 
       
    98             var wsuri;
       
    99             if (window.location.protocol === 'file:') {
       
   100                 wsuri = 'ws://127.0.0.1:8090/annot';
       
   101             } else {
       
   102                 wsuri = 'ws://' + window.location.hostname + ':8090/annot';
       
   103             }
       
   104             
       
   105             var eventCode = context.event_code;
       
   106             if(typeof eventCode==='undefined' || eventCode===''){
       
   107                 eventCode = $location.search().event;
       
   108                 if(typeof eventCode==='undefined' || eventCode===''){
       
   109                     eventCode = getURLParameter('event');
       
   110                     if(typeof eventCode==='undefined' || eventCode===''){
       
   111                         alert('le code de l\'événement doit être indiqué dans un paramètre de template u dans l\'url selon ?event=CODE_EVENEMENT.');
       
   112                         return;
       
   113                     }
       
   114                 }
       
   115             }
       
   116             wsuri = wsuri + '?event=' + eventCode;
       
   117 
       
   118             if ('WebSocket' in window) {
       
   119                 sock = new WebSocket(wsuri);
       
   120             } else if ('MozWebSocket' in window) {
       
   121                 sock = new window.MozWebSocket(wsuri);
       
   122             } else {
       
   123                 if(context.logging===true){
       
   124                     log('Browser does not support WebSocket!');
       
   125                 }
       
   126                 window.location = 'http://autobahn.ws/unsupportedbrowser';
       
   127             }
       
   128 
       
   129             if (sock) {
       
   130                 sock.onopen = function() {
       
   131                     if(context.logging===true){
       
   132                         log('Connected to ' + wsuri);
       
   133                     }
       
   134                 };
       
   135 
       
   136                 sock.onclose = function(e) {
       
   137                     if(context.logging===true){
       
   138                         log('Connection closed (wasClean = ' + e.wasClean + ', code = ' + e.code + ', reason = ' + e.reason + ')');
       
   139                     }
       
   140                     showAlert('Communication interrompue : la socket vient de se fermer.', false);
       
   141                     sock = null;
       
   142                 };
       
   143 
       
   144                 sock.onmessage = function(e) {
       
   145                     var data_json = JSON.parse(e.data);
       
   146                     //console.log('1', data_json);
       
   147                     if(context.logging){
       
   148                         log('Got message: ' + e.data);
       
   149                     }
       
   150                     //showAlert('Annotation bien reçue.', true);
       
   151                     //console.log('2 message', $scope.annotPile.length);
       
   152                     if($scope.annotPile.length>0){
       
   153                         var c = $scope.annotPile.shift();
       
   154                         //console.log('3 message', c);
       
   155                         //console.log('2',data_json.status,data_json.status==='OK');
       
   156                         var ok = data_json.status==='OK';
       
   157                         var i;
       
   158                         if(c===false){
       
   159                             //showAlert('Annotation envoyée.', true);
       
   160                             $scope.sendBtnSuccess = ok;
       
   161                             $scope.sendBtnError = !ok;
       
   162                             i = $interval(function(){ $interval.cancel(i); $scope.sendBtnSuccess = false; $scope.sendBtnError = false; }, 2000, 1);
       
   163                         }
       
   164                         else{
       
   165                             c.sendSuccess = ok;
       
   166                             c.sendError = !ok;
       
   167                             i = $interval(function(){ $interval.cancel(i); c.sendSuccess = false; c.sendError = false; }, 2000, 1);
       
   168                         }
       
   169                         if(!$scope.$$phase) {
       
   170                             $scope.$apply();
       
   171                         }
       
   172                     }
       
   173                 };
       
   174             }
       
   175 
       
   176             $scope.sendAnnotation = function(label, code, c){
       
   177                 if($scope.username==='' || typeof $scope.username==='undefined'){
       
   178                     showAlert('Vous devez indiquer un nom d\'utilisateur.', false);
       
   179                     return;
       
   180                 }
       
   181                 if(label==='' || typeof label==='undefined'){
       
   182                     showAlert('Vous devez indiquer un nom de catégorie.', false);
       
   183                     return;
       
   184                 }
       
   185                 // Send query
       
   186                 if (sock) {
       
   187                     if(typeof code==='undefined' || code===''){
       
   188                         code = window.S(label).slugify().s;
       
   189                     }
       
   190                     var new_annot = {
       
   191                             category: {code: code, label: label},
       
   192                             user : $scope.username
       
   193                     };
       
   194                     sock.send(JSON.stringify(new_annot));
       
   195                     if(context.logging===true){
       
   196                         log('Sent: ' + JSON.stringify(new_annot));
       
   197                     }
       
   198                     if(typeof c==='undefined'){
       
   199                         $scope.annotPile.push(false);
       
   200                     }
       
   201                     else{
       
   202                         $scope.annotPile.push(c);
       
   203                     }
       
   204                 } else {
       
   205                     showAlert('La socket ne fonctionne pas.', false);
       
   206                     if(context.logging===true){
       
   207                         log('Not connected.');
       
   208                     }
       
   209                 }
       
   210             };
       
   211 
       
   212             // Interface management
       
   213             $scope.selectLevel = function(label, code, c){
       
   214                 if(typeof c==='undefined'){
       
   215                     $scope.returnVisStyle = {visibility:'hidden'};
       
   216                     $scope.selectedlevel = false;
       
   217                     return;
       
   218                 }
       
   219                 if(typeof c.subcategories!=='undefined' && c.subcategories.length>0){
       
   220                     $scope.selectedlevel = c.subcategories;
       
   221                     $scope.returnVisStyle = {visibility:'show'};
       
   222                 }
       
   223                 else{
       
   224                     // Send query
       
   225                     //console.log('send ntm', c);
       
   226                     $scope.sendAnnotation(label, code, c);
       
   227                 }
       
   228             };
       
   229 
       
   230         });
       
   231 
       
   232 })();