Skip to content

Commit

Permalink
Added priority and isfallback properties to web search interface
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Dec 22, 2018
1 parent af7cd91 commit 859e492
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 99 deletions.
25 changes: 6 additions & 19 deletions main.html
Original file line number Diff line number Diff line change
Expand Up @@ -217,25 +217,6 @@ <h1 class="setting-section-title">User Settings</h1>
</table>
</div>
</div>
<div class="setting-group" v-if="config.features.webSearch">
<div class="setting-title">Fallback web searches</div>
<div class="setting">
<div class="sub-setting" v-for="fallbackWebSearch in config.fallbackWebSearches">
<input disabled class="setting-text-input" type="text" v-model="fallbackWebSearch">
<svg class="setting-action-icon sub-setting-icon" v-on:click="settingsActionRemoveFallbackWebSearch(fallbackWebSearch)" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 26 26" version="1.1">
<g id="surface1">
<path d="M 13.5 3.1875 C 7.804688 3.1875 3.1875 7.804688 3.1875 13.5 C 3.1875 19.195313 7.804688 23.8125 13.5 23.8125 C 19.195313 23.8125 23.8125 19.195313 23.8125 13.5 C 23.8125 7.804688 19.195313 3.1875 13.5 3.1875 Z M 19 15 L 8 15 L 8 12 L 19 12 Z "></path>
</g>
</svg>
</div>
<div v-if="config.fallbackWebSearches.length !== config.webSearches.length" class="sub-setting">
<select class="setting-select" v-model="configEdit.newFallbackWebSearch" v-on:change="settingsActionAddFallbackWebSearch(configEdit.newFallbackWebSearch)">
<option selected value="">Select a web search engine</option>
<option v-for="webSearch in getUnusedFallbackWebSearches()" :value="webSearch.name">{{ webSearch.name }}</option>
</select>
</div>
</div>
</div>
<div class="setting-group">
<div class="setting-title">Features</div>
<div class="setting">
Expand Down Expand Up @@ -593,6 +574,8 @@ <h1 class="setting-section-title">User Settings</h1>
<th>Name</th>
<th>Prefix</th>
<th>Url</th>
<th>Priority</th>
<th class="text-center">Fallback</th>
<th>Icon</th>
<th class="text-center">Preview</th>
<th class="text-center">+/-</th>
Expand All @@ -603,6 +586,8 @@ <h1 class="setting-section-title">User Settings</h1>
<td><input class="setting-text-input" type="text" v-model="webSearch.name" v-on:blur="updateUserConfig"></td>
<td><input class="setting-text-input" type="text" v-model="webSearch.prefix" v-on:blur="updateUserConfig"></td>
<td><input class="setting-text-input" type="text" v-model="webSearch.url" v-on:blur="updateUserConfig"></td>
<td><input class="setting-text-input" type="number" min="0" v-model="webSearch.priority" v-on:blur="updateUserConfig"></td>
<td class="text-center"><input type="checkbox" v-model="webSearch.isFallback" v-on:blur="updateUserConfig"></td>
<td><input class="setting-text-input" type="text" v-model="webSearch.icon" v-on:blur="updateUserConfig"></td>
<td class="text-center" v-html="webSearch.icon"></td>
<td class="text-center">
Expand All @@ -617,6 +602,8 @@ <h1 class="setting-section-title">User Settings</h1>
<td><input type="text" class="setting-text-input" v-model="configEdit.newWebSearch.name" placeholder="Name"></td>
<td><input type="text" class="setting-text-input" v-model="configEdit.newWebSearch.prefix" placeholder="Prefix"></td>
<td><input type="text" class="setting-text-input" v-model="configEdit.newWebSearch.url" placeholder="URL"></td>
<td><input type="number" class="setting-text-input" min="0" v-model="configEdit.newWebSearch.priority" placeholder="Priority"></td>
<td class="text-center"><input type="checkbox" v-model="configEdit.newWebSearch.isFallback"></td>
<td><input type="text" class="setting-text-input" v-model="configEdit.newWebSearch.icon" placeholder="Icon"></td>
<td class="text-center" v-html="configEdit.newWebSearch.icon"></td>
<td class="text-center">
Expand Down
40 changes: 1 addition & 39 deletions src/tests/unit/input-validation-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { InputValidatorSearcherCombination } from "../../ts/input-validator-sear

