Skip to content

EspoCRM – Open Source CRM Application

search manager ref

Yuri Kuznetsovfb4379ea345a
diff --git a/client/src/controllers/admin.js b/client/src/controllers/admin.js
index 2757ca1bf3..92466620d2 100644
--- a/client/src/controllers/admin.js
+++ b/client/src/controllers/admin.js
@@ -125,7 +125,7 @@ class AdminController extends Controller {
this.listenTo(view, 'rebuild', this.rebuild);
}, {
useStored: isReturn,
- key: key,
+ storageKey: key,
});
}
@@ -352,12 +352,7 @@ class AdminController extends Controller {
// noinspection JSUnusedGlobalSymbols
actionAuthTokens() {
this.collectionFactory.create('AuthToken', collection => {
- const searchManager = new SearchManager(
- collection,
- 'list',
- this.getStorage(),
- this.getDateTime()
- );
+ const searchManager = new SearchManager(collection, {storageKey: 'list'});
searchManager.loadStored();
collection.where = searchManager.getWhere();
@@ -374,12 +369,7 @@ class AdminController extends Controller {
// noinspection JSUnusedGlobalSymbols
actionAuthLog() {
this.collectionFactory.create('AuthLogRecord', collection => {
- const searchManager = new SearchManager(
- collection,
- 'list',
- this.getStorage(),
- this.getDateTime()
- );
+ const searchManager = new SearchManager(collection, {storageKey: 'list'});
searchManager.loadStored();
@@ -402,12 +392,7 @@ class AdminController extends Controller {
// noinspection JSUnusedGlobalSymbols
actionJobs() {
this.collectionFactory.create('Job', collection => {
- const searchManager = new SearchManager(
- collection,
- 'list',
- this.getStorage(),
- this.getDateTime()
- );
+ const searchManager = new SearchManager(collection, {storageKey: 'list'});
searchManager.loadStored();
@@ -425,12 +410,7 @@ class AdminController extends Controller {
// noinspection JSUnusedGlobalSymbols
actionAppLog() {
this.collectionFactory.create('AppLogRecord', collection => {
- const searchManager = new SearchManager(
- collection,
- 'list',
- this.getStorage(),
- this.getDateTime()
- );
+ const searchManager = new SearchManager(collection, {storageKey: 'list'});
searchManager.loadStored();
diff --git a/client/src/controllers/global-stream.js b/client/src/controllers/global-stream.js
index 35ef27e23e..4456156676 100644
--- a/client/src/controllers/global-stream.js
+++ b/client/src/controllers/global-stream.js
@@ -43,7 +43,7 @@ class GlobalStreamController extends Controller {
const view = new GlobalStreamView();
this.main(view, undefined, undefined, {
- key: key,
+ storageKey: key,
useStored: isReturn,
});
}
diff --git a/client/src/controllers/stream.js b/client/src/controllers/stream.js
index 7a57ef8821..4310006f3d 100644
--- a/client/src/controllers/stream.js
+++ b/client/src/controllers/stream.js
@@ -42,7 +42,7 @@ class StreamController extends Controller {
}
this.main('views/stream', {displayTitle: true}, undefined, {
- key: key,
+ storageKey: key,
useStored: isReturn,
});
}
@@ -57,7 +57,7 @@ class StreamController extends Controller {
}
this.main('views/stream', {displayTitle: true, filter: 'posts'}, undefined, {
- key: key,
+ storageKey: key,
useStored: isReturn,
});
}
@@ -72,7 +72,7 @@ class StreamController extends Controller {
}
this.main('views/stream', {displayTitle: true, filter: 'updates'}, undefined, {
- key: key,
+ storageKey: key,
useStored: isReturn,
});
}
diff --git a/client/src/search-manager.js b/client/src/search-manager.js
index 48d338a45e..136ff7bbdc 100644
--- a/client/src/search-manager.js
+++ b/client/src/search-manager.js
@@ -64,6 +64,10 @@
* @property {Object.<string, *>} [data] Additional data for UI.
*/
+import {inject} from 'di';
+import DateTime from 'date-time';
+import Storage from 'storage';
+
/**
* A search manager.
*/
@@ -73,24 +77,40 @@ class SearchManager {
* @type {string|null}
* @private
*/
- timeZone
+ timeZone = null
+
+ /**
+ * @private
+ * @type {module:search-manager~data}
+ */
+ defaultData
+
+ /**
+ * @private
+ * @type {DateTime}
+ */
+ @inject(DateTime)
+ dateTime
+
+ /**
+ * @private
+ * @type {Storage}
+ */
+ @inject(Storage)
+ storage
+
+ /**
+ * @typedef {Object} module:search-manager~Options
+ * @property {string} [storageKey] A storage key. If not specified, the storage won't be used.
+ * @property {module:search-manager~data} [defaultData] Default data.
+ * @property {boolean} [emptyOnReset] To empty on reset.
+ */
/**
* @param {module:collection} collection A collection.
- * @param {string|null} type A type. Used for a storage key.
- * @param {module:storage|null} storage A storage.
- * @param {module:date-time|null} dateTime A date-time util.
- * @param {module:search-manager~data|null} [defaultData=null] Default search data.
- * @param {boolean} [emptyOnReset=false] To empty on reset.
+ * @param {module:search-manager~Options} [options]
*/
- constructor(
- collection,
- type,
- storage,
- dateTime,
- defaultData,
- emptyOnReset
- ) {
+ constructor(collection, options = {}) {
/**
* @private
* @type {module:collection}
@@ -100,34 +120,28 @@ class SearchManager {
/**
* An entity type.
*
- * @public
+ * @private
* @type {string}
*/
this.scope = collection.entityType;
- /**
- * @private
- * @type {module:storage|null}
- */
- this.storage = storage;
-
/**
* @private
* @type {string}
*/
- this.type = type || 'list';
+ this.storageKey = options.storageKey;
/**
* @private
- * @type {module:date-time|null}
+ * @type {boolean}
*/
- this.dateTime = dateTime;
+ this.useStorage = !!this.storageKey;
/**
* @private
* @type {boolean}
*/
- this.emptyOnReset = emptyOnReset;
+ this.emptyOnReset = options.emptyOnReset || false;
/**
* @private
@@ -140,12 +154,19 @@ class SearchManager {
primary: null,
};
+ let defaultData = options.defaultData;
+
+ if (!defaultData && arguments[4]) {
+ // For bc.
+ defaultData = arguments[4];
+ }
+
if (defaultData) {
this.defaultData = defaultData;
- for (const p in this.emptyData) {
- if (!(p in defaultData)) {
- defaultData[p] = Espo.Utils.clone(this.emptyData[p]);
+ for (const key in this.emptyData) {
+ if (!(key in defaultData)) {
+ defaultData[key] = Espo.Utils.clone(this.emptyData[key]);
}
}
}
@@ -319,8 +340,7 @@ class SearchManager {
* @returns {module:search-manager}
*/
loadStored() {
- this.data =
- this.storage.get(this.type + 'Search', this.scope) ||
+ this.data = this.getFromStorageIfEnabled() ||
Espo.Utils.clone(this.defaultData) ||
Espo.Utils.clone(this.emptyData);
@@ -329,6 +349,18 @@ class SearchManager {
return this;
}
+ /**
+ * @private
+ * @return {module:search-manager~data|null}
+ */
+ getFromStorageIfEnabled() {
+ if (!this.useStorage) {
+ return null;
+ }
+
+ return this.storage.get(`${this.storageKey}Search`, this.scope);
+ }
+
/**
* Get data.
*
@@ -387,11 +419,11 @@ class SearchManager {
set(data) {
this.data = data;
- if (this.storage) {
+ if (this.useStorage) {
data = Espo.Utils.clone(data);
delete data['textFilter'];
- this.storage.set(this.type + 'Search', this.scope, data);
+ this.storage.set(this.storageKey + 'Search', this.scope, data);
}
}
@@ -405,8 +437,8 @@ class SearchManager {
empty() {
this.data = Espo.Utils.clone(this.emptyData);
- if (this.storage) {
- this.storage.clear(this.type + 'Search', this.scope);
+ if (this.useStorage) {
+ this.storage.clear(this.storageKey + 'Search', this.scope);
}
}
@@ -422,8 +454,8 @@ class SearchManager {
this.data = Espo.Utils.clone(this.defaultData) || Espo.Utils.clone(this.emptyData);
- if (this.storage) {
- this.storage.clear(this.type + 'Search', this.scope);
+ if (this.useStorage) {
+ this.storage.clear(this.storageKey + 'Search', this.scope);
}
}
diff --git a/client/src/views/dashlets/abstract/record-list.js b/client/src/views/dashlets/abstract/record-list.js
index d5f2377c00..87215f7b87 100644
--- a/client/src/views/dashlets/abstract/record-list.js
+++ b/client/src/views/dashlets/abstract/record-list.js
@@ -93,7 +93,7 @@ class RecordListDashletView extends BaseDashletView {
this.getCollectionFactory().create(this.scope, collection => {
const searchData = this.getSearchData();
- this.searchManager = new SearchManager(collection, 'list', null, this.getDateTime(), searchData);
+ this.searchManager = new SearchManager(collection, {defaultData: searchData});
if (!this.scope) {
this.$el.find('.list-container')
diff --git a/client/src/views/global-stream.js b/client/src/views/global-stream.js
index 4d6c0eaacb..a1608a3dfa 100644
--- a/client/src/views/global-stream.js
+++ b/client/src/views/global-stream.js
@@ -70,12 +70,7 @@ class GlobalStreamView extends View {
}
setupSearchManager() {
- const searchManager = new SearchManager(
- this.collection,
- undefined,
- this.getStorage(),
- this.getDateTime()
- );
+ const searchManager = new SearchManager(this.collection);
searchManager.loadStored();
diff --git a/client/src/views/list-related.js b/client/src/views/list-related.js
index 981a3574a4..7c8fa6d9e6 100644
--- a/client/src/views/list-related.js
+++ b/client/src/views/list-related.js
@@ -449,13 +449,7 @@ class ListRelatedView extends MainView {
setupSearchManager() {
const collection = this.collection;
- const searchManager = new SearchManager(
- collection,
- 'list',
- null,
- this.getDateTime(),
- null
- );
+ const searchManager = new SearchManager(collection);
if (this.panelDefs.primaryFilter) {
searchManager.setPrimary(this.panelDefs.primaryFilter);
diff --git a/client/src/views/list.js b/client/src/views/list.js
index 972c39c3c8..88201288cd 100644
--- a/client/src/views/list.js
+++ b/client/src/views/list.js
@@ -464,7 +464,7 @@ class ListView extends MainView {
* @returns {Object}
*/
getSearchDefaultData() {
- return this.getMetadata().get('clientDefs.' + this.scope + '.defaultFilterData');
+ return this.getMetadata().get(`clientDefs.${this.scope}.defaultFilterData`);
}
/**
@@ -479,13 +479,10 @@ class ListView extends MainView {
key += 'Filter' + Espo.Utils.upperCaseFirst(this._primaryFilter);
}
- const searchManager = new SearchManager(
- collection,
- key,
- this.getStorage(),
- this.getDateTime(),
- this.getSearchDefaultData()
- );
+ const searchManager = new SearchManager(collection, {
+ storageKey: key,
+ defaultData: this.getSearchDefaultData(),
+ });
searchManager.scope = this.scope;
searchManager.loadStored();
diff --git a/client/src/views/modals/action-history.js b/client/src/views/modals/action-history.js
index 92c3175fa8..161c5a0deb 100644
--- a/client/src/views/modals/action-history.js
+++ b/client/src/views/modals/action-history.js
@@ -75,16 +75,15 @@ class ActionHistoryModalView extends ModalView {
}
setupSearch() {
- const searchManager = this.searchManager =
- new SearchManager(this.collection, 'listSelect', null, this.getDateTime());
+ this.searchManager = new SearchManager(this.collection);
this.collection.data.boolFilterList = ['onlyMy'];
- this.collection.where = searchManager.getWhere();
+ this.collection.where = this.searchManager.getWhere();
this.createView('search', 'views/record/search', {
collection: this.collection,
fullSelector: this.containerSelector + ' .search-container',
- searchManager: searchManager,
+ searchManager: this.searchManager,
disableSavePreset: true,
textFilterDisabled: true,
});
diff --git a/client/src/views/modals/related-list.js b/client/src/views/modals/related-list.js
index f6734dd506..78d2a943e6 100644
--- a/client/src/views/modals/related-list.js
+++ b/client/src/views/modals/related-list.js
@@ -354,18 +354,15 @@ class RelatedListModalView extends ModalView {
}
setupSearch() {
- const searchManager = this.searchManager =
- new SearchManager(this.collection, 'listSelect', null, this.getDateTime());
-
- searchManager.emptyOnReset = true;
+ this.searchManager = new SearchManager(this.collection, {emptyOnReset: true});
const primaryFilterName = this.primaryFilterName;
if (primaryFilterName) {
- searchManager.setPrimary(primaryFilterName);
+ this.searchManager.setPrimary(primaryFilterName);
}
- this.collection.where = searchManager.getWhere();
+ this.collection.where = this.searchManager.getWhere();
let filterList = Espo.Utils.clone(this.getMetadata().get(['clientDefs', this.scope, 'filterList']) || []);
@@ -405,7 +402,7 @@ class RelatedListModalView extends ModalView {
this.createView('search', 'views/record/search', {
collection: this.collection,
fullSelector: this.containerSelector + ' .search-container',
- searchManager: searchManager,
+ searchManager: this.searchManager,
disableSavePreset: true,
filterList: filterList,
filtersLayoutName: this.options.filtersLayoutName,
diff --git a/client/src/views/modals/select-category-tree-records.js b/client/src/views/modals/select-category-tree-records.js
index 3ca8726694..609a3e37d7 100644
--- a/client/src/views/modals/select-category-tree-records.js
+++ b/client/src/views/modals/select-category-tree-records.js
@@ -27,6 +27,7 @@
************************************************************************/
import SelectRecordsModalView from 'views/modals/select-records';
+import SearchManager from 'search-manager';
class SelectCategoryTreeRecordsModalView extends SelectRecordsModalView {
@@ -104,61 +105,59 @@ class SelectCategoryTreeRecordsModalView extends SelectRecordsModalView {
this.waitForView('list');
- Espo.loader.require('search-manager', SearchManager => {
- this.getCollectionFactory().create(this.entityType, collection => {
- collection.maxSize = this.getConfig().get('recordsPerPageSelect') || 5;
+ this.getCollectionFactory().create(this.entityType, collection => {
+ collection.maxSize = this.getConfig().get('recordsPerPageSelect') || 5;
- this.collection = collection;
+ this.collection = collection;
- const searchManager = new SearchManager(collection, 'listSelect', null, this.getDateTime());
+ const searchManager = new SearchManager(collection);
- searchManager.emptyOnReset = true;
+ searchManager.emptyOnReset = true;
- if (this.filters) {
- searchManager.setAdvanced(this.filters);
- }
-
- if (this.boolFilterList) {
- searchManager.setBool(this.boolFilterList);
- }
-
- if (this.primaryFilterName) {
- searchManager.setPrimary(this.primaryFilterName);
- }
-
- collection.where = searchManager.getWhere();
- collection.url = collection.entityType + '/action/listTree';
+ if (this.filters) {
+ searchManager.setAdvanced(this.filters);
+ }
- const viewName =
- this.getMetadata().get(`clientDefs.${this.entityType}.recordViews.listSelectCategoryTree`) ||
- 'views/record/list-tree';
+ if (this.boolFilterList) {
+ searchManager.setBool(this.boolFilterList);
+ }
- this.listenToOnce(collection, 'sync', () => {
- this.createView('list', viewName, {
- collection: collection,
- fullSelector: this.containerSelector + ' .list-container',
- readOnly: true,
- selectable: true,
- checkboxes: this.multiple,
- massActionsDisabled: true,
- searchManager: searchManager,
- checkAllResultDisabled: true,
- buttonsDisabled: true,
- }, listView => {
- listView.once('select', model => {
- this.trigger('select', model);
+ if (this.primaryFilterName) {
+ searchManager.setPrimary(this.primaryFilterName);
+ }
- if (this.options.onSelect) {
- this.options.onSelect([model]);
- }
+ collection.where = searchManager.getWhere();
+ collection.url = collection.entityType + '/action/listTree';
+
+ const viewName =
+ this.getMetadata().get(`clientDefs.${this.entityType}.recordViews.listSelectCategoryTree`) ||
+ 'views/record/list-tree';
+
+ this.listenToOnce(collection, 'sync', () => {
+ this.createView('list', viewName, {
+ collection: collection,
+ fullSelector: this.containerSelector + ' .list-container',
+ readOnly: true,
+ selectable: true,
+ checkboxes: this.multiple,
+ massActionsDisabled: true,
+ searchManager: searchManager,
+ checkAllResultDisabled: true,
+ buttonsDisabled: true,
+ }, listView => {
+ listView.once('select', model => {
+ this.trigger('select', model);
+
+ if (this.options.onSelect) {
+ this.options.onSelect([model]);
+ }
- this.close();
- });
+ this.close();
});
});
-
- collection.fetch();
});
+
+ collection.fetch();
});
}
}
diff --git a/client/src/views/modals/select-records.js b/client/src/views/modals/select-records.js
index 8af5e73257..90b7f35dfb 100644
--- a/client/src/views/modals/select-records.js
+++ b/client/src/views/modals/select-records.js
@@ -270,36 +270,33 @@ class SelectRecordsModalView extends ModalView {
}
setupSearch() {
- const searchManager = this.searchManager =
- new SearchManager(this.collection, 'listSelect', null, this.getDateTime());
-
- searchManager.emptyOnReset = true;
+ this.searchManager = new SearchManager(this.collection, {emptyOnReset: true});
if (this.filters) {
- searchManager.setAdvanced(this.filters);
+ this.searchManager.setAdvanced(this.filters);
}
const boolFilterList = this.boolFilterList ||
this.getMetadata().get(`clientDefs.${this.scope}.selectDefaultFilters.boolFilterList`);
if (boolFilterList) {
- searchManager.setBool(boolFilterList);
+ this.searchManager.setBool(boolFilterList);
}
const primaryFilterName = this.primaryFilterName ||
this.getMetadata().get(`clientDefs.${this.scope}.selectDefaultFilters.filter`);
if (primaryFilterName) {
- searchManager.setPrimary(primaryFilterName);
+ this.searchManager.setPrimary(primaryFilterName);
}
- this.collection.where = searchManager.getWhere();
+ this.collection.where = this.searchManager.getWhere();
if (this.searchPanel) {
this.createView('search', 'views/record/search', {
collection: this.collection,
fullSelector: this.containerSelector + ' .search-container',
- searchManager: searchManager,
+ searchManager: this.searchManager,
disableSavePreset: true,
filterList: this.filterList,
}, view => {
@@ -309,8 +306,8 @@ class SelectRecordsModalView extends ModalView {
}
setupList() {
- const viewName = this.getMetadata().get('clientDefs.' + this.scope + '.recordViews.listSelect') ||
- this.getMetadata().get('clientDefs.' + this.scope + '.recordViews.list') ||
+ const viewName = this.getMetadata().get(`clientDefs.${this.scope}.recordViews.listSelect`) ||
+ this.getMetadata().get(`clientDefs.${this.scope}.recordViews.list`) ||
'views/record/list';
const promise = this.createView('list', viewName, {
@@ -346,8 +343,7 @@ class SelectRecordsModalView extends ModalView {
this.listenTo(view, 'check', () => {
if (view.checkedList.length) {
this.enableButton('select');
- }
- else {
+ } else {
this.disableButton('select');
}
});
diff --git a/client/src/views/record/panels/relationship.js b/client/src/views/record/panels/relationship.js
index c72ddb2a94..aa58a7326f 100644
--- a/client/src/views/record/panels/relationship.js
+++ b/client/src/views/record/panels/relationship.js
@@ -291,9 +291,10 @@ class RelationshipPanelView extends BottomPanelView {
collection.maxSize = this.recordsPerPage || this.getConfig().get('recordsPerPageSmall') || 5;
if (this.defs.filters) {
- const searchManager = new SearchManager(collection, 'listRelationship', null, this.getDateTime());
+ const searchManager = new SearchManager(collection);
searchManager.setAdvanced(this.defs.filters);
+
collection.where = searchManager.getWhere();
}