client/app/app.js
author cavaliet
Thu, 16 Oct 2014 10:24:38 +0200
changeset 27 68b29e36c9a2
parent 23 16a1925df2df
child 34 dc21d793b5d4
permissions -rw-r--r--
correct annotationclient for jshint

(function(){
    'use strict';

    angular.module('mons', [ 'ngResource', 'ngRoute', 'autocomplete' ])
        .config(function($routeProvider) {
            $routeProvider.
                when('/', {
                    controller: 'homeCtrl'
               }).
               otherwise({
                   redirectTo: '/'
               });
        })
        .config(function($logProvider){
            $logProvider.debugEnabled(true);
        })
        .service('dataApi', function($resource, context) {
            console.log('dataApi',$resource, context);
            this.dataResource = $resource(context.urls.dataUrl);
        })
        .service('dataModel', function(dataApi) {
            //console.log('dataModel',this,dataApi);
            this.data = dataApi.dataResource.get();
        })
        .controller('homeCtrl', function($scope, $location, dataModel, context, $interval){
            //console.log('homeCtrl 1', $scope, $location, context);
            console.log('homeCtrl 2', context);
            $scope.data = dataModel.data;

            dataModel.data.$promise.then(function(data) {
                if(typeof data.categories!=='undefined' && data.categories.length>0){
                    var cats = [];
                    var nbCat = data.categories.length;
                    for(var i=0;i<nbCat;i++){
                        cats.push(data.categories[i].label);
                        if(typeof data.categories[i].subcategories!=='undefined' && data.categories[i].subcategories.length>0){
                            var nbSubCat = data.categories[i].subcategories.length;
                            for(var j=0;j<nbSubCat;j++){
                                cats.push(data.categories[i].subcategories[j].label);
                            }
                        }
                    }
                    $scope.allCatLabels = cats;
                }
            });

            $scope.selectedlevel = false;

            $scope.currentInterval = false;
            $scope.showSuccessAlert = false;
            $scope.showAlertDiv = false;
            function showAlert(m, success){
                $scope.alertMessage = m;
                $scope.showSuccessAlert = success;
                $scope.showAlertDiv = true;
                if(!$scope.$$phase) {
                    $scope.$apply();
                }
                if($scope.currentInterval){
                    $interval.cancel($scope.currentInterval);
                    $scope.currentInterval = false;
                }
                $scope.currentInterval = $interval(function(){ $interval.cancel($scope.currentInterval); $scope.showAlertDiv = false; }, 2000, 1);
            }
            $scope.annotPile = [];



            // Socket management
            var sock = null;
            var ellog = null;

            ellog = document.getElementById('log');
            function log(m) {
                if(ellog){
                    ellog.innerHTML += m + '\n';
                    ellog.scrollTop = ellog.scrollHeight;
                }
            }

            var wsuri;
            if (window.location.protocol === 'file:') {
                wsuri = 'ws://127.0.0.1:8090/annot';
            } else {
                wsuri = 'ws://' + window.location.hostname + ':8090/annot';
            }
            wsuri = wsuri + '?event=test';

            if ('WebSocket' in window) {
                sock = new WebSocket(wsuri);
            } else if ('MozWebSocket' in window) {
                sock = new window.MozWebSocket(wsuri);
            } else {
                if(context.logging===true){
                    log('Browser does not support WebSocket!');
                }
                window.location = 'http://autobahn.ws/unsupportedbrowser';
            }

            if (sock) {
                sock.onopen = function() {
                    if(context.logging===true){
                        log('Connected to ' + wsuri);
                    }
                };

                sock.onclose = function(e) {
                    if(context.logging===true){
                        log('Connection closed (wasClean = ' + e.wasClean + ', code = ' + e.code + ', reason = ' + e.reason + ')');
                    }
                    showAlert('Communication interrompue : la socket vient de se fermer.', false);
                    sock = null;
                };

                sock.onmessage = function(e) {
                    if(context.logging){
                        console.log('Got message: ', e.data);
                    }
                    //var data_json = JSON.parse(e.data);
                    //showAlert('Annotation bien reçue.', true);
                    console.log('2 message', $scope.annotPile.length);
                    if($scope.annotPile.length>0){
                        var c = $scope.annotPile.shift();
                        //console.log('3 message', c);
                        var i;
                        if(c===false){
                            //showAlert('Annotation envoyée.', true);
                            $scope.sendBtnSuccess = true;
                            i = $interval(function(){ $interval.cancel(i); $scope.sendBtnSuccess = false; }, 2000, 1);
                        }
                        else{
                            c.sendSuccess = true;
                            i = $interval(function(){ $interval.cancel(i); c.sendSuccess = false; }, 2000, 1);
                        }
                        if(!$scope.$$phase) {
                            $scope.$apply();
                        }
                    }
                };
            }

            $scope.sendAnnotation = function(label, code, c){
                if($scope.username==='' || typeof $scope.username==='undefined'){
                    showAlert('Vous devez indiquer un nom d\'utilisateur.', false);
                    return;
                }
                if(label==='' || typeof label==='undefined'){
                    showAlert('Vous devez indiquer un nom de catégorie.', false);
                    return;
                }
                // Send query
                if (sock) {
                    if(typeof code==='undefined' || code===''){
                        code = window.S(label).slugify().s;
                    }
                    var new_annot = {
                            category: {code: code, label: label},
                            user : $scope.username
                    };
                    sock.send(JSON.stringify(new_annot));
                    if(context.logging===true){
                        log('Sent: ' + JSON.stringify(new_annot));
                    }
                    if(typeof c==='undefined'){
                        $scope.annotPile.push(false);
                    }
                    else{
                        $scope.annotPile.push(c);
                    }
                } else {
                    showAlert('La socket ne fonctionne pas.', false);
                    if(context.logging===true){
                        log('Not connected.');
                    }
                }
            };

            // Interface management
            $scope.selectLevel = function(label, code, c){
                if(typeof c==='undefined'){
                    $scope.returnVisStyle = {visibility:'hidden'};
                    $scope.selectedlevel = false;
                    return;
                }
                if(typeof c.subcategories!=='undefined' && c.subcategories.length>0){
                    $scope.selectedlevel = c.subcategories;
                    $scope.returnVisStyle = {visibility:'show'};
                }
                else{
                    // Send query
                    console.log('send ntm', c);
                    $scope.sendAnnotation(label, code, c);
                }
            };

        });

})();