src/js/widgets/arrowWidget.js
author hamidouk
Tue, 31 Jan 2012 11:58:26 +0100
branchpopcorn-port
changeset 748 ec1f7aa873f4
parent 664 c548eb0be4ef
child 754 b119a956b647
permissions -rw-r--r--
parse the url received by the platform.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
299
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
     1
IriSP.ArrowWidget = function(Popcorn, config, Serializer) {
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
     2
  IriSP.Widget.call(this, Popcorn, config, Serializer);
321
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
     3
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
     4
  this._oldAnnotation = null;
563
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 560
diff changeset
     5
  this._blockArrow = false;
299
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
     6
};
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
     7
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
     8
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
     9
IriSP.ArrowWidget.prototype = new IriSP.Widget();
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
    10
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
    11
IriSP.ArrowWidget.prototype.clear = function() {
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
    12
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
    13
};
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
    14
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
    15
IriSP.ArrowWidget.prototype.clearWidget = function() {
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
    16
};
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
    17
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
    18
IriSP.ArrowWidget.prototype.draw = function() {
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
    19
  var templ = Mustache.to_html(IriSP.arrowWidget_template, {});
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
    20
  this.selector.append(templ);
321
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    21
  this._Popcorn.listen("timeupdate", IriSP.wrap(this, this.timeUpdateHandler));
563
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 560
diff changeset
    22
  this._Popcorn.listen("IriSP.ArrowWidget.blockArrow", IriSP.wrap(this, this.blockArrow));
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 560
diff changeset
    23
  this._Popcorn.listen("IriSP.ArrowWidget.releaseArrow", IriSP.wrap(this, this.releaseArrow));
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 560
diff changeset
    24
  
299
d2005f747d5a added a new widget, the arrowWidget.
hamidouk
parents:
diff changeset
    25
};
305
e8d05c3f77ed created an arrow widget.
hamidouk
parents: 299
diff changeset
    26
