client/annotviz/app/js/utils.js
author rougeronj
Fri, 23 Jan 2015 01:00:00 +0100
changeset 129 7181e1f28eb0
parent 128 9f2334598088
parent 125 f9dd7bfed997
child 131 0bb70072a56f
permissions -rw-r--r--
Merge with f9dd7bfed9976461c79bf521ed01bf4caaf4fbcf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
99
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents:
diff changeset
     1
/**
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents:
diff changeset
     2
* js/utils.js
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents:
diff changeset
     3
*
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents:
diff changeset
     4
* basic tools
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents:
diff changeset
     5
*
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents:
diff changeset
     6
*/
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents:
diff changeset
     7
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents:
diff changeset
     8
'use strict';
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents:
diff changeset
     9
124
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    10
var PIXI = require('pixi');
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    11
var _ = require('lodash');
125
f9dd7bfed997 introduce moment.js to correctly show the time
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    12
var moment = require('moment');
124
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    13
105
25ac8802c189 Improve interface + Add horizontal pianoroll to annotsvizview
rougeronj
parents: 99
diff changeset
    14
function formatTime (ts) {
125
f9dd7bfed997 introduce moment.js to correctly show the time
ymh <ymh.work@gmail.com>
parents: 124
diff changeset
    15
	return moment(ts).format("HH:mm:ss");
105
25ac8802c189 Improve interface + Add horizontal pianoroll to annotsvizview
rougeronj
parents: 99
diff changeset
    16
}
25ac8802c189 Improve interface + Add horizontal pianoroll to annotsvizview
rougeronj
parents: 99
diff changeset
    17
119
e6605fecb175 move colorToHex to utils
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    18
function colorToHex(c) {
e6605fecb175 move colorToHex to utils
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    19
	var m = /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/.exec(c);
e6605fecb175 move colorToHex to utils
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    20
	return m ? '#' + (1 << 24 | m[1] << 16 | m[2] << 8 | m[3]).toString(16).substr(1) : c;
e6605fecb175 move colorToHex to utils
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    21
}
e6605fecb175 move colorToHex to utils
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    22
121
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    23
function getAnnotCategories(urlCategories, annotCategories) {
119
e6605fecb175 move colorToHex to utils
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    24
121
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    25
    var jsonLoader = new PIXI.JsonLoader(urlCategories, true);
124
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    26
121
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    27
    jsonLoader.on('loaded', function(res) {
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    28
        var data = res.target.json;
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    29
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    30
        while(annotCategories.length > 0) {
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    31
        	annotCategories.pop();
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    32
        }
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    33
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    34
        data.sessions.forEach(function(session) {
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    35
            var annotCat = {
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    36
                ts: session.start_ts === null ? new Date(0) : Date.parse(session.start_ts),
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    37
                colors: {}
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    38
            }
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    39
            var categoriesJson = session.categories_json;
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    40
            annotCat.order = categoriesJson.order;
128
9f2334598088 add archive variable to desactivate the socket listening for the static timeLines
rougeronj
parents: 124
diff changeset
    41
            if (typeof(annotCat.order['default']) === 'undefined'){
9f2334598088 add archive variable to desactivate the socket listening for the static timeLines
rougeronj
parents: 124
diff changeset
    42
            	annotCat.order.push('default');
9f2334598088 add archive variable to desactivate the socket listening for the static timeLines
rougeronj
parents: 124
diff changeset
    43
            }
124
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    44
			var catList = _.clone(categoriesJson.categories);
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    45
			while(catList.length > 0) {
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    46
				var cat = catList.pop();
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    47
				if(cat.code) {
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    48
					annotCat.colors[cat.code] = colorToHex(cat.color);
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    49
				}
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    50
				if(cat.subcategories) {
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    51
					catList = catList.concat(cat.subcategories);
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    52
				}
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    53
			}
121
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    54
            categoriesJson.categories.forEach(function(cat) {
124
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    55
				if(cat.code) {
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    56
                	annotCat.colors[cat.code] = colorToHex(cat.color);
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    57
				}
121
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    58
            });
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    59
            annotCat.defaultColor = categoriesJson.defaultColor || "#536991";
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    60
            annotCategories.push(annotCat);
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    61
        });
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    62
    });
124
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    63
121
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    64
    jsonLoader.load();
124
b5697bcdbaff add history load for time lines
ymh <ymh.work@gmail.com>
parents: 122
diff changeset
    65
121
df6b39f962bc Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents: 105
diff changeset
    66
}
105
25ac8802c189 Improve interface + Add horizontal pianoroll to annotsvizview
rougeronj
parents: 99
diff changeset
    67
25ac8802c189 Improve interface + Add horizontal pianoroll to annotsvizview
rougeronj
parents: 99
diff changeset
    68
module.exports = {
119
e6605fecb175 move colorToHex to utils
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    69
	formatTime: formatTime,
122
3b11017a76a4 Merge with e6605fecb175a2d880aa3448085f48bfcaffd29b + minor update about color pic in annostroll
rougeronj
parents: 121 119
diff changeset
    70
	getAnnotCategories: getAnnotCategories,
119
e6605fecb175 move colorToHex to utils
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    71
	colorToHex: colorToHex
99
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents:
diff changeset
    72
};