From 5dc99ef5ad2be4caa55a557709d9a8b108858a6a Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Tue, 30 Nov 2021 15:18:59 -0500 Subject: [PATCH 1/4] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20move=20resource?= =?UTF-8?q?=20filter=20logic=20into=20dedicated=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../addon/decorators/resource-filter-param.js | 130 ++++++++++++++---- .../has-resource-filter-param-selections.js | 6 +- .../addon/helpers/resource-filter-param.js | 10 +- .../scopes/scope/auth-methods/index.hbs | 10 +- .../templates/scopes/scope/sessions/index.hbs | 11 +- .../scopes/scope/projects/sessions/index.hbs | 6 +- 6 files changed, 125 insertions(+), 48 deletions(-) diff --git a/addons/core/addon/decorators/resource-filter-param.js b/addons/core/addon/decorators/resource-filter-param.js index b8395a20f..6619ee6ee 100644 --- a/addons/core/addon/decorators/resource-filter-param.js +++ b/addons/core/addon/decorators/resource-filter-param.js @@ -1,3 +1,94 @@ +import EmberObject from '@ember/object'; +import { getOwner } from '@ember/application'; + +/** + * + */ +class ResourceFilter extends EmberObject { + // =attributes + + route; + name; + allowedValues; + defaultValue; + + /** + * + */ + get filterKey() { + return `filter-${this.name}`; + } + + /** + * + */ + get value() { + const value = + this.route.paramsFor(this.route.routeName)[this.filterKey] || + JSON.stringify(this.defaultValue); + return value ? JSON.parse(value) : null; + } + + /** + * + */ + set value(value) { + const queryParams = {}; + queryParams[this.filterKey] = JSON.stringify(value); + this.route.transitionTo({ queryParams }); + } + + // =static methods + + /** + * + */ + static setupRouteQueryParams(routeClass, name) { + const filterKey = `filter-${name}`; + + // If the route has no query params specified yet, declare them. + if (!Object.keys(routeClass.queryParams).length) + routeClass.queryParams = {}; + + // If the route has no resource filters list specified yet, create one. + if (!routeClass.resourceFilterParams) routeClass.resourceFilterParams = []; + + // Add the resource filter query parameter to the route. Resource filter + // param changes refresh the model but do not contribute to browser history. + // See https://guides.emberjs.com/release/routing/query-params/ + routeClass.queryParams[filterKey] = { + refreshModel: true, + replace: true, + }; + } + + /** + * + */ + static getOrCreateRouteResourceFilter( + routeInstance, + name, + allowedValues, + defaultValue + ) { + 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 { + route = routeInstance; + name = name; + allowedValues = allowedValues; + defaultValue = defaultValue; + } + owner.register(containerKey, RouteResourceFilter); + } + const instance = owner.lookup(containerKey); + return instance; + } +} + /** * A resource filter param is a concise way to express a route query parameter. * Resource filter params abstract away a route's query parameter @@ -40,29 +131,9 @@ export function resourceFilterParam(allowedValues, defaultValue) { * @return {object{get, set}} */ return function (target, name /*, descriptor*/) { - const filterKey = `filter-${name}`; - const filterAllowedValuesKey = `filter-allowed-values-${name}`; + ResourceFilter.setupRouteQueryParams(target, name); - // If the route has no query params specified yet, declare them. - if (!Object.keys(target.queryParams).length) target.queryParams = {}; - - // If the route has no resource filters list specified yet, create one. - if (!target.resourceFilterParams) target.resourceFilterParams = []; - - // Add the resource filter query parameter to the route. Resource filter - // param changes refresh the model but do not contribute to browser history. - // See https://guides.emberjs.com/release/routing/query-params/ - target.queryParams[filterKey] = { - refreshModel: true, - replace: true, - }; - - // Note the name of this filter param. - target.resourceFilterParams.push(name); - - // Store the allowed values for future lookup - // (via `ResourceFilterParamHelper`). - target[filterAllowedValuesKey] = allowedValues; + let instance; // Override the decorated attribute with a getter and setter. return { @@ -70,10 +141,13 @@ export function resourceFilterParam(allowedValues, defaultValue) { * Returns the JSON-parsed query parameter value OR defaultValue. */ get() { - const value = - this.paramsFor(this.routeName)[filterKey] || - JSON.stringify(defaultValue); - return value ? JSON.parse(value) : null; + instance = ResourceFilter.getOrCreateRouteResourceFilter( + this, + name, + allowedValues, + defaultValue + ); + return instance.value; }, /** @@ -83,9 +157,7 @@ export function resourceFilterParam(allowedValues, defaultValue) { * @param value */ set(value) { - const queryParams = {}; - queryParams[filterKey] = JSON.stringify(value); - this.transitionTo({ queryParams }); + instance.value = value; }, }; }; diff --git a/addons/core/addon/helpers/has-resource-filter-param-selections.js b/addons/core/addon/helpers/has-resource-filter-param-selections.js index 6f6015078..2768c5187 100644 --- a/addons/core/addon/helpers/has-resource-filter-param-selections.js +++ b/addons/core/addon/helpers/has-resource-filter-param-selections.js @@ -44,14 +44,14 @@ export default class HasResourceFilterParamSelectionsHelper extends Helper { /** * Returns true if the specified route has a value set for - * any resource filter. + * any resource filter passed into the helper. * @param {string} routeName + * @param {string[]} names - names of resource filter fields * @return {boolean} */ - compute([routeName]) { + compute([routeName, ...names]) { const owner = getOwner(this); const route = owner.lookup(`route:${routeName}`); - const names = route.resourceFilterParams; const selectedValues = names.map((name) => route[name] || null).flat(); const anyTruthy = selectedValues.reduce( (previousValue, currentValue) => previousValue || currentValue, diff --git a/addons/core/addon/helpers/resource-filter-param.js b/addons/core/addon/helpers/resource-filter-param.js index e78ac20a7..d777c3737 100644 --- a/addons/core/addon/helpers/resource-filter-param.js +++ b/addons/core/addon/helpers/resource-filter-param.js @@ -50,13 +50,9 @@ export default class ResourceFilterParamHelper extends Helper { */ compute([routeName, name]) { const owner = getOwner(this); - // Filter options - const route = owner.lookup(`route:${routeName}`); - const filterAllowedValuesKey = `filter-allowed-values-${name}`; - const allowedValues = route[filterAllowedValuesKey]; - // Selected filters - const rawValue = route[name]; - const selectedValue = rawValue || null; + const containerKey = `route-resource-filter:${name}@${routeName}`; + const resourceFilter = owner.lookup(containerKey); + const { allowedValues, value: selectedValue } = resourceFilter; return { name, allowedValues, selectedValue }; } 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 d6075980e..cf69c278e 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,9 @@ {{#if (or @model - (has-resource-filter-param-selections 'scopes.scope.auth-methods') + (has-resource-filter-param-selections + 'scopes.scope.auth-methods' 'type' + ) ) }} @@ -74,7 +76,9 @@ {{/with}} {{#if - (has-resource-filter-param-selections 'scopes.scope.auth-methods') + (has-resource-filter-param-selections + 'scopes.scope.auth-methods' 'type' + ) }} {{else if (has-resource-filter-param-selections - 'scopes.scope.auth-methods' + '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 d5052b1ee..c3492de75 100644 --- a/ui/admin/app/templates/scopes/scope/sessions/index.hbs +++ b/ui/admin/app/templates/scopes/scope/sessions/index.hbs @@ -16,7 +16,8 @@ {{#if (feature-flag 'filter')}} {{#if (or - @model (has-resource-filter-param-selections 'scopes.scope.sessions') + @model + (has-resource-filter-param-selections 'scopes.scope.sessions' 'type') ) }} @@ -51,7 +52,9 @@ {{/with}} {{#if - (has-resource-filter-param-selections 'scopes.scope.sessions') + (has-resource-filter-param-selections + 'scopes.scope.sessions' 'type' + ) }} - {{else if (has-resource-filter-param-selections 'scopes.scope.sessions')}} + {{else if (has-resource-filter-param-selections + 'scopes.scope.sessions' 'type' + )}} 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 2ebe5491b..a92fb4fe9 100644 --- a/ui/desktop/app/templates/scopes/scope/projects/sessions/index.hbs +++ b/ui/desktop/app/templates/scopes/scope/projects/sessions/index.hbs @@ -14,7 +14,7 @@ (or @model (has-resource-filter-param-selections - 'scopes.scope.projects.sessions' + 'scopes.scope.projects.sessions' 'type' ) ) }} @@ -51,7 +51,7 @@ {{#if (has-resource-filter-param-selections - 'scopes.scope.projects.sessions' + 'scopes.scope.projects.sessions' 'type' ) }} {{else if (has-resource-filter-param-selections - 'scopes.scope.projects.sessions' + 'scopes.scope.projects.sessions' 'type' )}} From b3d0bf56855116aad7f16e369cfc21b4e8ad59d4 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Fri, 3 Dec 2021 13:58:12 -0500 Subject: [PATCH 2/4] refactor: support dynamic resource filters and apply to auth methods --- .../addon/decorators/resource-filter-param.js | 86 ++++++++++++++++--- .../app/routes/scopes/scope/auth-methods.js | 2 +- 2 files changed, 74 insertions(+), 14 deletions(-) diff --git a/addons/core/addon/decorators/resource-filter-param.js b/addons/core/addon/decorators/resource-filter-param.js index 6619ee6ee..fbe18975a 100644 --- a/addons/core/addon/decorators/resource-filter-param.js +++ b/addons/core/addon/decorators/resource-filter-param.js @@ -1,5 +1,11 @@ import EmberObject from '@ember/object'; import { getOwner } from '@ember/application'; +import { typeOf } from '@ember/utils'; + +/** + * + */ +const isFunction = (value) => typeOf(value) === 'function'; /** * @@ -9,8 +15,10 @@ class ResourceFilter extends EmberObject { route; name; - allowedValues; defaultValue; + allowed; + serialize; + deserialize; /** * @@ -19,14 +27,32 @@ class ResourceFilter extends EmberObject { return `filter-${this.name}`; } + /** + * + */ + get allowedValues() { + if (isFunction(this.allowed)) { + return this.allowed(this.route); + } + return this.allowed; + } + /** * */ get value() { - const value = + const decodedValue = this.route.paramsFor(this.route.routeName)[this.filterKey] || JSON.stringify(this.defaultValue); - return value ? JSON.parse(value) : null; + const value = decodedValue ? JSON.parse(decodedValue) : null; + const deserializedValue = value + ? value.map((serializedValue) => + this.allowedValues.find((item) => + this.deserializeValue(item, serializedValue) + ) + ) + : null; + return deserializedValue; } /** @@ -34,10 +60,33 @@ class ResourceFilter extends EmberObject { */ set value(value) { const queryParams = {}; - queryParams[this.filterKey] = JSON.stringify(value); + const serialized = value.map((value) => this.serializeValue(value)); + queryParams[this.filterKey] = JSON.stringify(serialized); this.route.transitionTo({ queryParams }); } + // =methods + + /** + * + */ + serializeValue(value) { + if (this.serialize) { + return this.serialize(...arguments); + } + return value; + } + + /** + * + */ + deserializeValue(item, serializedValue) { + if (this.deserialize) { + return this.deserialize(...arguments); + } + return item === serializedValue; + } + // =static methods /** @@ -68,8 +117,10 @@ class ResourceFilter extends EmberObject { static getOrCreateRouteResourceFilter( routeInstance, name, - allowedValues, - defaultValue + allowed, + defaultValue, + serialize, + deserialize ) { const owner = getOwner(routeInstance); const containerKey = `route-resource-filter:${name}@${routeInstance.routeName}`; @@ -79,8 +130,10 @@ class ResourceFilter extends EmberObject { class RouteResourceFilter extends this { route = routeInstance; name = name; - allowedValues = allowedValues; + allowed = allowed; defaultValue = defaultValue; + serialize = serialize; + deserialize = deserialize; } owner.register(containerKey, RouteResourceFilter); } @@ -97,9 +150,9 @@ class ResourceFilter extends EmberObject { * parameter. Setting the attribute is equivalent to transitioning to the route * and passing the associated query parameter value. * - * Additionally, resource filter params may declare `allowedValues`, an array + * Additionally, resource filter params may declare `allowed`, an array * containing the set of values the parameter may take. While unenforced, - * `allowedValues` are useful metadata about a resource filter and may be fetch + * `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. * @@ -123,7 +176,12 @@ class ResourceFilter extends EmberObject { * } * */ -export function resourceFilterParam(allowedValues, defaultValue) { +export function resourceFilterParam({ + allowed, + defaultValue, + serialize, + deserialize, +}) { /** * @param {object} target * @param {string} name @@ -138,14 +196,16 @@ export function resourceFilterParam(allowedValues, defaultValue) { // Override the decorated attribute with a getter and setter. return { /** - * Returns the JSON-parsed query parameter value OR defaultValue. + * Returns the current value of the resource filter. */ get() { instance = ResourceFilter.getOrCreateRouteResourceFilter( this, name, - allowedValues, - defaultValue + allowed, + defaultValue, + serialize, + deserialize ); return instance.value; }, diff --git a/ui/admin/app/routes/scopes/scope/auth-methods.js b/ui/admin/app/routes/scopes/scope/auth-methods.js index e83a95911..155345a39 100644 --- a/ui/admin/app/routes/scopes/scope/auth-methods.js +++ b/ui/admin/app/routes/scopes/scope/auth-methods.js @@ -17,7 +17,7 @@ export default class ScopesScopeAuthMethodsRoute extends Route { // =attributes - @resourceFilterParam(['password', 'oidc']) type; + @resourceFilterParam({ allowed: ['password', 'oidc'] }) type; // =methods From 400b0b5f0b6b9fb3ae250be08e21649f2192e4d3 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Mon, 6 Dec 2021 16:50:45 -0500 Subject: [PATCH 3/4] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20more=20resource?= =?UTF-8?q?=20filtering=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' )}} From ddc7fe939deafd2ad4efbb4c9159b92c6b197bc8 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Mon, 6 Dec 2021 17:26:36 -0500 Subject: [PATCH 4/4] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20more=20renaming?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...{route-resource-filter.js => resource-filter.js} | 6 +++--- ...ections.js => has-resource-filter-selections.js} | 2 +- ...{route-resource-filter.js => resource-filter.js} | 8 ++++---- addons/core/app/decorators/resource-filter.js | 1 + addons/core/app/decorators/route-resource-filter.js | 1 - .../app/helpers/has-resource-filter-selections.js | 1 + .../helpers/has-route-resource-filter-selections.js | 1 - addons/core/app/helpers/resource-filter.js | 1 + addons/core/app/helpers/route-resource-filter.js | 1 - ui/admin/app/routes/scopes/scope/auth-methods.js | 4 ++-- ui/admin/app/routes/scopes/scope/sessions.js | 7 +++++-- .../templates/scopes/scope/auth-methods/index.hbs | 12 ++++-------- .../app/templates/scopes/scope/sessions/index.hbs | 13 ++++--------- .../app/routes/scopes/scope/projects/sessions.js | 7 +++++-- .../scopes/scope/projects/sessions/index.hbs | 8 ++++---- 15 files changed, 35 insertions(+), 38 deletions(-) rename addons/core/addon/decorators/{route-resource-filter.js => resource-filter.js} (97%) rename addons/core/addon/helpers/{has-route-resource-filter-selections.js => has-resource-filter-selections.js} (96%) rename addons/core/addon/helpers/{route-resource-filter.js => resource-filter.js} (86%) create mode 100644 addons/core/app/decorators/resource-filter.js delete mode 100644 addons/core/app/decorators/route-resource-filter.js create mode 100644 addons/core/app/helpers/has-resource-filter-selections.js delete mode 100644 addons/core/app/helpers/has-route-resource-filter-selections.js create mode 100644 addons/core/app/helpers/resource-filter.js delete mode 100644 addons/core/app/helpers/route-resource-filter.js diff --git a/addons/core/addon/decorators/route-resource-filter.js b/addons/core/addon/decorators/resource-filter.js similarity index 97% rename from addons/core/addon/decorators/route-resource-filter.js rename to addons/core/addon/decorators/resource-filter.js index 4bc519f36..df84cb09a 100644 --- a/addons/core/addon/decorators/route-resource-filter.js +++ b/addons/core/addon/decorators/resource-filter.js @@ -147,7 +147,7 @@ class RouteResourceFilter extends EmberObject { findBySerialized ) { const owner = getOwner(routeInstance); - const containerKey = `route-resource-filter:${name}@${routeInstance.routeName}`; + const containerKey = `resource-filter:${name}@${routeInstance.routeName}`; const factory = owner.factoryFor(containerKey); if (!factory) { // create factory @@ -186,7 +186,7 @@ class RouteResourceFilter extends EmberObject { * * export default class MyRoute extends Route { * - * @resourceFilterParam({ + * @resourceFilter({ * allowed: ['active', 'pending'], * defaultvalue: ['active'], // optional * }) @@ -203,7 +203,7 @@ class RouteResourceFilter extends EmberObject { * } * */ -export function resourceFilterParam({ +export function resourceFilter({ allowed, defaultValue, serialize, diff --git a/addons/core/addon/helpers/has-route-resource-filter-selections.js b/addons/core/addon/helpers/has-resource-filter-selections.js similarity index 96% rename from addons/core/addon/helpers/has-route-resource-filter-selections.js rename to addons/core/addon/helpers/has-resource-filter-selections.js index fabdddab7..3a026c5aa 100644 --- a/addons/core/addon/helpers/has-route-resource-filter-selections.js +++ b/addons/core/addon/helpers/has-resource-filter-selections.js @@ -11,7 +11,7 @@ import { action } from '@ember/object'; * * @example * - * {{#if (has-route-resource-filter-selections 'route.name' 'field')}} + * {{#if (has-resource-filter-selections 'route.name' 'field')}} * Field has filter selections. * {{/if}} */ diff --git a/addons/core/addon/helpers/route-resource-filter.js b/addons/core/addon/helpers/resource-filter.js similarity index 86% rename from addons/core/addon/helpers/route-resource-filter.js rename to addons/core/addon/helpers/resource-filter.js index 483202b0c..1872a1d5d 100644 --- a/addons/core/addon/helpers/route-resource-filter.js +++ b/addons/core/addon/helpers/resource-filter.js @@ -7,15 +7,15 @@ import { action } from '@ember/object'; * Use this helper to fetch the current value of a resource filter param, * as well as its allowedValues. * - * @see resourceFilterParam (decorator) + * @see resourceFilter (decorator) * * @example * - * {{#with route-resource-filter 'status' as |param|}} + * {{#with resource-filter 'status' as |param|}} * {{param.name}} / {{param.value}} / {{param.selectedValue}} * {{/with}} */ -export default class RouteResourceFilterHelper extends Helper { +export default class ResourceFilterHelper extends Helper { // =services @service router; @@ -50,7 +50,7 @@ export default class RouteResourceFilterHelper extends Helper { */ compute([routeName, name]) { const owner = getOwner(this); - const containerKey = `route-resource-filter:${name}@${routeName}`; + const containerKey = `resource-filter:${name}@${routeName}`; const resourceFilter = owner.lookup(containerKey); const { allowedValues, value: selectedValue } = resourceFilter; return { name, allowedValues, selectedValue }; diff --git a/addons/core/app/decorators/resource-filter.js b/addons/core/app/decorators/resource-filter.js new file mode 100644 index 000000000..48ab848b6 --- /dev/null +++ b/addons/core/app/decorators/resource-filter.js @@ -0,0 +1 @@ +export { default } from 'core/decorators/resource-filter'; diff --git a/addons/core/app/decorators/route-resource-filter.js b/addons/core/app/decorators/route-resource-filter.js deleted file mode 100644 index 0cb7c2631..000000000 --- a/addons/core/app/decorators/route-resource-filter.js +++ /dev/null @@ -1 +0,0 @@ -export { default } from 'core/decorators/route-resource-filter'; diff --git a/addons/core/app/helpers/has-resource-filter-selections.js b/addons/core/app/helpers/has-resource-filter-selections.js new file mode 100644 index 000000000..26765e488 --- /dev/null +++ b/addons/core/app/helpers/has-resource-filter-selections.js @@ -0,0 +1 @@ +export { default } from 'core/helpers/has-resource-filter-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 deleted file mode 100644 index 53d9763a0..000000000 --- a/addons/core/app/helpers/has-route-resource-filter-selections.js +++ /dev/null @@ -1 +0,0 @@ -export { default } from 'core/helpers/has-route-resource-filter-selections'; diff --git a/addons/core/app/helpers/resource-filter.js b/addons/core/app/helpers/resource-filter.js new file mode 100644 index 000000000..573aa1050 --- /dev/null +++ b/addons/core/app/helpers/resource-filter.js @@ -0,0 +1 @@ +export { default } from 'core/helpers/resource-filter'; diff --git a/addons/core/app/helpers/route-resource-filter.js b/addons/core/app/helpers/route-resource-filter.js deleted file mode 100644 index 42f9b4ea1..000000000 --- a/addons/core/app/helpers/route-resource-filter.js +++ /dev/null @@ -1 +0,0 @@ -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 f77cb4409..491cdd44d 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/route-resource-filter'; +import { resourceFilter } from 'core/decorators/resource-filter'; export default class ScopesScopeAuthMethodsRoute extends Route { // =services @@ -17,7 +17,7 @@ export default class ScopesScopeAuthMethodsRoute extends Route { // =attributes - @resourceFilterParam({ allowed: ['password', 'oidc'] }) type; + @resourceFilter({ allowed: ['password', 'oidc'] }) type; // =methods diff --git a/ui/admin/app/routes/scopes/scope/sessions.js b/ui/admin/app/routes/scopes/scope/sessions.js index f572bb15d..29a0d87a5 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/route-resource-filter'; +import { resourceFilter } from 'core/decorators/resource-filter'; const POLL_TIMEOUT_SECONDS = config.sessionPollingTimeoutSeconds; @@ -21,7 +21,10 @@ export default class ScopesScopeSessionsRoute extends Route { // =attributes - @resourceFilterParam(['active', 'pending', 'canceling', 'terminated'], ['active','pending', 'canceling']) status; + @resourceFilter({ + allowed: ['active', 'pending', 'canceling', 'terminated'], + defaultValue: ['active','pending', 'canceling'] + }) status; /** * A simple Ember Concurrency-based polling task that refreshes the route 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 1160284be..ea12808c3 100644 --- a/ui/admin/app/templates/scopes/scope/auth-methods/index.hbs +++ b/ui/admin/app/templates/scopes/scope/auth-methods/index.hbs @@ -39,15 +39,13 @@ {{#if (or @model - (has-route-resource-filter-selections - 'scopes.scope.auth-methods' 'type' - ) + (has-resource-filter-selections 'scopes.scope.auth-methods' 'type') ) }} {{#with - (route-resource-filter 'scopes.scope.auth-methods' 'type') + (resource-filter 'scopes.scope.auth-methods' 'type') as |filter| }} - {{else if (has-route-resource-filter-selections + {{else if (has-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 591b6ecc6..ec412bb5e 100644 --- a/ui/admin/app/templates/scopes/scope/sessions/index.hbs +++ b/ui/admin/app/templates/scopes/scope/sessions/index.hbs @@ -16,14 +16,13 @@ {{#if (feature-flag 'filter')}} {{#if (or - @model - (has-route-resource-filter-selections 'scopes.scope.sessions' 'type') + @model (has-resource-filter-selections 'scopes.scope.sessions' 'type') ) }} {{#with - (route-resource-filter 'scopes.scope.sessions' 'status') + (resource-filter 'scopes.scope.sessions' 'status') as |filter| }} - {{else if (has-route-resource-filter-selections - 'scopes.scope.sessions' 'type' - )}} + {{else if (has-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 3f351ad6f..12618d741 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/route-resource-filter'; +import { resourceFilter } from 'core/decorators/resource-filter'; const POLL_TIMEOUT_SECONDS = config.sessionPollingTimeoutSeconds; @@ -18,7 +18,10 @@ export default class ScopesScopeProjectsSessionsRoute extends Route { // =attributes - @resourceFilterParam(['active', 'pending', 'canceling', 'terminated'], ['active','pending', 'canceling']) status; + @resourceFilter({ + allowed: ['active', 'pending', 'canceling', 'terminated'], + defaultValue: ['active','pending', 'canceling'] + }) status; /** * A simple Ember Concurrency-based polling task that refreshes the route 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 055b1558d..798489e7e 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-route-resource-filter-selections + (has-resource-filter-selections 'scopes.scope.projects.sessions' 'type' ) ) @@ -21,7 +21,7 @@ {{#with - (route-resource-filter 'scopes.scope.projects.sessions' 'status') + (resource-filter 'scopes.scope.projects.sessions' 'status') as |filter| }} - {{else if (has-route-resource-filter-selections + {{else if (has-resource-filter-selections 'scopes.scope.projects.sessions' 'type' )}}