Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
6fb47427
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
6fb47427
编写于
11月 09, 2016
作者:
C
Clement Ho
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor tokenizer
上级
3845bf37
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
115 addition
and
95 deletion
+115
-95
app/assets/javascripts/filtered_search/filtered_search_manager.js.es6
...avascripts/filtered_search/filtered_search_manager.js.es6
+25
-95
app/assets/javascripts/filtered_search/filtered_search_tokenizer.es6
...javascripts/filtered_search/filtered_search_tokenizer.es6
+90
-0
未找到文件。
app/assets/javascripts/filtered_search/filtered_search_manager.js.es6
浏览文件 @
6fb47427
...
...
@@ -18,13 +18,21 @@
param: 'name[]',
}];
function clearSearch(event) {
event.stopPropagation();
event.preventDefault();
document.querySelector('.filtered-search').value = '';
document.querySelector('.clear-search').classList.add('hidden');
}
function toggleClearSearchButton(event) {
const clearSearch = document.querySelector('.clear-search');
const clearSearch
Button
= document.querySelector('.clear-search');
if (event.target.value) {
clearSearch.classList.remove('hidden');
clearSearch
Button
.classList.remove('hidden');
} else {
clearSearch.classList.add('hidden');
clearSearch
Button
.classList.add('hidden');
}
}
...
...
@@ -74,105 +82,24 @@
class FilteredSearchManager {
constructor() {
this.tokenizer = new gl.FilteredSearchTokenizer(validTokenKeys);
this.bindEvents();
loadSearchParamsFromURL();
this.clearTokens();
}
bindEvents() {
const input = document.querySelector('.filtered-search');
const clearSearch = document.querySelector('.clear-search');
input.addEventListener('input', this.tokenize.bind(this));
input.addEventListener('input', toggleClearSearchButton);
input.addEventListener('keydown', this.checkForEnter.bind(this));
const filteredSearchInput = document.querySelector('.filtered-search');
clearSearch.addEventListener('click', this.clearSearch.bind(this));
}
clearSearch(event) {
event.stopPropagation();
event.preventDefault();
this.clearTokens();
document.querySelector('.filtered-search').value = '';
document.querySelector('.clear-search').classList.add('hidden');
}
filteredSearchInput.addEventListener('input', this.processInput.bind(this));
filteredSearchInput.addEventListener('input', toggleClearSearchButton);
filteredSearchInput.addEventListener('keydown', this.checkForEnter.bind(this));
clearTokens() {
this.tokens = [];
this.searchToken = '';
document.querySelector('.clear-search').addEventListener('click', clearSearch);
}
tokenize(event) {
// Re-calculate tokens
this.clearTokens();
processInput(event) {
const input = event.target.value;
const inputs = input.split(' ');
let searchTerms = '';
let lastQuotation = '';
let incompleteToken = false;
const addSearchTerm = function addSearchTerm(term) {
// Add space for next term
searchTerms += `${term} `;
};
inputs.forEach((i) => {
if (incompleteToken) {
const prevToken = this.tokens[this.tokens.length - 1];
prevToken.value += ` ${i}`;
// Remove last quotation
const lastQuotationRegex = new RegExp(lastQuotation, 'g');
prevToken.value = prevToken.value.replace(lastQuotationRegex, '');
this.tokens[this.tokens.length - 1] = prevToken;
// Check to see if this quotation completes the token value
if (i.indexOf(lastQuotation)) {
incompleteToken = !incompleteToken;
}
return;
}
const colonIndex = i.indexOf(':');
if (colonIndex !== -1) {
const tokenKey = i.slice(0, colonIndex).toLowerCase();
const tokenValue = i.slice(colonIndex + 1);
const match = validTokenKeys.find(v => v.key === tokenKey);
if (tokenValue.indexOf('"') !== -1) {
lastQuotation = '"';
incompleteToken = true;
} else if (tokenValue.indexOf('\'') !== -1) {
lastQuotation = '\'';
incompleteToken = true;
}
if (match && tokenValue.length > 0) {
this.tokens.push({
key: match.key,
value: tokenValue,
});
} else {
addSearchTerm(i);
}
} else {
addSearchTerm(i);
}
}, this);
this.searchToken = searchTerms.trim();
this.printTokens();
}
printTokens() {
console.log('tokens:');
this.tokens.forEach(token => console.log(token));
console.log(`search: ${this.searchToken}`);
this.tokenizer.processTokens(input);
}
checkForEnter(event) {
...
...
@@ -193,6 +120,9 @@
const defaultState = 'opened';
let currentState = defaultState;
const tokens = this.tokenizer.getTokens();
const searchToken = this.tokenizer.getSearchToken();
if (stateIndex !== -1) {
const remaining = currentPath.slice(stateIndex + 6);
const separatorIndex = remaining.indexOf('&');
...
...
@@ -201,13 +131,13 @@
}
path += `&state=${currentState}`;
t
his.t
okens.forEach((token) => {
tokens.forEach((token) => {
const param = validTokenKeys.find(t => t.key === token.key).param;
path += `&${token.key}_${param}=${encodeURIComponent(token.value)}`;
});
if (
this.
searchToken) {
path += `&search=${encodeURIComponent(
this.
searchToken)}`;
if (searchToken) {
path += `&search=${encodeURIComponent(searchToken)}`;
}
window.location = path;
...
...
app/assets/javascripts/filtered_search/filtered_search_tokenizer.es6
0 → 100644
浏览文件 @
6fb47427
/* eslint-disable no-param-reassign */
((global) => {
class FilteredSearchTokenizer {
constructor(validTokenKeys) {
this.validTokenKeys = validTokenKeys;
this.resetTokens();
}
getTokens() {
return this.tokens;
}
getSearchToken() {
return this.searchToken;
}
resetTokens() {
this.tokens = [];
this.searchToken = '';
}
printTokens() {
console.log('tokens:');
this.tokens.forEach(token => console.log(token));
console.log(`search: ${this.searchToken}`);
}
processTokens(input) {
// Re-calculate tokens
this.resetTokens();
const inputs = input.split(' ');
let searchTerms = '';
let lastQuotation = '';
let incompleteToken = false;
inputs.forEach((i) => {
if (incompleteToken) {
const prevToken = this.tokens[this.tokens.length - 1];
prevToken.value += ` ${i}`;
// Remove last quotation
const lastQuotationRegex = new RegExp(lastQuotation, 'g');
prevToken.value = prevToken.value.replace(lastQuotationRegex, '');
this.tokens[this.tokens.length - 1] = prevToken;
// Check to see if this quotation completes the token value
if (i.indexOf(lastQuotation)) {
incompleteToken = !incompleteToken;
}
return;
}
const colonIndex = i.indexOf(':');
if (colonIndex !== -1) {
const tokenKey = i.slice(0, colonIndex).toLowerCase();
const tokenValue = i.slice(colonIndex + 1);
const match = this.validTokenKeys.find(v => v.key === tokenKey);
if (tokenValue.indexOf('"') !== -1) {
lastQuotation = '"';
incompleteToken = true;
} else if (tokenValue.indexOf('\'') !== -1) {
lastQuotation = '\'';
incompleteToken = true;
}
if (match && tokenValue.length > 0) {
this.tokens.push({
key: match.key,
value: tokenValue,
});
return;
}
}
// Add space for next term
searchTerms += `${i} `;
}, this);
this.searchToken = searchTerms.trim();
this.printTokens();
}
}
global.FilteredSearchTokenizer = FilteredSearchTokenizer;
})(window.gl || (window.gl = {}));
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录