client/general: reduce lodash usages

This commit is contained in:
rr- 2016-05-21 08:07:16 +02:00
parent a32c5d1399
commit bae238794a
3 changed files with 38 additions and 37 deletions

View file

@ -1,6 +1,5 @@
'use strict'; 'use strict';
const lodash = require('lodash');
const views = require('../util/views.js'); const views = require('../util/views.js');
const KEY_TAB = 9; const KEY_TAB = 9;
@ -27,7 +26,8 @@ function _getSelectionStart(input) {
class AutoCompleteControl { class AutoCompleteControl {
constructor(sourceInputNode, options) { constructor(sourceInputNode, options) {
this._sourceInputNode = sourceInputNode; this._sourceInputNode = sourceInputNode;
this._options = lodash.extend({}, { this._options = {};
Object.assign(this._options, {
verticalShift: 2, verticalShift: 2,
source: null, source: null,
maxResults: 15, maxResults: 15,
@ -202,7 +202,9 @@ class AutoCompleteControl {
this._results = this._results =
this._options.getMatches(textToFind) this._options.getMatches(textToFind)
.slice(0, this._options.maxResults); .slice(0, this._options.maxResults);
if (!lodash.isEqual(oldResults, this._results)) { const oldResultsHash = JSON.stringify(oldResults);
const newResultsHash = JSON.stringify(this._results);
if (oldResultsHash !== newResultsHash) {
this._activeResult = -1; this._activeResult = -1;
} }
} }
@ -216,32 +218,30 @@ class AutoCompleteControl {
while (this._suggestionList.firstChild) { while (this._suggestionList.firstChild) {
this._suggestionList.removeChild(this._suggestionList.firstChild); this._suggestionList.removeChild(this._suggestionList.firstChild);
} }
lodash.each( for (let [resultIndex, resultItem] of this._results.entries()) {
this._results, const listItem = document.createElement('li');
(resultItem, resultIndex) => { const link = document.createElement('a');
const listItem = document.createElement('li'); link.href = '#';
const link = document.createElement('a'); link.innerHTML = resultItem.caption;
link.href = '#'; link.setAttribute('data-key', resultItem.value);
link.innerHTML = resultItem.caption; link.addEventListener(
link.setAttribute('data-key', resultItem.value); 'mouseenter',
link.addEventListener( e => {
'mouseenter', e.preventDefault();
e => { this._activeResult = resultIndex;
e.preventDefault(); this._refreshActiveResult();
this._activeResult = resultIndex; });
this._refreshActiveResult(); link.addEventListener(
}); 'mousedown',
link.addEventListener( e => {
'mousedown', e.preventDefault();
e => { this._activeResult = resultIndex;
e.preventDefault(); this._options.confirm(this._getActiveSuggestion());
this._activeResult = resultIndex; this.hide();
this._options.confirm(this._getActiveSuggestion()); });
this.hide(); listItem.appendChild(link);
}); this._suggestionList.appendChild(listItem);
listItem.appendChild(link); }
this._suggestionList.appendChild(listItem);
});
this._refreshActiveResult(); this._refreshActiveResult();
// display the suggestions offscreen to get the height // display the suggestions offscreen to get the height

View file

@ -1,7 +1,6 @@
'use strict'; 'use strict';
const unindent = require('../util/misc.js').unindent; const unindent = require('../util/misc.js').unindent;
const lodash = require('lodash');
const tags = require('../tags.js'); const tags = require('../tags.js');
const AutoCompleteControl = require('./auto_complete_control.js'); const AutoCompleteControl = require('./auto_complete_control.js');
@ -16,13 +15,15 @@ class TagAutoCompleteControl extends AutoCompleteControl {
} }
options.getMatches = text => { options.getMatches = text => {
const regex = new RegExp( const transform = caseSensitive ?
text.length < minLengthForPartialSearch ? x => x :
'^' + lodash.escapeRegExp(text) : x => x.toLowerCase();
lodash.escapeRegExp(text), const match = text.length < minLengthForPartialSearch ?
caseSensitive ? '' : 'i'); (a, b) => a.startsWith(b) :
(a, b) => a.includes(b);
text = transform(text);
return Array.from(allTags.entries()) return Array.from(allTags.entries())
.filter(kv => kv[0].match(regex)) .filter(kv => match(transform(kv[0]), text))
.sort((kv1, kv2) => { .sort((kv1, kv2) => {
return kv2[1].usages - kv1[1].usages; return kv2[1].usages - kv1[1].usages;
}) })

View file

@ -243,7 +243,7 @@ function getTemplate(templatePath) {
if (!ctx) { if (!ctx) {
ctx = {}; ctx = {};
} }
lodash.extend(ctx, { Object.assign(ctx, {
makeRelativeTime: makeRelativeTime, makeRelativeTime: makeRelativeTime,
makeThumbnail: makeThumbnail, makeThumbnail: makeThumbnail,
makeRadio: makeRadio, makeRadio: makeRadio,