| author | veltr |
| Wed, 29 Feb 2012 18:13:14 +0100 | |
| branch | popcorn-port |
| changeset 820 | 7968346b9689 |
| parent 766 | aa26ddaf2556 |
| child 821 | 49013af6d50b |
| permissions | -rw-r--r-- |
| 661 | 1 |
IriSP.StackGraphWidget = function(Popcorn, config, Serializer) { |
2 |
IriSP.Widget.call(this, Popcorn, config, Serializer); |
|
3 |
} |
|
4 |
||
5 |
IriSP.StackGraphWidget.prototype = new IriSP.Widget(); |
|
6 |
||
7 |
IriSP.StackGraphWidget.prototype.draw = function() { |
|
8 |
var _defaultTags = [ |
|
9 |
{ |
|
10 |
"keywords" : [ "++" ], |
|
11 |
"description" : "positif", |
|
|
766
aa26ddaf2556
fixed raphael's files to remove trailing colon at the end of objects to make
hamidouk
parents:
694
diff
changeset
|
12 |
"color" : "#1D973D" |
| 661 | 13 |
}, |
14 |
{ |
|
15 |
"keywords" : [ "--" ], |
|
16 |
"description" : "negatif", |
|
|
766
aa26ddaf2556
fixed raphael's files to remove trailing colon at the end of objects to make
hamidouk
parents:
694
diff
changeset
|
17 |
"color" : "#CE0A15" |
| 661 | 18 |
}, |
19 |
{ |
|
20 |
"keywords" : [ "==" ], |
|
21 |
"description" : "reference", |
|
|
766
aa26ddaf2556
fixed raphael's files to remove trailing colon at the end of objects to make
hamidouk
parents:
694
diff
changeset
|
22 |
"color" : "#C5A62D" |
| 661 | 23 |
}, |
24 |
{ |
|
25 |
"keywords" : [ "??" ], |
|
26 |
"description" : "question", |
|
|
766
aa26ddaf2556
fixed raphael's files to remove trailing colon at the end of objects to make
hamidouk
parents:
694
diff
changeset
|
27 |
"color" : "#036AAE" |
|
aa26ddaf2556
fixed raphael's files to remove trailing colon at the end of objects to make
hamidouk
parents:
694
diff
changeset
|
28 |
} |
| 661 | 29 |
], |
30 |
_defaultDefColor = "#585858"; |
|
|
675
82a5ebbedc83
Added a Tag Cloud Widget and corrected Stack Graph Widget
veltr
parents:
661
diff
changeset
|
31 |
this.height = this._config.height || 50; |
| 661 | 32 |
this.width = this.selector.width(); |
|
675
82a5ebbedc83
Added a Tag Cloud Widget and corrected Stack Graph Widget
veltr
parents:
661
diff
changeset
|
33 |
this.isStreamGraph = this._config.streamgraph || false; |
|
82a5ebbedc83
Added a Tag Cloud Widget and corrected Stack Graph Widget
veltr
parents:
661
diff
changeset
|
34 |
this.sliceCount = this._config.slices || ~~(this.width/(this.isStreamGraph ? 20 : 5)); |
| 661 | 35 |
this.tagconf = (this._config.tags |
36 |
? this._config.tags |
|
37 |
: _defaultTags); |
|
38 |
IriSP._(this.tagconf).each(function(_a) { |
|
39 |
_a.regexp = new RegExp(_a.keywords.map(function(_k) { |
|
40 |
return _k.replace(/([\W])/gm,'\\$1'); |
|
41 |
}).join("|"),"im") |
|
42 |
}); |
|
43 |
this.defaultcolorconf = (this._config.defaultcolor |
|
44 |
? this._config.defaultcolor |
|
45 |
: _defaultDefColor); |
|
46 |
this.paper = new Raphael(this.selector[0], this.width, this.height); |
|
47 |
this.groups = []; |
|
|
820
7968346b9689
Added compatibility with cinecast format (with get_aliased)
veltr
parents:
766
diff
changeset
|
48 |
this.duration = this._serializer.getDuration(); |
| 661 | 49 |
|
50 |
var _annotationType = this._serializer.getTweets(), |
|
51 |
_sliceDuration = ~~ ( this.duration / this.sliceCount), |
|
|
675
82a5ebbedc83
Added a Tag Cloud Widget and corrected Stack Graph Widget
veltr
parents:
661
diff
changeset
|
52 |
_annotations = this._serializer._data.annotations, |
|
82a5ebbedc83
Added a Tag Cloud Widget and corrected Stack Graph Widget
veltr
parents:
661
diff
changeset
|
53 |
_groupedAnnotations = IriSP._.range(this.sliceCount).map(function(_i) { |
|
82a5ebbedc83
Added a Tag Cloud Widget and corrected Stack Graph Widget
veltr
parents:
661
diff
changeset
|
54 |
return _annotations.filter(function(_a){ |
|
82a5ebbedc83
Added a Tag Cloud Widget and corrected Stack Graph Widget
veltr
parents:
661
diff
changeset
|
55 |
return (_a.begin <= (1 + _i) * _sliceDuration) && (_a.end >= _i * _sliceDuration) |
|
82a5ebbedc83
Added a Tag Cloud Widget and corrected Stack Graph Widget
veltr
parents:
661
diff
changeset
|
56 |
}); |
| 661 | 57 |
}), |
58 |
_max = IriSP._(_groupedAnnotations).max(function(_g) { |
|
59 |
return _g.length |
|
60 |
}).length, |
|
61 |
_scale = this.height / _max, |
|
|
675
82a5ebbedc83
Added a Tag Cloud Widget and corrected Stack Graph Widget
veltr
parents:
661
diff
changeset
|
62 |
_width = this.width / this.sliceCount |
|
82a5ebbedc83
Added a Tag Cloud Widget and corrected Stack Graph Widget
veltr
parents:
661
diff
changeset
|
63 |
_showTitle = !this._config.excludeTitle, |
|
82a5ebbedc83
Added a Tag Cloud Widget and corrected Stack Graph Widget
veltr
parents:
661
diff
changeset
|
64 |
_showDescription = !this._config.excludeDescription; |
| 661 | 65 |
|
66 |
|
|
67 |
var _paths = this.tagconf.map(function() { |
|
68 |
return []; |
|
69 |
}); |
|
70 |
_paths.push([]); |
|
71 |
|
|
72 |
for (var i = 0; i < this.sliceCount; i++) { |
|
73 |
var _group = _groupedAnnotations[i]; |
|
74 |
if (_group) { |
|
75 |
var _vol = this.tagconf.map(function() { |
|
76 |
return 0; |
|
77 |
}); |
|
78 |
for (var j = 0; j < _group.length; j++){ |
|
|
675
82a5ebbedc83
Added a Tag Cloud Widget and corrected Stack Graph Widget
veltr
parents:
661
diff
changeset
|
79 |
var _txt = (_showTitle ? _group[j].content.title : '') + ' ' + (_showDescription ? _group[j].content.description : '') |
| 661 | 80 |
var _tags = this.tagconf.map(function(_tag) { |
81 |
return (_txt.search(_tag.regexp) == -1 ? 0 : 1) |
|
82 |
}), |
|
83 |
_nbtags = _tags.reduce(function(_a,_b) { |
|
84 |
return _a + _b; |
|
85 |
}, 0); |
|
86 |
if (_nbtags) { |
|
87 |
IriSP._(_tags).each(function(_v, _k) { |
|
88 |
_vol[_k] += (_v / _nbtags); |
|
89 |
}); |
|
90 |
} |
|
91 |
} |
|
92 |
var _nbtags = _vol.reduce(function(_a,_b) { |
|
93 |
return _a + _b; |
|
94 |
}, 0), |
|
95 |
_nbneutre = _group.length - _nbtags, |
|
96 |
_h = _nbneutre * _scale, |
|
97 |
_base = this.height - _h; |
|
98 |
if (!this.isStreamGraph) { |
|
99 |
this.paper.rect(i * _width, _base, _width - 1, _h ).attr({ |
|
100 |
"stroke" : "none", |
|
|
766
aa26ddaf2556
fixed raphael's files to remove trailing colon at the end of objects to make
hamidouk
parents:
694
diff
changeset
|
101 |
"fill" : this.defaultcolorconf |
| 661 | 102 |
}); |
103 |
} |
|
104 |
_paths[0].push(_base); |
|
105 |
for (var j = 0; j < this.tagconf.length; j++) { |
|
106 |
_h = _vol[j] * _scale; |
|
107 |
_base = _base - _h; |
|
108 |
if (!this.isStreamGraph) { |
|
109 |
this.paper.rect(i * _width, _base, _width - 1, _h ).attr({ |
|
110 |
"stroke" : "none", |
|
|
766
aa26ddaf2556
fixed raphael's files to remove trailing colon at the end of objects to make
hamidouk
parents:
694
diff
changeset
|
111 |
"fill" : this.tagconf[j].color |
| 661 | 112 |
}); |
113 |
} |
|
114 |
_paths[j+1].push(_base); |
|
115 |
} |
|
116 |
this.groups.push(_vol.map(function(_v) { |
|
117 |
return _v / _group.length; |
|
118 |
})) |
|
119 |
} else { |
|
120 |
for (var j = 0; j < _paths.length; j++) { |
|
121 |
_paths[j].push(this.height); |
|
122 |
} |
|
123 |
this.groups.push(this.tagconf.map(function() { |
|
124 |
return 0; |
|
125 |
})); |
|
126 |
} |
|
127 |
} |
|
128 |
|
|
129 |
if (this.isStreamGraph) { |
|
130 |
for (var j = _paths.length - 1; j >= 0; j--) { |
|
131 |
var _d = _paths[j].reduce(function(_memo, _v, _k) { |
|
132 |
return _memo + ( _k |
|
133 |
? 'C' + (_k * _width) + ' ' + _paths[j][_k - 1] + ' ' + (_k * _width) + ' ' + _v + ' ' + ((_k + .5) * _width) + ' ' + _v |
|
134 |
: 'M0 ' + _v + 'L' + (.5*_width) + ' ' + _v ) |
|
135 |
},'') + 'L' + this.width + ' ' + _paths[j][_paths[j].length - 1] + 'L' + this.width + ' ' + this.height + 'L0 ' + this.height; |
|
136 |
this.paper.path(_d).attr({ |
|
137 |
"stroke" : "none", |
|
|
766
aa26ddaf2556
fixed raphael's files to remove trailing colon at the end of objects to make
hamidouk
parents:
694
diff
changeset
|
138 |
"fill" : (j ? this.tagconf[j-1].color : this.defaultcolorconf) |
| 661 | 139 |
}); |
140 |
} |
|
141 |
} |
|
142 |
this.rectangleFocus = this.paper.rect(0,0,_width,this.height) |
|
143 |
.attr({ |
|
144 |
"stroke" : "none", |
|
145 |
"fill" : "#ff00ff", |
|
|
766
aa26ddaf2556
fixed raphael's files to remove trailing colon at the end of objects to make
hamidouk
parents:
694
diff
changeset
|
146 |
"opacity" : 0 |
| 661 | 147 |
}) |
148 |
this.rectangleProgress = this.paper.rect(0,0,0,this.height) |
|
149 |
.attr({ |
|
150 |
"stroke" : "none", |
|
151 |
"fill" : "#808080", |
|
|
766
aa26ddaf2556
fixed raphael's files to remove trailing colon at the end of objects to make
hamidouk
parents:
694
diff
changeset
|
152 |
"opacity" : .3 |
| 661 | 153 |
}); |
154 |
this.ligneProgress = this.paper.path("M0 0L0 "+this.height).attr({"stroke":"#ff00ff", "line-width" : 2}) |
|
155 |
|
|
156 |
this._Popcorn.listen("timeupdate", IriSP.wrap(this, this.timeUpdateHandler)); |
|
157 |
var _this = this; |
|
158 |
this.selector |
|
| 694 | 159 |
.click(IriSP.wrap(this, this.clickHandler)) |
160 |
.mousemove(function(event) { |
|
161 |
_this.updateTooltip(event); |
|
|
673
18318dd611c2
made the stackGraphWidget send a message when hovered.
hamidouk
parents:
663
diff
changeset
|
162 |
|
|
18318dd611c2
made the stackGraphWidget send a message when hovered.
hamidouk
parents:
663
diff
changeset
|
163 |
// Also tell the world where the mouse is hovering. |
| 694 | 164 |
var relX = event.pageX - _this.selector.offset().left; |
|
820
7968346b9689
Added compatibility with cinecast format (with get_aliased)
veltr
parents:
766
diff
changeset
|
165 |
var duration = _this._serializer.getDuration(); |
|
673
18318dd611c2
made the stackGraphWidget send a message when hovered.
hamidouk
parents:
663
diff
changeset
|
166 |
var Time = ((relX / _this.width) * duration).toFixed(2); |
|
18318dd611c2
made the stackGraphWidget send a message when hovered.
hamidouk
parents:
663
diff
changeset
|
167 |
_this._Popcorn.trigger("IriSP.StackGraphWidget.mouseOver", Time); |
| 676 | 168 |
|
| 661 | 169 |
}) |
170 |
.mouseout(function() { |
|
171 |
_this.TooltipWidget.hide(); |
|
172 |
_this.rectangleFocus.attr({ |
|
|
766
aa26ddaf2556
fixed raphael's files to remove trailing colon at the end of objects to make
hamidouk
parents:
694
diff
changeset
|
173 |
"opacity" : 0 |
| 661 | 174 |
}) |
175 |
}) |
|
176 |
} |
|
177 |
||
178 |
IriSP.StackGraphWidget.prototype.timeUpdateHandler = function() { |
|
179 |
var _currentTime = this._Popcorn.currentTime(), |
|
180 |
_x = (1000 * _currentTime / this.duration) * this.width; |
|
181 |
this.rectangleProgress.attr({ |
|
|
766
aa26ddaf2556
fixed raphael's files to remove trailing colon at the end of objects to make
hamidouk
parents:
694
diff
changeset
|
182 |
"width" : _x |
| 661 | 183 |
}); |
184 |
this.ligneProgress.attr({ |
|
|
766
aa26ddaf2556
fixed raphael's files to remove trailing colon at the end of objects to make
hamidouk
parents:
694
diff
changeset
|
185 |
"path" : "M" + _x + " 0L" + _x + " " + this.height |
| 661 | 186 |
}) |
187 |
} |
|
188 |
||
189 |
IriSP.StackGraphWidget.prototype.clickHandler = function(event) { |
|
190 |
/* Ctrl-C Ctrl-V'ed from another widget |
|
191 |
*/ |
|
192 |
||
193 |
var relX = event.pageX - this.selector.offset().left; |
|
194 |
var newTime = ((relX / this.width) * this.duration/1000).toFixed(2); |
|
195 |
this._Popcorn.trigger("IriSP.StackGraphWidget.clicked", newTime); |
|
196 |
this._Popcorn.currentTime(newTime); |
|
197 |
}; |
|
198 |
||
199 |
IriSP.StackGraphWidget.prototype.updateTooltip = function(event) { |
|
200 |
var _segment = ~~(this.sliceCount * (event.pageX - this.selector.offset().left)/this.width), |
|
201 |
_valeurs = this.groups[_segment], |
|
202 |
_width = this.width / this.sliceCount, |
|
203 |
_html = '<ul style="list-style: none; margin: 0; padding: 0;">' + this.tagconf.map(function(_tag, _i) { |
|
204 |
return '<li style="clear: both;"><span style="float: left; width: 10px; height: 10px; margin: 2px; background: ' |
|
205 |
+ _tag.color |
|
206 |
+ ';"></span>' |
|
207 |
+ ~~(100 * _valeurs[_i]) |
|
208 |
+ '% de ' |
|
209 |
+ _tag.description |
|
210 |
+ '</li>'; |
|
211 |
}).join('') + '</ul>'; |
|
212 |
this.TooltipWidget._shown = false; // Vraiment, on ne peut pas ouvrir le widget s'il n'est pas encore ouvert ? |
|
213 |
this.TooltipWidget.show('','',event.pageX - 105, event.pageY - 160); |
|
214 |
this.TooltipWidget.selector.find(".tip").html(_html); |
|
215 |
this.rectangleFocus.attr({ |
|
216 |
"x" : _segment * _width, |
|
|
766
aa26ddaf2556
fixed raphael's files to remove trailing colon at the end of objects to make
hamidouk
parents:
694
diff
changeset
|
217 |
"opacity" : .4 |
| 661 | 218 |
}) |
219 |
} |
|
220 |