| author | hamidouk |
| Wed, 11 Jan 2012 15:04:27 +0100 | |
| branch | popcorn-port |
| changeset 608 | 2fb4eda4dcd2 |
| parent 606 | 13675511a01a |
| child 611 | d931a7d12519 |
| permissions | -rw-r--r-- |
| 543 | 1 |
IriSP.createAnnotationWidget = function(Popcorn, config, Serializer) { |
2 |
IriSP.Widget.call(this, Popcorn, config, Serializer); |
|
3 |
this._hidden = true; |
|
| 547 | 4 |
this.keywords = IriSP.widgetsDefaults["createAnnotationWidget"].keywords; |
|
553
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
5 |
this.cinecast_version = IriSP.widgetsDefaults["createAnnotationWidget"].cinecast_version; |
| 549 | 6 |
this.ids = {}; /* a dictionnary linking buttons ids to keywords */ |
| 543 | 7 |
}; |
8 |
||
9 |
||
10 |
IriSP.createAnnotationWidget.prototype = new IriSP.Widget(); |
|
11 |
||
12 |
IriSP.createAnnotationWidget.prototype.clear = function() { |
|
13 |
this.selector.find(".Ldt-SaTitle").text(""); |
|
14 |
this.selector.find(".Ldt-SaDescription").text(""); |
|
15 |
this.selector.find(".Ldt-SaKeywordText").text(""); |
|
16 |
}; |
|
17 |
||
18 |
IriSP.createAnnotationWidget.prototype.draw = function() { |
|
19 |
var _this = this; |
|
20 |
||
|
553
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
21 |
if (this.cinecast_version) { |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
22 |
var annotationMarkup = IriSP.templToHTML(IriSP.createAnnotationWidget_festivalCinecast_template); |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
23 |
} else { |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
24 |
var annotationMarkup = IriSP.templToHTML(IriSP.createAnnotationWidget_template); |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
25 |
} |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
26 |
|
| 543 | 27 |
this.selector.append(annotationMarkup); |
| 548 | 28 |
|
| 543 | 29 |
this.selector.hide(); |
| 548 | 30 |
for (var i = 0; i < this.keywords.length; i++) { |
| 549 | 31 |
var keyword = this.keywords[i]; |
32 |
var id = IriSP.guid("button_"); |
|
33 |
var templ = IriSP.templToHTML("<button id={{id}} class='Ldt-createAnnotation-absent-keyword'>{{keyword}}</button>", |
|
34 |
{keyword: keyword, id: id}); |
|
| 548 | 35 |
|
| 549 | 36 |
this.ids[keyword] = id; // save it for the function that handle textarea changes. |
37 |
|
|
| 548 | 38 |
this.selector.find(".Ldt-createAnnotation-keywords").append(templ); |
| 549 | 39 |
this.selector.find("#" + id).click(function(keyword) { return function() { |
40 |
var contents = _this.selector.find(".Ldt-createAnnotation-Description").val(); |
|
41 |
if (contents.indexOf(keyword) != -1) { |
|
42 |
var newVal = contents.replace(keyword, ""); |
|
43 |
} else { |
|
44 |
var newVal = contents + keyword; |
|
45 |
} |
|
46 |
|
|
47 |
_this.selector.find(".Ldt-createAnnotation-Description").val(newVal); |
|
| 570 | 48 |
// we use a custom event because there's no simple way to test for a js |
49 |
// change in a textfield. |
|
50 |
_this.selector.find(".Ldt-createAnnotation-Description").trigger("js_mod"); |
|
| 549 | 51 |
// also call our update function. |
52 |
_this.handleTextChanges(); |
|
53 |
} |
|
54 |
}(keyword)); |
|
| 548 | 55 |
} |
56 |
|
|
| 549 | 57 |
this.selector.find(".Ldt-createAnnotation-Description") |
58 |
.bind("propertychange keyup input paste", IriSP.wrap(this, this.handleTextChanges)); |
|
| 543 | 59 |
|
|
553
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
60 |
this.selector.find(".Ldt-createAnnotation-submitButton").click(IriSP.wrap(this, this.handleButtonClick)); |
| 543 | 61 |
this._Popcorn.listen("IriSP.PlayerWidget.AnnotateButton.clicked", |
| 547 | 62 |
IriSP.wrap(this, this.handleAnnotateSignal)); |
| 543 | 63 |
}; |
64 |
||
65 |
IriSP.createAnnotationWidget.prototype.handleAnnotateSignal = function() { |
|
66 |
if (this._hidden == false) { |
|
67 |
this.selector.hide(); |
|
68 |
this._hidden = true; |
|
|
553
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
69 |
/* reinit the fields */ |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
70 |
|
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
71 |
this.selector.find(".Ldt-createAnnotation-DoubleBorder").children().show(); |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
72 |
this.selector.find("Ldt-createAnnotation-Description").val(""); |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
73 |
this.selector.find(".Ldt-createAnnotation-endScreen").hide(); |
| 570 | 74 |
|
75 |
// free the arrow. |
|
76 |
this._Popcorn.trigger("IriSP.ArrowWidget.releaseArrow"); |
|
| 543 | 77 |
} else { |
|
553
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
78 |
if (this.cinecast_version) { |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
79 |
var currentTime = this._Popcorn.currentTime(); |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
80 |
var currentAnnotation = this._serializer.currentAnnotations(currentTime)[0]; |
|
594
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
81 |
|
|
553
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
82 |
var beginTime = IriSP.msToTime(currentAnnotation.begin); |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
83 |
var endTime = IriSP.msToTime(currentAnnotation.end); |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
84 |
|
|
594
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
85 |
/* save the variable because the user may take some time writing his |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
86 |
comment so the currentAnnottion may change when it's time to post it */ |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
87 |
this._currentAnnotation = currentAnnotation; |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
88 |
|
|
553
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
89 |
if (typeof(currentAnnotation.content) !== "undefined") |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
90 |
this.selector.find(".Ldt-createAnnotation-Title").html(currentAnnotation.content.title); |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
91 |
|
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
92 |
var timeTemplate = IriSP.templToHTML("- ({{begin}} - {{ end }})", {begin: beginTime, end: endTime }); |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
93 |
this.selector.find(".Ldt-createAnnotation-TimeFrame").html(timeTemplate); |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
94 |
} |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
95 |
|
| 543 | 96 |
this.selector.show(); |
97 |
this._hidden = false; |
|
| 563 | 98 |
|
99 |
// block the arrow. |
|
100 |
this._Popcorn.trigger("IriSP.ArrowWidget.blockArrow"); |
|
| 543 | 101 |
} |
| 549 | 102 |
}; |
103 |
||
104 |
/** watch for changes in the textfield and change the buttons accordingly */ |
|
105 |
IriSP.createAnnotationWidget.prototype.handleTextChanges = function(event) { |
|
106 |
var contents = this.selector.find(".Ldt-createAnnotation-Description").val(); |
|
107 |
||
108 |
for(var keyword in this.ids) { |
|
109 |
|
|
110 |
var id = this.ids[keyword]; |
|
111 |
||
112 |
if (contents.indexOf(keyword) != -1) { |
|
113 |
/* the word is present in the textarea but the button is not toggled */ |
|
114 |
if (this.selector.find("#" + id).hasClass("Ldt-createAnnotation-absent-keyword")) |
|
115 |
this.selector.find("#" + id).removeClass("Ldt-createAnnotation-absent-keyword") |
|
116 |
.addClass("Ldt-createAnnotation-present-keyword"); |
|
117 |
} else { |
|
118 |
/* the word is absent from the textarea but the button is toggled */ |
|
119 |
if (this.selector.find("#" + id).hasClass("Ldt-createAnnotation-present-keyword")) { |
|
120 |
this.selector.find("#" + id).removeClass("Ldt-createAnnotation-present-keyword") |
|
121 |
.addClass("Ldt-createAnnotation-absent-keyword"); |
|
122 |
} |
|
123 |
} |
|
124 |
} |
|
|
553
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
125 |
}; |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
126 |
|
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
127 |
/** handle clicks on "send annotation" button */ |
|
10d08f43c534
widget works now more or less like in samuel's crea.
hamidouk
parents:
549
diff
changeset
|
128 |
IriSP.createAnnotationWidget.prototype.handleButtonClick = function(event) { |
| 597 | 129 |
var _this = this; |
|
561
a10e7b6fdb08
display an error message when the user forget to enter text.
hamidouk
parents:
553
diff
changeset
|
130 |
var textfield = this.selector.find(".Ldt-createAnnotation-Description"); |
|
a10e7b6fdb08
display an error message when the user forget to enter text.
hamidouk
parents:
553
diff
changeset
|
131 |
var contents = textfield.val(); |
|
a10e7b6fdb08
display an error message when the user forget to enter text.
hamidouk
parents:
553
diff
changeset
|
132 |
|
|
a10e7b6fdb08
display an error message when the user forget to enter text.
hamidouk
parents:
553
diff
changeset
|
133 |
if (contents === "") { |
| 570 | 134 |
|
135 |
if (this.selector.find(".Ldt-createAnnotation-errorMessage").length === 0) { |
|
136 |
this.selector.find(".Ldt-createAnnotation-Container") |
|
137 |
.after(IriSP.templToHTML(IriSP.createAnnotation_errorMessage_template)); |
|
| 571 | 138 |
textfield.css("background-color", "#d93c71"); |
139 |
} else { |
|
140 |
this.selector.find(".Ldt-createAnnotation-errorMessage").show(); |
|
141 |
} |
|
| 570 | 142 |
// use namespaced events to be able to unbind them quickly and without unbinding |
143 |
// the other event handlers. |
|
144 |
textfield.bind("js_mod.tmp propertychange.tmp keyup.tmp input.tmp paste.tmp", IriSP.wrap(this, function() { |
|
145 |
var contents = textfield.val(); |
|
146 |
console.log(contents); |
|
147 |
if (contents !== "") { |
|
148 |
this.selector.find(".Ldt-createAnnotation-errorMessage").hide(); |
|
149 |
textfield.css("background-color", ""); |
|
150 |
textfield.unbind(".tmp"); |
|
151 |
} |
|
152 |
})); |
|
|
561
a10e7b6fdb08
display an error message when the user forget to enter text.
hamidouk
parents:
553
diff
changeset
|
153 |
} else { |
|
a10e7b6fdb08
display an error message when the user forget to enter text.
hamidouk
parents:
553
diff
changeset
|
154 |
this.selector.find(".Ldt-createAnnotation-DoubleBorder").children().hide(); |
| 570 | 155 |
|
156 |
if (this.cinecast_version) { |
|
157 |
this.selector.find(".Ldt-createAnnotation-Title").parent().show(); |
|
158 |
} |
|
| 575 | 159 |
|
160 |
var twStatus = IriSP.mkTweetUrl(document.location.href); |
|
161 |
var gpStatus = IriSP.mkGplusUrl(document.location.href); |
|
162 |
var fbStatus = IriSP.mkFbUrl(document.location.href); |
|
| 570 | 163 |
|
| 575 | 164 |
this.selector.find(".Ldt-createAnnotation-endScreen-TweetLink").attr("href", twStatus); |
165 |
this.selector.find(".Ldt-createAnnotation-endScreen-FbLink").attr("href", fbStatus); |
|
166 |
this.selector.find(".Ldt-createAnnotation-endScreen-GplusLink").attr("href", gpStatus); |
|
167 |
|
|
|
594
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
168 |
this.selector.find(".Ldt-createAnnotation-endScreen").show(); |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
169 |
|
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
170 |
|
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
171 |
if (typeof(this._currentAnnotation) === "undefined") { |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
172 |
console.log("this._currentAnnotation undefined"); |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
173 |
return; |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
174 |
} |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
175 |
|
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
176 |
var apiJson = {annotations : [{}], meta: {}}; |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
177 |
var annotation = apiJson["annotations"][0]; |
| 608 | 178 |
|
| 597 | 179 |
annotation["media"] = this._serializer.currentMedia()["id"]; |
|
594
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
180 |
annotation["begin"] = this._currentAnnotation.begin; |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
181 |
annotation["end"] = this._currentAnnotation.end; |
| 597 | 182 |
annotation["type"] = this._serializer.getContributions(); |
| 608 | 183 |
if (typeof(annotation["type"]) === "undefined") |
184 |
annotation["type"] = ""; |
|
185 |
|
|
186 |
annotation["type_title"] = "Contributions"; |
|
|
594
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
187 |
annotation.content = {}; |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
188 |
annotation.content["data"] = contents; |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
189 |
|
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
190 |
var meta = apiJson["meta"]; |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
191 |
meta.creator = "An User"; |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
192 |
meta.created = Date().toString(); |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
193 |
|
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
194 |
annotation["tags"] = []; |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
195 |
|
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
196 |
for (var i = 0; i < this.keywords.length; i++) { |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
197 |
var keyword = this.keywords[i]; |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
198 |
if (contents.indexOf(keyword) != -1) |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
199 |
annotation["tags"].push(keyword); |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
200 |
} |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
201 |
|
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
202 |
var jsonString = JSON.stringify(apiJson); |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
203 |
var project_id = this._serializer._data.meta.id; |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
204 |
|
| 597 | 205 |
var url = Mustache.to_html("{{platf_url}}/ldtplatform/api/ldt/projects/{{id}}.json", |
206 |
{platf_url: IriSP.platform_url, id: project_id}); |
|
| 608 | 207 |
console.log(url, jsonString); |
|
594
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
208 |
IriSP.jQuery.ajax({ |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
209 |
url: url, |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
210 |
type: 'PUT', |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
211 |
contentType: 'application/json', |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
212 |
data: jsonString, |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
213 |
// bug with jquery >= 1.5, "json" adds a callback so we don't specify dataType |
| 597 | 214 |
dataType: 'json', |
|
594
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
215 |
success: function(json, textStatus, XMLHttpRequest) { |
| 597 | 216 |
/* add the annotation to the annotations and tell the world */ |
217 |
delete annotation.tags; |
|
218 |
annotation.content.description = annotation.content.data; |
|
219 |
delete annotation.content.data; |
|
220 |
annotation.id = json.annotations[0].id; |
|
221 |
annotation.title = _this._currentAnnotation.content.title; |
|
| 606 | 222 |
annotation.meta = meta; |
223 |
annotation.meta["id-ref"] = annotation["type"]; |
|
| 597 | 224 |
// everything is shared so there's no need to propagate the change |
225 |
_this._serializer._data.annotations.push(annotation); |
|
226 |
_this._Popcorn.trigger("IriSP.createAnnotationWidget.addedAnnotation"); |
|
|
594
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
227 |
}, |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
228 |
error: function(jqXHR, textStatus, errorThrown) { |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
229 |
alert("ERROR = " + jqXHR.responseText + ", " + errorThrown); |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
230 |
} |
|
96af41097260
WIP - adapting the widget to the new segments conventions.
hamidouk
parents:
575
diff
changeset
|
231 |
}); |
|
561
a10e7b6fdb08
display an error message when the user forget to enter text.
hamidouk
parents:
553
diff
changeset
|
232 |
} |
| 543 | 233 |
}; |