Skip to content

Commit

Permalink
🔍 support for search
Browse files Browse the repository at this point in the history
  • Loading branch information
undergroundwires committed Jan 10, 2020
1 parent cafe6e8 commit 89862b2
Show file tree
Hide file tree
Showing 19 changed files with 202 additions and 88 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

## [Unreleased]

- Added search
- Some styling improvements

## [0.3.0] - 2020-01-09

- Added support for grouping
Expand Down
19 changes: 9 additions & 10 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<template>
<div id="app">
<div class="wrapper">
<TheHeader class="row"
github-url="https://github.com/undergroundwires/privacy.sexy" />
<!-- <TheSearchBar> </TheSearchBar> -->
<TheHeader class="row" />
<TheSearchBar class="row" />
<TheScripts class="row"/>
<TheCodeArea class="row" theme="xcode" />
<TheCodeButtons class="row" />
Expand All @@ -15,20 +14,20 @@
<script lang="ts">
import { Component, Vue, Prop } from 'vue-property-decorator';
import { ApplicationState, IApplicationState } from '@/application/State/ApplicationState';
import TheHeader from './presentation/TheHeader.vue';
import TheFooter from './presentation/TheFooter.vue';
import TheCodeArea from './presentation/TheCodeArea.vue';
import TheCodeButtons from './presentation/TheCodeButtons.vue';
import TheSearchBar from './presentation/TheSearchBar.vue';
import TheScripts from './presentation/Scripts/TheScripts.vue';
import TheHeader from '@/presentation/TheHeader.vue';
import TheFooter from '@/presentation/TheFooter.vue';
import TheCodeArea from '@/presentation/TheCodeArea.vue';
import TheCodeButtons from '@/presentation/TheCodeButtons.vue';
import TheSearchBar from '@/presentation/TheSearchBar.vue';
import TheScripts from '@/presentation/Scripts/TheScripts.vue';
@Component({
components: {
TheHeader,
TheCodeArea,
TheCodeButtons,
TheSearchBar,
TheScripts,
TheSearchBar,
TheFooter,
},
})
Expand Down
8 changes: 5 additions & 3 deletions src/application/Parser/ApplicationParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import applicationFile from 'js-yaml-loader!./../application.yaml';
import { parseCategory } from './CategoryParser';

