src/widgets/CreateAnnotation.js
branchnew-model
changeset 908 f56199193fad
parent 904 510ebab76fa3
child 909 aa0e42229784
--- a/src/widgets/CreateAnnotation.js	Tue May 22 16:49:48 2012 +0200
+++ b/src/widgets/CreateAnnotation.js	Wed May 30 17:07:01 2012 +0200
@@ -1,3 +1,5 @@
+/* TODO: Add Social Network Sharing and from field */
+
 IriSP.Widgets.CreateAnnotation = function(player, config) {
     IriSP.Widgets.Widget.call(this, player, config);
     this.lastAnnotation = false;
@@ -7,7 +9,8 @@
 
 IriSP.Widgets.CreateAnnotation.prototype.defaults = {
     show_title_field : false,
-    user_avatar : "https://si0.twimg.com/sticky/default_profile_images/default_profile_1_normal.png",
+    creator_name : "",
+    creator_avatar : "https://si0.twimg.com/sticky/default_profile_images/default_profile_1_normal.png",
     tags : false,
     max_tags : 8,
     polemics : [{
@@ -28,35 +31,10 @@
         text_color: "#000000"
     }],
     annotation_type: "Contributions",
-    creator_name: "",
-    api_serializer: "ldt_annotate"
-/*
-
-        remote_tags : false,
-        random_tags : false,
-        show_from_field : false,
-        disable_share : false,
-        polemic_mode : true, // enable polemics
-        polemics : [{
-            className: "positive",
-            keyword: "++"
-        }, {
-            className: "negative",
-            keyword: "--"
-        }, {
-            className: "reference",
-            keyword: "=="
-        }, {
-            className: "question",
-            keyword: "??"
-        }],
-        cinecast_version : false, // put to false to enable the platform version, true for the festival cinecast one.
-
-        // where does the widget PUT the annotations - this is a mustache template. id refers to the id of the media and is filled by the widget.
-         
-        api_endpoint_template : "", // platform_url + "/ldtplatform/api/ldt/annotations/{{id}}.json",
-        api_method : "PUT"
- */
+    api_serializer: "ldt_annotate",
+    api_endpoint_template: "",
+    api_method: "PUT",
+    close_widget_timeout: 0
 }
 
 IriSP.Widgets.CreateAnnotation.prototype.messages = {
@@ -77,7 +55,8 @@
         share_annotation: "Would you like to share it on social networks ?",
         share_on: "Share on",
         more_tags: "More tags",
-        cancel: "Cancel"
+        cancel: "Cancel",
+        close_widget: "Cacher la zone de création d'annotations"
     },
     fr: {
         from_time: "de",
@@ -96,7 +75,8 @@
         share_annotation: "Souhaitez-vous la partager sur les réseaux sociaux ?",
         share_on: "Partager sur",
         more_tags: "Plus de mots-clés",
-        cancel: "Cancel"
+        cancel: "Cancel",
+        close_widget: "Hide the annotation creating block"
     }
 }
 
@@ -108,14 +88,17 @@
     + ' <span class="Ldt-CreateAnnotation-Times">{{l10n.from_time}} <span class="Ldt-CreateAnnotation-Begin"></span>'
     + ' {{l10n.to_time}} <span class="Ldt-CreateAnnotation-End"></span></span></h3>'
     + '<textarea class="Ldt-CreateAnnotation-Description" placeholder="{{l10n.type_description}}"></textarea>'
