aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSam Potts <me@sampotts.me>2015-05-17 17:23:09 +1000
committerSam Potts <me@sampotts.me>2015-05-17 17:23:09 +1000
commit7ccbfad6adf721799869a9b826778199116f63ed (patch)
treeede22dab30c6ae6ed722f0945182c1e1dab6f515 /src
parent91f8a158d27423012fa0b8459802c86867b035f3 (diff)
downloadplyr-7ccbfad6adf721799869a9b826778199116f63ed.tar.lz
plyr-7ccbfad6adf721799869a9b826778199116f63ed.tar.xz
plyr-7ccbfad6adf721799869a9b826778199116f63ed.zip
New API methods (fixes #77), Fix for non strict mode (fixes #78)
Diffstat (limited to 'src')
-rw-r--r--src/js/plyr.js78
1 files changed, 70 insertions, 8 deletions
diff --git a/src/js/plyr.js b/src/js/plyr.js
index bf4b3e44..59847333 100644
--- a/src/js/plyr.js
+++ b/src/js/plyr.js
@@ -1,6 +1,6 @@
// ==========================================================================
// Plyr
-// plyr.js v1.1.5
+// plyr.js v1.1.6
// https://github.com/selz/plyr
// License: The MIT License (MIT)
// ==========================================================================
@@ -366,12 +366,28 @@
// append it to the parent.
if (sibling) {
parent.insertBefore(child, sibling);
- } else {
+ }
+ else {
parent.appendChild(child);
}
}
}
+ // Unwrap an element
+ // http://plainjs.com/javascript/manipulation/unwrap-a-dom-element-35/
+ function _unwrap(wrapper) {
+ // Get the element's parent node
+ var parent = wrapper.parentNode;
+
+ // Move all children out of the element
+ while (wrapper.firstChild) {
+ parent.insertBefore(wrapper.firstChild, wrapper);
+ }
+
+ // Remove the empty element
+ parent.removeChild(wrapper);
+ }
+
// Remove an element
function _remove(element) {
element.parentNode.removeChild(element);
@@ -404,7 +420,7 @@
// Toggle event
function _toggleHandler(element, events, callback, toggle) {
- events = events.split(" ");
+ var eventList = events.split(" ");
// If a nodelist is passed, call itself on each node
if(element instanceof NodeList) {
@@ -417,8 +433,8 @@
}
// If a single node is passed, bind the event listener
- for (var i = 0; i < events.length; i++) {
- element[toggle ? "addEventListener" : "removeEventListener"](events[i], callback, false);
+ for (var i = 0; i < eventList.length; i++) {
+ element[toggle ? "addEventListener" : "removeEventListener"](eventList[i], callback, false);
}
}
@@ -1512,7 +1528,47 @@
}
}
+ // Destroy an instance
+ function _destroy() {
+ // Bail if the element is not initialized
+ if(!player.init) {
+ return null;
+ }
+
+ // Event listeners are removed when elements are removed
+ // http://stackoverflow.com/questions/12528049/if-a-dom-element-is-removed-are-its-listeners-also-removed-from-memory
+
+ // Remove controls
+ _remove(_getElement(config.selectors.controls));
+
+ // If video, we need to remove some more
+ if(player.type === "video") {
+ // Remove captions
+ _remove(_getElement(config.selectors.captions));
+
+ // Remove video wrapper
+ _unwrap(player.videoContainer);
+ }
+
+ // Restore native video controls
+ player.media.setAttribute("controls", "");
+
+ // Clone the media element to remove listeners
+ // http://stackoverflow.com/questions/19469881/javascript-remove-all-event-listeners-of-specific-type
+ var clone = player.media.cloneNode(true);
+ player.media.parentNode.replaceChild(clone, player.media);
+
+ // Remove init flag
+ player.init = false;
+ }
+
+ // Setup a player
function _init() {
+ // Bail if the element is initialized
+ if(player.init) {
+ return null;
+ }
+
// Setup the fullscreen api
fullscreen = _fullscreen();
@@ -1571,10 +1627,14 @@
}
// Successful setup
- return true;
+ player.init = true;
}
- if(!_init()) {
+ // Initialize instance
+ _init();
+
+ // If init failed, return an empty object
+ if(!player.init) {
return {};
}
@@ -1593,7 +1653,9 @@
toggleCaptions: _toggleCaptions,
toggleFullscreen: _toggleFullscreen,
isFullscreen: function() { return player.isFullscreen || false; },
- support: function(mimeType) { return _supportMime(player, mimeType); }
+ support: function(mimeType) { return _supportMime(player, mimeType); },
+ destroy: _destroy,
+ restore: _init
}
}