diff --git a/addon/adapters/file.js b/addon/adapters/file.js index d8ccd871..60476157 100644 --- a/addon/adapters/file.js +++ b/addon/adapters/file.js @@ -25,17 +25,5 @@ export default function (BaseClass) { return ajaxOptions; } - - // Overwrite and replicate the query function, - // because ember doesnt pass adapterOptions to urlForQuery - query(_, type, query, __, options) { - let url = this.buildURL(type.modelName, null, null, "query", query); - - if (options?.adapterOptions?.customEndpoint) { - url = `${this.buildURL()}/${options.adapterOptions.customEndpoint}`; - } - - return this.ajax(url, "GET", { data: query }); - } }; } diff --git a/addon/adapters/search-result.js b/addon/adapters/search-result.js new file mode 100644 index 00000000..02f492cf --- /dev/null +++ b/addon/adapters/search-result.js @@ -0,0 +1,15 @@ +export default function (BaseClass) { + return class SearchResultAdapter extends BaseClass { + // Overwrite and replicate the query function, + // because ember doesnt pass adapterOptions to urlForQuery + query(_, type, query, __, options) { + let url = this.buildURL(type.modelName, null, null, "query", query); + + if (options?.adapterOptions?.customEndpoint) { + url = `${this.buildURL()}/${options.adapterOptions.customEndpoint}`; + } + + return this.ajax(url, "GET", { data: query }); + } + }; +} diff --git a/addon/components/document-view.hbs b/addon/components/document-view.hbs index ff9f4293..b5fb5449 100644 --- a/addon/components/document-view.hbs +++ b/addon/components/document-view.hbs @@ -42,7 +42,6 @@ >
{{! List & Grid View }} {{#if this.listView}} diff --git a/addon/components/document-view.js b/addon/components/document-view.js index f680f7e0..6271005c 100644 --- a/addon/components/document-view.js +++ b/addon/components/document-view.js @@ -61,17 +61,43 @@ export default class DocumentViewComponent extends Component { @task *fetchDocuments() { - const documents = yield this.store.query("document", { - include: "category,files,tags", - filter: this.args.filters || {}, - sort: this.sort ? `${this.sortDirection}${this.sort}` : "", - }); + let documents = []; + if (this.args.filters.query) { + this.args.filters.only_newest = true; + const searchResult = yield this.store.query( + "search-result", + { + include: "document,matched_file", + filter: this.args.filters || {}, + page: { number: 1 }, + }, + { + adapterOptions: { + customEndpoint: "search", + }, + }, + ); + + documents = searchResult.reduce((acc, result) => { + if (!acc.some((doc) => doc.id === result.document.id)) { + acc.push(result.document); + } + return acc; + }, []); + } else { + documents = yield this.store.query("document", { + include: "category,files,tags", + filter: this.args.filters || {}, + sort: this.sort ? `${this.sortDirection}${this.sort}` : "", + }); + } + + this.initialiseDocumentSelection(documents); return yield this.config.documentsPostProcess(documents); } - @task - *initialiseDocumentSelection() { + initialiseDocumentSelection(docs) { let docIds = []; if (this.router.externalRouter.currentRoute?.queryParams?.document) { docIds = decodeURIComponent( @@ -79,10 +105,6 @@ export default class DocumentViewComponent extends Component { ).split(","); } if (docIds.length !== 0) { - const docs = yield this.store.query("document", { - filter: this.args.filters || {}, - sort: this.sort ? `${this.sortDirection}${this.sort}` : "", - }); const selectedDocs = [...docs].filter((doc) => docIds.includes(doc.id)); selectedDocs.forEach((doc) => this.documents.selectDocument(doc)); } diff --git a/addon/components/file-search.hbs b/addon/components/search-view.hbs similarity index 100% rename from addon/components/file-search.hbs rename to addon/components/search-view.hbs diff --git a/addon/components/file-search.js b/addon/components/search-view.js similarity index 81% rename from addon/components/file-search.js rename to addon/components/search-view.js index ea040004..e9020ee6 100644 --- a/addon/components/file-search.js +++ b/addon/components/search-view.js @@ -5,7 +5,7 @@ import { tracked } from "@glimmer/tracking"; import { task } from "ember-concurrency"; import { trackedFunction } from "reactiveweb/function"; -export default class DocumentViewComponent extends Component { +export default class SearchViewComponent extends Component { @service store; @service("alexandria-config") config; @service("alexandria-documents") documents; @@ -22,10 +22,10 @@ export default class DocumentViewComponent extends Component { return []; } - const files = await this.store.query( - "file", + const search = await this.store.query( + "search-result", { - include: "document,renderings", + include: "document,matched_file", filter: this.args.filters || {}, page: { number: 1 }, }, @@ -36,14 +36,7 @@ export default class DocumentViewComponent extends Component { }, ); - const documents = Array.from( - new Map( - files.map((file) => [ - file.document.id, - this.store.peekRecord("document", file.document.id), - ]), - ).values(), - ); + const documents = search.map((result) => result.document); return await this.config.documentsPostProcess(documents); }); diff --git a/addon/controllers/index.js b/addon/controllers/index.js index b391ab28..dd39b741 100644 --- a/addon/controllers/index.js +++ b/addon/controllers/index.js @@ -28,7 +28,7 @@ export default class IndexController extends Controller { category: this.category, tags: this.tags.length ? this.tags.join(",") : undefined, marks: this.marks.length ? this.marks.join(",") : undefined, - search: this.search, + query: this.search, activeGroup: this.activeGroup, }; diff --git a/addon/controllers/search.js b/addon/controllers/search.js index 89de58d9..69db7d44 100644 --- a/addon/controllers/search.js +++ b/addon/controllers/search.js @@ -12,6 +12,7 @@ export default class SearchController extends Controller { get filters() { const filters = { + onlyNewest: true, query: this.search, }; diff --git a/addon/models/search-result.js b/addon/models/search-result.js new file mode 100644 index 00000000..e08aa6ac --- /dev/null +++ b/addon/models/search-result.js @@ -0,0 +1,11 @@ +import Model, { attr, belongsTo } from "@ember-data/model"; + +export default class SearchResultModel extends Model { + @attr searchRank; + @attr searchContext; + @attr fileName; + @attr documentName; + + @belongsTo("document", { inverse: null, async: false }) document; + @belongsTo("matched-file", { inverse: null, async: false }) file; +} diff --git a/addon/serializers/search-result.js b/addon/serializers/search-result.js new file mode 100644 index 00000000..fd21b6a8 --- /dev/null +++ b/addon/serializers/search-result.js @@ -0,0 +1,3 @@ +import { LocalizedSerializer } from "ember-localized-model"; + +export default class SearchResultSerializer extends LocalizedSerializer {} diff --git a/addon/templates/search.hbs b/addon/templates/search.hbs index 06d9f53c..9d0a61cd 100644 --- a/addon/templates/search.hbs +++ b/addon/templates/search.hbs @@ -1,7 +1,7 @@
- diff --git a/blueprints/ember-alexandria/files/__root__/adapters/search-result.js b/blueprints/ember-alexandria/files/__root__/adapters/search-result.js new file mode 100644 index 00000000..1529070d --- /dev/null +++ b/blueprints/ember-alexandria/files/__root__/adapters/search-result.js @@ -0,0 +1,5 @@ +import ApplicationAdapter from "./application"; + +import adapterFactory from "ember-alexandria/adapters/search-result"; + +export default adapterFactory(ApplicationAdapter); diff --git a/blueprints/ember-alexandria/files/__root__/models/search-result.js b/blueprints/ember-alexandria/files/__root__/models/search-result.js new file mode 100644 index 00000000..32f35a44 --- /dev/null +++ b/blueprints/ember-alexandria/files/__root__/models/search-result.js @@ -0,0 +1 @@ +export { default } from "ember-alexandria/models/search-result"; diff --git a/blueprints/ember-alexandria/files/__root__/serializers/search-result.js b/blueprints/ember-alexandria/files/__root__/serializers/search-result.js new file mode 100644 index 00000000..b2c52884 --- /dev/null +++ b/blueprints/ember-alexandria/files/__root__/serializers/search-result.js @@ -0,0 +1 @@ +export { default } from "ember-alexandria/serializers/search-result"; diff --git a/tests/dummy/app/adapters/search-result.js b/tests/dummy/app/adapters/search-result.js new file mode 100644 index 00000000..1529070d --- /dev/null +++ b/tests/dummy/app/adapters/search-result.js @@ -0,0 +1,5 @@ +import ApplicationAdapter from "./application"; + +import adapterFactory from "ember-alexandria/adapters/search-result"; + +export default adapterFactory(ApplicationAdapter); diff --git a/tests/dummy/app/models/search-result.js b/tests/dummy/app/models/search-result.js new file mode 100644 index 00000000..32f35a44 --- /dev/null +++ b/tests/dummy/app/models/search-result.js @@ -0,0 +1 @@ +export { default } from "ember-alexandria/models/search-result"; diff --git a/tests/dummy/app/serializers/search-result.js b/tests/dummy/app/serializers/search-result.js new file mode 100644 index 00000000..b2c52884 --- /dev/null +++ b/tests/dummy/app/serializers/search-result.js @@ -0,0 +1 @@ +export { default } from "ember-alexandria/serializers/search-result";