src/js/widgets/createAnnotationWidget.js
author hamidouk
Wed, 11 Jan 2012 16:13:29 +0100
branchpopcorn-port
changeset 611 d931a7d12519
parent 608 2fb4eda4dcd2
child 614 116de1c38a7d
permissions -rw-r--r--
append a small space before keywords
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) {
611
d931a7d12519 append a small space before keywords
hamidouk
parents: 608
diff changeset
    42
        var newVal = contents.replace(" " + keyword, "");        
549
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    43
      } else {
611
d931a7d12519 append a small space before keywords
hamidouk
parents: 608
diff changeset
    44
        if (contents === "")
d931a7d12519 append a small space before keywords
hamidouk
parents: 608
diff changeset
    45
          var newVal = keyword;
d931a7d12519 append a small space before keywords
hamidouk
parents: 608
diff changeset
    46
        else
d931a7d12519 append a small space before keywords
hamidouk
parents: 608
diff changeset
    47
          var newVal = contents + " " + keyword;      
549
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    48
      }
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    49
      
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    50
      _this.selector.find(".Ldt-createAnnotation-Description").val(newVal);
570
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
    51
      // 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
    52
      // change in a textfield.
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
    53
      _this.selector.find(".Ldt-createAnnotation-Description").trigger("js_mod");
549
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    54
      // also call our update function.
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    55
      _this.handleTextChanges();
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    56
    }
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    57
   }(keyword));
548
96a188138c20 refactored the code a bit.
hamidouk
parents: 547
diff changeset
    58
  }
96a188138c20 refactored the code a bit.
hamidouk
parents: 547
diff changeset
    59
  
549
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    60
  this.selector.find(".Ldt-createAnnotation-Description")
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    61
               .bind("propertychange keyup input paste", IriSP.wrap(this, this.handleTextChanges));
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    62
  
