Add local default icon + implements files search cache

This commit is contained in:
2015-09-02 14:26:23 +02:00
parent 99c60aae12
commit 411894586b
7 changed files with 84 additions and 13 deletions

View File

@ -3,6 +3,7 @@ var Util = require('../util');
var LazyLoad = require('./mixins/lazy-load');
var LOADING_ICON = 'img/hourglass.svg';
var DEFAULT_ICON = 'img/default-icon.svg';
module.exports = React.createClass({
@ -53,7 +54,6 @@ module.exports = React.createClass({
_findIcon: function(iconPath, theme) {
var self = this;
var DEFAULT_ICON = 'application-default-icon';
console.log('Search icon %s:%s', iconPath, theme);

View File

@ -34,6 +34,10 @@ module.exports = React.createClass({
);
});
options.unshift(
<option key="__none__"></option>
);
return (
<select value={selectedTheme} onChange={this.onChange}>
{options}

View File

@ -1,5 +1,7 @@
var React = require('react');
var _listeners = [];
module.exports = {
isInViewport: function() {
@ -10,35 +12,54 @@ module.exports = {
var rect = el.getBoundingClientRect();
var viewportHeight = global.window.innerHeight || global.document.documentElement.clientHeight;
var viewportWidth = global.window.innerWidth || global.document.documentElement.clientWidth;
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (global.window.innerHeight || global.document.documentElement.clientHeight) && /*or $(window).height() */
rect.right <= (global.window.innerWidth || global.document.documentElement.clientWidth) /*or $(window).width() */
rect.bottom >= 0 &&
rect.right >= 0 &&
rect.top <= viewportHeight &&
rect.left <= viewportWidth
);
},
componentDidMount: function() {
function _onInViewport(){
if(typeof this.onInViewport === 'function') {
_listeners.push(this);
if( this.isInViewport() ) {
this.onInViewport();
}
}
var el = React.findDOMNode(this);
if(typeof this.onInViewport === 'function') {
window.document.addEventListener('scroll', debounce(_onInViewport.bind(this), 250), true);
}
_onInViewport.call(this);
},
componentWillUnmount: function() {
var index = _listeners.indexOf(this);
if(index !== -1) return _listeners.splice(index, 1);
}
};
var computeComponentsVisibilityDebounced = debounce(computeComponentsVisibility, 250);
// Start listening for changes
window.document.addEventListener('scroll', computeComponentsVisibilityDebounced, true);
window.addEventListener('resize', computeComponentsVisibilityDebounced);
function computeComponentsVisibility() {
_listeners.forEach(function(listener) {
if( listener.isInViewport() ) {
listener.onInViewport();
}
});
}
function debounce(func, wait, immediate) {
var timeout;
return function() {

33
js/util/cache.js Normal file
View File

@ -0,0 +1,33 @@
function Cache() {
this._store = {};
}
Cache.prototype.get = function(key) {
key = this._serialize(key);
return key in this._store ? this._store[key] : undefined;
};
Cache.prototype.set = function(key, value) {
key = this._serialize(key);
this._store[key] = value;
return this;
};
Cache.prototype._serialize = function(mixedKey) {
var json = JSON.stringify(mixedKey);
return this._hash(json);
};
Cache.prototype._hash = function(str) {
var hash = 0, i, chr, len;
if (str.length === 0) return hash;
for (i = 0, len = str.length; i < len; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
module.exports = Cache;

View File

@ -1,2 +1,3 @@
exports.System = require('./system');
exports.DesktopApps = require('./desktop-apps');
exports.Cache = require('./cache');

View File

@ -2,6 +2,7 @@ var fs = require('fs');
var cp = require('child_process');
var glob = require('glob');
var ini = require('ini');
var Cache = require('./cache');
/**
* Load a JSON file
@ -52,12 +53,22 @@ exports.runApp = function(execPath) {
});
};
var _searchCache = new Cache();
exports.findFiles = function(pattern, opts) {
return new Promise(function(resolve, reject) {
var cachedResult = _searchCache.get([pattern, opts]);
if( cachedResult !== undefined) {
return resolve(cachedResult);
}
glob(pattern, opts, function(err, files) {
if(err) return reject(err);
_searchCache.set([pattern, opts], files);
return resolve(files);
});
});
};