src/js/widgets/createAnnotationWidget.js
author hamidouk
Tue, 03 Jan 2012 17:14:01 +0100
branchpopcorn-port
changeset 575 8f483e430f51
parent 571 a471ef7efc0e
child 594 96af41097260
permissions -rw-r--r--
added link sharing for created annotations.
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];
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    81
      var beginTime = IriSP.msToTime(currentAnnotation.begin);
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    82
      var endTime = IriSP.msToTime(currentAnnotation.end);
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    83
      
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    84
      if (typeof(currentAnnotation.content) !== "undefined")
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    85
        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
    86
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    87
      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
    88
      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
    89
    }
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
    90
    
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    91
    this.selector.show();
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    92
    this._hidden = false;
563
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 562
diff changeset
    93
    
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 562
diff changeset
    94
    // block the arrow.
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 562
diff changeset
    95
    this._Popcorn.trigger("IriSP.ArrowWidget.blockArrow");
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
    96
  }
549
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    97
};
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    98
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
    99
/** watch for changes in the textfield and change the buttons accordingly */
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   100
IriSP.createAnnotationWidget.prototype.handleTextChanges = function(event) {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   101
  var contents = this.selector.find(".Ldt-createAnnotation-Description").val();
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   102
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   103
  for(var keyword in this.ids) {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   104
  
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   105
    var id = this.ids[keyword];
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   106
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   107
    if (contents.indexOf(keyword) != -1) {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   108
      /* the word is present in the textarea but the button is not toggled */
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   109
      if (this.selector.find("#" + id).hasClass("Ldt-createAnnotation-absent-keyword"))
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   110
          this.selector.find("#" + id).removeClass("Ldt-createAnnotation-absent-keyword")
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   111
                                      .addClass("Ldt-createAnnotation-present-keyword");      
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   112
    } else {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   113
      /* the word is absent from the textarea but the button is toggled */
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   114
      if (this.selector.find("#" + id).hasClass("Ldt-createAnnotation-present-keyword")) {
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   115
          this.selector.find("#" + id).removeClass("Ldt-createAnnotation-present-keyword")
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   116
                                      .addClass("Ldt-createAnnotation-absent-keyword");
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   117
      }
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   118
    }
8abffd2de54a implemented correct button behaviour.
hamidouk
parents: 548
diff changeset
   119
  }
553
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
   120
};
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
   121
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
   122
/** handle clicks on "send annotation" button */
10d08f43c534 widget works now more or less like in samuel's crea.
hamidouk
parents: 549
diff changeset
   123
IriSP.createAnnotationWidget.prototype.handleButtonClick = function(event) {
561
a10e7b6fdb08 display an error message when the user forget to enter text.
hamidouk
parents: 553
diff changeset
   124
  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
   125
  var contents = textfield.val();
a10e7b6fdb08 display an error message when the user forget to enter text.
hamidouk
parents: 553
diff changeset
   126
a10e7b6fdb08 display an error message when the user forget to enter text.
hamidouk
parents: 553
diff changeset
   127
  if (contents === "") {
570
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   128
  
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   129
    if (this.selector.find(".Ldt-createAnnotation-errorMessage").length === 0) {
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   130
      this.selector.find(".Ldt-createAnnotation-Container")
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   131
                   .after(IriSP.templToHTML(IriSP.createAnnotation_errorMessage_template));
571
a471ef7efc0e fixes to the fixes.
hamidouk
parents: 570
diff changeset
   132
      textfield.css("background-color", "#d93c71");      
a471ef7efc0e fixes to the fixes.
hamidouk
parents: 570
diff changeset
   133
    } else {
a471ef7efc0e fixes to the fixes.
hamidouk
parents: 570
diff changeset
   134
      this.selector.find(".Ldt-createAnnotation-errorMessage").show();
a471ef7efc0e fixes to the fixes.
hamidouk
parents: 570
diff changeset
   135
    }
570
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   136
      // use namespaced events to be able to unbind them quickly and without unbinding
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   137
      // the other event handlers.
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   138
      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
   139
                      var contents = textfield.val();
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   140
                      console.log(contents);
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   141
                      if (contents !== "") {
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   142
                        this.selector.find(".Ldt-createAnnotation-errorMessage").hide();
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   143
                        textfield.css("background-color", "");
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   144
                        textfield.unbind(".tmp");
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   145
                      }
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   146
                   }));
561
a10e7b6fdb08 display an error message when the user forget to enter text.
hamidouk
parents: 553
diff changeset
   147
  } else {
a10e7b6fdb08 display an error message when the user forget to enter text.
hamidouk
parents: 553
diff changeset
   148
    this.selector.find(".Ldt-createAnnotation-DoubleBorder").children().hide();
570
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   149
    
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   150
    if (this.cinecast_version) {
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   151
      this.selector.find(".Ldt-createAnnotation-Title").parent().show();      
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   152
    }
575
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   153
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   154
    var twStatus = IriSP.mkTweetUrl(document.location.href);
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   155
    var gpStatus = IriSP.mkGplusUrl(document.location.href);
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   156
    var fbStatus = IriSP.mkFbUrl(document.location.href);
570
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   157
    
575
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   158
    this.selector.find(".Ldt-createAnnotation-endScreen-TweetLink").attr("href", twStatus);
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   159
    this.selector.find(".Ldt-createAnnotation-endScreen-FbLink").attr("href", fbStatus);
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   160
    this.selector.find(".Ldt-createAnnotation-endScreen-GplusLink").attr("href", gpStatus);
8f483e430f51 added link sharing for created annotations.
hamidouk
parents: 571
diff changeset
   161
            
570
244f04a9af85 a couple changes.
hamidouk
parents: 563
diff changeset
   162
    this.selector.find(".Ldt-createAnnotation-endScreen").show();    
561
a10e7b6fdb08 display an error message when the user forget to enter text.
hamidouk
parents: 553
diff changeset
   163
  }
543
5856fab9596c WIP - adding widget to create annotations.
hamidouk
parents:
diff changeset
   164
};