|
880
|
1 |
IriSP.Widgets.Mediafragment = function(player, config) { |
|
|
2 |
IriSP.Widgets.Widget.call(this, player, config); |
|
905
|
3 |
this.last_hash_key = ""; |
|
|
4 |
this.last_hash_value = ""; |
|
880
|
5 |
window.onhashchange = this.functionWrapper("goToHash"); |
|
882
|
6 |
if (typeof window.addEventListener !== "undefined") { |
|
|
7 |
window.addEventListener('message', function(_msg) { |
|
906
|
8 |
if (/^#/.test(_msg.data)) { |
|
910
|
9 |
this.setWindowHash(_msg.data); |
|
882
|
10 |
} |
|
|
11 |
}) |
|
|
12 |
}; |
|
881
|
13 |
this.bindPopcorn("pause","setHashToTime"); |
|
|
14 |
this.bindPopcorn("seeked","setHashToTime"); |
|
|
15 |
this.bindPopcorn("IriSP.Mediafragment.setHashToAnnotation","setHashToAnnotation"); |
|
|
16 |
this.blocked = false; |
|
880
|
17 |
} |
|
|
18 |
|
|
|
19 |
IriSP.Widgets.Mediafragment.prototype = new IriSP.Widgets.Widget(); |
|
|
20 |
|
|
|
21 |
IriSP.Widgets.Mediafragment.prototype.draw = function() { |
|
|
22 |
this.goToHash(); |
|
|
23 |
} |
|
|
24 |
|
|
910
|
25 |
IriSP.Widgets.Mediafragment.prototype.setWindowHash = function(_hash) { |
|
|
26 |
if (typeof window.history !== "undefined" && typeof window.history.replaceState !== "undefined") { |
|
|
27 |
window.history.replaceState({}, "", _hash); |
|
|
28 |
} else { |
|
|
29 |
document.location.hash = _hash; |
|
|
30 |
} |
|
|
31 |
} |
|
|
32 |
|
|
905
|
33 |
IriSP.Widgets.Mediafragment.prototype.getLastHash = function() { |
|
|
34 |
var _tab = document.location.hash.replace(/^#/,'').split('&'); |
|
|
35 |
_tab = IriSP._(_tab).filter(function(_el) { |
|
|
36 |
return _el && !/^(id|t)=/.test(_el); |
|
|
37 |
}); |
|
|
38 |
if (this.last_hash_key) { |
|
|
39 |
_tab.push(this.last_hash_key + '=' + this.last_hash_value); |
|
|
40 |
} |
|
|
41 |
return '#' + _tab.join('&'); |
|
|
42 |
} |
|
|
43 |
|
|
880
|
44 |
IriSP.Widgets.Mediafragment.prototype.goToHash = function() { |
|
905
|
45 |
if (document.location.hash !== this.getLastHash()) { |
|
|
46 |
var _tab = document.location.hash.replace(/^#/,'').split('&'); |
|
|
47 |
for (var _i = 0; _i < _tab.length; _i++) { |
|
|
48 |
var _subtab = _tab[_i].split("="); |
|
|
49 |
if (_subtab[0] == "id" || _subtab[0] == "t") { |
|
|
50 |
this.last_hash_key = _subtab[0]; |
|
|
51 |
this.last_hash_value = _subtab[1]; |
|
|
52 |
if (this.last_hash_key == "id") { |
|
|
53 |
var _annotation = this.source.getElement(this.last_hash_value); |
|
|
54 |
if (typeof _annotation !== "undefined") { |
|
|
55 |
this.player.popcorn.currentTime(_annotation.begin.getSeconds()); |
|
|
56 |
} |
|
|
57 |
} |
|
|
58 |
if (this.last_hash_key == "t") { |
|
|
59 |
this.player.popcorn.currentTime(this.last_hash_value); |
|
|
60 |
} |
|
|
61 |
break; |
|
880
|
62 |
} |
|
|
63 |
} |
|
|
64 |
} |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
IriSP.Widgets.Mediafragment.prototype.setHashToAnnotation = function(_annotationId) { |
|
905
|
68 |
this.setHash( 'id', this.source.unNamespace(_annotationId) ); |
|
880
|
69 |
} |
|
|
70 |
|
|
881
|
71 |
IriSP.Widgets.Mediafragment.prototype.setHashToTime = function(_time) { |
|
883
|
72 |
if (_time !== NaN) { |
|
905
|
73 |
this.setHash( 't', this.player.popcorn.currentTime() ); |
|
883
|
74 |
} |
|
880
|
75 |
} |
|
|
76 |
|
|
905
|
77 |
IriSP.Widgets.Mediafragment.prototype.setHash = function(_key, _value) { |
|
|
78 |
if (!this.blocked && (this.last_hash_key !== _key || this.last_hash_value !== _value)) { |
|
|
79 |
this.last_hash_key = _key; |
|
|
80 |
this.last_hash_value = _value; |
|
|
81 |
var _hash = this.getLastHash(); |
|
910
|
82 |
this.setWindowHash(_hash); |
|
882
|
83 |
if (window.parent !== window) { |
|
906
|
84 |
window.parent.postMessage(_hash,"*") |
|
882
|
85 |
} |
|
881
|
86 |
this.block(); |
|
880
|
87 |
} |
|
881
|
88 |
} |
|
|
89 |
|
|
|
90 |
IriSP.Widgets.Mediafragment.prototype.unblock = function() { |
|
|
91 |
if (typeof this.blockTimeout !== "undefined") { |
|
|
92 |
window.clearTimeout(this.blockTimeout); |
|
|
93 |
} |
|
|
94 |
this.blockTimeout = undefined; |
|
|
95 |
this.blocked = false; |
|
|
96 |
} |
|
|
97 |
|
|
|
98 |
IriSP.Widgets.Mediafragment.prototype.block = function() { |
|
|
99 |
if (typeof this.blockTimeout !== "undefined") { |
|
|
100 |
window.clearTimeout(this.blockTimeout); |
|
|
101 |
} |
|
|
102 |
this.blocked = true; |
|
|
103 |
this.blockTimeout = window.setTimeout(this.functionWrapper("unblock"), 1000); |
|
|
104 |
} |