client/annotviz/app/js/utils.js
author ymh <ymh.work@gmail.com>
Fri, 23 Jan 2015 00:41:35 +0100
changeset 125 f9dd7bfed997
parent 124 b5697bcdbaff
child 129 7181e1f28eb0
permissions -rw-r--r--
introduce moment.js to correctly show the time

/**
* js/utils.js
*
* basic tools
*
*/

'use strict';

var PIXI = require('pixi');
var _ = require('lodash');
var moment = require('moment');

function formatTime (ts) {
	return moment(ts).format("HH:mm:ss");
}

function colorToHex(c) {
	var m = /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/.exec(c);
	return m ? '#' + (1 << 24 | m[1] << 16 | m[2] << 8 | m[3]).toString(16).substr(1) : c;
}

function getAnnotCategories(urlCategories, annotCategories) {

    var jsonLoader = new PIXI.JsonLoader(urlCategories, true);

    jsonLoader.on('loaded', function(res) {
        var data = res.target.json;

        while(annotCategories.length > 0) {
        	annotCategories.pop();
        }

        data.sessions.forEach(function(session) {
            var annotCat = {
                ts: session.start_ts === null ? new Date(0) : Date.parse(session.start_ts),
                colors: {}
            }
            var categoriesJson = session.categories_json;
            annotCat.order = categoriesJson.order;
			var catList = _.clone(categoriesJson.categories);
			while(catList.length > 0) {
				var cat = catList.pop();
				if(cat.code) {
					annotCat.colors[cat.code] = colorToHex(cat.color);
				}
				if(cat.subcategories) {
					catList = catList.concat(cat.subcategories);
				}
			}
            categoriesJson.categories.forEach(function(cat) {
				if(cat.code) {
                	annotCat.colors[cat.code] = colorToHex(cat.color);
				}
            });
            annotCat.defaultColor = categoriesJson.defaultColor || "#536991";
            annotCategories.push(annotCat);
        });
        console.log(annotCategories);
        annotCategories[0].order.push("default");
    });

    jsonLoader.load();

}

module.exports = {
	formatTime: formatTime,
	getAnnotCategories: getAnnotCategories,
	colorToHex: colorToHex
};