src/js/widgets/annotationsListWidget.js
author hamidouk
Tue, 24 Jan 2012 17:30:15 +0100
branchembed-playerapi-rewrite
changeset 703 9341d858bf8e
parent 698 329333e07267
child 714 9056928c46de
permissions -rw-r--r--
removed useless printfs.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
     1
IriSP.AnnotationsListWidget = function(Popcorn, config, Serializer) {
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
     2
  IriSP.Widget.call(this, Popcorn, config, Serializer);
605
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
     3
  this.__counter = 0;
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
     4
  this.__oldList = [];
674
835f5f454595 WIP - working on ajax autoupdate of the widget.
hamidouk
parents: 642
diff changeset
     5
  
835f5f454595 WIP - working on ajax autoupdate of the widget.
hamidouk
parents: 642
diff changeset
     6
  this.ajax_mode = IriSP.widgetsDefaults["AnnotationsListWidget"].ajax_mode;
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
     7
};
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
     8
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
     9
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    10
IriSP.AnnotationsListWidget.prototype = new IriSP.Widget();
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    11
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    12
IriSP.AnnotationsListWidget.prototype.clear = function() {
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    13
};
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    14
588
78cf49152d4a got a decent-looking list.
hamidouk
parents: 585
diff changeset
    15
IriSP.AnnotationsListWidget.prototype.clearWidget = function() {
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    16
};
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    17
629
b13bcfd2f9b1 DRYied the code.
hamidouk
parents: 621
diff changeset
    18
/** effectively redraw the widget - called by drawList */
b13bcfd2f9b1 DRYied the code.
hamidouk
parents: 621
diff changeset
    19
IriSP.AnnotationsListWidget.prototype.do_redraw = function(list) {
b13bcfd2f9b1 DRYied the code.
hamidouk
parents: 621
diff changeset
    20
    var widgetMarkup = IriSP.templToHTML(IriSP.annotationsListWidget_template, {annotations: list});
b13bcfd2f9b1 DRYied the code.
hamidouk
parents: 621
diff changeset
    21
    this.selector.html(widgetMarkup);
b13bcfd2f9b1 DRYied the code.
hamidouk
parents: 621
diff changeset
    22
};
b13bcfd2f9b1 DRYied the code.
hamidouk
parents: 621
diff changeset
    23
599
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
    24
/** draw the annotation list */
607
0b94ae49efbd added an option to force the redraw of a list, even if the cached lists are the
hamidouk
parents: 605
diff changeset
    25