-    + '<div class="Ldt-CreateAnnotation-Avatar"><img src="{{user_avatar}}"></img></div>'
+    + '<div class="Ldt-CreateAnnotation-Avatar"><img src="{{creator_avatar}}" title="{{creator_name}}"></img></div>'
     + '<input type="submit" class="Ldt-CreateAnnotation-Submit" value="{{l10n.submit}}" />'
     + '{{#tags.length}}<div class="Ldt-CreateAnnotation-Tags"><div class="Ldt-CreateAnnotation-TagTitle">{{l10n.add_keywords_}}</div><ul class="Ldt-CreateAnnotation-TagList">'
     + '{{#tags}}<li class="Ldt-CreateAnnotation-TagLi" tag-id="{{id}}"><span class="Ldt-CreateAnnotation-TagButton">{{title}}</span></li>{{/tags}}</ul></div>{{/tags.length}}'
     + '{{#polemics.length}}<div class="Ldt-CreateAnnotation-Polemics"><div class="Ldt-CreateAnnotation-PolemicTitle">{{l10n.add_polemic_keywords_}}</div><ul class="Ldt-CreateAnnotation-PolemicList">'
     + '{{#polemics}}<li class="Ldt-CreateAnnotation-PolemicLi" style="background-color: {{background_color}}; color: {{text_color}}">{{keyword}}</li>{{/polemics}}</ul></div>{{/polemics.length}}'
-    + '</form>'
-    + '<div style="clear: both;"></div></div></div>';
+    + '<div style="clear: both;"></div></form>'
+    + '<div class="Ldt-CreateAnnotation-Screen Ldt-CreateAnnotation-Wait"><div class="Ldt-CreateAnnotation-InnerBox">{{l10n.wait_while_processing}}</div></div>'
+    + '<div class="Ldt-CreateAnnotation-Screen Ldt-CreateAnnotation-Error"><a title="{{l10n.close_widget}}" class="Ldt-CreateAnnotation-Close" href="#"></a><div class="Ldt-CreateAnnotation-InnerBox">{{l10n.error_while_contacting}}</div></div>'
+    + '<div class="Ldt-CreateAnnotation-Screen Ldt-CreateAnnotation-Saved"><a title="{{l10n.close_widget}}" class="Ldt-CreateAnnotation-Close" href="#"></a><div class="Ldt-CreateAnnotation-InnerBox">{{l10n.annotation_saved}}</div></div>'
+    + '</div></div>';
     
 IriSP.Widgets.CreateAnnotation.prototype.draw = function() {
     if (!this.tags) {
@@ -129,8 +112,12 @@
             });
         // We have to use the map function because Mustache doesn't like our tags object
     }
+    var _this = this;
     this.renderTemplate();
