Skip to content
This repository has been archived by the owner on Apr 24, 2018. It is now read-only.

remove jQuery #2

Open
wants to merge 2 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
7 changes: 4 additions & 3 deletions www/js/app/uiWebAppInstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ define(function (require) {
//Make sure DOM is ready before modifying it.
$(function () {
var dom = $('body'),
$install = $(install),
installDom = dom.find('.webapp-install'),
installedDom = dom.find('.webapp-installed'),
errorDom = dom.find('.webapp-error');
Expand Down Expand Up @@ -57,12 +58,12 @@ define(function (require) {
});
}

install.on('change', onInstallStateChange);
$install.on('change', onInstallStateChange);

//Call it now, check the current state.
onInstallStateChange();

install.on('error', function (evt, err) {
$install.on('error', function (evt, err) {
//Make sure DOM is ready before modifying it.
$(function () {
var errorDom = $('body').find('.webapp-error');
Expand All @@ -73,7 +74,7 @@ define(function (require) {
});
});

install.on('showiOSInstall', function (evt, deviceType) {
$install.on('showiOSInstall', function (evt, deviceType) {
//Show the UI that tells the user what Safari
//button to hit
$('body').find('.ios').show(0, function () { $(this).addClass(deviceType); });
Expand Down
73 changes: 54 additions & 19 deletions www/js/lib/install.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
/*jslint nomen: true */
/*global define, navigator, location, window, chrome */
/*global define, navigator, location, window, chrome, document */

define(function (require) {
'use strict';

var $ = require('jquery'),
dispatcher = $({}),
prop;

//Create event functions based on dispatcher object
function createDispatchFn(id) {
return function () {
return dispatcher[id].apply(dispatcher, arguments);
};
}

/**
* Detects if the current app has been installed.
*
Expand All @@ -27,13 +16,13 @@ define(function (require) {
if (fn) {
fn();
} else {
install.trigger('error', 'unsupported install: ' + install.type);
triggerEvent('error', 'unsupported install: ' + install.type);
}
}

function triggerChange(state) {
install.state = state;
install.trigger('change', install.state);
triggerEvent('change', install.state);
}

/**
Expand Down Expand Up @@ -120,17 +109,63 @@ define(function (require) {
//element mentioning how to install using the Safari
//"Add to Home Screen" button.
install.iosInstall = function () {
install.trigger('showiOSInstall', navigator.platform.toLowerCase());
triggerEvent('showiOSInstall', navigator.platform.toLowerCase());
};

var listeners = {};

function addEventListener(type, func /*, don't use capture */) {
if (! func) {
throw new TypeError('The listener must not be null or undefined.');
}

var theseListeners = listeners[type] = listeners[type] || [];
if (theseListeners.indexOf(func) === -1) {
theseListeners.push(func);
}
}

function removeEventListener(type, func) {
var theseListeners = listeners[type];
if (theseListeners) {
var index = theseListeners.indexOf(func);
theseListeners.splice(index, 1);
}
}

function dispatchEvent(evt) {
var type = evt.type;
var theseListeners = listeners[type];

if (theseListeners) {
theseListeners.forEach(function(oneListener) {
window.setTimeout(dispatchOneEvent.bind(null, evt, oneListener));
});
}
}

function dispatchOneEvent(evt, oneListener) {
if (typeof oneListener === "function") {
oneListener(evt);
} else if (typeof oneListener.handleEvent === "function") {
oneListener.handleEvent(evt);
}
}

function triggerEvent(evtType, detail) {
var event = document.createEvent('CustomEvent');
event.initCustomEvent(evtType, false, false, detail);
dispatchEvent(event);
}

//Allow install to do events.
install.on = createDispatchFn('on');
install.off = createDispatchFn('off');
install.trigger = createDispatchFn('trigger');
install.addEventListener = addEventListener;
install.removeEventListener = removeEventListener;
install.dispatchEvent = dispatchEvent;


//Start up the checks
install.check();

return install;
});
});