From 400b0b5f0b6b9fb3ae250be08e21649f2192e4d3 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Mon, 6 Dec 2021 16:50:45 -0500 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20more=20resource=20fi?= =?UTF-8?q?ltering=20name=20changes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...lter-param.js => route-resource-filter.js} | 91 ++++++++++++------- ...> has-route-resource-filter-selections.js} | 4 +- ...lter-param.js => route-resource-filter.js} | 4 +- .../app/decorators/resource-filter-param.js | 1 - .../app/decorators/route-resource-filter.js | 1 + .../has-resource-filter-param-selections.js | 1 - .../has-route-resource-filter-selections.js | 1 + .../core/app/helpers/resource-filter-param.js | 1 - .../core/app/helpers/route-resource-filter.js | 1 + .../app/routes/scopes/scope/auth-methods.js | 2 +- ui/admin/app/routes/scopes/scope/sessions.js | 4 +- .../scopes/scope/auth-methods/index.hbs | 8 +- .../templates/scopes/scope/sessions/index.hbs | 8 +- .../routes/scopes/scope/projects/sessions.js | 4 +- .../scopes/scope/projects/sessions/index.hbs | 8 +- 15 files changed, 83 insertions(+), 56 deletions(-) rename addons/core/addon/decorators/{resource-filter-param.js => route-resource-filter.js} (63%) rename addons/core/addon/helpers/{has-resource-filter-param-selections.js => has-route-resource-filter-selections.js} (92%) rename addons/core/addon/helpers/{resource-filter-param.js => route-resource-filter.js} (93%) delete mode 100644 addons/core/app/decorators/resource-filter-param.js create mode 100644 addons/core/app/decorators/route-resource-filter.js delete mode 100644 addons/core/app/helpers/has-resource-filter-param-selections.js create mode 100644 addons/core/app/helpers/has-route-resource-filter-selections.js delete mode 100644 addons/core/app/helpers/resource-filter-param.js create mode 100644 addons/core/app/helpers/route-resource-filter.js diff --git a/addons/core/addon/decorators/resource-filter-param.js b/addons/core/addon/decorators/route-resource-filter.js similarity index 63% rename from addons/core/addon/decorators/resource-filter-param.js rename to addons/core/addon/decorators/route-resource-filter.js index fbe18975a..4bc519f36 100644 --- a/addons/core/addon/decorators/resource-filter-param.js +++ b/addons/core/addon/decorators/route-resource-filter.js @@ -3,14 +3,16 @@ import { getOwner } from '@ember/application'; import { typeOf } from '@ember/utils'; /** - * + * @param value + * @return {boolean} true if `value` is a function */ const isFunction = (value) => typeOf(value) === 'function'; /** - * + * Represents a filter on a specific route and maintains filter state. + * @class */ -class ResourceFilter extends EmberObject { +class RouteResourceFilter extends EmberObject { // =attributes route; @@ -18,17 +20,22 @@ class ResourceFilter extends EmberObject { defaultValue; allowed; serialize; - deserialize; + findBySerialized; + + // =computed /** - * + * Query parameter name where the selected value(s) for this filter are + * stored in the URL. + * @type {string} */ get filterKey() { return `filter-${this.name}`; } /** - * + * The allowed values for this filter. See `allowed`. + * @type {?array} */ get allowedValues() { if (isFunction(this.allowed)) { @@ -38,7 +45,9 @@ class ResourceFilter extends EmberObject { } /** - * + * Currently selected value(s) for this filter, deserialized from the value of + * the associated route query parameter. + * @type {?array} */ get value() { const decodedValue = @@ -54,10 +63,6 @@ class ResourceFilter extends EmberObject { : null; return deserializedValue; } - - /** - * - */ set value(value) { const queryParams = {}; const serialized = value.map((value) => this.serializeValue(value)); @@ -68,7 +73,8 @@ class ResourceFilter extends EmberObject { // =methods /** - * + * Calls `serialize` (if set) on the passed value. Otherwise returns the + * value unaltered. */ serializeValue(value) { if (this.serialize) { @@ -78,11 +84,16 @@ class ResourceFilter extends EmberObject { } /** - * + * Tests if `item` is represented by `serializedValue` when deserializing + * items from the query string. If `findBySerialized` is unspecified, + * a simple equality test is used. + * @param item + * @param {string} serializedValue + * @return {boolean} item is represented by `serializedValue` */ deserializeValue(item, serializedValue) { - if (this.deserialize) { - return this.deserialize(...arguments); + if (this.findBySerialized) { + return this.findBySerialized(...arguments); } return item === serializedValue; } @@ -90,7 +101,12 @@ class ResourceFilter extends EmberObject { // =static methods /** - * + * Configures the specified route with a query parameter of `name` that, when + * changed, refreshes the route model and replaces history (rather than + * advancing history). + * @static + * @param {Route} routeClass + * @param {string} name */ static setupRouteQueryParams(routeClass, name) { const filterKey = `filter-${name}`; @@ -112,7 +128,15 @@ class ResourceFilter extends EmberObject { } /** - * + * Factory method to instantiate an instance of this class. + * @static + * @param {Route} routeInstance + * @param {string} name + * @param {array|function} allowed + * @param {?array} defaultValue + * @param {?function} serialize + * @param {?function} findBySerialized + * @return {RouteResourceFilter} */ static getOrCreateRouteResourceFilter( routeInstance, @@ -120,22 +144,22 @@ class ResourceFilter extends EmberObject { allowed, defaultValue, serialize, - deserialize + findBySerialized ) { const owner = getOwner(routeInstance); const containerKey = `route-resource-filter:${name}@${routeInstance.routeName}`; const factory = owner.factoryFor(containerKey); if (!factory) { // create factory - class RouteResourceFilter extends this { + class AppliedRouteResourceFilter extends this { route = routeInstance; name = name; allowed = allowed; defaultValue = defaultValue; serialize = serialize; - deserialize = deserialize; + findBySerialized = findBySerialized; } - owner.register(containerKey, RouteResourceFilter); + owner.register(containerKey, AppliedRouteResourceFilter); } const instance = owner.lookup(containerKey); return instance; @@ -143,18 +167,18 @@ class ResourceFilter extends EmberObject { } /** - * A resource filter param is a concise way to express a route query parameter. + * This decorator provides a concise way to express a route resource filter. * Resource filter params abstract away a route's query parameter - * interface, treating a query param as a simple attribute on a route instance. + * interface, treating a query param as just an attribute on a route instance. * Getting the attribute value is equivalent to looking up the associated query * parameter. Setting the attribute is equivalent to transitioning to the route * and passing the associated query parameter value. * * Additionally, resource filter params may declare `allowed`, an array - * containing the set of values the parameter may take. While unenforced, - * `allowed` are useful metadata about a resource filter and may be fetch - * via `ResourceFilterParamHelper`. An optional default value may be specified - * via `defaultValue` passed to the decorator. + * (or function returning an array) containing the set of values the parameter + * may take. While unenforced, `allowed` are useful metadata about a resource + * filter and may be fetch via `ResourceFilterParamHelper`. An optional default + * value may be specified via `defaultValue` passed to the decorator. * * @see ResourceFilterParamHelper * @@ -162,7 +186,10 @@ class ResourceFilter extends EmberObject { * * export default class MyRoute extends Route { * - * @resourceFilterParam(['active', 'pending'], ['active']) + * @resourceFilterParam({ + * allowed: ['active', 'pending'], + * defaultvalue: ['active'], // optional + * }) * status; * * @action @@ -180,7 +207,7 @@ export function resourceFilterParam({ allowed, defaultValue, serialize, - deserialize, + findBySerialized, }) { /** * @param {object} target @@ -189,7 +216,7 @@ export function resourceFilterParam({ * @return {object{get, set}} */ return function (target, name /*, descriptor*/) { - ResourceFilter.setupRouteQueryParams(target, name); + RouteResourceFilter.setupRouteQueryParams(target, name); let instance; @@ -199,13 +226,13 @@ export function resourceFilterParam({ * Returns the current value of the resource filter. */ get() { - instance = ResourceFilter.getOrCreateRouteResourceFilter( + instance = RouteResourceFilter.getOrCreateRouteResourceFilter( this, name, allowed, defaultValue, serialize, - deserialize + findBySerialized ); return instance.value; }, diff --git a/addons/core/addon/helpers/has-resource-filter-param-selections.js b/addons/core/addon/helpers/has-route-resource-filter-selections.js similarity index 92% rename from addons/core/addon/helpers/has-resource-filter-param-selections.js rename to addons/core/addon/helpers/has-route-resource-filter-selections.js index 2768c5187..fabdddab7 100644 --- a/addons/core/addon/helpers/has-resource-filter-param-selections.js +++ b/addons/core/addon/helpers/has-route-resource-filter-selections.js @@ -11,11 +11,11 @@ import { action } from '@ember/object'; * * @example * - * {{#if (has-resource-filter-param-selections 'route.name' 'field')}} + * {{#if (has-route-resource-filter-selections 'route.name' 'field')}} * Field has filter selections. * {{/if}} */ -export default class HasResourceFilterParamSelectionsHelper extends Helper { +export default class HasRouteResourceFilterSelectionsHelper extends Helper { // =services @service router; diff --git a/addons/core/addon/helpers/resource-filter-param.js b/addons/core/addon/helpers/route-resource-filter.js similarity index 93% rename from addons/core/addon/helpers/resource-filter-param.js rename to addons/core/addon/helpers/route-resource-filter.js index d777c3737..483202b0c 100644 --- a/addons/core/addon/helpers/resource-filter-param.js +++ b/addons/core/addon/helpers/route-resource-filter.js @@ -11,11 +11,11 @@ import { action } from '@ember/object'; * * @example * - * {{#with resource-filter-param 'status' as |param|}} + * {{#with route-resource-filter 'status' as |param|}} * {{param.name}} / {{param.value}} / {{param.selectedValue}} * {{/with}} */ -export default class ResourceFilterParamHelper extends Helper { +export default class RouteResourceFilterHelper extends Helper { // =services @service router; diff --git a/addons/core/app/decorators/resource-filter-param.js b/addons/core/app/decorators/resource-filter-param.js deleted file mode 100644 index 4e5cebc3c..000000000 --- a/addons/core/app/decorators/resource-filter-param.js +++ /dev/null @@ -1 +0,0 @@ -export { default } from 'core/decorators/resource-filter-param'; diff --git a/addons/core/app/decorators/route-resource-filter.js b/addons/core/app/decorators/route-resource-filter.js new file mode 100644 index 000000000..0cb7c2631 --- /dev/null +++ b/addons/core/app/decorators/route-resource-filter.js @@ -0,0 +1 @@ +export { default } from 'core/decorators/route-resource-filter'; diff --git a/addons/core/app/helpers/has-resource-filter-param-selections.js b/addons/core/app/helpers/has-resource-filter-param-selections.js deleted file mode 100644 index b6ab1bc86..000000000 --- a/addons/core/app/helpers/has-resource-filter-param-selections.js +++ /dev/null @@ -1 +0,0 @@ -export { default } from 'core/helpers/has-resource-filter-param-selections'; diff --git a/addons/core/app/helpers/has-route-resource-filter-selections.js b/addons/core/app/helpers/has-route-resource-filter-selections.js new file mode 100644 index 000000000..53d9763a0 --- /dev/null +++ b/addons/core/app/helpers/has-route-resource-filter-selections.js @@ -0,0 +1 @@ +export { default } from 'core/helpers/has-route-resource-filter-selections'; diff --git a/addons/core/app/helpers/resource-filter-param.js b/addons/core/app/helpers/resource-filter-param.js deleted file mode 100644 index 8977c55f0..000000000 --- a/addons/core/app/helpers/resource-filter-param.js +++ /dev/null @@ -1 +0,0 @@ -export { default } from 'core/helpers/resource-filter-param'; diff --git a/addons/core/app/helpers/route-resource-filter.js b/addons/core/app/helpers/route-resource-filter.js new file mode 100644 index 000000000..42f9b4ea1 --- /dev/null +++ b/addons/core/app/helpers/route-resource-filter.js @@ -0,0 +1 @@ +export { default } from 'core/helpers/route-resource-filter'; diff --git a/ui/admin/app/routes/scopes/scope/auth-methods.js b/ui/admin/app/routes/scopes/scope/auth-methods.js index 155345a39..f77cb4409 100644 --- a/ui/admin/app/routes/scopes/scope/auth-methods.js +++ b/ui/admin/app/routes/scopes/scope/auth-methods.js @@ -4,7 +4,7 @@ import { action } from '@ember/object'; import loading from 'ember-loading/decorator'; import { confirm } from 'core/decorators/confirm'; import { notifySuccess, notifyError } from 'core/decorators/notify'; -import { resourceFilterParam } from 'core/decorators/resource-filter-param'; +import { resourceFilterParam } from 'core/decorators/route-resource-filter'; export default class ScopesScopeAuthMethodsRoute extends Route { // =services diff --git a/ui/admin/app/routes/scopes/scope/sessions.js b/ui/admin/app/routes/scopes/scope/sessions.js index 525cfdf1d..f572bb15d 100644 --- a/ui/admin/app/routes/scopes/scope/sessions.js +++ b/ui/admin/app/routes/scopes/scope/sessions.js @@ -6,7 +6,7 @@ import { task, timeout } from 'ember-concurrency'; import { A } from '@ember/array'; import { notifySuccess, notifyError } from 'core/decorators/notify'; import config from '../../../config/environment'; -import { resourceFilterParam } from 'core/decorators/resource-filter-param'; +import { resourceFilterParam } from 'core/decorators/route-resource-filter'; const POLL_TIMEOUT_SECONDS = config.sessionPollingTimeoutSeconds; @@ -128,7 +128,7 @@ export default class ScopesScopeSessionsRoute extends Route { filterBy(field, value) { this[field] = value; } - + /** * Clears and filter selections. */ diff --git a/ui/admin/app/templates/scopes/scope/auth-methods/index.hbs b/ui/admin/app/templates/scopes/scope/auth-methods/index.hbs index cf69c278e..1160284be 100644 --- a/ui/admin/app/templates/scopes/scope/auth-methods/index.hbs +++ b/ui/admin/app/templates/scopes/scope/auth-methods/index.hbs @@ -39,7 +39,7 @@ {{#if (or @model - (has-resource-filter-param-selections + (has-route-resource-filter-selections 'scopes.scope.auth-methods' 'type' ) ) @@ -47,7 +47,7 @@ {{#with - (resource-filter-param 'scopes.scope.auth-methods' 'type') + (route-resource-filter 'scopes.scope.auth-methods' 'type') as |filter| }} - {{else if (has-resource-filter-param-selections + {{else if (has-route-resource-filter-selections 'scopes.scope.auth-methods' 'type' )}} diff --git a/ui/admin/app/templates/scopes/scope/sessions/index.hbs b/ui/admin/app/templates/scopes/scope/sessions/index.hbs index c3492de75..591b6ecc6 100644 --- a/ui/admin/app/templates/scopes/scope/sessions/index.hbs +++ b/ui/admin/app/templates/scopes/scope/sessions/index.hbs @@ -17,13 +17,13 @@ {{#if (or @model - (has-resource-filter-param-selections 'scopes.scope.sessions' 'type') + (has-route-resource-filter-selections 'scopes.scope.sessions' 'type') ) }} {{#with - (resource-filter-param 'scopes.scope.sessions' 'status') + (route-resource-filter 'scopes.scope.sessions' 'status') as |filter| }} - {{else if (has-resource-filter-param-selections + {{else if (has-route-resource-filter-selections 'scopes.scope.sessions' 'type' )}} diff --git a/ui/desktop/app/routes/scopes/scope/projects/sessions.js b/ui/desktop/app/routes/scopes/scope/projects/sessions.js index f190e1b75..3f351ad6f 100644 --- a/ui/desktop/app/routes/scopes/scope/projects/sessions.js +++ b/ui/desktop/app/routes/scopes/scope/projects/sessions.js @@ -4,7 +4,7 @@ import { task, timeout } from 'ember-concurrency'; import config from '../../../../config/environment'; import { action } from '@ember/object'; import { notifySuccess, notifyError } from 'core/decorators/notify'; -import { resourceFilterParam } from 'core/decorators/resource-filter-param'; +import { resourceFilterParam } from 'core/decorators/route-resource-filter'; const POLL_TIMEOUT_SECONDS = config.sessionPollingTimeoutSeconds; @@ -100,7 +100,7 @@ export default class ScopesScopeProjectsSessionsRoute extends Route { filterBy(field, value) { this[field] = value; } - + /** * Clears and filter selections. */ diff --git a/ui/desktop/app/templates/scopes/scope/projects/sessions/index.hbs b/ui/desktop/app/templates/scopes/scope/projects/sessions/index.hbs index a92fb4fe9..055b1558d 100644 --- a/ui/desktop/app/templates/scopes/scope/projects/sessions/index.hbs +++ b/ui/desktop/app/templates/scopes/scope/projects/sessions/index.hbs @@ -13,7 +13,7 @@ {{#if (or @model - (has-resource-filter-param-selections + (has-route-resource-filter-selections 'scopes.scope.projects.sessions' 'type' ) ) @@ -21,7 +21,7 @@ {{#with - (resource-filter-param 'scopes.scope.projects.sessions' 'status') + (route-resource-filter 'scopes.scope.projects.sessions' 'status') as |filter| }} - {{else if (has-resource-filter-param-selections + {{else if (has-route-resource-filter-selections 'scopes.scope.projects.sessions' 'type' )}}