annot-server/static/js/app.js
changeset 107 6d41506f9482
parent 84 d7c5bffdd2d8
child 109 8546e2181a73
--- a/annot-server/static/js/app.js	Thu Jan 22 02:58:41 2015 +0100
+++ b/annot-server/static/js/app.js	Thu Jan 22 03:23:08 2015 +0100
@@ -35,6 +35,16 @@
                 );
             }
 
+            function parseColor(input) {
+                var m = input.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);
+                if( m) return [m[1],m[2],m[3]];
+                else throw new Error("Colour "+input+" could not be parsed.");
+            }
+
+            function rgbToHex(r, g, b) {
+                return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
+            }
+
             $scope.data = dataModel.data;
 
             var process_categories = function(data) {
@@ -50,7 +60,12 @@
                             }
                         }
                     }
-                    $scope.allCatLabels = cats;
+                    if(typeof data.autocomplete === 'undefined' || data.autocomplete.length === 0) {
+                        $scope.allCatLabels = cats;
+                    }
+                    else {
+                        $scope.allCatLabels = data.autocomplete;
+                    }
                 }
             };
 
@@ -101,7 +116,7 @@
             } else {
                 wsuri = 'ws://' + window.location.hostname + ':8090/annot';
             }
-            
+
             var eventCode = context.event_code;
             if(typeof eventCode==='undefined' || eventCode===''){
                 eventCode = $location.search().event;
@@ -173,7 +188,12 @@
                 };
             }
 
-            $scope.sendAnnotation = function(label, code, c){
+            $scope.sendFreeAnnotation = function(label, text) {
+                $scope.sendAnnotation(label, window.S(label).slugify().s, label, text, $scope.data.defaultColor || "#536991");
+            };
+
+            $scope.sendAnnotation = function(label, code, freeLabel, freetext, color, c){
+
                 if($scope.username==='' || typeof $scope.username==='undefined'){
                     showAlert('Vous devez indiquer un nom d\'utilisateur.', false);
                     return;
@@ -187,8 +207,21 @@
                     if(typeof code==='undefined' || code===''){
                         code = window.S(label).slugify().s;
                     }
+
+                    var hexc;
+                    if(color.substring(0, 4) === 'rgb(') {
+                        var rgbc = parseColor(color);
+                        hexc = rgbToHex(rgbc[0],rgbc[1], rgbc[2]);
+                    }
+                    else {
+                        hexc = color;
+                    }
+
+
                     var new_annot = {
                             category: {code: code, label: label},
+                            text: freetext,
+                            color: hexc,
                             user : $scope.username
                     };
                     sock.send(JSON.stringify(new_annot));
@@ -201,6 +234,8 @@
                     else{
                         $scope.annotPile.push(c);
                     }
+                    $scope.catText = "";
+                    $scope.catLabel = "";
                 } else {
                     showAlert('La socket ne fonctionne pas.', false);
                     if(context.logging===true){
@@ -210,7 +245,7 @@
             };
 
             // Interface management
-            $scope.selectLevel = function(label, code, c){
+            $scope.selectLevel = function(label, code, freelabel, freetext, color, c){
                 if(typeof c==='undefined'){
                     $scope.returnVisStyle = {visibility:'hidden'};
                     $scope.selectedlevel = false;
@@ -223,7 +258,7 @@
                 else{
                     // Send query
                     //console.log('send ntm', c);
-                    $scope.sendAnnotation(label, code, c);
+                    $scope.sendAnnotation(label, code, freelabel, freetext, color, c);
                 }
             };