2015-08-21 14:47:06 +02:00
|
|
|
(function(Papaye, window) {
|
2015-08-20 18:41:51 +02:00
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2015-08-21 14:47:06 +02:00
|
|
|
// Load dependencies
|
2015-08-20 18:41:51 +02:00
|
|
|
var path = require('path');
|
|
|
|
var fs = require('fs');
|
|
|
|
var Handlebars = require('handlebars');
|
|
|
|
var cp = require("child_process");
|
|
|
|
var gui = require('nw.gui');
|
|
|
|
var minimist = require('minimist');
|
|
|
|
|
2015-08-21 14:47:06 +02:00
|
|
|
// Load templates
|
|
|
|
var itemsListTpl = Handlebars.compile(Papaye.DOM.select('#items-list-tpl').innerHTML);
|
|
|
|
|
|
|
|
// Internal constants
|
|
|
|
var DEFAULT_PROFILE = './default-profile.json';
|
2015-08-20 18:41:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the app
|
|
|
|
*
|
|
|
|
* @param rootEl The application root element selector
|
2015-08-21 14:47:06 +02:00
|
|
|
* @return Papaye
|
2015-08-20 18:41:51 +02:00
|
|
|
*/
|
2015-08-21 14:47:06 +02:00
|
|
|
Papaye.start = function(rootEl) {
|
2015-08-20 18:41:51 +02:00
|
|
|
|
2015-08-21 14:47:06 +02:00
|
|
|
Papaye._opts = minimist(gui.App.argv);
|
|
|
|
Papaye._rootEl = Papaye.DOM.select(rootEl);
|
|
|
|
Papaye._initListeners();
|
2015-08-20 18:41:51 +02:00
|
|
|
|
2015-08-21 14:47:06 +02:00
|
|
|
var profilePath = Papaye._opts.profile || DEFAULT_PROFILE;
|
2015-08-20 18:41:51 +02:00
|
|
|
|
2015-08-21 14:47:06 +02:00
|
|
|
return Papaye.loadProfile(profilePath)
|
2015-08-20 18:41:51 +02:00
|
|
|
.then(function() {
|
2015-08-21 14:47:06 +02:00
|
|
|
return Papaye;
|
2015-08-20 18:41:51 +02:00
|
|
|
})
|
|
|
|
;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a profile file and render the application
|
|
|
|
*
|
|
|
|
* @param profilePath The path of the profile file
|
|
|
|
* @return Promise
|
|
|
|
*/
|
2015-08-21 14:47:06 +02:00
|
|
|
Papaye.loadProfile = function(profilePath) {
|
|
|
|
return Papaye._loadJSONFile(profilePath)
|
2015-08-20 18:41:51 +02:00
|
|
|
.then(function(profile) {
|
2015-08-21 14:47:06 +02:00
|
|
|
Papaye._profile = profile;
|
|
|
|
Papaye.render();
|
2015-08-20 18:41:51 +02:00
|
|
|
return profile;
|
|
|
|
})
|
|
|
|
;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the application view
|
|
|
|
*
|
2015-08-21 14:47:06 +02:00
|
|
|
* @return Papaye
|
2015-08-20 18:41:51 +02:00
|
|
|
*/
|
2015-08-21 14:47:06 +02:00
|
|
|
Papaye.render = function() {
|
|
|
|
var rootEl = Papaye._rootEl;
|
|
|
|
var profile = Papaye._profile;
|
2015-08-20 18:41:51 +02:00
|
|
|
rootEl.innerHTML = itemsListTpl(profile);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize DOM event listeners
|
|
|
|
* @private
|
|
|
|
*/
|
2015-08-21 14:47:06 +02:00
|
|
|
Papaye._initListeners = function() {
|
|
|
|
var rootEl = Papaye._rootEl;
|
|
|
|
rootEl.addEventListener('click', Papaye._onItemClick);
|
2015-08-20 18:41:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* App item click handler
|
|
|
|
* @private
|
|
|
|
*/
|
2015-08-21 14:47:06 +02:00
|
|
|
Papaye._onItemClick = function(evt) {
|
2015-08-20 18:41:51 +02:00
|
|
|
|
|
|
|
var appItemEl = evt.srcElement.matches( '.app-item') ? evt.srcElement :
|
2015-08-21 14:47:06 +02:00
|
|
|
Papaye.DOM.getClosestAncestor(evt.srcElement, '.app-item')
|
2015-08-20 18:41:51 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
if( !appItemEl ) return;
|
|
|
|
|
|
|
|
var execPath = appItemEl.dataset.exec;
|
|
|
|
|
|
|
|
console.info('Launching application "'+execPath+'"...');
|
|
|
|
|
|
|
|
if(execPath) {
|
|
|
|
appItemEl.classList.add('loading');
|
|
|
|
cp.exec(execPath, function(err) {
|
|
|
|
appItemEl.classList.remove('loading');
|
|
|
|
if(err) return console.error(err.stack || err);
|
|
|
|
console.info('Application closed "'+execPath+'".');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a JSON file
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param filePath The path of the json file
|
|
|
|
* @return Promise
|
|
|
|
*/
|
2015-08-21 14:47:06 +02:00
|
|
|
Papaye._loadJSONFile = function(filePath) {
|
2015-08-20 18:41:51 +02:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
fs.readFile(filePath, 'utf8', function(err, fileContent) {
|
|
|
|
if(err) return reject(err);
|
|
|
|
try {
|
|
|
|
var json = JSON.parse(fileContent);
|
|
|
|
return resolve(json);
|
|
|
|
} catch(err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-21 14:47:06 +02:00
|
|
|
}(window.Papaye = window.Papaye || {}, window));
|