-    var _this = this;
+    this.$.find(".Ldt-CreateAnnotation-Close").click(function() {
+        _this.hide();
+        return false;
+    });
     this.$.find(".Ldt-CreateAnnotation-TagLi, .Ldt-CreateAnnotation-PolemicLi").click(function() {
         _this.addKeyword(IriSP.jQuery(this).text().replace(/(^\s+|\s+$)/g,''));
         return false;
@@ -149,8 +136,17 @@
     this.$.find("form").submit(this.functionWrapper("onSubmit"));
 }
 
+IriSP.Widgets.CreateAnnotation.prototype.showScreen = function(_screenName) {
+    this.$.find('.Ldt-CreateAnnotation-' + _screenName).show()
+        .siblings().hide();
+}
+
 IriSP.Widgets.CreateAnnotation.prototype.show = function() {
     this.visible = true;
+    this.showScreen('Main');
+    this.$.find(".Ldt-CreateAnnotation-Description").val("").css("border-color", "#666666");
+    this.$.find(".Ldt-CreateAnnotation-Title").val("").css("border-color", "#666666");
+    this.$.find(".Ldt-CreateAnnotation-TagLi, .Ldt-CreateAnnotation-PolemicLi").removeClass("selected");
     this.$.slideDown();
     this.player.popcorn.trigger("IriSP.Annotation.minimize");
     this.player.popcorn.trigger("IriSP.Slice.show");
@@ -172,8 +168,8 @@
 }
 
 IriSP.Widgets.CreateAnnotation.prototype.onBoundsChanged = function(_values) {
-    this.begin = new IriSP.Model.Time(_values[0]);
-    this.end = new IriSP.Model.Time(_values[1]);
+    this.begin = new IriSP.Model.Time(_values[0] || 0);
+    this.end = new IriSP.Model.Time(_values[1] || 0);
     this.$.find(".Ldt-CreateAnnotation-Begin").html(this.begin.toString());
     this.$.find(".Ldt-CreateAnnotation-End").html(this.end.toString());
 }
@@ -193,7 +189,7 @@
 IriSP.Widgets.CreateAnnotation.prototype.onDescriptionChange = function() {
     var _field = this.$.find(".Ldt-CreateAnnotation-Description"),
         _contents = _field.val();
-    _field.css("border-color", !!_contents ? "#666666" : "#c00000");
+    _field.css("border-color", !!_contents ? "#666666" : "#ff0000");
     this.$.find(".Ldt-CreateAnnotation-TagLi, .Ldt-CreateAnnotation-PolemicLi").each(function() {
         var _rx = IriSP.Model.regexpFromTextOrArray(IriSP.jQuery(this).text().replace(/(^\s+|\s+$)/g,''));
         if (_rx.test(_contents)) {
@@ -208,7 +204,7 @@
 IriSP.Widgets.CreateAnnotation.prototype.onTitleChange = function() {
     var _field = this.$.find(".Ldt-CreateAnnotation-Title"),
         _contents = _field.val();
-    _field.css("border-color", !!_contents ? "#666666" : "#c00000");
+    _field.css("border-color", !!_contents ? "#666666" : "#ff0000");
     return !!_contents;
 }
 
@@ -220,7 +216,8 @@
     var _exportedAnnotations = new IriSP.Model.List(this.player.sourceManager);
         _export = this.player.sourceManager.newLocalSource({serializer: IriSP.serializers[this.api_serializer]}),
         _annotation = new IriSP.Model.Annotation(false, _export),
-        _annotationType = new IriSP.Model.AnnotationType(false, _export);
+        _annotationType = new IriSP.Model.AnnotationType(false, _export),
+        _url = Mustache.to_html(this.api_endpoint_template, {id: this.source.projectId});
 
     _annotationType.title = this.annotation_type;
     _annotation.setBegin(this.begin);
@@ -234,84 +231,38 @@
     _annotation.description = this.$.find(".Ldt-CreateAnnotation-Description").val();
     _annotation.setTags(this.$.find(".Ldt-CreateAnnotation-TagLi.selected").map(function() { return IriSP.jQuery(this).attr("tag-id")}));
     
-    _export.creator = this.creator;
+    _export.creator = this.creator_name;
     _export.created = new Date();
     _exportedAnnotations.push(_annotation);
     _export.addList("annotation",_exportedAnnotations);
-    console.log(_export.serialize());
+    
+    var _this = this;
+    IriSP.jQuery.ajax({
+        url: _url,
+        type: this.api_method,
+        contentType: 'application/json',
+        data: _export.serialize(),
+        success: function(_data) {
+            _this.showScreen('Saved');
+            if (_this.close_widget_timeout) {
+                window.setTimeout(_this.functionWrapper("hide"),_this.close_widget_timeout);
+            }
+            _export.getAnnotations().removeElement(_annotation, true);
+            _export.deSerialize(_data);
+            _this.source.merge(_export);
+            _this.player.popcorn.trigger("IriSP.AnnotationsList.refresh");
+        },
+        error: function(_xhr, _error, _thrown) {
+            IriSP.log("Error when sending annotation", _thrown);
+            _this.showScreen('Error');
+            window.setTimeout(function(){
+                _this.showScreen("Main")
+            },
+            (_this.close_widget_timeout || 5000));
+        }
+    });
+    this.showScreen('Wait');
     
     return false;
 }
-    
-/*    
-    + '        <div class="Ldt-CreateAnnotation-Screen Ldt-createAnnotation-startScreen">'
-    + '            <div style="margin-bottom: 7px; overflow: auto;">'
-    + '                <div class="Ldt-createAnnotation-Title"></div>'
-    + '                <div class="Ldt-createAnnotation-TimeFrame"></div>'
-    + '                {{^cinecast_version}} <div class="Ldt-createAnnotation-Minimize Ldt-TraceMe" title="Cancel"></div>'
-    + '                {{/cinecast_version}}'
-    + '            </div>'
-    + '            <div class="Ldt-createAnnotation-Container">'
-    + '                {{#show_from_field}}'
-    + '                <label>{{l10n.your_name}}&nbsp;: </label><input class="Ldt-createAnnotation-userName Ldt-TraceMe" value="{{user_name}}" />'
-    + '                {{/show_from_field}}'
-    + '                <textarea class="Ldt-createAnnotation-Description Ldt-TraceMe"></textarea>'
-    + '                <div class="Ldt-createAnnotation-userAvatar Ldt-TraceMe">'
-    + '                    {{^user_avatar}} <img src="https://si0.twimg.com/sticky/default_profile_images/default_profile_1_normal.png"></img>'
-    + '                    {{/user_avatar}}'
-    + '                    {{#user_avatar}} <img src="{{ user_avatar }}"></img>'
-    + '                    {{/user_avatar}}'
-    + '                </div>'
-    + '                <div class="Ldt-createAnnotation-profileArrow"></div>'
-    + '            </div>'
-    + '            <button class="Ldt-createAnnotation-submitButton Ldt-TraceMe">{{l10n.submit}}</button>'
-    + '            {{#tags.length}}'
-    + '            <div class="Ldt-createAnnotation-btnblock Ldt-createAnnotation-keywords">'
-    + '                <label>{{l10n.add_keywords}} :</label>'
-    + '                <ul class="Ldt-floatList">'
-    + '                {{#tags}}'
-    + '                    <li><button class="Ldt-createAnnotation-keyword-button Ldt-TraceMe" tag-id="{{id}}">{{meta.description}}</button></li>'
-    + '                {{/tags}}'
-    + '                </ul>'
-    + '            </div>'
-    + '            {{#random_tags}}'
-    + '                <button class="Ldt-createAnnotation-moar-keywordz">{{l10n.more_tags}}</button>'
-    + '            {{/random_tags}}'
-    + '            {{/tags.length}}'
-    + '            {{#polemic_mode}}'
-    + '            {{#polemics.length}}'
-    + '            <div class="Ldt-createAnnotation-btnblock Ldt-createAnnotation-polemics">'
-    + '                <label>{{l10n.add_polemic_keywords}} :</label>'
-    + '                <ul class="Ldt-floatList">'
-    + '                {{#polemics}}'
-    + '                    <li><button class="Ldt-createAnnotation-polemic-{{className}} Ldt-createAnnotation-polemic-button Ldt-TraceMe">{{keyword}}</button></li>'
-    + '                {{/polemics}}'
-    + '                </ul>'
-    + '            </div>'
-    + '            {{/polemics.length}}'
-    + '            {{/polemic_mode}}'
-    + '        </div>'
-    + '        <div class="Ldt-createAnnotation-screen Ldt-createAnnotation-waitScreen" style="display: none; text-align: center">'
-    + '            <div class="Ldt-createAnnotation-spinner"></div>'
-    + '            {{l10n.wait_while_processed}}'
-    + '        </div>'
-    + '        <div class="Ldt-createAnnotation-screen Ldt-createAnnotation-errorScreen" style="display: none; text-align: center">'
-    + '            <div class="Ldt-createAnnotation-Minimize" title="Hide"></div>'
-    + '            {{l10n.error_while_contacting}}'
-    + '        </div>'
-    + '        <div class="Ldt-createAnnotation-screen Ldt-createAnnotation-endScreen" style="display: none">'
-    + '            <div class="Ldt-createAnnotation-Minimize" title="Hide"></div>'
-    + '            {{l10n.annotation_saved}}'
-    + '            <br>'
-    + '            {{^disable_share}}'
-    + '            {{l10n.share_annotation}}'
-    + '            <div style="margin-top: 12px; text-align: center;">'
-    + '                <a target="_blank" class="Ldt-createAnnotation-endScreen-TweetLink Ldt-TraceMe"></a>'
-    + '                <a target="_blank" class="Ldt-createAnnotation-endScreen-FbLink Ldt-TraceMe"></a>'
-    + '                <a target="_blank" class="Ldt-createAnnotation-endScreen-GplusLink Ldt-TraceMe"></a>'
-    + '            </div>'
-    + '            {{/disable_share}}'
-    + '        </div>'
-    + '        <div class="Ldt-floatClear"></div>'
-*/