| author | hamidouk |
| Tue, 31 Jan 2012 11:58:26 +0100 | |
| branch | popcorn-port |
| changeset 748 | ec1f7aa873f4 |
| parent 745 | a996a24e07d6 |
| child 752 | ebc72cbc35f4 |
| 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 = []; |
| 674 | 5 |
|
6 |
this.ajax_mode = IriSP.widgetsDefaults["AnnotationsListWidget"].ajax_mode; |
|
|
714
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
7 |
this.project_url = IriSP.widgetsDefaults["AnnotationsListWidget"].project_url; |
| 585 | 8 |
}; |
9 |
||
10 |
||
11 |
IriSP.AnnotationsListWidget.prototype = new IriSP.Widget(); |
|
12 |
||
13 |
IriSP.AnnotationsListWidget.prototype.clear = function() { |
|
14 |
}; |
|
15 |
||
| 588 | 16 |
IriSP.AnnotationsListWidget.prototype.clearWidget = function() { |
| 585 | 17 |
}; |
18 |
||
| 629 | 19 |
/** effectively redraw the widget - called by drawList */ |
20 |
IriSP.AnnotationsListWidget.prototype.do_redraw = function(list) { |
|
21 |
var widgetMarkup = IriSP.templToHTML(IriSP.annotationsListWidget_template, {annotations: list}); |
|
22 |
this.selector.html(widgetMarkup); |
|
23 |
}; |
|
24 |
||
| 599 | 25 |
/** 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
|
26 |
IriSP.AnnotationsListWidget.prototype.drawList = function(force_redraw) { |
| 585 | 27 |
var _this = this; |
| 674 | 28 |
|
|
595
29d86e6c61a6
finished going through the widgets to add stricter line checking.
hamidouk
parents:
588
diff
changeset
|
29 |
var view_type = this._serializer.getContributions(); |
| 585 | 30 |
var annotations = this._serializer._data.annotations; |
| 602 | 31 |
var currentTime = this._Popcorn.currentTime(); |
|
605
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
32 |
|
| 585 | 33 |
var list = []; |
| 588 | 34 |
|
| 621 | 35 |
if (typeof(view_type) === "undefined") { |
| 585 | 36 |
return; |
37 |
} |
|
| 588 | 38 |
|
| 585 | 39 |
for (i = 0; i < annotations.length; i++) { |
40 |
var annotation = annotations[i]; |
|
41 |
||
42 |
/* filter the annotations whose type is not the one we want */ |
|
43 |
if (typeof(annotation.meta) !== "undefined" && typeof(annotation.meta["id-ref"]) !== "undefined" |
|
| 615 | 44 |
&& annotation.meta["id-ref"] !== view_type) { |
| 585 | 45 |
continue; |
46 |
} |
|
47 |
||
| 602 | 48 |
/* only get the annotations happening in the current chapter */ |
| 603 | 49 |
if (!(annotation.begin <= currentTime * 1000 && annotation.end > currentTime * 1000)) { |
| 602 | 50 |
continue; |
51 |
} |
|
| 603 | 52 |
|
| 585 | 53 |
var a = annotation; |
54 |
var obj = {}; |
|
| 588 | 55 |
|
| 585 | 56 |
obj["id"] = a.id; |
57 |
obj["title"] = a.content.title; |
|
58 |
obj["desc"] = a.content.description; |
|
| 603 | 59 |
obj["begin"] = IriSP.msToTime(annotation.begin); |
60 |
obj["end"] = IriSP.msToTime(annotation.end); |
|
| 588 | 61 |
|
| 585 | 62 |
list.push(obj); |
63 |
} |
|
|
607
0b94ae49efbd
added an option to force the redraw of a list, even if the cached lists are the
hamidouk
parents:
605
diff
changeset
|
64 |
|
|
605
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
65 |
var idList = IriSP.underscore.pluck(list, "id").sort(); |
| 642 | 66 |
|
|
605
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
67 |
if (idList.length !== this.__oldList.length) { |
| 629 | 68 |
this.do_redraw(list); |
|
605
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
69 |
} |
|
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
70 |
|
|
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
71 |
var res = 1; |
|
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
72 |
for (var i = 0; i < idList.length; i++) { |
|
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
73 |
if (idList[i] !== this.__oldList[i]) |
|
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
74 |
res = 0; |
|
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
75 |
break; |
|
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
76 |
} |
|
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
77 |
|
|
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
78 |
this.__oldList = idList; /* save for next call */ |
| 698 | 79 |
|
|
607
0b94ae49efbd
added an option to force the redraw of a list, even if the cached lists are the
hamidouk
parents:
605
diff
changeset
|
80 |
if (typeof(force_redraw) !== "undefined") { |
| 629 | 81 |
this.do_redraw(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
|
82 |
} |
|
605
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
83 |
|
|
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
84 |
/* the two lists are equal, no need to redraw */ |
|
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
85 |
if (res === 1) { |
|
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
86 |
return; |
|
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
87 |
} else { |
| 629 | 88 |
this.do_redraw(list); |
|
605
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
89 |
} |
|
e1a6f73038b4
made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents:
603
diff
changeset
|
90 |
|
| 599 | 91 |
}; |
92 |
||
| 674 | 93 |
IriSP.AnnotationsListWidget.prototype.ajaxRedraw = function(timecode) { |
| 748 | 94 |
|
95 |
/* the platform gives us a special url - of the type : http://path/{media}/{begin}/{end} |
|
96 |
we double the braces using regexps and we feed it to mustache to build the correct url |
|
97 |
we have to do that because the platform only knows at run time what view it's displaying. |
|
98 |
*/ |
|
99 |
|
|
100 |
var platf_url = IriSP.widgetsDefaults["AnnotationsListWidget"].ajax_url |
|
101 |
.replace(/\{/g, '{{').replace(/\}/g, '}}'); |
|
| 698 | 102 |
var media_id = this._serializer.currentMedia()["id"]; |
103 |
var duration = +this._serializer.currentMedia().meta["dc:duration"]; |
|
104 |
|
|
105 |
var begin_timecode = (Math.floor(this._Popcorn.currentTime()) - 300) * 1000; |
|
106 |
if (begin_timecode < 0) |
|
107 |
begin_timecode = 0; |
|
108 |
|
|
109 |
var end_timecode = (Math.floor(this._Popcorn.currentTime()) + 300) * 1000; |
|
110 |
if (end_timecode > duration) |
|
111 |
end_timecode = duration; |
|
112 |
|
|
| 748 | 113 |
var templ = Mustache.to_html(platf_url, {media: media_id, begin: begin_timecode, |
114 |
end: end_timecode}); |
|
115 |
||
| 698 | 116 |
/* we create on the fly a serializer to get the ajax */ |
117 |
var serializer = new IriSP.JSONSerializer(IriSP.__dataloader, templ); |
|
118 |
serializer.sync(IriSP.wrap(this, function(json) { this.processJson(json, serializer) })); |
|
| 674 | 119 |
}; |
120 |
||
| 698 | 121 |
/** process the received json - it's a bit hackish */ |
122 |
IriSP.AnnotationsListWidget.prototype.processJson = function(json, serializer) { |
|
123 |
/* FIXME: DRY the whole thing */ |
|
124 |
var annotations = serializer._data.annotations; |
|
125 |
if (IriSP.null_or_undefined(annotations)) |
|
126 |
return; |
|
127 |
|
|
128 |
var view_types = serializer.getIds("Contributions"); |
|
129 |
var l = []; |
|
130 |
|
|
|
714
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
131 |
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
|
132 |
|
| 698 | 133 |
for (i = 0; i < annotations.length; i++) { |
134 |
var annotation = annotations[i]; |
|
| 703 | 135 |
|
| 698 | 136 |
/* filter the annotations whose type is not the one we want */ |
137 |
/* We want _all_ the annotations. |
|
138 |
if (typeof(annotation.meta) !== "undefined" && typeof(annotation.meta["id-ref"]) !== "undefined" |
|
139 |
&& !IriSP.underscore.include(view_types, annotation.meta["id-ref"])) { |
|
140 |
continue; |
|
141 |
} |
|
142 |
*/ |
|
143 |
var a = annotation; |
|
144 |
var obj = {}; |
|
145 |
||
146 |
obj["id"] = a.id; |
|
147 |
obj["title"] = a.content.title; |
|
148 |
obj["desc"] = a.content.description; |
|
149 |
obj["begin"] = IriSP.msToTime(annotation.begin); |
|
150 |
obj["end"] = IriSP.msToTime(annotation.end); |
|
|
714
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
151 |
|
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
152 |
/* 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
|
153 |
external link */ |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
154 |
if (!this.annotations_ids.hasOwnProperty(obj["id"])) { |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
155 |
// 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
|
156 |
// 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
|
157 |
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
|
158 |
annotation.meta["project"] + "/" + |
| 745 | 159 |
annotation.meta["id-ref"]; |
|
714
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
160 |
|
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
161 |
// 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
|
162 |
} |
| 716 | 163 |
|
164 |
l.push(obj); |
|
| 698 | 165 |
} |
| 703 | 166 |
|
| 698 | 167 |
this.do_redraw(l); |
168 |
}; |
|
| 599 | 169 |
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
|
170 |
|
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
171 |
/* 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
|
172 |
lookup |
|
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 |
this.annotations_ids = {}; |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
175 |
|
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
176 |
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
|
177 |
var i = 0; |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
178 |
for(i = 0; i < annotations.length; i++) { |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
179 |
this.annotations_ids[annotations[i]["id"]] = 1; |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
180 |
} |
|
9056928c46de
WIP - making links to only annotations which are not present in the opened
hamidouk
parents:
703
diff
changeset
|
181 |
|
| 599 | 182 |
this.drawList(); |
| 674 | 183 |
|
| 698 | 184 |
if (!this.ajax_mode) { |
| 674 | 185 |
this._Popcorn.listen("IriSP.createAnnotationWidget.addedAnnotation", IriSP.wrap(this, function() { this.drawList(true); })); |
| 698 | 186 |
this._Popcorn.listen("timeupdate", IriSP.wrap(this, this.redraw)); |
| 674 | 187 |
} else { |
|
729
7ba63d0315ad
load the widget when the video has finished loading.
hamidouk
parents:
716
diff
changeset
|
188 |
/* update the widget when the video has finished loading and when it's seeked and paused */ |
| 698 | 189 |
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
|
190 |
this._Popcorn.listen("loadedmetadata", IriSP.wrap(this, this.ajaxRedraw)); |
| 698 | 191 |
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
|
192 |
|
|
357fc047503b
redraw the widget after that an annotation has been added.
hamidouk
parents:
729
diff
changeset
|
193 |
this._Popcorn.listen("IriSP.createAnnotationWidget.addedAnnotation", IriSP.wrap(this, this.ajaxRedraw)); |
| 674 | 194 |
} |
| 698 | 195 |
|
| 599 | 196 |
}; |
197 |
||
198 |
IriSP.AnnotationsListWidget.prototype.redraw = function() { |
|
199 |
this.drawList(); |
|
| 585 | 200 |
}; |