client/annotviz/app/js/utils.js
changeset 134 119b6193c493
parent 131 0bb70072a56f
equal deleted inserted replaced
133:12f782a13fa2 134:119b6193c493
     2 * js/utils.js
     2 * js/utils.js
     3 *
     3 *
     4 * basic tools
     4 * basic tools
     5 *
     5 *
     6 */
     6 */
       
     7 /*jshint bitwise: false*/
       
     8 /*jshint camelcase: false */
     7 
     9 
     8 'use strict';
    10 'use strict';
     9 
    11 
    10 var PIXI = require('pixi');
    12 var PIXI = require('pixi');
    11 var _ = require('lodash');
    13 var _ = require('lodash');
    12 var moment = require('moment');
    14 var moment = require('moment');
    13 
    15 
    14 function formatTime (ts) {
    16 function formatTime (ts) {
    15 	return moment(ts).format("HH:mm:ss");
    17     return moment(ts).format('HH:mm:ss');
    16 }
    18 }
    17 
    19 
    18 function colorToHex(c) {
    20 function colorToHex(c) {
    19 	var m = /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/.exec(c);
    21     var m = /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/.exec(c);
    20 	return m ? '#' + (1 << 24 | m[1] << 16 | m[2] << 8 | m[3]).toString(16).substr(1) : c;
    22     return m ? '#' + (1 << 24 | m[1] << 16 | m[2] << 8 | m[3]).toString(16).substr(1) : c;
    21 }
    23 }
    22 
    24 
    23 function getAnnotCategories(urlCategories, annotCategories) {
    25 function getAnnotCategories(urlCategories, annotCategories) {
    24 
    26 
    25     var jsonLoader = new PIXI.JsonLoader(urlCategories, true);
    27     var jsonLoader = new PIXI.JsonLoader(urlCategories, true);
    26 
    28 
    27     jsonLoader.on('loaded', function(res) {
    29     jsonLoader.on('loaded', function(res) {
    28         var data = res.target.json;
    30         var data = res.target.json;
    29 
    31 
    30         while(annotCategories.length > 0) {
    32         while(annotCategories.length > 0) {
    31         	annotCategories.pop();
    33             annotCategories.pop();
    32         }
    34         }
    33 
    35 
    34         data.sessions.forEach(function(session) {
    36         data.sessions.forEach(function(session) {
    35             var annotCat = {
    37             var annotCat = {
    36                 ts: session.start_ts === null ? new Date(0) : Date.parse(session.start_ts),
    38                 ts: session.start_ts === null ? new Date(0) : Date.parse(session.start_ts),
    37                 colors: {}
    39                 colors: {}
    38             }
    40             };
    39             var categoriesJson = session.categories_json;
    41             var categoriesJson = session.categories_json;
    40             annotCat.order = categoriesJson.order;
    42             annotCat.order = categoriesJson.order;
    41             if (typeof(annotCat.order['default']) === 'undefined'){
    43             if (typeof(annotCat.order['default']) === 'undefined'){
    42             	annotCat.order.push('default');
    44                 annotCat.order.push('default');
    43             }
    45             }
    44 			var catList = _.clone(categoriesJson.categories);
    46             var catList = _.clone(categoriesJson.categories);
    45 			while(catList.length > 0) {
    47             while(catList.length > 0) {
    46 				var cat = catList.pop();
    48                 var cat = catList.pop();
    47 				if(cat.code) {
    49                 if(cat.code) {
    48 					annotCat.colors[cat.code] = colorToHex(cat.color);
    50                     annotCat.colors[cat.code] = colorToHex(cat.color);
    49 				}
    51                 }
    50 				if(cat.subcategories) {
    52                 if(cat.subcategories) {
    51 					catList = catList.concat(cat.subcategories);
    53                     catList = catList.concat(cat.subcategories);
    52 				}
    54                 }
    53 			}
    55             }
    54             categoriesJson.categories.forEach(function(cat) {
    56             categoriesJson.categories.forEach(function(cat) {
    55 				if(cat.code) {
    57                 if(cat.code) {
    56                 	annotCat.colors[cat.code] = colorToHex(cat.color);
    58                     annotCat.colors[cat.code] = colorToHex(cat.color);
    57 				}
    59                 }
    58             });
    60             });
    59             annotCat.defaultColor = categoriesJson.defaultColor || "#536991";
    61             annotCat.defaultColor = categoriesJson.defaultColor || '#536991';
    60             annotCategories.push(annotCat);
    62             annotCategories.push(annotCat);
    61         });
    63         });
    62     });
    64     });
    63 
    65 
    64     jsonLoader.load();
    66     jsonLoader.load();
    65 
    67 
    66 }
    68 }
    67 
    69 
    68 module.exports = {
    70 module.exports = {
    69 	formatTime: formatTime,
    71     formatTime: formatTime,
    70 	getAnnotCategories: getAnnotCategories,
    72     getAnnotCategories: getAnnotCategories,
    71 	colorToHex: colorToHex
    73     colorToHex: colorToHex
    72 };
    74 };