321
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    27
IriSP.ArrowWidget.prototype.timeUpdateHandler = function(percents) {
563
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 560
diff changeset
    28
  if (this._blockArrow)
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 560
diff changeset
    29
    return;
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 560
diff changeset
    30
  
321
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    31
  var currentTime = this._Popcorn.currentTime();
664
c548eb0be4ef fixed annotation type bug.
hamidouk
parents: 563
diff changeset
    32
  var currentAnnotation = this._serializer.currentChapitre(currentTime);
c548eb0be4ef fixed annotation type bug.
hamidouk
parents: 563
diff changeset
    33
  if (IriSP.null_or_undefined(currentAnnotation))
c548eb0be4ef fixed annotation type bug.
hamidouk
parents: 563
diff changeset
    34
    var currentAnnotation = this._serializer.currentAnnotation(currentTime)[0]; // FIXME : use the others ?
321
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    35
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    36
  /* move the arrow only if the current annotation changes */
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    37
  if (currentAnnotation != this._oldAnnotation) {
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    38
    var begin = (+ currentAnnotation.begin) / 1000;
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    39
    var end = (+ currentAnnotation.end) / 1000;
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    40
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    41
    var duration = +this._serializer.currentMedia().meta["dc:duration"] / 1000;
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    42
    var middle_time = (begin + end) / 2;
560
68e91efc5a58 the arrow is now aligned with the pixels under it.
hamidouk
parents: 535
diff changeset
    43
    var percents = middle_time / duration;
321
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    44
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    45
    // we need to apply a fix because the arrow has a certain length
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    46
    // it's half the length of the arrow (27 / 2). We need to convert
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    47
    // it in percents though.
664
c548eb0be4ef fixed annotation type bug.
hamidouk
parents: 563
diff changeset
    48
    var totalWidth = this.selector.width();    
560
68e91efc5a58 the arrow is now aligned with the pixels under it.
hamidouk
parents: 535
diff changeset
    49
    var pixels = percents * totalWidth;
68e91efc5a58 the arrow is now aligned with the pixels under it.
hamidouk
parents: 535
diff changeset
    50
    var correction = (27 / 2);
68e91efc5a58 the arrow is now aligned with the pixels under it.
hamidouk
parents: 535
diff changeset
    51
    var corrected_pixels = pixels - correction;
68e91efc5a58 the arrow is now aligned with the pixels under it.
hamidouk
parents: 535
diff changeset
    52
    
68e91efc5a58 the arrow is now aligned with the pixels under it.
hamidouk
parents: 535
diff changeset
    53
    /* make sure that the arrow is aligned with the pattern
68e91efc5a58 the arrow is now aligned with the pixels under it.
hamidouk
parents: 535
diff changeset
    54
       of the widget under it */
68e91efc5a58 the arrow is now aligned with the pixels under it.
hamidouk
parents: 535
diff changeset
    55
    if (corrected_pixels % 3 != 0)
68e91efc5a58 the arrow is now aligned with the pixels under it.
hamidouk
parents: 535
diff changeset
    56
      corrected_pixels -= (corrected_pixels % 3 - 1);
68e91efc5a58 the arrow is now aligned with the pixels under it.
hamidouk
parents: 535
diff changeset
    57
    
337
ee1d1e4fcf45 prevent the arrow from leaving the screen.
hamidouk
parents: 321
diff changeset
    58
    /* don't move out of the screen */
560
68e91efc5a58 the arrow is now aligned with the pixels under it.
hamidouk
parents: 535
diff changeset
    59
    if (corrected_pixels <= 0)
68e91efc5a58 the arrow is now aligned with the pixels under it.
hamidouk
parents: 535
diff changeset
    60
      corrected_pixels = 0;
68e91efc5a58 the arrow is now aligned with the pixels under it.
hamidouk
parents: 535
diff changeset
    61
    
68e91efc5a58 the arrow is now aligned with the pixels under it.
hamidouk
parents: 535
diff changeset
    62
    if (corrected_pixels <= 15) {
535
7c25af985344 made the arrowWidget.js changed its arrow at the edges of the screen.
hamidouk
parents: 337
diff changeset
    63
      var left_edge_img_templ = IriSP.templToHTML("url('{{img_dir}}/left_edge_arrow.png')"); 
7c25af985344 made the arrowWidget.js changed its arrow at the edges of the screen.
hamidouk
parents: 337
diff changeset
    64
      this.selector.children(".Ldt-arrowWidget").css("background-image", left_edge_img_templ); 
560
68e91efc5a58 the arrow is now aligned with the pixels under it.
hamidouk
parents: 535
diff changeset
    65
    } else if (corrected_pixels >= totalWidth - 25) {
535
7c25af985344 made the arrowWidget.js changed its arrow at the edges of the screen.
hamidouk
parents: 337
diff changeset
    66
      var right_edge_img_templ = IriSP.templToHTML("url('{{img_dir}}/right_edge_arrow.png')"); 
7c25af985344 made the arrowWidget.js changed its arrow at the edges of the screen.
hamidouk
parents: 337
diff changeset
    67
      this.selector.children(".Ldt-arrowWidget").css("background-image", right_edge_img_templ);
7c25af985344 made the arrowWidget.js changed its arrow at the edges of the screen.
hamidouk
parents: 337
diff changeset
    68
    } else {
7c25af985344 made the arrowWidget.js changed its arrow at the edges of the screen.
hamidouk
parents: 337
diff changeset
    69
      var img_templ = IriSP.templToHTML("url('{{img_dir}}/arrow.png')"); 
7c25af985344 made the arrowWidget.js changed its arrow at the edges of the screen.
hamidouk
parents: 337
diff changeset
    70
      this.selector.children(".Ldt-arrowWidget").css("background-image", img_templ);
7c25af985344 made the arrowWidget.js changed its arrow at the edges of the screen.
hamidouk
parents: 337
diff changeset
    71
    }
7c25af985344 made the arrowWidget.js changed its arrow at the edges of the screen.
hamidouk
parents: 337
diff changeset
    72
    
560
68e91efc5a58 the arrow is now aligned with the pixels under it.
hamidouk
parents: 535
diff changeset
    73
    this.selector.children(".Ldt-arrowWidget").animate({"left" : corrected_pixels + "px"});
321
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    74
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    75
    this._oldAnnotation = currentAnnotation;
21d840371c6b Arrow Positioning that Just Works (tm)
hamidouk
parents: 313
diff changeset
    76
  }
563
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 560
diff changeset
    77
};
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 560
diff changeset
    78
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 560
diff changeset
    79
/** Block the arrow for instance when the user is annotating */
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 560
diff changeset
    80
IriSP.ArrowWidget.prototype.blockArrow = function() {
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 560
diff changeset
    81
  this._blockArrow = true;
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 560
diff changeset
    82
};
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 560
diff changeset
    83
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 560
diff changeset
    84
IriSP.ArrowWidget.prototype.releaseArrow = function() {
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 560
diff changeset
    85
  this._blockArrow = false;   
4816e3425933 block the arrow when the user is annotating.
hamidouk
parents: 560
diff changeset
    86
};