author | rougeronj |
Fri, 23 Jan 2015 09:58:21 +0100 | |
changeset 134 | 119b6193c493 |
parent 131 | 0bb70072a56f |
permissions | -rw-r--r-- |
99 | 1 |
/** |
2 |
* js/utils.js |
|
3 |
* |
|
4 |
* basic tools |
|
5 |
* |
|
6 |
*/ |
|
131 | 7 |
/*jshint bitwise: false*/ |
8 |
/*jshint camelcase: false */ |
|
99 | 9 |
|
10 |
'use strict'; |
|
11 |
||
124 | 12 |
var PIXI = require('pixi'); |
13 |
var _ = require('lodash'); |
|
125
f9dd7bfed997
introduce moment.js to correctly show the time
ymh <ymh.work@gmail.com>
parents:
124
diff
changeset
|
14 |
var moment = require('moment'); |
124 | 15 |
|
105
25ac8802c189
Improve interface + Add horizontal pianoroll to annotsvizview
rougeronj
parents:
99
diff
changeset
|
16 |
function formatTime (ts) { |
131 | 17 |
return moment(ts).format('HH:mm:ss'); |
105
25ac8802c189
Improve interface + Add horizontal pianoroll to annotsvizview
rougeronj
parents:
99
diff
changeset
|
18 |
} |
25ac8802c189
Improve interface + Add horizontal pianoroll to annotsvizview
rougeronj
parents:
99
diff
changeset
|
19 |
|
119 | 20 |
function colorToHex(c) { |
131 | 21 |
var m = /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/.exec(c); |
22 |
return m ? '#' + (1 << 24 | m[1] << 16 | m[2] << 8 | m[3]).toString(16).substr(1) : c; |
|
119 | 23 |
} |
24 |
||
121
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
25 |
function getAnnotCategories(urlCategories, annotCategories) { |
119 | 26 |
|
121
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
27 |
var jsonLoader = new PIXI.JsonLoader(urlCategories, true); |
124 | 28 |
|
121
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
29 |
jsonLoader.on('loaded', function(res) { |
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
30 |
var data = res.target.json; |
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
31 |
|
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
32 |
while(annotCategories.length > 0) { |
131 | 33 |
annotCategories.pop(); |
121
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
34 |
} |
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
35 |
|
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
36 |
data.sessions.forEach(function(session) { |
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
37 |
var annotCat = { |
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
38 |
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
|
39 |
colors: {} |
131 | 40 |
}; |
121
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
41 |
var categoriesJson = session.categories_json; |
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
42 |
annotCat.order = categoriesJson.order; |
128
9f2334598088
add archive variable to desactivate the socket listening for the static timeLines
rougeronj
parents:
124
diff
changeset
|
43 |
if (typeof(annotCat.order['default']) === 'undefined'){ |
131 | 44 |
annotCat.order.push('default'); |
128
9f2334598088
add archive variable to desactivate the socket listening for the static timeLines
rougeronj
parents:
124
diff
changeset
|
45 |
} |
131 | 46 |
var catList = _.clone(categoriesJson.categories); |
47 |
while(catList.length > 0) { |
|
48 |
var cat = catList.pop(); |
|
49 |
if(cat.code) { |
|
50 |
annotCat.colors[cat.code] = colorToHex(cat.color); |
|
51 |
} |
|
52 |
if(cat.subcategories) { |
|
53 |
catList = catList.concat(cat.subcategories); |
|
54 |
} |
|
55 |
} |
|
121
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
56 |
categoriesJson.categories.forEach(function(cat) { |
131 | 57 |
if(cat.code) { |
58 |
annotCat.colors[cat.code] = colorToHex(cat.color); |
|
59 |
} |
|
121
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
60 |
}); |
131 | 61 |
annotCat.defaultColor = categoriesJson.defaultColor || '#536991'; |
121
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
62 |
annotCategories.push(annotCat); |
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
63 |
}); |
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
64 |
}); |
124 | 65 |
|
121
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
66 |
jsonLoader.load(); |
124 | 67 |
|
121
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
68 |
} |
105
25ac8802c189
Improve interface + Add horizontal pianoroll to annotsvizview
rougeronj
parents:
99
diff
changeset
|
69 |
|
25ac8802c189
Improve interface + Add horizontal pianoroll to annotsvizview
rougeronj
parents:
99
diff
changeset
|
70 |
module.exports = { |
131 | 71 |
formatTime: formatTime, |
72 |
getAnnotCategories: getAnnotCategories, |
|
73 |
colorToHex: colorToHex |
|
99 | 74 |
}; |