| author | veltr |
| Tue, 20 Mar 2012 21:17:48 +0100 | |
| branch | popcorn-port |
| changeset 835 | a8af9da7c622 |
| parent 833 | 33a5d5882be4 |
| child 836 | 526f91f5253e |
| permissions | -rw-r--r-- |
| 585 | 1 |
IriSP.AnnotationsListWidget = function(Popcorn, config, Serializer) { |
2 |
IriSP.Widget.call(this, Popcorn, config, Serializer); |
|
|
605
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
3 |
this.__counter = 0; |
|
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
4 |
this.__oldList = []; |
| 833 | 5 |
|
6 |
this.checkOption('ajax_mode'); |
|
7 |
this.checkOption('project_url'); |
|
8 |
this.checkOption('default_thumbnail'); |
|
9 |
this.checkOption("cinecast_version", false); |
|
10 |
var _this = this; |
|
| 585 | 11 |
}; |
12 |
||
13 |
||
14 |
IriSP.AnnotationsListWidget.prototype = new IriSP.Widget(); |
|
15 |
||
16 |
IriSP.AnnotationsListWidget.prototype.clear = function() { |
|
17 |
}; |
|
18 |
||
| 588 | 19 |
IriSP.AnnotationsListWidget.prototype.clearWidget = function() { |
| 585 | 20 |
}; |
21 |
||
| 629 | 22 |
/** effectively redraw the widget - called by drawList */ |
23 |
IriSP.AnnotationsListWidget.prototype.do_redraw = function(list) { |
|
24 |
var widgetMarkup = IriSP.templToHTML(IriSP.annotationsListWidget_template, {annotations: list}); |
|
25 |
this.selector.html(widgetMarkup); |
|
26 |
}; |
|
27 |
||
| 833 | 28 |
IriSP.AnnotationsListWidget.prototype.transformAnnotation = function(a) { |
29 |
var _this = this |
|
30 |
return { |
|
31 |
"id" : a.id, |
|
32 |
"title": this.cinecast_version ? IriSP.get_aliased(a.meta, ['creator_name', 'creator']) : a.content.title, |
|
33 |
"desc" : this.cinecast_version ? a.content.data : a.content.description, |
|
34 |
"begin": IriSP.msToTime(a.begin), |
|
35 |
"end" : IriSP.msToTime(a.end), |
|
36 |
"thumbnail" : (typeof a.meta == "object" && typeof a.meta.thumbnail == "string") ? a.meta.thumbnail : this.default_thumbnail, |
|
37 |
"url" : (typeof a.meta == "object" && typeof a.meta.url == "string") ? a.meta.url : null, |
|
38 |
"tags": typeof a.tags == "object" |
|
39 |
? IriSP.underscore(a.tags) |
|
40 |
.chain() |
|
41 |
.map(function(_t) { |
|
42 |
if (typeof _t == "string") { |
|
43 |
return _t.replace(/^.*:/,'#'); |
|
44 |
} else { |
|
45 |
if (typeof _t['id-ref'] != "undefined") { |
|
46 |
var _f = IriSP.underscore.find(_this._serializer._data.tags, function(_tag) { |
|
47 |
return _tag['id-ref'] == _t.id; |
|
48 |
}); |
|
49 |
if (typeof _f != "undefined") { |
|
50 |
return IriSP.get_aliased(_f.meta, ['dc:title', 'title']); |
|
51 |
} |
|
52 |
} |
|
53 |
} |
|
54 |
return null; |
|
55 |
}) |
|
56 |
.filter(function(_t) { |
|
57 |
return _t !== null && _t !== "" |
|
58 |
}) |
|
59 |
.value() |
|
60 |
: [] |
|
61 |
} |
|
62 |
} |
|
63 |
||
| 599 | 64 |
/** draw the annotation list */ |
|
607
0b94ae49efbd
added an option to force the redraw of a list, even if the cached lists are the
hamidouk
parents:
605
diff
changeset
|
65 |
IriSP.AnnotationsListWidget.prototype.drawList = function(force_redraw) { |
| 585 | 66 |
var _this = this; |
| 674 | 67 |
|
| 833 | 68 |
// var view_type = this._serializer.getContributions(); |
| 585 | 69 |
var annotations = this._serializer._data.annotations; |
| 602 | 70 |
var currentTime = this._Popcorn.currentTime(); |
| 585 | 71 |
var list = []; |
| 588 | 72 |
|
| 833 | 73 |
/* if (typeof(view_type) === "undefined") { |
| 585 | 74 |
return; |
| 833 | 75 |
} */ |
| 585 | 76 |
for (i = 0; i < annotations.length; i++) { |
| 833 | 77 |
var obj = this.transformAnnotation(annotations[i]); |
78 |
obj.iterator = i; |
|
79 |
obj.distance = Math.abs((annotations[i].end + annotations[i].begin) / 2000 - currentTime); |
|
| 835 | 80 |
if (!this.cinecast_version || annotations[i].type == "cinecast:UserAnnotation") { |
81 |
list.push(obj); |
|
82 |
} |
|
83 |
|
|
| 585 | 84 |
} |
|
607
0b94ae49efbd
added an option to force the redraw of a list, even if the cached lists are the
hamidouk
parents:
605
diff
changeset
|
85 |
|
| 833 | 86 |
list = IriSP.underscore(list) |
87 |
.chain() |
|
88 |
.sortBy(function(_o) { |
|
89 |
return _o.distance; |
|
90 |
}) |
|
91 |
.first(10) |
|
92 |
.sortBy(function(_o) { |
|
93 |
return _o.iterator; |
|
94 |
}) |
|
95 |
.value(); |
|
96 |
|
|
|
605
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
97 |
var idList = IriSP.underscore.pluck(list, "id").sort(); |
| 642 | 98 |
|
|
605
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
99 |
|
| 833 | 100 |
if (!IriSP.underscore.isEqual(this.__oldList, idList) || typeof(force_redraw) !== "undefined") { |
| 629 | 101 |
this.do_redraw(list); |
| 833 | 102 |
this.__oldList = idList; |
|
607
0b94ae49efbd
added an option to force the redraw of a list, even if the cached lists are the
hamidouk
parents:
605
diff
changeset
|
103 |
} |
| 833 | 104 |
/* save for next call */ |
|
605
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
105 |
|
|
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
106 |
|
| 599 | 107 |
}; |
108 |
||
| 674 | 109 |
IriSP.AnnotationsListWidget.prototype.ajaxRedraw = function(timecode) { |
| 748 | 110 |
|
| 752 | 111 |
/* the seeked signal sometimes passes an argument - depending on if we're using |
112 |
our popcorn lookalike or the real thing - if it's the case, use it as it's |
|
113 |
more precise than currentTime which sometimes contains the place we where at */ |
|
114 |
if (IriSP.null_or_undefined(timecode) || typeof(timecode) != "number") { |
|
115 |
var tcode = this._Popcorn.currentTime(); |
|
116 |
} else { |
|
117 |
var tcode = timecode; |
|
118 |
} |
|
119 |
|
|
120 |
|
|
| 748 | 121 |
/* the platform gives us a special url - of the type : http://path/{media}/{begin}/{end} |
122 |
we double the braces using regexps and we feed it to mustache to build the correct url |
|
123 |
we have to do that because the platform only knows at run time what view it's displaying. |
|
124 |
*/ |
|
125 |
|
|
126 |
var platf_url = IriSP.widgetsDefaults["AnnotationsListWidget"].ajax_url |
|
127 |
.replace(/\{/g, '{{').replace(/\}/g, '}}'); |
|
| 698 | 128 |
var media_id = this._serializer.currentMedia()["id"]; |
|
820
7968346b9689
Added compatibility with cinecast format (with get_aliased)
veltr
parents:
784
diff
changeset
|
129 |
var duration = this._serializer.getDuration(); |
| 698 | 130 |
|
| 752 | 131 |
var begin_timecode = (Math.floor(tcode) - 300) * 1000; |
| 698 | 132 |
if (begin_timecode < 0) |
133 |
begin_timecode = 0; |
|
134 |
|
|
| 752 | 135 |
var end_timecode = (Math.floor(tcode) + 300) * 1000; |
| 698 | 136 |
if (end_timecode > duration) |
137 |
end_timecode = duration; |
|
138 |
|
|
| 748 | 139 |
var templ = Mustache.to_html(platf_url, {media: media_id, begin: begin_timecode, |
140 |
end: end_timecode}); |
|
141 |
||
| 698 | 142 |
/* we create on the fly a serializer to get the ajax */ |
143 |
var serializer = new IriSP.JSONSerializer(IriSP.__dataloader, templ); |
|
| 833 | 144 |
serializer.sync(IriSP.wrap(this, function(json) { this.processJson(json, serializer) })); |
| 674 | 145 |
}; |
146 |
||
| 698 | 147 |
/** process the received json - it's a bit hackish */ |
148 |
IriSP.AnnotationsListWidget.prototype.processJson = function(json, serializer) { |
|
149 |
/* FIXME: DRY the whole thing */ |
|
150 |
var annotations = serializer._data.annotations; |
|
151 |
if (IriSP.null_or_undefined(annotations)) |
|
152 |
return; |
|
153 |
|
|
| 784 | 154 |
/* |
155 |
commented in case we wanted to discriminate against some annotation types. |
|
| 698 | 156 |
var view_types = serializer.getIds("Contributions"); |
| 784 | 157 |
*/ |
| 698 | 158 |
var l = []; |
159 |
|
|
|
714
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
160 |
var media = this._serializer.currentMedia()["id"]; |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
161 |
|
| 698 | 162 |
for (i = 0; i < annotations.length; i++) { |
| 833 | 163 |
var obj = this.transformAnnotation(annotations[i]) |
|
714
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
164 |
|
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
165 |
/* only if the annotation isn't present in the document create an |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
166 |
external link */ |
| 833 | 167 |
if (!this.annotations_ids.indexOf(obj["id"]) != -1) { |
|
714
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
168 |
// braindead url; jacques didn't want to create a new one in the platform, |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
169 |
// so we append the cutting id to the url. |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
170 |
obj["url"] = this.project_url + "/" + media + "/" + |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
171 |
annotation.meta["project"] + "/" + |
| 745 | 172 |
annotation.meta["id-ref"]; |
|
714
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
173 |
|
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
174 |
// obj["url"] = document.location.href.split("#")[0] + "/" + annotation.meta["project"]; |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
175 |
} |
| 716 | 176 |
|
177 |
l.push(obj); |
|
| 698 | 178 |
} |
| 703 | 179 |
|
| 698 | 180 |
this.do_redraw(l); |
181 |
}; |
|
| 599 | 182 |
IriSP.AnnotationsListWidget.prototype.draw = function() { |
|
714
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
183 |
|
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
184 |
/* build a table of the annotations present in the document for faster |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
185 |
lookup |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
186 |
*/ |
| 833 | 187 |
this.annotations_ids = []; |
|
714
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
188 |
|
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
189 |
var annotations = this._serializer._data.annotations; |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
190 |
var i = 0; |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
191 |
for(i = 0; i < annotations.length; i++) { |
| 833 | 192 |
this.annotations_ids.push(annotations[i]["id"]); |
|
714
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
193 |
} |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
194 |
|
| 599 | 195 |
this.drawList(); |
| 833 | 196 |
|
197 |
var _this = this; |
|
| 674 | 198 |
|
| 833 | 199 |
if (!this.ajax_mode) { |
200 |
var _throttled = IriSP.underscore.throttle(function() { |
|
201 |
_this.drawList(); |
|
202 |
}, 1500); |
|
203 |
this._Popcorn.listen("IriSP.createAnnotationWidget.addedAnnotation", _throttled); |
|
204 |
this._Popcorn.listen("timeupdate", _throttled); |
|
| 674 | 205 |
} else { |
|
729
7ba63d0315ad
load the widget when the video has finished loading.
hamidouk
parents:
716
diff
changeset
|
206 |
/* update the widget when the video has finished loading and when it's seeked and paused */ |
| 698 | 207 |
this._Popcorn.listen("seeked", IriSP.wrap(this, this.ajaxRedraw)); |
|
729
7ba63d0315ad
load the widget when the video has finished loading.
hamidouk
parents:
716
diff
changeset
|
208 |
this._Popcorn.listen("loadedmetadata", IriSP.wrap(this, this.ajaxRedraw)); |
| 698 | 209 |
this._Popcorn.listen("paused", IriSP.wrap(this, this.ajaxRedraw)); |
|
730
357fc047503b
redraw the widget after that an annotation has been added.
hamidouk
parents:
729
diff
changeset
|
210 |
|
|
357fc047503b
redraw the widget after that an annotation has been added.
hamidouk
parents:
729
diff
changeset
|
211 |
this._Popcorn.listen("IriSP.createAnnotationWidget.addedAnnotation", IriSP.wrap(this, this.ajaxRedraw)); |
| 674 | 212 |
} |
| 698 | 213 |
|
| 585 | 214 |
}; |