scripts/position.js
author Dan
Mon, 01 Sep 2008 13:05:52 -0400
changeset 39 38dbcda3cf20
parent 11 0faea3a6c881
child 42 774751e7faed
permissions -rw-r--r--
Fixed scroll-to-current-track being about 30 pixels off under iPhone
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
     1
var pos_supported = false;
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
     2
var pos_in_drag = false;
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
     3
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
     4
var posslide_init = function()
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
     5
{
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
     6
  // make sure the theme supports the playhead
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
     7
  var base = document.getElementById('playhead');
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
     8
  var inner = document.getElementById('playhead-filler');
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
     9
  var slider = document.getElementById('playhead-button');
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    10
  if ( !slider || !inner || !base )
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    11
    return false;
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    12
  
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    13
  pos_supported = true;
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    14
  
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    15
  var minX = $(base).Left() - 3;
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    16
  var minY = $(base).Top() + 3;
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    17
  var maxY = minY;
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    18
  var maxX = minX + $(base).Width() - 6;
11
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 10
diff changeset
    19
  Drag.init(slider, slider, minX, maxX, minY, maxY);
10
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    20
  
11
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 10
diff changeset
    21
  slider.onDrag = posslide_handle_drag;
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 10
diff changeset
    22
  slider.onDragEnd = posslide_handle_dragend;
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 10
diff changeset
    23
  slider.onDragStart = function(x, y) { pos_in_drag = true; };
10
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    24
  base.onclick = posslide_handle_click;
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    25
  
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    26
  posslide_set_position(0);
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    27
  slider.style.top = minY + 'px';
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    28
}
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    29
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    30
var posslide_handle_drag = function(x, y, do_inner)
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    31
{
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    32
  var inner = document.getElementById('playhead-filler');
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    33
  var slider = document.getElementById('playhead-button');
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    34
  var size = x - $(inner).Left() + 8;
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    35
  if ( do_inner )
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    36
    inner.style.width = size + 'px';
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    37
  if ( ( pos_in_drag && !do_inner ) || ( !pos_in_drag && do_inner ) )
11
0faea3a6c881 Fixed some position slider issues; added a pulsing effect to the current track (fun!)
Dan
parents: 10
diff changeset
    38
    slider.style.left = ( x ) + 'px';
10
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    39
}
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    40
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    41
var posslide_handle_dragend = function(x, y)
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    42
{
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    43
  pos_in_drag = false;
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    44
  var inner = document.getElementById('playhead-filler');
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    45
  var base = document.getElementById('playhead');
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    46
  var multiplier = $(base).Width();
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    47
  var pos = x - $(inner).Left() + 8;
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    48
  pos = 100 * ( pos / multiplier );
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    49
  set_playback_position(pos);
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    50
}
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    51
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    52
var posslide_handle_click = function(e)
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    53
{
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    54
  e = Drag.fixE(e);
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    55
  var base = document.getElementById('playhead');
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    56
  var val = e.clientX - $(base).Left();
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    57
  val = 100 * ( val / $(base).Width() );
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    58
  posslide_set_position(val);
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    59
  set_playback_position(val);
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    60
}
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    61
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    62
function posslide_set_position(pos)
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    63
{
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    64
  if ( !pos_supported )
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    65
    return false;
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    66
  
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    67
  // pos needs to be 1-100
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    68
  var base = document.getElementById('playhead');
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    69
  var multiplier = $(base).Width();
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    70
  pos = ( pos / 100 ) * multiplier;
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    71
  var left = pos + $(base).Left();
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    72
  posslide_handle_drag(left, 0, true);
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    73
}
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    74
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    75
addOnloadHook(posslide_init);
d3059e20b0fa SECURITY: Fix ability to crash server in ajax.php; added playback position slider and ability to seek through current song
Dan
parents:
diff changeset
    76