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

apps module from https://github.com/unhosted/u36/issues/6 #60

Merged
merged 1 commit into from
Aug 1, 2014
Merged
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
200 changes: 200 additions & 0 deletions src/apps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/**
* File: Apps
*
* Maintainer: Michiel de Jong <michiel@unhosted.org>
* Version: - 0.1.0
*
*/
RemoteStorage.defineModule('apps', function(privClient, pubClient) {
var apps = {},
defaultApps = {
litewrite: {
href: 'http://litewrite.net', // <-- optional; defaults to https://name.5apps.com/ (lowercased)
img: '/img/litewrite.png', // <-- optional; defaults to /img/name.png (lowercased)
name: 'litewrite' // <-- optional; defaults to name
},
Laverna: { href: 'https://laverna.cc/' },
mcnotes: { },
//(no rs) StackEdit: { href: 'https://stackedit.io/' },
gHost: { },
dogfeed: { },
dogtalk: { },
// sharesome: { img: 'none' },
grouptabs: { },
byoDB: { href: 'http://diafygi.github.io/byoDB/examples/diary/' },
'time tracker': { href: 'http://shybyte.github.io/unhosted-time-tracker/', img: '/img/time.png' },
'svg-edit': { href: 'https://svg-edit.5apps.com/editor/svg-editor.html'},
browser: { href: 'https://remotestorage-browser.5apps.com/' },
vidmarks: { },
music: { href: 'https://music-michiel.5apps.com/' },
drinks: { href: 'https://myfavoritedrinks.5apps.com/' },
todo: { href: 'https://todomvc.5apps.com/labs/architecture-examples/remotestorage/' },
'dspace-client': { href: 'https://dspace-nilclass.5apps.com/', img: '/img/dspace.png' },
strut: { href: 'http://tantaman.github.com/Strut/' },
//(no rs) TiddlyWiki: { href: 'http://www.tiddlywiki.com/' },
//(no rs) Fargo: { href: 'http://fargo.io/' },
// Crypton: { href: 'http://crypton.io/', img: 'none' },
// Dillinger: { href: 'http://dillinger.io/', img: 'none' },
// 'freedom js': { href: 'http://freedomjs.org/', img: 'none' },
// 'Peer CDN': { href: 'https://peercdn.com/', img: 'none' },
// '+PeerServer': { href: 'http://www.peer-server.com/', img: 'none' },
// Mylar: { href: 'http://css.csail.mit.edu/mylar/#Software', img: 'none' },
// CryptoSphere: { href: 'http://cryptosphere.org/', img: 'none' },
// editor: { href: 'https://editor-michiel.5apps.com/' },
// social: { href: 'https://social-michiel.5apps.com/' },
// email: { href: 'https://email-michiel.5apps.com/' },
// smarkers: { href: 'https://smarker-nilclass.5apps.com/' },
};

var changeHandler = function() {
console.log('Please call remoteStorage.apps.onChange(handler)');
};

function fillInBlanks(key, obj) {
obj.href = obj.href || 'https://'+key.toLowerCase()+'.5apps.com/';
obj.img = obj.img || '/img/'+key.toLowerCase()+'.png';
obj.name = obj.name || key;
return obj;
}


/**
* Function: remoteStorage.apps.installApp
*
* Add an app to the user's list of installed apps. Will trigger
* the change handler to be called if you previously set one using
* `remoteStorage.apps.onChange(handler);`.
*
* Parameters:
* name - name of the app (key in the defaultApps dictionary)
*/
function installApp(name) {
apps[name] = defaultApps[name];
privClient.storeObject('app', name, apps[name]);
}

/**
* Function: remoteStorage.apps.uninstallApp
*
* Remove an app to the user's list of installed apps. Will trigger
* the change handler to be called if you previously set one using
* `remoteStorage.apps.onChange(handler);`.
*
* Parameters:
* name - name of the app (key in the defaultApps dictionary)
*/
function uninstallApp(name) {
delete apps[name];
privClient.remove(name);
}

/**
* Function: remoteStorage.apps.onChange
*
* Set the change event handler. This will be called, with the
* dictionary of installed apps as the only argument, whenever the
* list of installed apps changes. Example:
*
* remoteStorage.apps.onChange(function(apps) {
* myAppsView.reset();
* for (var i in apps) {
* myAppsView.add(apps[i]);
* }
* myAppsView.render();
* });
*
* Parameters:
* handler - a Function that takes a dictionary of apps as its only argument
*/
function onChange(handler) {
changeHandler = handler;
}

function init() {
RemoteStorage.config.changeEvents.window = true;
privClient.cache('', 'ALL');
privClient.on('change', function(evt) {
if (evt.newValue) {
apps[evt.relativePath] = evt.newValue;
} else {
delete apps[evt.relativePath];
}
console.log('calling changeHandler with', apps, evt);
changeHandler(apps);
});

/**
* Schema: apps/app
*
* Info necessary for displaying a link to an app in an app store
*
* name - the name of the app that's being described here (string)
* href - launch URL (string)
* img - URL of a 128x128px app icon (string)
*/
privClient.declareType('app', {
type: 'object',
properties: {
name: { type: 'string' },
href: { type: 'string' },
img: { type: 'string' }
},
required: ['name']
});

for (var i in defaultApps) {
defaultApps[i] = fillInBlanks(i, defaultApps[i]);
}
}

/**
* Function: remoteStorage.apps.getInstalledApps
*
* Get a dictionary of apps whihch the user has installed.
*
* Parameters:
* (none)
*
* Returns: A dictionary from string app names to objects that follow the
* apps/app schema defined above.
*/
function getInstalledApps() {
return apps;
}


/**
* Function: remoteStorage.apps.getAvailableApps
*
* Get a dictionary of apps whihch the user does not have installed, but
* which are available to install.
*
* Parameters:
* (none)
*
* Returns: A dictionary from string app names to objects that follow the
* apps/app schema defined above.
*/
function getAvailableApps() {
var i, availableApps = {};
for (i in defaultApps) {
if (!apps[i]) {
availableApps[i] = defaultApps[i];
}
}
return availableApps;
}

//...
init();

return {
exports: {
onChange: onChange,
installApp: installApp,
uninstallApp: uninstallApp,
getInstalledApps: getInstalledApps,
getAvailableApps: getAvailableApps
}
};
});