IriSP.AnnotationsListWidget.prototype.drawList = function(force_redraw) {
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    26
  var _this = this;
674
835f5f454595 WIP - working on ajax autoupdate of the widget.
hamidouk
parents: 642
diff changeset
    27
  
595
29d86e6c61a6 finished going through the widgets to add stricter line checking.
hamidouk
parents: 588
diff changeset
    28
  var view_type = this._serializer.getContributions();
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    29
  var annotations = this._serializer._data.annotations;
602
b35862f9b0b0 WIP - working on the autoupdate of the widget.
hamidouk
parents: 599
diff changeset
    30
  var currentTime = this._Popcorn.currentTime();
605
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    31
    
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    32
  var list = [];
588
78cf49152d4a got a decent-looking list.
hamidouk
parents: 585
diff changeset
    33
621
170a8cd859d5 remove error message.
hamidouk
parents: 615
diff changeset
    34
  if (typeof(view_type) === "undefined") {    
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    35
    return;
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    36
  }
588
78cf49152d4a got a decent-looking list.
hamidouk
parents: 585
diff changeset
    37
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    38
  for (i = 0; i < annotations.length; i++) {
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    39
    var annotation = annotations[i];
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    40
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    41
    /* filter the annotations whose type is not the one we want */
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    42
    if (typeof(annotation.meta) !== "undefined" && typeof(annotation.meta["id-ref"]) !== "undefined"
615
7eb41c26a8b7 stricter equality check.
hamidouk
parents: 607
diff changeset
    43
          && annotation.meta["id-ref"] !== view_type) {
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    44
        continue;
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    45
    }
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    46
602
b35862f9b0b0 WIP - working on the autoupdate of the widget.
hamidouk
parents: 599
diff changeset
    47
    /* only get the annotations happening in the current chapter */
603
58dfdafb5410 fixed stupid ms/s bug.
hamidouk
parents: 602
diff changeset
    48
    if (!(annotation.begin <= currentTime * 1000 && annotation.end > currentTime * 1000)) {        
602
b35862f9b0b0 WIP - working on the autoupdate of the widget.
hamidouk
parents: 599
diff changeset
    49
        continue;
b35862f9b0b0 WIP - working on the autoupdate of the widget.
hamidouk
parents: 599
diff changeset
    50
    }
603
58dfdafb5410 fixed stupid ms/s bug.
hamidouk
parents: 602
diff changeset
    51
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    52
    var a = annotation;
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    53
    var obj = {};
588
78cf49152d4a got a decent-looking list.
hamidouk
parents: 585
diff changeset
    54
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    55
    obj["id"] = a.id;
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    56
    obj["title"] = a.content.title;
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    57
    obj["desc"] = a.content.description;
603
58dfdafb5410 fixed stupid ms/s bug.
hamidouk
parents: 602
diff changeset
    58
    obj["begin"] = IriSP.msToTime(annotation.begin);
58dfdafb5410 fixed stupid ms/s bug.
hamidouk
parents: 602
diff changeset
    59
    obj["end"] = IriSP.msToTime(annotation.end);
588
78cf49152d4a got a decent-looking list.
hamidouk
parents: 585
diff changeset
    60
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    61
    list.push(obj);
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    62
  }
607
0b94ae49efbd added an option to force the redraw of a list, even if the cached lists are the
hamidouk
parents: 605
diff changeset
    63
  
605
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    64
  var idList = IriSP.underscore.pluck(list, "id").sort();
642
37693f217f8c redraw the list when a new annotation is added.
hamidouk
parents: 629
diff changeset
    65
605
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    66
  if (idList.length !== this.__oldList.length) {
629
b13bcfd2f9b1 DRYied the code.
hamidouk
parents: 621
diff changeset
    67
    this.do_redraw(list);
605
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    68
  }
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    69
    
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    70
  var res = 1;
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    71
  for (var i = 0; i < idList.length; i++) {
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    72
    if (idList[i] !== this.__oldList[i])
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    73
      res = 0;
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    74
      break;
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    75
  }
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    76
  
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    77
  this.__oldList = idList; /* save for next call */
698
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
    78
  
607
0b94ae49efbd added an option to force the redraw of a list, even if the cached lists are the
hamidouk
parents: 605
diff changeset
    79
  if (typeof(force_redraw) !== "undefined") {
629
b13bcfd2f9b1 DRYied the code.
hamidouk
parents: 621
diff changeset
    80
    this.do_redraw(list);
607
0b94ae49efbd added an option to force the redraw of a list, even if the cached lists are the
hamidouk
parents: 605
diff changeset
    81
  }
605
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    82
  
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    83
  /* the two lists are equal, no need to redraw */
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    84
  if (res === 1) {
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    85
    return;
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    86
  } else {
629
b13bcfd2f9b1 DRYied the code.
hamidouk
parents: 621
diff changeset
    87
    this.do_redraw(list);
605
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    88
  }
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    89
  
599
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
    90
};
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
    91
674
835f5f454595 WIP - working on ajax autoupdate of the widget.
hamidouk
parents: 642
diff changeset
    92
IriSP.AnnotationsListWidget.prototype.ajaxRedraw = function(timecode) {
835f5f454595 WIP - working on ajax autoupdate of the widget.
hamidouk
parents: 642
diff changeset
    93
  var pre_url = IriSP.widgetsDefaults["AnnotationsListWidget"].ajax_url;
698
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
    94
  var media_id = this._serializer.currentMedia()["id"];
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
    95
  var duration = +this._serializer.currentMedia().meta["dc:duration"];
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
    96
  
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
    97
  var begin_timecode = (Math.floor(this._Popcorn.currentTime()) - 300) * 1000;
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
    98
  if (begin_timecode < 0)
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
    99
    begin_timecode = 0;
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   100
    
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   101
  var end_timecode = (Math.floor(this._Popcorn.currentTime()) + 300) * 1000;
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   102
  if (end_timecode > duration)
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   103
    end_timecode = duration;
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   104
  
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   105
  var templ = Mustache.to_html("{{pre_url}}/{{media_id}}/{{begin_timecode}}/{{end_timecode}}",
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   106
                                {pre_url: pre_url, media_id: media_id, begin_timecode: begin_timecode,
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   107
                                 end_timecode: end_timecode});
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   108
  
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   109
  /* we create on the fly a serializer to get the ajax */
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   110
  var serializer = new IriSP.JSONSerializer(IriSP.__dataloader, templ);
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   111
  serializer.sync(IriSP.wrap(this, function(json) { this.processJson(json, serializer) }));                  
674
835f5f454595 WIP - working on ajax autoupdate of the widget.
hamidouk
parents: 642
diff changeset
   112
};
835f5f454595 WIP - working on ajax autoupdate of the widget.
hamidouk
parents: 642
diff changeset
   113
698
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   114
/** process the received json - it's a bit hackish */
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   115
IriSP.AnnotationsListWidget.prototype.processJson = function(json, serializer) {
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   116
  /* FIXME: DRY the whole thing */
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   117
  var annotations = serializer._data.annotations;
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   118
  if (IriSP.null_or_undefined(annotations))
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   119
    return;
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   120
  
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   121
  var view_types = serializer.getIds("Contributions");
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   122
  var l = [];
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   123
  
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   124
  for (i = 0; i < annotations.length; i++) {
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   125
      var annotation = annotations[i];
703
9341d858bf8e removed useless printfs.
hamidouk
parents: 698
diff changeset
   126
698
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   127
      /* filter the annotations whose type is not the one we want */
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   128
      /* We want _all_ the annotations.
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   129
      if (typeof(annotation.meta) !== "undefined" && typeof(annotation.meta["id-ref"]) !== "undefined"
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   130
            && !IriSP.underscore.include(view_types, annotation.meta["id-ref"])) {
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   131
          continue;
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   132
      }
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   133
      */
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   134
      var a = annotation;
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   135
      var obj = {};
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   136
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   137
      obj["id"] = a.id;
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   138
      obj["title"] = a.content.title;
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   139
      obj["desc"] = a.content.description;
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   140
      obj["begin"] = IriSP.msToTime(annotation.begin);
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   141
      obj["end"] = IriSP.msToTime(annotation.end);
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   142
      obj["url"] = document.location.href.split("#")[0] + "/" + annotation.meta["project"];
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   143
      l.push(obj);
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   144
  }
703
9341d858bf8e removed useless printfs.
hamidouk
parents: 698
diff changeset
   145
698
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   146
  this.do_redraw(l);
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   147
};
599
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
   148
IriSP.AnnotationsListWidget.prototype.draw = function() {
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
   149
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
   150
  this.drawList();
674
835f5f454595 WIP - working on ajax autoupdate of the widget.
hamidouk
parents: 642
diff changeset
   151
    
698
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   152
  if (!this.ajax_mode) {    
674
835f5f454595 WIP - working on ajax autoupdate of the widget.
hamidouk
parents: 642
diff changeset
   153
    this._Popcorn.listen("IriSP.createAnnotationWidget.addedAnnotation", IriSP.wrap(this, function() { this.drawList(true); }));
698
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   154
    this._Popcorn.listen("timeupdate", IriSP.wrap(this, this.redraw));
674
835f5f454595 WIP - working on ajax autoupdate of the widget.
hamidouk
parents: 642
diff changeset
   155
  } else {
698
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   156
    this._Popcorn.listen("seeked", IriSP.wrap(this, this.ajaxRedraw));
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   157
    this._Popcorn.listen("paused", IriSP.wrap(this, this.ajaxRedraw));
674
835f5f454595 WIP - working on ajax autoupdate of the widget.
hamidouk
parents: 642
diff changeset
   158
  }
698
329333e07267 lot of work on the ajax update front.
hamidouk
parents: 674
diff changeset
   159
599
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
   160
};
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
   161
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
   162
IriSP.AnnotationsListWidget.prototype.redraw = function() {
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
   163
  this.drawList();
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
   164
};