pitaya-launcher/src/components/common/app-icon.js

82 lines
1.8 KiB
JavaScript
Raw Normal View History

2015-09-03 15:50:23 +02:00
var React = require('react');
var Util = require('../../util');
var LazyLoad = require('../mixins/lazy-load');
var debug = Util.Debug('common:app-icon');
2015-09-03 15:50:23 +02:00
var LOADING_ICON = 'img/hourglass.svg';
var DEFAULT_ICON = 'img/default-icon.svg';
module.exports = React.createClass({
mixins: [LazyLoad],
getInitialState: function() {
return { icon: DEFAULT_ICON, iconPath: DEFAULT_ICON, currentTheme: '' };
2015-09-03 15:50:23 +02:00
},
onInViewport: function() {
this.updateIconIfInViewport();
},
updateIconIfInViewport: function() {
var currentIcon = this.state.icon;
var newIcon = this.props.icon;
2015-09-03 15:50:23 +02:00
var currentTheme = this.state.currentTheme;
var newTheme = this.props.theme;
var shouldUpdate = this.isInViewport() &&
( (newTheme && newTheme !== currentTheme) || (newIcon && newIcon !== currentIcon) )
;
2015-09-03 15:50:23 +02:00
if( !shouldUpdate ) return;
2015-09-03 15:50:23 +02:00
this.setState({ icon: newIcon, iconPath: LOADING_ICON, currentTheme: newTheme });
2015-09-03 15:50:23 +02:00
this._findIcon(newIcon, newTheme);
2015-09-03 15:50:23 +02:00
},
componentDidUpdate: function() {
this.updateIconIfInViewport();
},
render: function() {
var icon = this.state.iconPath;
2015-09-03 15:50:23 +02:00
var style = {
backgroundImage: 'url('+icon+')'
};
2015-09-03 15:50:23 +02:00
return (
<div className="app-icon" style={style}></div>
2015-09-03 15:50:23 +02:00
);
},
_findIcon: function(iconPath, theme) {
var self = this;
debug('Search icon %s:%s', iconPath, theme);
2015-09-03 15:50:23 +02:00
Util.DesktopApps.findIcon(iconPath || DEFAULT_ICON, theme)
.then(function(iconPath) {
if(!iconPath) return DEFAULT_ICON;
return Util.System.exists(iconPath)
.then(function(exists) {
return exists ? iconPath : DEFAULT_ICON;
})
;
})
2015-09-03 15:50:23 +02:00
.then(function(iconPath) {
debug('Found icon %s', iconPath);
self.setState({ iconPath: iconPath });
2015-09-03 15:50:23 +02:00
})
;
}
});