pitaya-launcher/js/app.js

127 lines
2.8 KiB
JavaScript
Raw Normal View History

2015-08-26 14:08:12 +02:00
(function(Pitaya, window) {
2015-08-20 18:41:51 +02:00
"use strict";
// 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');
// Load templates
2015-08-26 14:08:12 +02:00
var itemsListTpl = Handlebars.compile(Pitaya.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-26 14:08:12 +02:00
* @return Pitaya
2015-08-20 18:41:51 +02:00
*/
2015-08-26 14:08:12 +02:00
Pitaya.start = function(rootEl) {
2015-08-20 18:41:51 +02:00
2015-08-26 14:08:12 +02:00
Pitaya._opts = minimist(gui.App.argv);
Pitaya._rootEl = Pitaya.DOM.select(rootEl);
Pitaya._initListeners();
2015-08-20 18:41:51 +02:00
2015-08-26 14:08:12 +02:00
var profilePath = Pitaya._opts.profile || DEFAULT_PROFILE;
2015-08-20 18:41:51 +02:00
2015-08-26 14:08:12 +02:00
return Pitaya.loadProfile(profilePath)
2015-08-20 18:41:51 +02:00
.then(function() {
2015-08-26 14:08:12 +02:00
return Pitaya;
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-26 14:08:12 +02:00
Pitaya.loadProfile = function(profilePath) {
return Pitaya._loadJSONFile(profilePath)
2015-08-20 18:41:51 +02:00
.then(function(profile) {
2015-08-26 14:08:12 +02:00
Pitaya._profile = profile;
Pitaya.render();
2015-08-20 18:41:51 +02:00
return profile;
})
;
};
/**
* Update the application view
*
2015-08-26 14:08:12 +02:00
* @return Pitaya
2015-08-20 18:41:51 +02:00
*/
2015-08-26 14:08:12 +02:00
Pitaya.render = function() {
var rootEl = Pitaya._rootEl;
var profile = Pitaya._profile;
2015-08-20 18:41:51 +02:00
rootEl.innerHTML = itemsListTpl(profile);
};
/**
* Initialize DOM event listeners
* @private
*/
2015-08-26 14:08:12 +02:00
Pitaya._initListeners = function() {
var rootEl = Pitaya._rootEl;
rootEl.addEventListener('click', Pitaya._onItemClick);
2015-08-20 18:41:51 +02:00
};
/**
* App item click handler
* @private
*/
2015-08-26 14:08:12 +02:00
Pitaya._onItemClick = function(evt) {
2015-08-20 18:41:51 +02:00
var appItemEl = evt.srcElement.matches( '.app-item') ? evt.srcElement :
2015-08-26 14:08:12 +02:00
Pitaya.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-26 14:08:12 +02:00
Pitaya._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-26 14:08:12 +02:00
}(window.Pitaya = window.Pitaya || {}, window));