(function(Pitaya, window) { "use strict"; // Load dependencies 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... 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); // Internal constants var DEFAULT_PROFILE = './default-profile.json'; /** * Start the app * * @param rootEl The application root element selector * @return Pitaya */ Pitaya.start = function(rootEl) { Pitaya._opts = minimist(gui.App.argv); Pitaya._rootEl = Pitaya.DOM.select(rootEl); Pitaya._initListeners(); var profilePath = Pitaya._opts.profile || DEFAULT_PROFILE; return Pitaya.loadProfile(profilePath) .then(function() { return Pitaya; }) .catch(Pitaya._onError) ; }; /** * Load a profile file and render the application * * @param profilePath The path of the profile file * @return Promise */ Pitaya.loadProfile = function(profilePath) { return Pitaya._loadJSONFile(profilePath) .then(function(profile) { Pitaya._profile = profile; Pitaya.renderLauncherView(); return profile; }) ; }; /** * Update the application view * * @return Pitaya */ Pitaya.renderLauncherView = function(currentItemPath) { currentItemPath = Pitaya._normalizeItemPath(currentItemPath); var rootEl = Pitaya._rootEl; var currentItem = Pitaya._getItemByPath(currentItemPath); var data = { currentItemPath: currentItemPath.join('.'), currentItem: currentItem, isRoot: currentItemPath.length === 0 }; rootEl.innerHTML = launcherViewTpl(data); }; /** * Initialize DOM event listeners * @private */ Pitaya._initListeners = function() { var rootEl = Pitaya._rootEl; rootEl.addEventListener('click', Pitaya._onItemClick); rootEl.addEventListener('click', Pitaya._onGoBackClick); }; /** * App item click handler * @private */ Pitaya._onItemClick = function(evt) { var appItemEl = evt.srcElement.matches( '.app-item') ? evt.srcElement : Pitaya.DOM.getClosestAncestor(evt.srcElement, '.app-item') ; if( !appItemEl ) return; var itemPath = appItemEl.dataset.itemPath; var item = Pitaya._getItemByPath(itemPath); if(!item) return; if('items' in item) { var rootEl = Pitaya._rootEl; Pitaya.Anim.play(rootEl, 'slide-out-left 250ms ease-in-out') .then(function() { Pitaya.renderLauncherView(itemPath); return Pitaya.Anim.play(rootEl, 'slide-in-right 250ms ease-in-out'); }) ; } if(item.exec) { console.info('Launching application "'+item.exec+'"...'); appItemEl.classList.add('pulse'); Pitaya._runApp(item.exec) .then(function() { appItemEl.classList.remove('pulse'); }) .catch(function(err) { Pitaya._onError(err); appItemEl.classList.remove('pulse'); }) ; } }; /** * 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(); var rootEl = Pitaya._rootEl; Pitaya.Anim.play(rootEl, 'slide-out-right 250ms ease-in-out') .then(function() { Pitaya.renderLauncherView(parentItemPath); return Pitaya.Anim.play(rootEl, 'slide-in-left 250ms ease-in-out'); }) ; }; 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 itemIndex = itemPath[0]; if(itemIndex === undefined) { return rootItem; } if(!('items' in rootItem)) { return undefined; } var subItem = rootItem.items[itemIndex]; if(itemPath.length === 0) { return subItem; } return Pitaya._getItemByPath(itemPath.slice(1), subItem); }; Pitaya._runApp = function(execPath) { return new Promise(function(resolve, reject) { cp.exec(execPath, function(err) { if(err) return reject(err); return resolve(); }); }); }; /** * Load a JSON file * * @private * @param filePath The path of the json file * @return Promise */ Pitaya._loadJSONFile = function(filePath) { 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); } }); }); }; Pitaya._onError = function(err) { console.error(err.stack ? err.stack : err); }; }(window.Pitaya = window.Pitaya || {}, window));