export function parseApplication(): Application {
const name = applicationFile.name as string;
const version = applicationFile.version as number;
const categories = new Array<Category>();
if (!applicationFile.actions || applicationFile.actions.length <= 0) {
throw new Error('Application does not define any action');
Expand All @@ -14,6 +12,10 @@ export function parseApplication(): Application {
const category = parseCategory(action);
categories.push(category);
}
const app = new Application(name, version, categories);
const app = new Application(
applicationFile.name,
applicationFile.repositoryUrl,
applicationFile.version,
categories);
return app;
}
18 changes: 18 additions & 0 deletions src/application/State/Filter/FilterResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { IFilterResult } from './IFilterResult';
import { IScript } from '@/domain/Script';
import { ICategory } from '@/domain/ICategory';

export class FilterResult implements IFilterResult {
constructor(
public readonly scriptMatches: ReadonlyArray<IScript>,
public readonly categoryMatches: ReadonlyArray<ICategory>,
public readonly query: string) {
if (!query) { throw new Error('Query is empty or undefined'); }
if (!scriptMatches) { throw new Error('Script matches is undefined'); }
if (!categoryMatches) { throw new Error('Category matches is undefined'); }
}
public hasAnyMatches(): boolean {
return this.scriptMatches.length > 0
|| this.categoryMatches.length > 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { IScript, ICategory } from '@/domain/ICategory';

export interface IFilterMatches {
readonly scriptMatches: ReadonlyArray<IScript>;
export interface IFilterResult {
readonly categoryMatches: ReadonlyArray<ICategory>;
readonly scriptMatches: ReadonlyArray<IScript>;
readonly query: string;
hasAnyMatches(): boolean;
}
4 changes: 2 additions & 2 deletions src/application/State/Filter/IUserFilter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IFilterMatches } from './IFilterMatches';
import { IFilterResult } from './IFilterResult';
import { ISignal } from '@/infrastructure/Events/Signal';

export interface IUserFilter {
readonly filtered: ISignal<IFilterMatches>;
readonly filtered: ISignal<IFilterResult>;
readonly filterRemoved: ISignal<void>;
setFilter(filter: string): void;
removeFilter(): void;
Expand Down
20 changes: 12 additions & 8 deletions src/application/State/Filter/UserFilter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { IFilterMatches } from './IFilterMatches';
import { FilterResult } from './FilterResult';
import { IFilterResult } from './IFilterResult';
import { Application } from '../../../domain/Application';
import { IUserFilter } from './IUserFilter';
import { Signal } from '@/infrastructure/Events/Signal';

export class UserFilter implements IUserFilter {
public readonly filtered = new Signal<IFilterMatches>();
public readonly filtered = new Signal<IFilterResult>();
public readonly filterRemoved = new Signal<void>();

constructor(private application: Application) {
Expand All @@ -16,14 +17,17 @@ export class UserFilter implements IUserFilter {
throw new Error('Filter must be defined and not empty. Use removeFilter() to remove the filter');
}
const filteredScripts = this.application.getAllScripts().filter(
(script) => script.name.toLowerCase().includes(filter.toLowerCase()) ||
(script) =>
script.name.toLowerCase().includes(filter.toLowerCase()) ||
script.code.toLowerCase().includes(filter.toLowerCase()));
const filteredCategories = this.application.getAllCategories().filter(
(script) => script.name.toLowerCase().includes(filter.toLowerCase()));

const matches: IFilterMatches = {
scriptMatches: filteredScripts,
categoryMatches: null,
query: filter,
};
const matches = new FilterResult(
filteredScripts,
filteredCategories,
filter,
);

this.filtered.notify(matches);
}
Expand Down
1 change: 1 addition & 0 deletions src/application/application.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: privacy.sexy
version: 0.3.0
repositoryUrl: https://github.com/undergroundwires/privacy.sexy
actions:
-
category: Privacy cleanup
Expand Down
1 change: 1 addition & 0 deletions src/application/application.yaml.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ declare module 'js-yaml-loader!*' {
interface ApplicationYaml {
name: string;
version: number;
repositoryUrl: string;
actions: ReadonlyArray<YamlCategory>;
}

Expand Down
14 changes: 8 additions & 6 deletions src/domain/Application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ export class Application implements IApplication {

constructor(
public readonly name: string,
public readonly repositoryUrl: string,
public readonly version: number,
public readonly categories: ReadonlyArray<ICategory>) {
if (!name) {
throw Error('Application has no name');
}
if (!version) {
throw Error('Version cannot be zero');
}
if (!name) { throw Error('Application has no name'); }
if (!repositoryUrl) { throw Error('Application has no repository url'); }
if (!version) { throw Error('Version cannot be zero'); }
this.flattened = flatten(categories);
if (this.flattened.allCategories.length === 0) {
throw new Error('Application must consist of at least one category');
Expand Down Expand Up @@ -48,6 +46,10 @@ export class Application implements IApplication {
public getAllScripts(): IScript[] {
return this.flattened.allScripts;
}

public getAllCategories(): ICategory[] {
return this.flattened.allCategories;
}
}

function ensureNoDuplicates<TKey>(entities: ReadonlyArray<IEntity<TKey>>) {
Expand Down
2 changes: 2 additions & 0 deletions src/domain/IApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ICategory } from '@/domain/ICategory';

export interface IApplication {
readonly name: string;
readonly repositoryUrl: string;
readonly version: number;
readonly categories: ReadonlyArray<ICategory>;
readonly totalScripts: number;
Expand All @@ -12,6 +13,7 @@ export interface IApplication {
findCategory(categoryId: number): ICategory | undefined;
findScript(scriptId: string): IScript | undefined;
getAllScripts(): ReadonlyArray<IScript>;
getAllCategories(): ReadonlyArray<ICategory>;
}

export { IScript } from '@/domain/IScript';
Expand Down
36 changes: 27 additions & 9 deletions src/presentation/Scripts/Grouping/TheGrouper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<span class="part">
<span
class="part"
v-bind:class="{ 'disabled': isGrouped, 'enabled': !isGrouped}"
@click="!isGrouped ? toggleGrouping() : undefined">Cards</span>
v-bind:class="{ 'disabled': cardsSelected, 'enabled': !cardsSelected}"
@click="groupByCard()">Cards</span>
<span class="part">|</span>
<span class="part"
v-bind:class="{ 'disabled': !isGrouped, 'enabled': isGrouped}"
@click="isGrouped ? toggleGrouping() : undefined">None</span>
v-bind:class="{ 'disabled': noneSelected, 'enabled': !noneSelected}"
@click="groupByNone()">None</span>
</span>
</div>
</template>
Expand All @@ -22,12 +22,30 @@ import { Grouping } from './Grouping';
@Component
export default class TheGrouper extends StatefulVue {
public currentGrouping: Grouping;
public isGrouped = true;
public cardsSelected = false;
public noneSelected = false;
public toggleGrouping() {
this.currentGrouping = this.currentGrouping === Grouping.None ? Grouping.Cards : Grouping.None;
this.isGrouped = this.currentGrouping === Grouping.Cards;
private currentGrouping: Grouping;
public mounted() {
this.changeGrouping(Grouping.Cards);
}
public groupByCard() {
this.changeGrouping(Grouping.Cards);
}
public groupByNone() {
this.changeGrouping(Grouping.None);
}
private changeGrouping(newGrouping: Grouping) {
if (this.currentGrouping === newGrouping) {
return;
}
this.currentGrouping = newGrouping;
this.cardsSelected = newGrouping === Grouping.Cards;
this.noneSelected = newGrouping === Grouping.None;
this.$emit('groupingChanged', this.currentGrouping);
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/presentation/Scripts/ScriptsTree/ScriptNodeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ export function parseSingleCategory(categoryId: number, state: IApplicationState
return tree;
}

export function getScriptNodeId(script: IScript): string {
return script.id;
}
export function getCategoryNodeId(category: ICategory): string {
return `${category.id}`;
}

function parseCategoryRecursively(
parentCategory: ICategory,
selection: IUserSelection): INode[] {
Expand All @@ -44,7 +51,7 @@ function parseCategoryRecursively(
function convertCategoryToNode(
category: ICategory, children: readonly INode[]): INode {
return {
id: `${category.id}`,
id: getCategoryNodeId(category),
text: category.name,
selected: false,
children,
Expand All @@ -54,7 +61,7 @@ function convertCategoryToNode(

function convertScriptToNode(script: IScript, selection: IUserSelection): INode {
return {
id: `${script.id}`,
id: getScriptNodeId(script),
text: script.name,
selected: selection.isSelected(script),
children: undefined,
Expand Down
33 changes: 19 additions & 14 deletions src/presentation/Scripts/ScriptsTree/ScriptsTree.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<template>
<span id="container">
<span v-if="nodes != null && nodes.length > 0">
<SelectableTree
:nodes="nodes"
:selectedNodeIds="selectedNodeIds"
:filterPredicate="filterPredicate"
:filterText="filterText"
v-on:nodeSelected="checkNodeAsync($event)">
<SelectableTree
:nodes="nodes"
:selectedNodeIds="selectedNodeIds"
:filterPredicate="filterPredicate"
:filterText="filterText"
v-on:nodeSelected="checkNodeAsync($event)">
</SelectableTree>
</span>
<span v-else>Nooo 😢</span>
Expand All @@ -19,9 +19,11 @@
import { Category } from '@/domain/Category';
import { IRepository } from '@/infrastructure/Repository/IRepository';
import { IScript } from '@/domain/IScript';
import { ICategory } from '@/domain/ICategory';
import { IApplicationState, IUserSelection } from '@/application/State/IApplicationState';
import { IFilterMatches } from '@/application/State/Filter/IFilterMatches';
import { parseAllCategories, parseSingleCategory } from './ScriptNodeParser';
import { IFilterResult } from '@/application/State/Filter/IFilterResult';
import { parseAllCategories, parseSingleCategory, getScriptNodeId, getCategoryNodeId } from './ScriptNodeParser';
import SelectableTree, { FilterPredicate } from './SelectableTree/SelectableTree.vue';
import { INode } from './SelectableTree/INode';
Expand All @@ -37,11 +39,11 @@
public selectedNodeIds?: string[] = null;
public filterText?: string = null;
private matches?: IFilterMatches;
private filtered?: IFilterResult;
public async mounted() {
// React to state changes
const state = await this.getCurrentStateAsync();
// React to state changes
state.selection.changed.on(this.handleSelectionChanged);
state.filter.filterRemoved.on(this.handleFilterRemoved);
state.filter.filtered.on(this.handleFiltered);
Expand Down Expand Up @@ -72,7 +74,10 @@
}
public filterPredicate(node: INode): boolean {
return this.matches.scriptMatches.some((script: IScript) => script.id === node.id);
return this.filtered.scriptMatches.some(
(script: IScript) => node.id === getScriptNodeId(script))
|| this.filtered.categoryMatches.some(
(category: ICategory) => node.id === getCategoryNodeId(category));
}
private handleSelectionChanged(selectedScripts: ReadonlyArray<IScript>) {
Expand All @@ -83,9 +88,9 @@
this.filterText = '';
}
private handleFiltered(matches: IFilterMatches) {
this.filterText = matches.query;
this.matches = matches;
private handleFiltered(result: IFilterResult) {
this.filterText = result.query;
this.filtered = result;
}
}
Expand Down
Loading

0 comments on commit 89862b2

Please sign in to comment.