pitaya-launcher/js/app.jsx

140 lines
3.4 KiB
React
Raw Normal View History

2015-08-27 18:24:29 +02:00
var React = require('react');
2015-08-27 22:52:30 +02:00
var minimist = require('minimist');
var gui = global.window.require('nw.gui');
var Util = require('./util');
2015-08-27 18:24:29 +02:00
var CategoryHeader = require('./components/category-header.jsx');
var AppList = require('./components/app-list.jsx');
2015-08-27 22:52:30 +02:00
var AnimateMixin = require('./mixins/animate');
// Internal constants
var DEFAULT_PROFILE = './default-profile.json';
var PROCESS_OPTS = minimist(gui.App.argv);
2015-08-27 18:24:29 +02:00
var App = React.createClass({
2015-08-27 22:52:30 +02:00
mixins: [AnimateMixin],
2015-08-27 18:24:29 +02:00
getInitialState: function() {
return {
2015-08-27 22:52:30 +02:00
currentItemPath: '',
2015-08-27 18:24:29 +02:00
currentItem: null
};
},
componentDidMount: function() {
2015-08-27 22:52:30 +02:00
Util.System.loadJSONFile(PROCESS_OPTS.profile || DEFAULT_PROFILE)
.then(function(profile) {
this.setState({ profile: profile, currentItem: profile, currentItemPath: '' });
}.bind(this))
;
2015-08-27 18:24:29 +02:00
},
render: function() {
2015-08-27 22:52:30 +02:00
var currentItem = this.state.currentItem;
var items = currentItem ? currentItem.items : [];
var currentItemPath = this.state.currentItemPath;
var header = currentItemPath !== '' ?
( <CategoryHeader
onBackClick={this.onBackClick}
item={currentItem}
itemPath={currentItemPath} /> ) :
null
;
2015-08-27 18:24:29 +02:00
return (
<div className="launcher">
2015-08-27 22:52:30 +02:00
{header}
<AppList ref="appList" items={items} parentPath={currentItemPath} onItemClick={this.onItemClick} />
2015-08-27 18:24:29 +02:00
</div>
);
2015-08-27 22:52:30 +02:00
},
onBackClick: function(itemPath) {
var parentPath = this._normalizeItemPath(itemPath).slice(0, -1);
var parentItem = this._getItemByPath(parentPath);
this.play(this.refs.appList, 'slide-out-right 250ms ease-in-out')
.then(function() {
this.setState({currentItem: parentItem, currentItemPath: parentPath.join('.')});
return this.play(this.refs.appList, 'slide-in-left 250ms ease-in-out');
}.bind(this))
;
},
onItemClick: function(evt, itemPath, item) {
if(item.exec) {
console.info('Launching application "'+item.exec+'"...');
evt.currentTarget.classList.add('pulse');
Util.System.runApp(item.exec)
.then(function() {
evt.currentTarget.classList.remove('pulse');
})
.catch(function(err) {
evt.currentTarget.classList.remove('pulse');
})
;
} else {
this.play(this.refs.appList, 'slide-out-left 250ms ease-in-out')
.then(function() {
this.setState({ currentItemPath: itemPath, currentItem: item });
return this.play(this.refs.appList, 'slide-in-right 250ms ease-in-out');
}.bind(this))
;
}
},
_getItemByPath: function(itemPath, rootItem) {
rootItem = rootItem || this.state.profile;
itemPath = this._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 this._getItemByPath(itemPath.slice(1), subItem);
},
_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;
}, []);
2015-08-27 18:24:29 +02:00
}
});
var rootEl = document.getElementById('pitaya');
React.render(<App />, rootEl);