describe(InputValidationService.name, (): void => {
const config = {
fallbackWebSearches: [] as string[],
webSearches: [] as WebSearch[],
} as UserConfigOptions;

Expand Down Expand Up @@ -150,53 +149,16 @@ describe(InputValidationService.name, (): void => {
expect(actual.length).toBe(0);
});

it("should return an emtpy array if defined fallback web search does not match any web searches", (): void => {
const fallBackWebSearchName = "DuckDuckGo";
const webSearchName = "Google";
const fallBackWebSearchUrl = "https://google.com/search?q=";
const userInput = "something";
const webSearch = {
name: webSearchName,
url: fallBackWebSearchUrl,
} as WebSearch;

config.fallbackWebSearches = [fallBackWebSearchName];
config.webSearches = [webSearch];

const shouldBlockOtherSearchers = false;

const combinations: InputValidatorSearcherCombination[] = [
{
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 1" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
{
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 2" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
{
searcher: new FakeSearcher(shouldBlockOtherSearchers, [{ name: "Search Result 3" }] as SearchResultItem[]),
validator: new FakeInputValidator(false),
},
];

const inputValidationService = new InputValidationService(config, combinations);

const actual = inputValidationService.getSearchResult(userInput);

expect(actual.length).toBe(0);
});

it("should return a search result for each fallback search result if user input does not match any searcher", (): void => {
const fallBackWebSearchName = "Google";
const fallBackWebSearchUrl = "https://google.com/search?q=";
const userInput = "something";
const webSearch = {
isFallback: true,
name: fallBackWebSearchName,
url: fallBackWebSearchUrl,
} as WebSearch;

config.fallbackWebSearches = [fallBackWebSearchName];
config.webSearches = [webSearch];

const shouldBlockOtherSearchers = false;
Expand Down
65 changes: 63 additions & 2 deletions src/tests/unit/searcher/fallback-web-search-searcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,71 @@
import { FallbackWebSearchSercher } from "../../../ts/searcher/fallback-web-search-searcher";
import { WebSearch } from "../../../ts/web-search";
import { WebSearchBuilder } from "../../../ts/builders/web-search-builder";

describe(FallbackWebSearchSercher.name, (): void => {
const searcher = new FallbackWebSearchSercher([], []);

it("should not block other searchers", (): void => {
const searcher = new FallbackWebSearchSercher([]);
const actual = searcher.blockOthers;
expect(actual).toBe(false);
});

it("should return a search result for earch fallback web searcher", (): void => {
const webSearches: WebSearch[] = [
{
icon: "",
isFallback: false,
name: "non fallback",
prefix: "n",
priority: 1,
url: "url",
},
{
icon: "",
isFallback: true,
name: "fallback",
prefix: "f",
priority: 2,
url: "url",
},
];

const userInput = "user-input";

const searcher = new FallbackWebSearchSercher(webSearches);
const actual = searcher.getSearchResult(userInput);

expect(actual.length).toBe(1);
});

it("should sort the result by priority", () => {
const webSearches = [
{ isFallback: true, name: "asdf", priority: 4 },
{ isFallback: true, name: "asdf", priority: 0 },
{ isFallback: true, name: "asdf", priority: 3 },
{ isFallback: true, name: "asdf", priority: 2 },
{ isFallback: true, name: "asdf", priority: 1 },
{ isFallback: true, name: "asdf", priority: 4 },
{ isFallback: true, name: "asdf", priority: 0 },
{ isFallback: true, name: "asdf", priority: -1 },
] as WebSearch[];

const userInput = "asdf";

const searcher = new FallbackWebSearchSercher(webSearches);
const actual = searcher.getSearchResult(userInput);
expect(actual.length).toBe(webSearches.length);

const sortedWebSearches = webSearches.sort((a, b) => {
if (a.priority < b.priority) {
return 1;
} else if (a.priority > b.priority) {
return -1;
}
return 0;
});

for (let i = 0; i < actual.length; i++) {
expect(actual[i].name).toBe(WebSearchBuilder.buildSearchResultItem(userInput, sortedWebSearches[i]).name);
}
});
});
4 changes: 2 additions & 2 deletions src/ts/input-validation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class InputValidationService {
}
}

if (result.length === 0 && this.configOptions.fallbackWebSearches.length > 0) {
const fallBackWebSearchSearcher = new FallbackWebSearchSercher(this.configOptions.fallbackWebSearches, this.configOptions.webSearches);
if (result.length === 0 && this.configOptions.webSearches.filter((w) => w.isFallback).length > 0) {
const fallBackWebSearchSearcher = new FallbackWebSearchSercher(this.configOptions.webSearches);
result = fallBackWebSearchSearcher.getSearchResult(userInput);
}

Expand Down
20 changes: 0 additions & 20 deletions src/ts/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const configEdit = {
newApplicationFileExtension: "",
newApplicationFolder: "",
newCustomCommand: {},
newFallbackWebSearch: "",
newFileSearchBlackListEntry: "",
newFileSearchOption: {} as FileSearchOption,
newShortcut: {} as Shortcut,
Expand Down Expand Up @@ -59,13 +58,6 @@ const vue = new Vue({
colorTheme: (): string => {
return `./styles/${config.colorTheme}.css`;
},
getUnusedFallbackWebSearches: (): WebSearch[] => {
return config.webSearches.filter((w): boolean => {
return config.fallbackWebSearches.filter((f): boolean => {
return f === w.name;
}).length === 0;
});
},
handleCheckForUpdateButtonClick: (): void => {
ipcRenderer.send(IpcChannels.ueliCheckForUpdates);
},
Expand Down Expand Up @@ -173,13 +165,6 @@ const vue = new Vue({
vue.updateUserConfig();
configEdit.newCustomCommand = {};
},
settingsActionAddFallbackWebSearch: (newFallbackWebSearch: string): void => {
if (newFallbackWebSearch.length > 0) {
config.fallbackWebSearches.push(newFallbackWebSearch);
vue.updateUserConfig();
configEdit.newFallbackWebSearch = "";
}
},
settingsActionAddFileSearchBlackListEntry: (newFileSearchBlackListEntry: string): void => {
config.fileSearchBlackList.push(newFileSearchBlackListEntry);
vue.updateUserConfig();
Expand Down Expand Up @@ -215,11 +200,6 @@ const vue = new Vue({
config.customCommands.splice(indexToRemove, 1);
vue.updateUserConfig();
},
settingsActionRemoveFallbackWebSearch: (fallbackWebSearch: string): void => {
const indexToRemove = config.fallbackWebSearches.indexOf(fallbackWebSearch);
config.fallbackWebSearches.splice(indexToRemove, 1);
vue.updateUserConfig();
},
settingsActionRemoveFileSearchBlackListEntry: (blackListEntry: string): void => {
const indexToRemove = config.fileSearchBlackList.indexOf(blackListEntry);
config.fileSearchBlackList.splice(indexToRemove, 1);
Expand Down
25 changes: 15 additions & 10 deletions src/ts/searcher/fallback-web-search-searcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@ import { WebSearchBuilder } from "../builders/web-search-builder";

export class FallbackWebSearchSercher implements Searcher {
public readonly blockOthers = false;

private readonly fallbackWebSearches: string[];
private readonly webSearches: WebSearch[];

constructor(fallbackWebSearches: string[], webSearches: WebSearch[]) {
this.fallbackWebSearches = fallbackWebSearches;
constructor(webSearches: WebSearch[]) {
this.webSearches = webSearches;
}

public getSearchResult(userInput: string): SearchResultItem[] {
const result = [] as SearchResultItem[];
const fallbackWebSearches = this.webSearches.filter((w) => w.isFallback);
const sortedFallbackWebSearches = fallbackWebSearches.sort(this.sortByPriority);

for (const fallBackWebSearchName of this.fallbackWebSearches) {
for (const webSearch of this.webSearches) {
if (fallBackWebSearchName === webSearch.name) {
result.push(WebSearchBuilder.buildSearchResultItem(userInput, webSearch));
}
}
for (const webSearch of sortedFallbackWebSearches) {
result.push(WebSearchBuilder.buildSearchResultItem(userInput, webSearch));
}

return result;
}

private sortByPriority(a: WebSearch, b: WebSearch): number {
if (a.priority < b.priority) {
return 1;
} else if (a.priority > b.priority) {
return -1;
} else {
return 0;
}
}
}
1 change: 0 additions & 1 deletion src/ts/user-config/default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export class DefaultUserConfigManager {
autoStartApp: true,
colorTheme: "dark",
customCommands: [],
fallbackWebSearches: [],
features: defaultFeatures,
fileSearchBlackList: [
"vendor",
Expand Down
12 changes: 12 additions & 0 deletions src/ts/user-config/default-web-searches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export const defaultWebSearches: WebSearch[] = [
<path style="fill:#689F38;" d="M22.5,35.7c0,1.5-0.2,2.1,0.4,2.3c0.6,0.1,1.9,0,2.3-0.3c0.4-0.3,0.1-2.2-0.1-2.6 C25,34.8,22.5,35.1,22.5,35.7"></path>
<path style="fill:#FFCA28;" d="M22.3,26.8c0.1-0.7,2-2.1,3.3-2.2c1.3-0.1,1.7-0.1,2.8-0.3c1.1-0.3,3.9-1,4.7-1.3 c0.8-0.4,4.1,0.2,1.8,1.5c-1,0.6-3.7,1.6-5.7,2.2c-1.9,0.6-3.1-0.6-3.8,0.4c-0.5,0.8-0.1,1.8,2.2,2c3.1,0.3,6.2-1.4,6.5-0.5 c0.3,0.9-2.7,2-4.6,2.1c-1.8,0-5.6-1.2-6.1-1.6C22.9,28.7,22.2,27.8,22.3,26.8"></path>
</svg>`,
isFallback: false,
name: "DuckDuckGo",
prefix: "d",
priority: 1,
url: "https://duckduckgo.com/?q=",
},
{
Expand All @@ -24,8 +26,10 @@ export const defaultWebSearches: WebSearch[] = [
<path style=" fill:#1976D2;" d="M 43.609375 20.082031 L 42 20.082031 L 42 20 L 24 20 L 24 28 L 35.304688 28 C 34.511719 30.238281 33.070313 32.164063 31.214844 33.570313 C 31.21875 33.570313 31.21875 33.570313 31.21875 33.570313 L 37.410156 38.808594 C 36.972656 39.203125 44 34 44 24 C 44 22.660156 43.863281 21.351563 43.609375 20.082031 Z "></path>
</g>
</svg>`,
isFallback: false,
name: "Google",
prefix: "g",
priority: 2,
url: "https://google.com/search?q=",
},
{
Expand All @@ -41,8 +45,10 @@ export const defaultWebSearches: WebSearch[] = [
<path style=" fill:#1565C0;" d="M 24 37 L 24 30.0625 C 24 28.628906 24.5 27.308594 25.332031 26.269531 L 36 37 Z "></path>
</g>
</svg>`,
isFallback: false,
name: "Google Images",
prefix: "gi",
priority: 3,
url: "https://www.google.com/search?tbm=isch&q=",
},
{
Expand Down Expand Up @@ -72,8 +78,10 @@ export const defaultWebSearches: WebSearch[] = [
<path style="fill:#788B9C;" d="M7,21v-2c8.7,0,10.9-8.9,11-9.2l1.9,0.5C19.9,10.3,17.3,21,7,21z"></path>
<path style="fill:#788B9C;" d="M18,19c-6,0-9.7-5.2-9.8-5.4l1.6-1.1c0,0,3.2,4.6,8.2,4.6V19z"></path>
</svg>`,
isFallback: false,
name: "Linguee",
prefix: "l",
priority: 4,
url: "http://www.linguee.de/deutsch-englisch/search?source=auto&query=",
},
{
Expand All @@ -82,8 +90,10 @@ export const defaultWebSearches: WebSearch[] = [
<path d="M 30 9.214844 C 30 9.386719 29.863281 9.523438 29.6875 9.523438 L 28.007813 9.523438 L 20.390625 25.738281 C 20.339844 25.847656 20.230469 25.917969 20.113281 25.917969 C 20.109375 25.917969 20.109375 25.917969 20.109375 25.917969 C 19.988281 25.917969 19.882813 25.851563 19.828125 25.746094 L 16.214844 18.578125 L 12.3125 25.757813 C 12.257813 25.859375 12.15625 25.917969 12.03125 25.917969 C 11.914063 25.914063 11.808594 25.847656 11.757813 25.742188 L 4.054688 9.523438 L 2.3125 9.523438 C 2.140625 9.523438 2 9.386719 2 9.214844 L 2 8.390625 C 2 8.21875 2.140625 8.082031 2.3125 8.082031 L 8.523438 8.082031 C 8.695313 8.082031 8.835938 8.21875 8.835938 8.390625 L 8.835938 9.214844 C 8.835938 9.386719 8.695313 9.523438 8.523438 9.523438 L 7.1875 9.523438 L 12.503906 21.785156 L 15.269531 16.617188 L 11.761719 9.527344 L 10.917969 9.527344 C 10.746094 9.527344 10.605469 9.386719 10.605469 9.214844 L 10.605469 8.394531 C 10.605469 8.222656 10.746094 8.082031 10.917969 8.082031 L 15.515625 8.082031 C 15.6875 8.082031 15.824219 8.222656 15.824219 8.394531 L 15.824219 9.214844 C 15.824219 9.386719 15.6875 9.523438 15.515625 9.523438 L 14.703125 9.523438 L 16.722656 13.9375 L 19.125 9.523438 L 17.652344 9.523438 C 17.476563 9.523438 17.339844 9.386719 17.339844 9.214844 L 17.339844 8.394531 C 17.339844 8.222656 17.476563 8.082031 17.652344 8.082031 L 22.117188 8.082031 C 22.289063 8.082031 22.425781 8.222656 22.425781 8.394531 L 22.425781 9.214844 C 22.425781 9.386719 22.289063 9.523438 22.117188 9.523438 L 21.136719 9.523438 L 17.632813 15.894531 L 20.488281 21.769531 L 26 9.523438 L 24.253906 9.523438 C 24.082031 9.523438 23.941406 9.386719 23.941406 9.214844 L 23.941406 8.394531 C 23.941406 8.222656 24.082031 8.082031 24.253906 8.082031 L 29.6875 8.082031 C 29.863281 8.082031 30 8.222656 30 8.394531 Z "></path>
</g>
</svg>`,
isFallback: false,
name: "Wikipedia",
prefix: "w",
priority: 5,
url: "https://en.wikipedia.org/wiki/",
},
{
Expand All @@ -93,8 +103,10 @@ export const defaultWebSearches: WebSearch[] = [
<path style=" fill:#FFFFFF;" d="M 20 31 L 20 17 L 32 24 Z "></path>
</g>
</svg>`,
isFallback: false,
name: "YouTube",
prefix: "yt",
priority: 6,
url: "https://www.youtube.com/results?search_query=",
},
];
Loading

0 comments on commit 859e492

Please sign in to comment.