1 IriSP.Widgets.Mediafragment = function(player, config) { |
1 IriSP.Widgets.Mediafragment = function(player, config) { |
2 IriSP.Widgets.Widget.call(this, player, config); |
2 IriSP.Widgets.Widget.call(this, player, config); |
3 this.last_hash = ""; |
3 this.last_hash_key = ""; |
|
4 this.last_hash_value = ""; |
4 window.onhashchange = this.functionWrapper("goToHash"); |
5 window.onhashchange = this.functionWrapper("goToHash"); |
5 if (typeof window.addEventListener !== "undefined") { |
6 if (typeof window.addEventListener !== "undefined") { |
6 window.addEventListener('message', function(_msg) { |
7 window.addEventListener('message', function(_msg) { |
7 if (_msg.data.type === "hashchange") { |
8 if (_msg.data.type === "hashchange") { |
8 document.location.hash = _msg.data.hash; |
9 document.location.hash = _msg.data.hash; |
19 |
20 |
20 IriSP.Widgets.Mediafragment.prototype.draw = function() { |
21 IriSP.Widgets.Mediafragment.prototype.draw = function() { |
21 this.goToHash(); |
22 this.goToHash(); |
22 } |
23 } |
23 |
24 |
|
25 IriSP.Widgets.Mediafragment.prototype.getLastHash = function() { |
|
26 var _tab = document.location.hash.replace(/^#/,'').split('&'); |
|
27 _tab = IriSP._(_tab).filter(function(_el) { |
|
28 return _el && !/^(id|t)=/.test(_el); |
|
29 }); |
|
30 if (this.last_hash_key) { |
|
31 _tab.push(this.last_hash_key + '=' + this.last_hash_value); |
|
32 } |
|
33 return '#' + _tab.join('&'); |
|
34 } |
|
35 |
24 IriSP.Widgets.Mediafragment.prototype.goToHash = function() { |
36 IriSP.Widgets.Mediafragment.prototype.goToHash = function() { |
25 if (document.location.hash !== this.last_hash) { |
37 if (document.location.hash !== this.getLastHash()) { |
26 this.last_hash = document.location.hash; |
38 var _tab = document.location.hash.replace(/^#/,'').split('&'); |
27 var _tab = this.last_hash.split("="); |
39 for (var _i = 0; _i < _tab.length; _i++) { |
28 if (_tab[0] === '#id') { |
40 var _subtab = _tab[_i].split("="); |
29 var _annotation = this.source.getElement(_tab[1]); |
41 if (_subtab[0] == "id" || _subtab[0] == "t") { |
30 if (typeof _annotation !== "undefined") { |
42 this.last_hash_key = _subtab[0]; |
31 this.player.popcorn.currentTime(_annotation.begin.getSeconds()); |
43 this.last_hash_value = _subtab[1]; |
|
44 if (this.last_hash_key == "id") { |
|
45 var _annotation = this.source.getElement(this.last_hash_value); |
|
46 if (typeof _annotation !== "undefined") { |
|
47 this.player.popcorn.currentTime(_annotation.begin.getSeconds()); |
|
48 } |
|
49 } |
|
50 if (this.last_hash_key == "t") { |
|
51 this.player.popcorn.currentTime(this.last_hash_value); |
|
52 } |
|
53 break; |
32 } |
54 } |
33 } |
|
34 if (_tab[0] === '#t') { |
|
35 this.player.popcorn.currentTime(_tab[1]); |
|
36 } |
55 } |
37 } |
56 } |
38 } |
57 } |
39 |
58 |
40 IriSP.Widgets.Mediafragment.prototype.setHashToAnnotation = function(_annotationId) { |
59 IriSP.Widgets.Mediafragment.prototype.setHashToAnnotation = function(_annotationId) { |
41 this.setHash( '#id=' + this.source.unNamespace(_annotationId) ); |
60 this.setHash( 'id', this.source.unNamespace(_annotationId) ); |
42 } |
61 } |
43 |
62 |
44 IriSP.Widgets.Mediafragment.prototype.setHashToTime = function(_time) { |
63 IriSP.Widgets.Mediafragment.prototype.setHashToTime = function(_time) { |
45 if (_time !== NaN) { |
64 if (_time !== NaN) { |
46 this.setHash( '#t=' + this.player.popcorn.currentTime() ); |
65 this.setHash( 't', this.player.popcorn.currentTime() ); |
47 } |
66 } |
48 } |
67 } |
49 |
68 |
50 IriSP.Widgets.Mediafragment.prototype.setHash = function(_hash) { |
69 IriSP.Widgets.Mediafragment.prototype.setHash = function(_key, _value) { |
51 if (!this.blocked && this.last_hash !== _hash) { |
70 if (!this.blocked && (this.last_hash_key !== _key || this.last_hash_value !== _value)) { |
52 this.last_hash = _hash; |
71 this.last_hash_key = _key; |
|
72 this.last_hash_value = _value; |
|
73 var _hash = this.getLastHash(); |
53 document.location.hash = _hash; |
74 document.location.hash = _hash; |
54 if (window.parent !== window) { |
75 if (window.parent !== window) { |
55 window.parent.postMessage({ |
76 window.parent.postMessage({ |
56 type: "hashchange", |
77 type: "hashchange", |
57 hash: _hash |
78 hash: _hash |