2015-08-28 14:20:07 +02:00
|
|
|
var React = require('react');
|
|
|
|
var CategoryHeader = require('./category-header.jsx');
|
|
|
|
var AppList = require('./app-list.jsx');
|
2015-09-04 12:10:08 +02:00
|
|
|
var AnimateMixin = require('../mixins/animate');
|
2015-09-11 16:25:45 +02:00
|
|
|
var actions = require('../../store/actions');
|
2015-09-04 12:10:08 +02:00
|
|
|
var connect = require('react-redux').connect;
|
|
|
|
var debug = require('../../util/debug')('launcher-view');
|
2015-08-28 14:20:07 +02:00
|
|
|
|
2015-09-04 12:10:08 +02:00
|
|
|
var DEFAULT_PROFILE = './default-profile.json';
|
2015-08-28 14:20:07 +02:00
|
|
|
|
2015-09-04 12:10:08 +02:00
|
|
|
var LauncherView = React.createClass({
|
2015-08-28 14:20:07 +02:00
|
|
|
|
2015-09-04 12:10:08 +02:00
|
|
|
mixins: [AnimateMixin],
|
2015-08-28 14:20:07 +02:00
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
currentItemPath: '',
|
|
|
|
currentItem: null
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount: function() {
|
2015-09-04 12:10:08 +02:00
|
|
|
var profilePath = this.props.processOpts.profile || DEFAULT_PROFILE;
|
2015-09-17 17:29:59 +02:00
|
|
|
this.props.dispatch(actions.common.loadProfile(profilePath));
|
2015-09-04 12:10:08 +02:00
|
|
|
},
|
2015-08-28 14:20:07 +02:00
|
|
|
|
2015-09-04 12:10:08 +02:00
|
|
|
componentWillReceiveProps: function(nextProps) {
|
|
|
|
if( nextProps.profile && !this.state.currentItem ) {
|
|
|
|
this.setState({ currentItem: nextProps.profile });
|
|
|
|
}
|
2015-08-28 14:20:07 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
|
|
|
|
var currentItem = this.state.currentItem;
|
2015-09-04 12:10:08 +02:00
|
|
|
var items = currentItem && currentItem.items ? currentItem.items : [];
|
2015-08-28 14:20:07 +02:00
|
|
|
var currentItemPath = this.state.currentItemPath;
|
|
|
|
|
|
|
|
var header = currentItemPath !== '' ?
|
|
|
|
( <CategoryHeader
|
|
|
|
onBackClick={this.onBackClick}
|
|
|
|
item={currentItem}
|
|
|
|
itemPath={currentItemPath} /> ) :
|
|
|
|
null
|
|
|
|
;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="launcher">
|
|
|
|
{header}
|
|
|
|
<AppList ref="appList"
|
|
|
|
items={items}
|
|
|
|
parentPath={currentItemPath}
|
|
|
|
onItemClick={this.onItemClick} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
onBackClick: function(itemPath) {
|
|
|
|
|
|
|
|
var parentPath = this._normalizeItemPath(itemPath).slice(0, -1);
|
2015-09-04 12:10:08 +02:00
|
|
|
var parentItem = this._getItemByPath(parentPath, this.props.profile);
|
2015-08-28 14:20:07 +02:00
|
|
|
|
|
|
|
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) {
|
|
|
|
|
2015-09-04 12:10:08 +02:00
|
|
|
debug('Launching application "'+item.exec+'"...');
|
|
|
|
var el = evt.currentTarget;
|
|
|
|
el.classList.add('pulse');
|
2015-08-28 14:20:07 +02:00
|
|
|
|
2015-09-04 12:10:08 +02:00
|
|
|
this.props.dispatch(actions.launcher.runApp(item.exec))
|
2015-08-28 14:20:07 +02:00
|
|
|
.then(function() {
|
2015-09-04 12:10:08 +02:00
|
|
|
el.classList.remove('pulse');
|
2015-08-28 14:20:07 +02:00
|
|
|
})
|
2015-09-04 12:10:08 +02:00
|
|
|
.catch(function() {
|
|
|
|
el.classList.remove('pulse');
|
2015-08-28 14:20:07 +02:00
|
|
|
})
|
|
|
|
;
|
|
|
|
|
|
|
|
} 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) {
|
|
|
|
|
|
|
|
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-09-04 12:10:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
function select(state) {
|
|
|
|
return {
|
|
|
|
processOpts: state.processOpts,
|
|
|
|
profile: state.profile
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = connect(select)(LauncherView);
|