aboutsummaryrefslogtreecommitdiffstats
path: root/demo/src
diff options
context:
space:
mode:
authorSam Potts <me@sampotts.me>2017-05-16 09:58:53 +1000
committerSam Potts <me@sampotts.me>2017-05-16 09:58:53 +1000
commit966c3d875d68a096b6aaa5b86e3b9ae4d93696b5 (patch)
treec0c69346e14692b8ab7de9ba71b50dc30cec1bca /demo/src
parentfe9383bed57e6458fe40c6e1c22402b37987e5de (diff)
downloadplyr-966c3d875d68a096b6aaa5b86e3b9ae4d93696b5.tar.lz
plyr-966c3d875d68a096b6aaa5b86e3b9ae4d93696b5.tar.xz
plyr-966c3d875d68a096b6aaa5b86e3b9ae4d93696b5.zip
Single instance only
Diffstat (limited to 'demo/src')
-rw-r--r--demo/src/js/lib/sprite.js89
-rw-r--r--demo/src/js/main.js43
2 files changed, 110 insertions, 22 deletions
diff --git a/demo/src/js/lib/sprite.js b/demo/src/js/lib/sprite.js
new file mode 100644
index 00000000..bcd8a0b7
--- /dev/null
+++ b/demo/src/js/lib/sprite.js
@@ -0,0 +1,89 @@
+// ==========================================================================
+// SVG sprite loading and caching
+// This file should be at the top of the body to avoid a flash
+// Usage: loadSprite('https://cdn.com/path/to/sprite.svg', 'sprite-id');
+// The second argument is optional but prevents loading twice
+// ==========================================================================
+
+(function() {
+ window.loadSprite = function(url, id) {
+ if (typeof url !== "string") {
+ return;
+ }
+
+ var body = document.body;
+ var prefix = "cache-";
+ var hasId = typeof id === "string";
+ var isCached = false;
+
+ // Check for *actual* storage support
+ var cacheSupported = (function() {
+ if (!hasId) {
+ return false;
+ }
+ var test = '___test';
+ try {
+ localStorage.setItem(test, test);
+ localStorage.removeItem(test);
+ return true;
+ } catch (e) {
+ return false;
+ }
+ })();
+
+ function updateSprite(container, data) {
+ // Inject content
+ container.innerHTML = data;
+
+ // Inject the SVG to the body
+ body.insertBefore(container, body.childNodes[0]);
+ }
+
+ // Only load once
+ if (!hasId || document.querySelectorAll("#" + id).length === 0) {
+ // Create container
+ var container = document.createElement("div");
+ container.setAttribute("hidden", "");
+
+ if (hasId) {
+ container.setAttribute("id", id);
+ }
+
+ // Check in cache
+ if (cacheSupported) {
+ var cached = localStorage.getItem(prefix + id);
+ isCached = cached !== null;
+
+ if (isCached) {
+ var data = JSON.parse(cached);
+ updateSprite(container, data.content);
+ }
+ }
+
+ // ReSharper disable once InconsistentNaming
+ var xhr = new XMLHttpRequest();
+
+ // XHR for Chrome/Firefox/Opera/Safari
+ if ("withCredentials" in xhr) {
+ xhr.open("GET", url, true);
+ }
+ // Not supported
+ else {
+ return;
+ }
+
+ // Once loaded, inject to container and body
+ xhr.onload = function() {
+ if (cacheSupported) {
+ localStorage.setItem(prefix + id, JSON.stringify({
+ content: xhr.responseText
+ }));
+ }
+
+ updateSprite(container, xhr.responseText);
+ };
+
+ xhr.send();
+ }
+ }
+})();
diff --git a/demo/src/js/main.js b/demo/src/js/main.js
index b3b6f517..b5d5ad15 100644
--- a/demo/src/js/main.js
+++ b/demo/src/js/main.js
@@ -4,16 +4,18 @@
// Please see readme.md in the root or github.com/selz/plyr
// ==========================================================================
-/*global plyr*/
+/*global Plyr*/
// General functions
(function() {
- //document.body.addEventListener('ready', function(event) { console.log(event); });
+ document.body.addEventListener('ready', function(event) {
+ console.log(event);
+ });
// Setup the player
- var instances = plyr.setup({
+ var player = new Plyr('#player', {
debug: true,
- title: 'Video demo',
+ title: 'View From A Blue Moon',
iconUrl: '../dist/plyr.svg',
tooltips: {
controls: true
@@ -35,25 +37,22 @@
'airplay'
]
});
- plyr.loadSprite('dist/demo.svg');
-
- // Plyr returns an array regardless
- var player = instances[0];
+ window.loadSprite('dist/demo.svg', 'demo-sprite');
// Setup type toggle
- var buttons = document.querySelectorAll('[data-source]'),
- types = {
- video: 'video',
- audio: 'audio',
- youtube: 'youtube',
- vimeo: 'vimeo'
- },
- currentType = window.location.hash.replace('#', ''),
- historySupport = (window.history && window.history.pushState);
+ var buttons = document.querySelectorAll('[data-source]');
+ var types = {
+ video: 'video',
+ audio: 'audio',
+ youtube: 'youtube',
+ vimeo: 'vimeo'
+ };
+ var currentType = window.location.hash.replace('#', '');
+ var historySupport = (window.history && window.history.pushState);
// Bind to each button
- for (var i = buttons.length - 1; i >= 0; i--) {
- buttons[i].addEventListener('click', function() {
+ [].forEach.call(buttons, function(button) {
+ button.addEventListener('click', function() {
var type = this.getAttribute('data-source');
newSource(type);
@@ -64,7 +63,7 @@
}, '', '#' + type);
}
});
- }
+ });
// List for backwards/forwards
window.addEventListener('popstate', function(event) {
@@ -199,6 +198,6 @@ if (document.domain.indexOf('plyr.io') > -1) {
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
- ga('create', 'UA-40881672-11', 'auto');
- ga('send', 'pageview');
+ window.ga('create', 'UA-40881672-11', 'auto');
+ window.ga('send', 'pageview');
}