Created
February 13, 2017 18:47
-
-
Save betobaz/6358c1b9331d364b240b8ecf7247160a to your computer and use it in GitHub Desktop.
SugarCRM: Default filter on search view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
extendsFrom: "FilterLayout", | |
/** | |
* Applies filter on current contexts | |
* @param {String} query search string | |
* @param {Object} dynamicFilterDef(optional) | |
*/ | |
applyFilter: function(query, dynamicFilterDef) { | |
// TODO: getRelevantContextList needs to be refactored to handle filterpanels in drawer layouts, | |
// as it will return the global context instead of filtering a list view within the drawer context. | |
// As a result, this flag is needed to prevent filtering on the global context. | |
var filterOptions = this.context.get('filterOptions') || {}; | |
if (filterOptions.auto_apply === false) { | |
return; | |
} | |
/** | |
* Start custom filter | |
**/ | |
// Put your custom filter here | |
if(this.module === 'GPE_Grupo_empresas' && app.controller.context.get('module') === 'Accounts'){ | |
dynamicFilterDef = dynamicFilterDef || []; | |
dynamicFilterDef.push({ | |
estado: "Activo" | |
}); | |
} | |
/** | |
* End custom filter | |
**/ | |
// to make sure quick filter is handled properly | |
if (_.isEmpty(query)) { | |
var filterQuicksearchView = this.getComponent('filter-quicksearch'); | |
query = filterQuicksearchView && filterQuicksearchView.$el.val() || ''; | |
} | |
//If the quicksearch field is not empty, append a remove icon so the user can clear the search easily | |
this._toggleClearQuickSearchIcon(!_.isEmpty(query)); | |
var self = this; | |
var ctxList = this.getRelevantContextList(); | |
// Here we split the relevant contexts into two groups, 'count', and | |
// 'fetch'. For the 'count' contexts, we do a 'fetchOnlyIds' on their | |
// collection so we can update the count and highlight the subpanel | |
// icon, even though they are collapsed. For the 'fetch' group, we do a | |
// full collection fetch so the subpanel can render its list view. | |
var relevantCtx = _.groupBy(ctxList, function(ctx) { | |
return ctx.get('collapsed') ? 'count' : 'fetch'; | |
}); | |
var batchId = relevantCtx.count && relevantCtx.count.length > 1 ? _.uniqueId() : false; | |
_.each(relevantCtx.count, function(ctx) { | |
var ctxCollection = ctx.get('collection'); | |
var origFilterDef = dynamicFilterDef || ctxCollection.origFilterDef || []; | |
var filterDef = self.buildFilterDef(origFilterDef, query, ctx); | |
var options = { | |
//Show alerts for this request | |
showAlerts: true, | |
apiOptions: { | |
bulk: batchId | |
} | |
}; | |
ctxCollection.filterDef = filterDef; | |
ctxCollection.origFilterDef = origFilterDef; | |
ctxCollection.resetPagination(); | |
options = _.extend(options, ctx.get('collectionOptions')); | |
ctx.resetLoadFlag(false); | |
ctx.set('skipFetch', true); | |
ctx.loadData(options); | |
// We need to reset twice so we can trigger the other bulk call. | |
ctx.resetLoadFlag(false); | |
options.success = _.bind(function(hasAmount, properties) { | |
if (!this.disposed) { | |
ctx.trigger('refresh:count', hasAmount, properties); | |
} | |
}, this); | |
ctxCollection.hasAtLeast(ctx.get('limit'), options); | |
}); | |
// FIXME: Filters should not be triggering the bulk request and should | |
// be moved to subpanels instead. Will be fixed as part of SC-4533. | |
if (batchId) { | |
app.api.triggerBulkCall(batchId); | |
} | |
batchId = relevantCtx.fetch && relevantCtx.fetch.length > 1 ? _.uniqueId() : false; | |
_.each(relevantCtx.fetch, function(ctx) { | |
var ctxCollection = ctx.get('collection'); | |
var origFilterDef = dynamicFilterDef || ctxCollection.origFilterDef || []; | |
var filterDef = self.buildFilterDef(origFilterDef, query, ctx); | |
var options = { | |
//Show alerts for this request | |
showAlerts: true, | |
apiOptions: { | |
bulk: batchId | |
}, | |
success: function(collection, response, options) { | |
// Close the preview pane to ensure that the preview | |
// collection is in sync with the list collection. | |
app.events.trigger('preview:close'); | |
} | |
}; | |
ctxCollection.filterDef = filterDef; | |
ctxCollection.origFilterDef = origFilterDef; | |
ctx.resetLoadFlag(false); | |
if (!_.isEmpty(ctx._recordListFields)) { | |
ctx.set('fields', ctx._recordListFields); | |
} | |
ctx.set('skipFetch', false); | |
ctx.loadData(options); | |
}); | |
if (batchId) { | |
app.api.triggerBulkCall(batchId); | |
} | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment