src/js/widgets/annotationsListWidget.js
author hamidouk
Tue, 10 Jan 2012 16:09:30 +0100
branchpopcorn-port
changeset 605 e1a6f73038b4
parent 603 58dfdafb5410
child 607 0b94ae49efbd
permissions -rw-r--r--
made the listWidget redraw at every timeupdate event (added a small optimization though, to make sure that the widget isn't redrawn all the time).
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 = [];
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
     5
};
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
     6
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
     7
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
     8
IriSP.AnnotationsListWidget.prototype = new IriSP.Widget();
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.clear = function() {
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    11
};
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    12
588
78cf49152d4a got a decent-looking list.
hamidouk
parents: 585
diff changeset
    13
IriSP.AnnotationsListWidget.prototype.clearWidget = function() {
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    14
};
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    15
599
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
    16
/** draw the annotation list */
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
    17
IriSP.AnnotationsListWidget.prototype.drawList = function() {
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    18
  var _this = this;
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    19
595
29d86e6c61a6 finished going through the widgets to add stricter line checking.
hamidouk
parents: 588
diff changeset
    20
  var view_type = this._serializer.getContributions();
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    21
  var annotations = this._serializer._data.annotations;
602
b35862f9b0b0 WIP - working on the autoupdate of the widget.
hamidouk
parents: 599
diff changeset
    22
  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
    23
    
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    24
  var list = [];
588
78cf49152d4a got a decent-looking list.
hamidouk
parents: 585
diff changeset
    25
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    26
  if (typeof(view_type) === "undefined") {
602
b35862f9b0b0 WIP - working on the autoupdate of the widget.
hamidouk
parents: 599
diff changeset
    27
    console.log("no type suitable for display");
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    28
    return;
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    29
  }
588
78cf49152d4a got a decent-looking list.
hamidouk
parents: 585
diff changeset
    30
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    31
  for (i = 0; i < annotations.length; i++) {
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    32
    var annotation = annotations[i];
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    33
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    34
    /* filter the annotations whose type is not the one we want */
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    35
    if (typeof(annotation.meta) !== "undefined" && typeof(annotation.meta["id-ref"]) !== "undefined"
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    36
          && annotation.meta["id-ref"] != view_type) {
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    37
        continue;
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    38
    }
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    39
602
b35862f9b0b0 WIP - working on the autoupdate of the widget.
hamidouk
parents: 599
diff changeset
    40
    /* only get the annotations happening in the current chapter */
603
58dfdafb5410 fixed stupid ms/s bug.
hamidouk
parents: 602
diff changeset
    41
    if (!(annotation.begin <= currentTime * 1000 && annotation.end > currentTime * 1000)) {        
602
b35862f9b0b0 WIP - working on the autoupdate of the widget.
hamidouk
parents: 599
diff changeset
    42
        continue;
b35862f9b0b0 WIP - working on the autoupdate of the widget.
hamidouk
parents: 599
diff changeset
    43
    }
603
58dfdafb5410 fixed stupid ms/s bug.
hamidouk
parents: 602
diff changeset
    44
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    45
    var a = annotation;
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    46
    var obj = {};
588
78cf49152d4a got a decent-looking list.
hamidouk
parents: 585
diff changeset
    47
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    48
    obj["id"] = a.id;
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    49
    obj["title"] = a.content.title;
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    50
    obj["desc"] = a.content.description;
603
58dfdafb5410 fixed stupid ms/s bug.
hamidouk
parents: 602
diff changeset
    51
    obj["begin"] = IriSP.msToTime(annotation.begin);
58dfdafb5410 fixed stupid ms/s bug.
hamidouk
parents: 602
diff changeset
    52
    obj["end"] = IriSP.msToTime(annotation.end);
588
78cf49152d4a got a decent-looking list.
hamidouk
parents: 585
diff changeset
    53
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    54
    list.push(obj);
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    55
  }
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
    56
605
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    57
  var idList = IriSP.underscore.pluck(list, "id").sort();
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    58
  
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    59
  if (idList.length !== this.__oldList.length) {
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    60
    var widgetMarkup = IriSP.templToHTML(IriSP.annotationsListWidget_template, {annotations: list});
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    61
    this.selector.html(widgetMarkup);
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    62
  }
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    63
    
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    64
  var res = 1;
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    65
  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
    66
    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
    67
      res = 0;
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    68
      break;
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
  
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    71
  this.__oldList = idList; /* save for next call */
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    72
  
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    73
  /* 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
    74
  if (res === 1) {
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    75
    return;
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    76
  } else {
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    77
    var widgetMarkup = IriSP.templToHTML(IriSP.annotationsListWidget_template, {annotations: list});
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    78
    this.selector.html(widgetMarkup);
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    79
  }
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    80
  
599
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
    81
};
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
    82
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
    83
IriSP.AnnotationsListWidget.prototype.draw = function() {
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
    84
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
    85
  this.drawList();
605
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    86
  this._Popcorn.listen("IriSP.createAnnotationWidget.addedAnnotation", IriSP.wrap(this, this.drawList));
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    87
  this._Popcorn.listen("timeupdate", IriSP.wrap(this, this.redraw));
599
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
    88
};
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
    89
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
    90
IriSP.AnnotationsListWidget.prototype.redraw = function() {
605
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    91
  /* we use some kind of counter to mitigate the fact that the function
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    92
     is supposed to be called at every timeupdate */
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    93
/*  if (this.__counter < 4) {
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    94
    this.__counter++;
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    95
  } else {
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    96
    this.drawList();
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    97
    this.__counter = 0;
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    98
  }*/
e1a6f73038b4 made the listWidget redraw at every timeupdate event (added a small optimization though,
hamidouk
parents: 603
diff changeset
    99
  
599
a5a5e70d46a7 added support for widget redrawing.
hamidouk
parents: 595
diff changeset
   100
  this.drawList();
585
44a4a4a179c1 first version of the AnnotationListWidget.
hamidouk
parents:
diff changeset
   101
};