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