2015-09-03 15:50:23 +02:00
|
|
|
var React = require('react');
|
2015-09-04 12:10:08 +02:00
|
|
|
var Util = require('../../util');
|
|
|
|
var LazyLoad = require('../mixins/lazy-load');
|
2015-10-29 16:36:44 +01:00
|
|
|
var logger = Util.Logger;
|
2015-10-19 13:24:05 +02:00
|
|
|
var _ = require('lodash');
|
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],
|
|
|
|
|
2015-10-19 13:24:05 +02:00
|
|
|
componentWillMount: function() {
|
|
|
|
if(!this._findIconDebounced) {
|
|
|
|
this._findIconDebounced = _.debounce(this._findIcon, 250);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-09-03 15:50:23 +02:00
|
|
|
getInitialState: function() {
|
2015-10-19 09:02:10 +02:00
|
|
|
return { icon: DEFAULT_ICON, iconPath: DEFAULT_ICON, currentTheme: '' };
|
2015-09-03 15:50:23 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
onInViewport: function() {
|
|
|
|
this.updateIconIfInViewport();
|
|
|
|
},
|
|
|
|
|
|
|
|
updateIconIfInViewport: function() {
|
|
|
|
|
2015-10-19 09:02:10 +02:00
|
|
|
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;
|
|
|
|
|
2015-10-19 09:02:10 +02:00
|
|
|
var shouldUpdate = this.isInViewport() &&
|
|
|
|
( (newTheme && newTheme !== currentTheme) || (newIcon && newIcon !== currentIcon) )
|
|
|
|
;
|
2015-09-03 15:50:23 +02:00
|
|
|
|
2015-10-19 09:02:10 +02:00
|
|
|
if( !shouldUpdate ) return;
|
2015-09-03 15:50:23 +02:00
|
|
|
|
2015-10-19 09:02:10 +02:00
|
|
|
this.setState({ icon: newIcon, iconPath: LOADING_ICON, currentTheme: newTheme });
|
2015-09-03 15:50:23 +02:00
|
|
|
|
2015-10-19 13:24:05 +02:00
|
|
|
this._findIconDebounced(newIcon, newTheme);
|
2015-09-03 15:50:23 +02:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
componentDidUpdate: function() {
|
|
|
|
this.updateIconIfInViewport();
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
|
2015-10-19 09:02:10 +02:00
|
|
|
var icon = this.state.iconPath;
|
2015-09-03 15:50:23 +02:00
|
|
|
|
2015-10-16 15:12:49 +02:00
|
|
|
var style = {
|
|
|
|
backgroundImage: 'url('+icon+')'
|
|
|
|
};
|
|
|
|
|
2015-09-03 15:50:23 +02:00
|
|
|
return (
|
2015-10-16 15:12:49 +02:00
|
|
|
<div className="app-icon" style={style}></div>
|
2015-09-03 15:50:23 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
_findIcon: function(iconPath, theme) {
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2015-10-29 16:36:44 +01:00
|
|
|
logger.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) {
|
2015-10-19 09:02:10 +02:00
|
|
|
if(!iconPath) return DEFAULT_ICON;
|
2015-10-16 15:12:49 +02:00
|
|
|
return Util.System.exists(iconPath)
|
|
|
|
.then(function(exists) {
|
|
|
|
return exists ? iconPath : DEFAULT_ICON;
|
|
|
|
})
|
|
|
|
;
|
|
|
|
})
|
2015-09-03 15:50:23 +02:00
|
|
|
.then(function(iconPath) {
|
2015-10-29 16:36:44 +01:00
|
|
|
logger.debug('Found icon %s', iconPath);
|
2015-10-19 09:02:10 +02:00
|
|
|
self.setState({ iconPath: iconPath });
|
2015-09-03 15:50:23 +02:00
|
|
|
})
|
|
|
|
;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|