Skip to content
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
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import Manager from './manager';

const factory = new Manager();
let factory;
if(typeof window !== 'undefined'){
factory = new Manager();
}
export default {
create: function (options) {
if(!factory) {
throw new Error('Nipplejs library can only run in a browser. \'window\' is not defined.');
}
return factory.create(options);
},
factory: factory
Expand Down
39 changes: 25 additions & 14 deletions src/super.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import * as u from './utils';

// Constants
var isTouch = !!('ontouchstart' in window);
var isPointer = window.PointerEvent ? true : false;
var isMSPointer = window.MSPointerEvent ? true : false;
var events = {
touch: {
start: 'touchstart',
Expand All @@ -29,19 +26,31 @@ var events = {
end: 'MSPointerUp'
}
};
var toBind;
var secondBind = {};
if (isPointer) {
toBind = events.pointer;
} else if (isMSPointer) {
toBind = events.MSPointer;
} else if (isTouch) {
toBind = events.touch;
secondBind = events.mouse;
} else {
toBind = events.mouse;

function getBindings() {
var toBind;
var secondBind = {};
var isTouch = !!('ontouchstart' in window);
var isPointer = window.PointerEvent ? true : false;
var isMSPointer = window.MSPointerEvent ? true : false;

if (isPointer) {
toBind = events.pointer;
} else if (isMSPointer) {
toBind = events.MSPointer;
} else if (isTouch) {
toBind = events.touch;
secondBind = events.mouse;
} else {
toBind = events.mouse;
}
return {
toBind,
secondBind
};
}


function Super () {}

// Basic event system.
Expand Down Expand Up @@ -117,6 +126,7 @@ Super.prototype.bindEvt = function (el, type) {
}
};

var { toBind, secondBind } = getBindings();
u.bindEvt(el, toBind[type], self._domHandlers_[type]);

if (secondBind[type]) {
Expand All @@ -132,6 +142,7 @@ Super.prototype.unbindEvt = function (el, type) {
var self = this;
self._domHandlers_ = self._domHandlers_ || {};

var { toBind, secondBind } = getBindings();
u.unbindEvt(el, toBind[type], self._domHandlers_[type]);

if (secondBind[type]) {
Expand Down
3 changes: 2 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module.exports = {
library: NAME,
libraryExport: 'default',
libraryTarget: 'umd',
umdNamedDefine: true
umdNamedDefine: true,
globalObject: 'typeof self !== \'undefined\' ? self : this',
},
module: {
rules: [
Expand Down