src/js/widgets/createAnnotationWidget.js
author hamidouk
Mon, 09 Jan 2012 15:38:14 +0100
branchpopcorn-port
changeset 597 93feb85bb07f
parent 594 96af41097260
child 606 13675511a01a
permissions -rw-r--r--
correctly add an annotation.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
     1
IriSP.createAnnotationWidget = function(Popcorn, config, Serializer) {
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
     2
  IriSP.Widget.call(this, Popcorn, config, Serializer);
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
     3
  this._hidden = true;
547
5b58a108086a adding support for keywords.
hamidouk
parents: 543
diff changeset
     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
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
     6
  this.ids = {}; /* a dictionnary linking buttons ids to keywords */
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
     7
};
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
     8
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
     9
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    10
IriSP.createAnnotationWidget.prototype = new IriSP.Widget();
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    11
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    12
IriSP.createAnnotationWidget.prototype.clear = function() {
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    13
    this.selector.find(".Ldt-SaTitle").text("");
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    14
    this.selector.find(".Ldt-SaDescription").text("");
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    15
    this.selector.find(".Ldt-SaKeywordText").text("");
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    16
};
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    17
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    18
IriSP.createAnnotationWidget.prototype.draw = function() {
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    19
  var _this = this;
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    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
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    27
	this.selector.append(annotationMarkup);
548
96a188138c20 refactored the code a bit.
hamidouk
parents: 547
diff changeset
    28
  
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    29
  this.selector.hide();
548
96a188138c20 refactored the code a bit.
hamidouk
parents: 547
diff changeset
    30
  for (var i = 0; i < this.keywords.length; i++) {
549
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    31
    var keyword = this.keywords[i];
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    32
    var id = IriSP.guid("button_");
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    33
    var templ = IriSP.templToHTML("<button id={{id}} class='Ldt-createAnnotation-absent-keyword'>{{keyword}}</button>", 
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    34
                                  {keyword: keyword, id: id});
548
96a188138c20 refactored the code a bit.
hamidouk
parents: 547
diff changeset
    35
                                  
549
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    36
    this.ids[keyword] = id; // save it for the function that handle textarea changes.
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    37
    
548
96a188138c20 refactored the code a bit.
hamidouk
parents: 547
diff changeset
    38
    this.selector.find(".Ldt-createAnnotation-keywords").append(templ);
549
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    39
    this.selector.find("#" + id).click(function(keyword) { return function() {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    40
      var contents = _this.selector.find(".Ldt-createAnnotation-Description").val();
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    41
      if (contents.indexOf(keyword) != -1) {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    42
        var newVal = contents.replace(keyword, "");        
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    43
      } else {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    44
        var newVal = contents + keyword;      
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    45
      }
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    46
      
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    47
      _this.selector.find(".Ldt-createAnnotation-Description").val(newVal);
570
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
    48
      // we use a custom event because there's no simple way to test for a js
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
    49
      // change in a textfield.
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
    50
      _this.selector.find(".Ldt-createAnnotation-Description").trigger("js_mod");
549
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    51
      // also call our update function.
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    52
      _this.handleTextChanges();
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    53
    }
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    54
   }(keyword));
548
96a188138c20 refactored the code a bit.
hamidouk
parents: 547
diff changeset
    55
  }
96a188138c20 refactored the code a bit.
hamidouk
parents: 547
diff changeset
    56
  
549
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    57
  this.selector.find(".Ldt-createAnnotation-Description")
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    58
               .bind("propertychange keyup input paste", IriSP.wrap(this, this.handleTextChanges));
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    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
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    61
  this._Popcorn.listen("IriSP.PlayerWidget.AnnotateButton.clicked", 
547
5b58a108086a adding support for keywords.
hamidouk
parents: 543
diff changeset
    62
                        IriSP.wrap(this, this.handleAnnotateSignal));  
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    63
};
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    64
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    65
IriSP.createAnnotationWidget.prototype.handleAnnotateSignal = function() {
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    66
  if (this._hidden == false) {
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    67
    this.selector.hide();
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    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
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
    74
    
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
    75
    // free the arrow.
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
    76
    this._Popcorn.trigger("IriSP.ArrowWidget.releaseArrow");
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    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
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    96
    this.selector.show();
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    97
    this._hidden = false;
563
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 562
diff changeset
    98
    
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 562
diff changeset
    99
    // block the arrow.
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 562
diff changeset
   100
    this._Popcorn.trigger("IriSP.ArrowWidget.blockArrow");
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
   101
  }
549
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   102
};
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   103
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   104
/** watch for changes in the textfield and change the buttons accordingly */
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   105
IriSP.createAnnotationWidget.prototype.handleTextChanges = function(event) {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   106
  var contents = this.selector.find(".Ldt-createAnnotation-Description").val();
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   107
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   108
  for(var keyword in this.ids) {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   109
  
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   110
    var id = this.ids[keyword];
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   111
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   112
    if (contents.indexOf(keyword) != -1) {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   113
      /* the word is present in the textarea but the button is not toggled */
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   114
      if (this.selector.find("#" + id).hasClass("Ldt-createAnnotation-absent-keyword"))
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   115
          this.selector.find("#" + id).removeClass("Ldt-createAnnotation-absent-keyword")
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   116
                                      .addClass("Ldt-createAnnotation-present-keyword");      
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   117
    } else {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   118
      /* the word is absent from the textarea but the button is toggled */
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   119
      if (this.selector.find("#" + id).hasClass("Ldt-createAnnotation-present-keyword")) {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   120
          this.selector.find("#" + id).removeClass("Ldt-createAnnotation-present-keyword")
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   121
                                      .addClass("Ldt-createAnnotation-absent-keyword");
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   122
      }
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   123
    }
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   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
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   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
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   134
  
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   135
    if (this.selector.find(".Ldt-createAnnotation-errorMessage").length === 0) {
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   136
      this.selector.find(".Ldt-createAnnotation-Container")
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   137
                   .after(IriSP.templToHTML(IriSP.createAnnotation_errorMessage_template));
571
a471ef7efc0e fixes to the fixes.
hamidouk
parents: 570
diff changeset
   138
      textfield.css("background-color", "#d93c71");      
a471ef7efc0e fixes to the fixes.
hamidouk
parents: 570
diff changeset
   139
    } else {
a471ef7efc0e fixes to the fixes.
hamidouk
parents: 570
diff changeset
   140
      this.selector.find(".Ldt-createAnnotation-errorMessage").show();
a471ef7efc0e fixes to the fixes.
hamidouk
parents: 570
diff changeset
   141
    }
570
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   142
      // use namespaced events to be able to unbind them quickly and without unbinding
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   143
      // the other event handlers.
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   144
      textfield.bind("js_mod.tmp propertychange.tmp keyup.tmp input.tmp paste.tmp", IriSP.wrap(this, function() {
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   145
                      var contents = textfield.val();
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   146
                      console.log(contents);
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   147
                      if (contents !== "") {
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   148
                        this.selector.find(".Ldt-createAnnotation-errorMessage").hide();
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   149
                        textfield.css("background-color", "");
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   150
                        textfield.unbind(".tmp");
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   151
                      }
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   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
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   155
    
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   156
    if (this.cinecast_version) {
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   157
      this.selector.find(".Ldt-createAnnotation-Title").parent().show();      
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   158
    }
575
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   159
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   160
    var twStatus = IriSP.mkTweetUrl(document.location.href);
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   161
    var gpStatus = IriSP.mkGplusUrl(document.location.href);
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   162
    var fbStatus = IriSP.mkFbUrl(document.location.href);
570
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   163
    
575
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   164
    this.selector.find(".Ldt-createAnnotation-endScreen-TweetLink").attr("href", twStatus);
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   165
    this.selector.find(".Ldt-createAnnotation-endScreen-FbLink").attr("href", fbStatus);
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   166
    this.selector.find(".Ldt-createAnnotation-endScreen-GplusLink").attr("href", gpStatus);
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   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];
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   178
    //annotation["type_title"] = "Contributions";
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   179
    annotation["type_title"] = "";
597
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   180
    annotation["media"] = this._serializer.currentMedia()["id"];
594
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   181
    annotation["begin"] = this._currentAnnotation.begin;
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   182
    annotation["end"] = this._currentAnnotation.end;
597
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   183
    annotation["type"] = this._serializer.getContributions();
594
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   184
    annotation.content = {};
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   185
    annotation.content["data"] = contents;
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   186
    
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   187
    var meta = apiJson["meta"];
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   188
    meta.creator = "An User";    
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   189
    meta.created = Date().toString();
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   190
    
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   191
    annotation["tags"] = [];
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   192
    
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   193
    for (var i = 0; i < this.keywords.length; i++) {
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   194
      var keyword = this.keywords[i];
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   195
      if (contents.indexOf(keyword) != -1)
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   196
        annotation["tags"].push(keyword);
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   197
    }
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   198
    
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   199
    var jsonString = JSON.stringify(apiJson);
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   200
    var project_id = this._serializer._data.meta.id;
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   201
    
597
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   202
    var url = Mustache.to_html("{{platf_url}}/ldtplatform/api/ldt/projects/{{id}}.json",
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   203
                                {platf_url: IriSP.platform_url, id: project_id});
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   204
    console.log(url);
594
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   205
    IriSP.jQuery.ajax({
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   206
                url: url,
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   207
                type: 'PUT',
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   208
                contentType: 'application/json',
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   209
                data: jsonString,
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   210
                // bug with jquery >= 1.5, "json" adds a callback so we don't specify dataType
597
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   211
                dataType: 'json',
594
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   212
                success: function(json, textStatus, XMLHttpRequest) {
597
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   213
                    /* add the annotation to the annotations and tell the world */
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   214
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   215
                    delete annotation.tags;
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   216
                    annotation.content.description = annotation.content.data;
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   217
                    delete annotation.content.data;
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   218
                    annotation.id = json.annotations[0].id;
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   219
                    annotation.title = _this._currentAnnotation.content.title;
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   220
                    // everything is shared so there's no need to propagate the change
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   221
                    _this._serializer._data.annotations.push(annotation);
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   222
                    _this._Popcorn.trigger("IriSP.createAnnotationWidget.addedAnnotation");
594
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   223
                },
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   224
                error: function(jqXHR, textStatus, errorThrown) {
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   225
                    alert("ERROR = " + jqXHR.responseText + ", " + errorThrown);
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   226
                }
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   227
            });
561
a10e7b6fdb08 display an error message when the user forget to enter text.
hamidouk
parents: 553
diff changeset
   228
  }
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
   229
};