553
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    63
  this.selector.find(".Ldt-createAnnotation-submitButton").click(IriSP.wrap(this, this.handleButtonClick));
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    64
  this._Popcorn.listen("IriSP.PlayerWidget.AnnotateButton.clicked", 
547
5b58a108086a adding support for keywords.
hamidouk
parents: 543
diff changeset
    65
                        IriSP.wrap(this, this.handleAnnotateSignal));  
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    66
};
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    67
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    68
IriSP.createAnnotationWidget.prototype.handleAnnotateSignal = function() {
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    69
  if (this._hidden == false) {
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    70
    this.selector.hide();
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    71
    this._hidden = true;
553
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    72
    /* reinit the fields */
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    73
    
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    74
    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
    75
    this.selector.find("Ldt-createAnnotation-Description").val("");
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    76
    this.selector.find(".Ldt-createAnnotation-endScreen").hide();
570
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
    77
    
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
    78
    // free the arrow.
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
    79
    this._Popcorn.trigger("IriSP.ArrowWidget.releaseArrow");
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    80
  } else {
553
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    81
    if (this.cinecast_version) {
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    82
      var currentTime = this._Popcorn.currentTime();
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    83
      var currentAnnotation = this._serializer.currentAnnotations(currentTime)[0];
594
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
    84
553
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    85
      var beginTime = IriSP.msToTime(currentAnnotation.begin);
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    86
      var endTime = IriSP.msToTime(currentAnnotation.end);
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    87
      
594
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
    88
      /* 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
    89
         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
    90
      this._currentAnnotation = currentAnnotation;
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
    91
      
553
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    92
      if (typeof(currentAnnotation.content) !== "undefined")
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    93
        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
    94
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    95
      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
    96
      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
    97
    }
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    98
    
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    99
    this.selector.show();
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
   100
    this._hidden = false;
563
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 562
diff changeset
   101
    
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 562
diff changeset
   102
    // block the arrow.
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 562
diff changeset
   103
    this._Popcorn.trigger("IriSP.ArrowWidget.blockArrow");
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
   104
  }
549
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   105
};
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   106
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   107
/** watch for changes in the textfield and change the buttons accordingly */
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   108
IriSP.createAnnotationWidget.prototype.handleTextChanges = function(event) {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   109
  var contents = this.selector.find(".Ldt-createAnnotation-Description").val();
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   110
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   111
  for(var keyword in this.ids) {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   112
  
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   113
    var id = this.ids[keyword];
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   114
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   115
    if (contents.indexOf(keyword) != -1) {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   116
      /* the word is present in the textarea but the button is not toggled */
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   117
      if (this.selector.find("#" + id).hasClass("Ldt-createAnnotation-absent-keyword"))
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   118
          this.selector.find("#" + id).removeClass("Ldt-createAnnotation-absent-keyword")
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   119
                                      .addClass("Ldt-createAnnotation-present-keyword");      
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   120
    } else {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   121
      /* the word is absent from the textarea but the button is toggled */
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   122
      if (this.selector.find("#" + id).hasClass("Ldt-createAnnotation-present-keyword")) {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   123
          this.selector.find("#" + id).removeClass("Ldt-createAnnotation-present-keyword")
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   124
                                      .addClass("Ldt-createAnnotation-absent-keyword");
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   125
      }
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   126
    }
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   127
  }
553
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
   128
};
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
   129
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
   130
/** handle clicks on "send annotation" button */
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
   131
IriSP.createAnnotationWidget.prototype.handleButtonClick = function(event) {
597
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   132
  var _this = this;
561
a10e7b6fdb08 display an error message when the user forget to enter text.
hamidouk
parents: 553
diff changeset
   133
  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
   134
  var contents = textfield.val();
a10e7b6fdb08 display an error message when the user forget to enter text.
hamidouk
parents: 553
diff changeset
   135
a10e7b6fdb08 display an error message when the user forget to enter text.
hamidouk
parents: 553
diff changeset
   136
  if (contents === "") {
570
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   137
  
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   138
    if (this.selector.find(".Ldt-createAnnotation-errorMessage").length === 0) {
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   139
      this.selector.find(".Ldt-createAnnotation-Container")
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   140
                   .after(IriSP.templToHTML(IriSP.createAnnotation_errorMessage_template));
571
a471ef7efc0e fixes to the fixes.
hamidouk
parents: 570
diff changeset
   141
      textfield.css("background-color", "#d93c71");      
a471ef7efc0e fixes to the fixes.
hamidouk
parents: 570
diff changeset
   142
    } else {
a471ef7efc0e fixes to the fixes.
hamidouk
parents: 570
diff changeset
   143
      this.selector.find(".Ldt-createAnnotation-errorMessage").show();
a471ef7efc0e fixes to the fixes.
hamidouk
parents: 570
diff changeset
   144
    }
570
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   145
      // use namespaced events to be able to unbind them quickly and without unbinding
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   146
      // the other event handlers.
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   147
      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
   148
                      var contents = textfield.val();
611
d931a7d12519 append a small space before keywords
hamidouk
parents: 608
diff changeset
   149
                      
570
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   150
                      if (contents !== "") {
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   151
                        this.selector.find(".Ldt-createAnnotation-errorMessage").hide();
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   152
                        textfield.css("background-color", "");
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   153
                        textfield.unbind(".tmp");
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   154
                      }
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   155
                   }));
561
a10e7b6fdb08 display an error message when the user forget to enter text.
hamidouk
parents: 553
diff changeset
   156
  } else {
a10e7b6fdb08 display an error message when the user forget to enter text.
hamidouk
parents: 553
diff changeset
   157
    this.selector.find(".Ldt-createAnnotation-DoubleBorder").children().hide();
570
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   158
    
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   159
    if (this.cinecast_version) {
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   160
      this.selector.find(".Ldt-createAnnotation-Title").parent().show();      
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   161
    }
575
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   162
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   163
    var twStatus = IriSP.mkTweetUrl(document.location.href);
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   164
    var gpStatus = IriSP.mkGplusUrl(document.location.href);
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   165
    var fbStatus = IriSP.mkFbUrl(document.location.href);
570
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   166
    
575
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   167
    this.selector.find(".Ldt-createAnnotation-endScreen-TweetLink").attr("href", twStatus);
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   168
    this.selector.find(".Ldt-createAnnotation-endScreen-FbLink").attr("href", fbStatus);
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   169
    this.selector.find(".Ldt-createAnnotation-endScreen-GplusLink").attr("href", gpStatus);
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   170
            
594
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   171
    this.selector.find(".Ldt-createAnnotation-endScreen").show();
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   172
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   173
        
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   174
    if (typeof(this._currentAnnotation) === "undefined") {      
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   175
      console.log("this._currentAnnotation undefined");
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   176
      return;
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   177
    }
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   178
    
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   179
    var apiJson = {annotations : [{}], meta: {}};
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   180
    var annotation = apiJson["annotations"][0];
608
2fb4eda4dcd2 slight fix
hamidouk
parents: 606
diff changeset
   181
    
597
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   182
    annotation["media"] = this._serializer.currentMedia()["id"];
594
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   183
    annotation["begin"] = this._currentAnnotation.begin;
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   184
    annotation["end"] = this._currentAnnotation.end;
597
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   185
    annotation["type"] = this._serializer.getContributions();
608
2fb4eda4dcd2 slight fix
hamidouk
parents: 606
diff changeset
   186
    if (typeof(annotation["type"]) === "undefined")
2fb4eda4dcd2 slight fix
hamidouk
parents: 606
diff changeset
   187
      annotation["type"] = "";
2fb4eda4dcd2 slight fix
hamidouk
parents: 606
diff changeset
   188
    
2fb4eda4dcd2 slight fix
hamidouk
parents: 606
diff changeset
   189
    annotation["type_title"] = "Contributions";
594
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   190
    annotation.content = {};
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   191
    annotation.content["data"] = contents;
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
    var meta = apiJson["meta"];
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   194
    meta.creator = "An User";    
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   195
    meta.created = Date().toString();
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   196
    
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   197
    annotation["tags"] = [];
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
    for (var i = 0; i < this.keywords.length; i++) {
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   200
      var keyword = this.keywords[i];
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   201
      if (contents.indexOf(keyword) != -1)
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   202
        annotation["tags"].push(keyword);
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   203
    }
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   204
    
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   205
    var jsonString = JSON.stringify(apiJson);
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   206
    var project_id = this._serializer._data.meta.id;
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   207
    
597
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   208
    var url = Mustache.to_html("{{platf_url}}/ldtplatform/api/ldt/projects/{{id}}.json",
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   209
                                {platf_url: IriSP.platform_url, id: project_id});
608
2fb4eda4dcd2 slight fix
hamidouk
parents: 606
diff changeset
   210
    console.log(url, jsonString);
594
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   211
    IriSP.jQuery.ajax({
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   212
                url: url,
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   213
                type: 'PUT',
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   214
                contentType: 'application/json',
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   215
                data: jsonString,
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   216
                // 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
   217
                dataType: 'json',
594
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   218
                success: function(json, textStatus, XMLHttpRequest) {
597
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   219
                    /* add the annotation to the annotations and tell the world */
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   220
                    delete annotation.tags;
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   221
                    annotation.content.description = annotation.content.data;
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   222
                    delete annotation.content.data;
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   223
                    annotation.id = json.annotations[0].id;
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   224
                    annotation.title = _this._currentAnnotation.content.title;
606
13675511a01a fixed creation bug.
hamidouk
parents: 597
diff changeset
   225
                    annotation.meta = meta;
13675511a01a fixed creation bug.
hamidouk
parents: 597
diff changeset
   226
                    annotation.meta["id-ref"] = annotation["type"];
597
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   227
                    // everything is shared so there's no need to propagate the change
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   228
                    _this._serializer._data.annotations.push(annotation);
93feb85bb07f correctly add an annotation.
hamidouk
parents: 594
diff changeset
   229
                    _this._Popcorn.trigger("IriSP.createAnnotationWidget.addedAnnotation");
594
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
                error: function(jqXHR, textStatus, errorThrown) {
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   232
                    alert("ERROR = " + jqXHR.responseText + ", " + errorThrown);
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   233
                }
96af41097260 WIP - adapting the widget to the new segments conventions.
hamidouk
parents: 575
diff changeset
   234
            });
561
a10e7b6fdb08 display an error message when the user forget to enter text.
hamidouk
parents: 553
diff changeset
   235
  }
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
   236
};