| author | ymh <ymh.work@gmail.com> |
| Sun, 12 Nov 2017 22:07:33 +0100 | |
| changeset 1071 | 02c04d2c8fd8 |
| parent 1068 | 7623f9af9272 |
| child 1072 | ac1eacb3aa33 |
| permissions | -rw-r--r-- |
| 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 = ""; |
|
|
1068
7623f9af9272
merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
5 |
this.last_extra_key = ""; |
|
7623f9af9272
merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
6 |
this.last_extra_value = ""; |
|
7623f9af9272
merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
7 |
|
| 880 | 8 |
window.onhashchange = this.functionWrapper("goToHash"); |
| 882 | 9 |
if (typeof window.addEventListener !== "undefined") { |
| 986 | 10 |
var _this = this; |
| 882 | 11 |
window.addEventListener('message', function(_msg) { |
| 906 | 12 |
if (/^#/.test(_msg.data)) { |
| 986 | 13 |
_this.setWindowHash(_msg.data); |
| 882 | 14 |
} |
| 1013 | 15 |
}); |
| 882 | 16 |
}; |
| 957 | 17 |
this.onMdpEvent("Mediafragment.setHashToAnnotation","setHashToAnnotation"); |
| 881 | 18 |
this.blocked = false; |
| 1013 | 19 |
}; |
| 880 | 20 |
|
21 |
IriSP.Widgets.Mediafragment.prototype = new IriSP.Widgets.Widget(); |
|
22 |
||
23 |
IriSP.Widgets.Mediafragment.prototype.draw = function() { |
|
| 987 | 24 |
this.onMediaEvent("setpause","setHashToTime"); |
| 964 | 25 |
var _this = this; |
26 |
this.getWidgetAnnotations().forEach(function(_annotation) { |
|
27 |
_annotation.on("click", function() { |
|
|
1068
7623f9af9272
merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
28 |
_this.setHashToAnnotation(_annotation); |
| 1013 | 29 |
}); |
| 986 | 30 |
}); |
| 987 | 31 |
if (this.media.loadedMetadata) { |
32 |
this.goToHash(); |
|
33 |
} else { |
|
34 |
this.onMediaEvent("loadedmetadata","goToHash"); |
|
35 |
} |
|
| 1013 | 36 |
}; |
| 880 | 37 |
|
| 910 | 38 |
IriSP.Widgets.Mediafragment.prototype.setWindowHash = function(_hash) { |
39 |
if (typeof window.history !== "undefined" && typeof window.history.replaceState !== "undefined") { |
|
40 |
window.history.replaceState({}, "", _hash); |
|
41 |
} else { |
|
42 |
document.location.hash = _hash; |
|
43 |
} |
|
| 1013 | 44 |
}; |
| 910 | 45 |
|
| 905 | 46 |
IriSP.Widgets.Mediafragment.prototype.getLastHash = function() { |
47 |
var _tab = document.location.hash.replace(/^#/,'').split('&'); |
|
48 |
_tab = IriSP._(_tab).filter(function(_el) { |
|
49 |
return _el && !/^(id|t)=/.test(_el); |
|
50 |
}); |
|
51 |
if (this.last_hash_key) { |
|
52 |
_tab.push(this.last_hash_key + '=' + this.last_hash_value); |
|
53 |
} |
|
|
1068
7623f9af9272
merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
54 |
if (this.last_extra_key) { |
|
7623f9af9272
merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
55 |
_tab.push(this.last_extra_key + '=' + this.last_extra_value); |
|
7623f9af9272
merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
56 |
} |
| 905 | 57 |
return '#' + _tab.join('&'); |
| 1013 | 58 |
}; |
| 905 | 59 |
|
| 880 | 60 |
IriSP.Widgets.Mediafragment.prototype.goToHash = function() { |
| 905 | 61 |
if (document.location.hash !== this.getLastHash()) { |
62 |
var _tab = document.location.hash.replace(/^#/,'').split('&'); |
|
63 |
for (var _i = 0; _i < _tab.length; _i++) { |
|
64 |
var _subtab = _tab[_i].split("="); |
|
65 |
if (_subtab[0] == "id" || _subtab[0] == "t") { |
|
66 |
this.last_hash_key = _subtab[0]; |
|
67 |
this.last_hash_value = _subtab[1]; |
|
68 |
if (this.last_hash_key == "id") { |
|
69 |
var _annotation = this.source.getElement(this.last_hash_value); |
|
70 |
if (typeof _annotation !== "undefined") { |
|
| 957 | 71 |
this.media.setCurrentTime(_annotation.begin); |
|
1068
7623f9af9272
merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
72 |
} else { |
|
7623f9af9272
merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
73 |
/* Proceed parsing elements, maybe a t was specified */ |
|
7623f9af9272
merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
74 |
continue; |
| 905 | 75 |
} |
76 |
} |
|
77 |
if (this.last_hash_key == "t") { |
|
|
1068
7623f9af9272
merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
78 |
this.media.setCurrentTime(1000 * this.last_hash_value); |
| 905 | 79 |
} |
80 |
break; |
|
| 880 | 81 |
} |
82 |
} |
|
83 |
} |
|
| 1013 | 84 |
}; |
| 880 | 85 |
|
|
1068
7623f9af9272
merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
86 |
IriSP.Widgets.Mediafragment.prototype.setHashToAnnotation = function(_annotation) { |
|
7623f9af9272
merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
87 |
this.setHash( 'id', _annotation.id, 't', _annotation.begin / 1000.0 ); |
| 1013 | 88 |
}; |
| 880 | 89 |
|
| 957 | 90 |
IriSP.Widgets.Mediafragment.prototype.setHashToTime = function() { |
91 |
this.setHash( 't', this.media.getCurrentTime().getSeconds() ); |
|
| 1013 | 92 |
}; |
| 880 | 93 |
|
|
1068
7623f9af9272
merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
94 |
IriSP.Widgets.Mediafragment.prototype.setHash = function(_key, _value, _key2, _value2) { |
| 905 | 95 |
if (!this.blocked && (this.last_hash_key !== _key || this.last_hash_value !== _value)) { |
96 |
this.last_hash_key = _key; |
|
97 |
this.last_hash_value = _value; |
|
|
1068
7623f9af9272
merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
98 |
this.last_extra_key = _key2; |
|
7623f9af9272
merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
99 |
this.last_extra_value = _value2; |
|
7623f9af9272
merge pull request #3 from O. Auber
ymh <ymh.work@gmail.com>
parents:
1013
diff
changeset
|
100 |
|
| 905 | 101 |
var _hash = this.getLastHash(); |
| 910 | 102 |
this.setWindowHash(_hash); |
| 882 | 103 |
if (window.parent !== window) { |
| 1013 | 104 |
window.parent.postMessage(_hash,"*"); |
| 882 | 105 |
} |
| 881 | 106 |
this.block(); |
| 880 | 107 |
} |
| 1013 | 108 |
}; |
| 881 | 109 |
|
110 |
IriSP.Widgets.Mediafragment.prototype.unblock = function() { |
|
111 |
if (typeof this.blockTimeout !== "undefined") { |
|
112 |
window.clearTimeout(this.blockTimeout); |
|
113 |
} |
|
114 |
this.blockTimeout = undefined; |
|
115 |
this.blocked = false; |
|
| 1013 | 116 |
}; |
| 881 | 117 |
|
118 |
IriSP.Widgets.Mediafragment.prototype.block = function() { |
|
119 |
if (typeof this.blockTimeout !== "undefined") { |
|
120 |
window.clearTimeout(this.blockTimeout); |
|
121 |
} |
|
122 |
this.blocked = true; |
|
| 957 | 123 |
this.blockTimeout = window.setTimeout(this.functionWrapper("unblock"), 1500); |
| 1013 | 124 |
}; |