Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add on/off methods for subscribing to events #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 67 additions & 7 deletions bigscreen.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*! BigScreen
* v2.0.4 - 2014-02-06
* v2.1.0 - 2014-09-30
* https://github.com/bdougherty/BigScreen
* Copyright 2014 Brad Dougherty; Apache 2.0 License
*/
Expand Down Expand Up @@ -28,6 +28,35 @@
}
return properties;
}();
var EVENT = {
ENTER: "enter",
EXIT: "exit",
CHANGE: "change",
ERROR: "error"
};
var events = [];
var attachedEvents = {};
Object.keys(EVENT).forEach(function(key) {
events.push(EVENT[key]);
attachedEvents[EVENT[key]] = [];
});
function fire() {
var args = Array.prototype.slice.apply(arguments);
var event = args.shift();
attachedEvents[event].forEach(function(callback) {
if (typeof callback === "function") {
callback.apply(callback, args);
}
});
}
function makeListener(fn) {
return function(type, handler) {
if (events.indexOf(type) === -1) {
return;
}
fn.call(this, type, handler);
};
}
function _getVideo(element) {
var videoElement = null;
if (element.tagName === "VIDEO") {
Expand Down Expand Up @@ -89,6 +118,9 @@
}
var callOnEnter = function(actualElement) {
var lastElement = elements[elements.length - 1];
if (lastElement) {
return;
}
if ((actualElement === lastElement.element || actualElement === lastVideoElement) && lastElement.hasEntered) {
return;
}
Expand All @@ -100,6 +132,7 @@
}
lastElement.enter.call(lastElement.element, actualElement || lastElement.element);
lastElement.hasEntered = true;
fire(EVENT.ENTER, bigscreen.element);
};
var callOnExit = function() {
if (lastVideoElement && !hasControls && !iOS7) {
Expand All @@ -111,9 +144,11 @@
var element = elements.pop();
if (element) {
element.exit.call(element.element);
fire(EVENT.EXIT, element.element);
if (!bigscreen.element) {
elements.forEach(function(element) {
element.exit.call(element.element);
fire(EVENT.EXIT, element.element);
});
elements = [];
bigscreen.onexit();
Expand All @@ -126,6 +161,7 @@
element = element || obj.element;
obj.error.call(element, reason);
bigscreen.onerror(element, reason);
fire(EVENT.ERROR, element, reason);
}
};
var bigscreen = {
Expand Down Expand Up @@ -196,6 +232,15 @@
}
return video.readyState < video.HAVE_METADATA ? "maybe" : video.webkitSupportsFullscreen;
},
on: makeListener(function(type, callback) {
attachedEvents[type].push(callback);
}),
off: makeListener(function(type, callback) {
var index = attachedEvents[type].indexOf(callback);
if (index > -1) {
attachedEvents[type].splice(index, 1);
}
}),
onenter: emptyFunction,
onexit: emptyFunction,
onchange: emptyFunction,
Expand Down Expand Up @@ -232,6 +277,7 @@
if (fn.change) {
document.addEventListener(fn.change, function onFullscreenChange(event) {
bigscreen.onchange(bigscreen.element);
fire(EVENT.CHANGE, bigscreen.element);
if (bigscreen.element) {
var previousElement = elements[elements.length - 2];
if (previousElement && previousElement.element === bigscreen.element) {
Expand All @@ -246,17 +292,31 @@
}, false);
}
document.addEventListener("webkitbeginfullscreen", function onBeginFullscreen(event) {
elements.push({
element: event.srcElement,
enter: emptyFunction,
exit: emptyFunction,
error: emptyFunction
});
var shouldPushElement = true;
if (elements.length > 0) {
for (var i = 0, length = elements.length; i < length; i++) {
var video = _getVideo(elements[i].element);
if (video === event.srcElement) {
shouldPushElement = false;
break;
}
}
}
if (shouldPushElement) {
elements.push({
element: event.srcElement,
enter: emptyFunction,
exit: emptyFunction,
error: emptyFunction
});
}
bigscreen.onchange(event.srcElement);
fire(EVENT.CHANGE, bigscreen.srcElement);
callOnEnter(event.srcElement);
}, true);
document.addEventListener("webkitendfullscreen", function onEndFullscreen(event) {
bigscreen.onchange(event.srcElement);
fire(EVENT.CHANGE, event.srcElement);
callOnExit(event.srcElement);
}, true);
if (fn.error) {
Expand Down
4 changes: 2 additions & 2 deletions bigscreen.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "BigScreen",
"version": "2.0.4",
"version": "2.1.0",
"description": "A simple library for using the JavaScript Fullscreen API.",
"keywords": [
"fullscreen"
Expand Down
62 changes: 62 additions & 0 deletions src/bigscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,43 @@
return properties;
}());

// Add allowed events
var EVENT = {
ENTER: 'enter',
EXIT: 'exit',
CHANGE: 'change',
ERROR: 'error'
};
var events = [];
var attachedEvents = {};

Object.keys(EVENT).forEach(function(key) {
events.push(EVENT[key]);
attachedEvents[EVENT[key]] = [];

});

function fire() {
var args = Array.prototype.slice.apply(arguments);
var event = args.shift();

attachedEvents[event].forEach(function(callback) {
if (typeof callback === 'function') {
callback.apply(callback, args);
}
});
}

function makeListener(fn) {
return function(type, handler) {
if (events.indexOf(type) === -1) {
return;
}

fn.call(this, type, handler);
};
}

// Find a child video in the element passed.
function _getVideo(element) {
var videoElement = null;
Expand Down Expand Up @@ -121,6 +158,11 @@
// browser will fire 2 `webkitfullscreenchange` events when entering full screen from inside an
// iframe. This is the result of the same bug as the resizeExitHack.
var lastElement = elements[elements.length - 1];

if (lastElement) {
return;
}

if ((actualElement === lastElement.element || actualElement === lastVideoElement) && lastElement.hasEntered) {
return;
}
Expand All @@ -139,6 +181,8 @@
// again if there is a duplicate call (see above).
lastElement.enter.call(lastElement.element, actualElement || lastElement.element);
lastElement.hasEntered = true;

fire(EVENT.ENTER, bigscreen.element);
};

var callOnExit = function() {
Expand All @@ -159,12 +203,14 @@
// time from the iframe resize hack.
if (element) {
element.exit.call(element.element);
fire(EVENT.EXIT, element.element);

// When the browser has fully exited full screen, make sure to loop
// through and call the rest of the callbacks and then the global exit.
if (!bigscreen.element) {
elements.forEach(function(element) {
element.exit.call(element.element);
fire(EVENT.EXIT, element.element);
});
elements = [];

Expand All @@ -182,6 +228,7 @@

obj.error.call(element, reason);
bigscreen.onerror(element, reason);
fire(EVENT.ERROR, element, reason);
}
};

Expand Down Expand Up @@ -301,6 +348,18 @@
return video.readyState < video.HAVE_METADATA ? 'maybe' : video.webkitSupportsFullscreen;
},

on: makeListener(function(type, callback) {
attachedEvents[type].push(callback);
}),

off: makeListener(function(type, callback) {
var index = attachedEvents[type].indexOf(callback);

if (index > -1) {
attachedEvents[type].splice(index, 1);
}
}),

// ### onenter, onexit, onchange, onerror
// Populate the global handlers with empty functions.
onenter: emptyFunction,
Expand Down Expand Up @@ -356,6 +415,7 @@
if (fn.change) {
document.addEventListener(fn.change, function onFullscreenChange(event) {
bigscreen.onchange(bigscreen.element);
fire(EVENT.CHANGE, bigscreen.element);

if (bigscreen.element) {
// This should be treated an exit if the element that is in full screen
Expand Down Expand Up @@ -404,11 +464,13 @@
}

bigscreen.onchange(event.srcElement);
fire(EVENT.CHANGE, bigscreen.srcElement);
callOnEnter(event.srcElement);
}, true);

document.addEventListener('webkitendfullscreen', function onEndFullscreen(event) {
bigscreen.onchange(event.srcElement);
fire(EVENT.CHANGE, event.srcElement);
callOnExit(event.srcElement);
}, true);

Expand Down