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

(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){
            //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){
                    console.log('    JE RENTRE');
                    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;
            
            
            

            // Socket management
            var sock = null;
            var ellog = null;
            
            ellog = document.getElementById('log');
            
            function log(m) {
                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 {
                log('Browser does not support WebSocket!');
                window.location = 'http://autobahn.ws/unsupportedbrowser';
            }
            console.log('hi 1', sock);
            if (sock) {
                sock.onopen = function() {
                    log('Connected to ' + wsuri);
                };

                sock.onclose = function(e) {
                    log('Connection closed (wasClean = ' + e.wasClean + ', code = ' + e.code + ', reason = "' + e.reason + '")');
                    sock = null;
                };

                sock.onmessage = function(e) {
                    log('Got message: ' + e.data);
                };
            }

            $scope.sendAnnotation = function(eventCode){
                if($scope.username==='' || typeof $scope.username==='undefined'){
                    alert('Vous devez indiquer un nom d\'utilisateur.');
                    return;
                }
                if(eventCode==='' || typeof eventCode==='undefined'){
                    alert('Vous devez indiquer un nom de catégorie.');
                    return;
                }
                // Send query
                if (sock) {
                    var new_annot = {
                            categories : eventCode,
                            user : 'admin'
                    };
                    sock.send(JSON.stringify(new_annot));
                    log('Sent: ' + JSON.stringify(new_annot));
                } else {
                    log('Not connected.');
                }
                //alert('annotation envoyée ! eventCode = ' + eventCode);
            };
            
            // Interface management
            $scope.selectLevel = function(i, eventCode){
                if(i===false){
                    $scope.returnVisStyle = {visibility:'hidden'};
                    $scope.selectedlevel = false;
                    return;
                }
                if(typeof $scope.data.categories[i].subcategories!=='undefined' && $scope.data.categories[i].subcategories.length>0){
                    $scope.selectedlevel = $scope.data.categories[i].subcategories;
                    $scope.returnVisStyle = {visibility:'show'};
                }
                else{
                    // Send query
                    $scope.sendAnnotation(eventCode);
                }
            };
            
        });

})();