2015-08-26 14:08:12 +02:00
|
|
|
(function(Pitaya, 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-26 17:53:46 +02:00
|
|
|
// Load templates...
|
|
|
|
var launcherViewTpl = Handlebars.compile(Pitaya.DOM.select('#launcher-view-tpl').innerHTML);
|
|
|
|
|
|
|
|
// ... and partials
|
|
|
|
Handlebars.registerPartial('itemListTpl', Pitaya.DOM.select('#items-list-tpl').innerHTML);
|
|
|
|
Handlebars.registerPartial('itemTpl', Pitaya.DOM.select('#item-tpl').innerHTML);
|
2015-08-21 14:47:06 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
})
|
2015-08-26 17:53:46 +02:00
|
|
|
.catch(Pitaya._onError)
|
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;
|
2015-08-26 17:53:46 +02:00
|
|
|
Pitaya.renderLauncherView();
|
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 17:53:46 +02:00
|
|
|
Pitaya.renderLauncherView = function(currentItemPath) {
|
|
|
|
|
|
|
|
console.log('render', currentItemPath);
|
|
|
|
|
|
|
|
currentItemPath = Pitaya._normalizeItemPath(currentItemPath);
|
2015-08-26 14:08:12 +02:00
|
|
|
var rootEl = Pitaya._rootEl;
|
2015-08-26 17:53:46 +02:00
|
|
|
var currentItem = Pitaya._getItemByPath(currentItemPath);
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
currentItemPath: currentItemPath.join('.'),
|
|
|
|
currentItem: currentItem,
|
|
|
|
isRoot: currentItemPath.length === 0
|
|
|
|
};
|
|
|
|
|
|
|
|
console.log('render data', data);
|
|
|
|
|
|
|
|
rootEl.innerHTML = launcherViewTpl(data);
|
|
|
|
|
2015-08-20 18:41:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-26 17:53:46 +02:00
|
|
|
rootEl.addEventListener('click', Pitaya._onGoBackClick);
|
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;
|
|
|
|
|
2015-08-26 17:53:46 +02:00
|
|
|
var itemPath = appItemEl.dataset.itemPath;
|
|
|
|
var item = Pitaya._getItemByPath(itemPath);
|
2015-08-20 18:41:51 +02:00
|
|
|
|
2015-08-26 17:53:46 +02:00
|
|
|
console.log('item click',itemPath, item);
|
2015-08-20 18:41:51 +02:00
|
|
|
|
2015-08-26 17:53:46 +02:00
|
|
|
if(!item) return;
|
|
|
|
|
|
|
|
if('items' in item) {
|
|
|
|
return Pitaya.renderLauncherView(itemPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(item.exec) {
|
|
|
|
|
|
|
|
console.info('Launching application "'+item.exec+'"...');
|
2015-08-20 18:41:51 +02:00
|
|
|
appItemEl.classList.add('loading');
|
2015-08-26 17:53:46 +02:00
|
|
|
|
|
|
|
Pitaya._runApp(item.exec)
|
|
|
|
.then(function() {
|
|
|
|
appItemEl.classList.remove('loading');
|
|
|
|
console.info('Application closed "'+item.exec+'".');
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
Pitaya._onError(err);
|
|
|
|
appItemEl.classList.remove('loading');
|
|
|
|
})
|
|
|
|
;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GoBack button click handler
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
Pitaya._onGoBackClick = function(evt) {
|
|
|
|
|
|
|
|
var goBackEl = evt.srcElement.matches( '.goback') ? evt.srcElement :
|
|
|
|
Pitaya.DOM.getClosestAncestor(evt.srcElement, '.goback')
|
|
|
|
;
|
|
|
|
|
|
|
|
if(!goBackEl) return;
|
|
|
|
|
|
|
|
var currentItemPath = goBackEl.dataset.itemPath;
|
|
|
|
var parentItemPath = Pitaya._normalizeItemPath(currentItemPath);
|
|
|
|
|
|
|
|
parentItemPath.pop();
|
|
|
|
|
|
|
|
return Pitaya.renderLauncherView(parentItemPath);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Pitaya._normalizeItemPath = function(itemPath) {
|
|
|
|
|
|
|
|
if( Array.isArray(itemPath) ) return itemPath;
|
|
|
|
|
|
|
|
if((typeof itemPath === 'string' && itemPath.length === 0) || !itemPath) return [];
|
|
|
|
|
|
|
|
return itemPath.split('.').reduce(function(arr, index) {
|
|
|
|
if(index !== '') {
|
|
|
|
arr.push(+index);
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Pitaya._getItemByPath = function(itemPath, rootItem) {
|
|
|
|
|
|
|
|
rootItem = rootItem || Pitaya._profile;
|
|
|
|
itemPath = Pitaya._normalizeItemPath(itemPath);
|
|
|
|
|
|
|
|
var index = itemPath.slice(0,1)[0];
|
|
|
|
|
|
|
|
if(index === undefined) {
|
|
|
|
return rootItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!('items' in rootItem)) {
|
|
|
|
return undefined;
|
2015-08-20 18:41:51 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 17:53:46 +02:00
|
|
|
var subItem = rootItem.items[index];
|
|
|
|
|
|
|
|
if(itemPath.length === 0) {
|
|
|
|
return subItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Pitaya._getItemByPath(itemPath, subItem);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Pitaya._runApp = function(execPath) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
cp.exec(execPath, function(err) {
|
|
|
|
if(err) return reject(err);
|
|
|
|
return resolve();
|
|
|
|
});
|
|
|
|
});
|
2015-08-20 18:41:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 17:53:46 +02:00
|
|
|
|
|
|
|
Pitaya._onError = function(err) {
|
|
|
|
console.error(err.stack ? err.stack : err);
|
|
|
|
};
|
|
|
|
|
2015-08-26 14:08:12 +02:00
|
|
|
}(window.Pitaya = window.Pitaya || {}, window));
|