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-- |
99 | 1 |
/** |
2 |
* js/utils.js |
|
3 |
* |
|
4 |
* basic tools |
|
5 |
* |
|
6 |
*/ |
|
7 |
||
8 |
'use strict'; |
|
9 |
||
124 | 10 |
var PIXI = require('pixi'); |
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 | 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 | 18 |
function colorToHex(c) { |
19 |
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; |
|
21 |
} |
|
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 | 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 | 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; |
124 | 41 |
var catList = _.clone(categoriesJson.categories); |
42 |
while(catList.length > 0) { |
|
43 |
var cat = catList.pop(); |
|
44 |
if(cat.code) { |
|
45 |
annotCat.colors[cat.code] = colorToHex(cat.color); |
|
46 |
} |
|
47 |
if(cat.subcategories) { |
|
48 |
catList = catList.concat(cat.subcategories); |
|
49 |
} |
|
50 |
} |
|
121
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
51 |
categoriesJson.categories.forEach(function(cat) { |
124 | 52 |
if(cat.code) { |
53 |
annotCat.colors[cat.code] = colorToHex(cat.color); |
|
54 |
} |
|
121
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
55 |
}); |
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
56 |
annotCat.defaultColor = categoriesJson.defaultColor || "#536991"; |
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
57 |
annotCategories.push(annotCat); |
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 |
console.log(annotCategories); |
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
60 |
annotCategories[0].order.push("default"); |
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
61 |
}); |
124 | 62 |
|
121
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
63 |
jsonLoader.load(); |
124 | 64 |
|
121
df6b39f962bc
Add getAnnotCategories to utils and propagate the modification to annotsvizview and annotstimeline
rougeronj
parents:
105
diff
changeset
|
65 |
} |
105
25ac8802c189
Improve interface + Add horizontal pianoroll to annotsvizview
rougeronj
parents:
99
diff
changeset
|
66 |
|
25ac8802c189
Improve interface + Add horizontal pianoroll to annotsvizview
rougeronj
parents:
99
diff
changeset
|
67 |
module.exports = { |
119 | 68 |
formatTime: formatTime, |
122
3b11017a76a4
Merge with e6605fecb175a2d880aa3448085f48bfcaffd29b + minor update about color pic in annostroll
rougeronj
diff
changeset
|
69 |
getAnnotCategories: getAnnotCategories, |
119 | 70 |
colorToHex: colorToHex |
99 | 71 |
}; |