open-nomad/ui/app/serializers/application.js

181 lines
5.1 KiB
JavaScript
Raw Normal View History

2019-03-26 04:55:06 +00:00
import { copy } from 'ember-copy';
import { get } from '@ember/object';
import { makeArray } from '@ember/array';
import JSONSerializer from '@ember-data/serializer/json';
2019-03-26 04:24:11 +00:00
import { pluralize, singularize } from 'ember-inflector';
import removeRecord from '../utils/remove-record';
import { assign } from '@ember/polyfills';
Upgrade Ember and friends 3.28 (#12215) * chore: upgrade forward compatible packages * chore: v3.20.2...v3.24.0 * chore: silence string prototype extension deprecation * refact: don't test clicking disabled button job-list Recent test-helper upgrades will guard against clicking disabled buttons as this is not something that real users can do. We need to change our tests accordingly. * fix: await async test helper `expectError` We have to await this async test function otherwise the test's rendering context will be torn down before we run assertions against it. * fix: don't try to click disabled two-step-button Recent test-helper updates prohibit clicking disabled buttons. We need to adapt the tests accordingly. * fix: recommendation-accordion Use up-to-date semantics for handling list-accordion closing in recommendation-accordion. * fixes toggling recommendation-accordion toggle. * fix: simple-unless linting error application.hbs There's no reason to use unless here - we can use if instead. * fix: no-quoteless-attributes recommendation accordion * fix: no-quoteless-attributes recommendation-chart * fix: allow `unless` - global-header.hbs This is a valid use of unless in our opinion. * fix: allow unless in job-diff This is not a great use for unless but we don't want to change this behavior atm. * fix: no-attrs-in-components list-pager There is no need to use this.attrs in classic components. When we will convert to glimmer we will use `@`-instead. * fix: simple-unless job/definition We can convert to a simple if here. * fix: allow inline-styles stats-box component To make linter happy. * fix: disable no-action and no-invalid-interactive Will be adressed in follow-up PRs. * chore: update ember-classic-decorator to latest * chore: upgrade ember-can to latest * chore: upgrade ember-composable-helpers to latest * chore: upgrade ember-concurrency * fix: recomputation deprecation `Trigger` schedule `do` on actions queue to work around recomputation deprecation when triggering Trigger on `did-insert`. * chore: upgrade ember-cli-string-helpers * chore: upgrade ember-copy * chore: upgrade ember-data-model-fragments * chore: upgrade ember-deprecation-workflow * chore: upgrade ember-inline-svg * chore: upgrade ember-modifier * chore: upgrade ember-truth-helpers * chore: upgrade ember-moment & ember-cli-moment-shim * chore: upgrade ember-power-select * chore: upgrade ember-responsive * chore: upgrade ember-sinon * chore: upgrade ember-cli-mirage For now we will stay on 2.2 - upgrades > 2.3 break the build. * chore: upgrade 3.24.0 to 3.28.5 * fix: add missing classic decorators on adapters * fix: missing classic decorators to serializers * fix: don't reopen Ember.Object anymore * fix: remove unused useNativeEvents ember-cli-page-objects doesn't provide this method anymore * fix: add missing attributeBindings for test-selectors ember-test-selectors doesn't provides automatic bindings for data-test-* attributes anymore. * fix: classic decorator for application serializer test * fix: remove `removeContext` from tests. It is unneeded and ember-cli-page-objects doesn't provides this method anymore. * fix: remove deprecations `run.*`-invocations * fix: `collapseWhitespace` in optimize test * fix: make sure to load async relationship before access * fix: dependent keys for relationship computeds We need to add `*.isFulfilled` as dependent keys for computeds that access async relationships. * fix: `computed.read`-invocations use `read` instead * chore: prettify templates * fix: use map instead of mapBy ember-cli-page-object Doesn't work with updated ember-cli-page-object anymore. * fix: remove remaining deprecated `run.*`-calls * chore: add more deprecations deprecation-workflow * fix: `implicit-injection`-deprecation All routes that add watchers will need to inject the store-service as the store service is internally used in watchers. * fix: more implicit injection deprecations * chore: silence implicit-injection deprecation We can tackle the deprecation when we find the time. * fix: new linting errors after upgrade * fix: remove merge conflicts prettierignore * chore: upgrade to run node 12.22 when building binaries
2022-03-08 17:28:36 +00:00
import classic from 'ember-classic-decorator';
import { camelize, capitalize, dasherize } from '@ember/string';
Upgrade Ember and friends 3.28 (#12215) * chore: upgrade forward compatible packages * chore: v3.20.2...v3.24.0 * chore: silence string prototype extension deprecation * refact: don't test clicking disabled button job-list Recent test-helper upgrades will guard against clicking disabled buttons as this is not something that real users can do. We need to change our tests accordingly. * fix: await async test helper `expectError` We have to await this async test function otherwise the test's rendering context will be torn down before we run assertions against it. * fix: don't try to click disabled two-step-button Recent test-helper updates prohibit clicking disabled buttons. We need to adapt the tests accordingly. * fix: recommendation-accordion Use up-to-date semantics for handling list-accordion closing in recommendation-accordion. * fixes toggling recommendation-accordion toggle. * fix: simple-unless linting error application.hbs There's no reason to use unless here - we can use if instead. * fix: no-quoteless-attributes recommendation accordion * fix: no-quoteless-attributes recommendation-chart * fix: allow `unless` - global-header.hbs This is a valid use of unless in our opinion. * fix: allow unless in job-diff This is not a great use for unless but we don't want to change this behavior atm. * fix: no-attrs-in-components list-pager There is no need to use this.attrs in classic components. When we will convert to glimmer we will use `@`-instead. * fix: simple-unless job/definition We can convert to a simple if here. * fix: allow inline-styles stats-box component To make linter happy. * fix: disable no-action and no-invalid-interactive Will be adressed in follow-up PRs. * chore: update ember-classic-decorator to latest * chore: upgrade ember-can to latest * chore: upgrade ember-composable-helpers to latest * chore: upgrade ember-concurrency * fix: recomputation deprecation `Trigger` schedule `do` on actions queue to work around recomputation deprecation when triggering Trigger on `did-insert`. * chore: upgrade ember-cli-string-helpers * chore: upgrade ember-copy * chore: upgrade ember-data-model-fragments * chore: upgrade ember-deprecation-workflow * chore: upgrade ember-inline-svg * chore: upgrade ember-modifier * chore: upgrade ember-truth-helpers * chore: upgrade ember-moment & ember-cli-moment-shim * chore: upgrade ember-power-select * chore: upgrade ember-responsive * chore: upgrade ember-sinon * chore: upgrade ember-cli-mirage For now we will stay on 2.2 - upgrades > 2.3 break the build. * chore: upgrade 3.24.0 to 3.28.5 * fix: add missing classic decorators on adapters * fix: missing classic decorators to serializers * fix: don't reopen Ember.Object anymore * fix: remove unused useNativeEvents ember-cli-page-objects doesn't provide this method anymore * fix: add missing attributeBindings for test-selectors ember-test-selectors doesn't provides automatic bindings for data-test-* attributes anymore. * fix: classic decorator for application serializer test * fix: remove `removeContext` from tests. It is unneeded and ember-cli-page-objects doesn't provides this method anymore. * fix: remove deprecations `run.*`-invocations * fix: `collapseWhitespace` in optimize test * fix: make sure to load async relationship before access * fix: dependent keys for relationship computeds We need to add `*.isFulfilled` as dependent keys for computeds that access async relationships. * fix: `computed.read`-invocations use `read` instead * chore: prettify templates * fix: use map instead of mapBy ember-cli-page-object Doesn't work with updated ember-cli-page-object anymore. * fix: remove remaining deprecated `run.*`-calls * chore: add more deprecations deprecation-workflow * fix: `implicit-injection`-deprecation All routes that add watchers will need to inject the store-service as the store service is internally used in watchers. * fix: more implicit injection deprecations * chore: silence implicit-injection deprecation We can tackle the deprecation when we find the time. * fix: new linting errors after upgrade * fix: remove merge conflicts prettierignore * chore: upgrade to run node 12.22 when building binaries
2022-03-08 17:28:36 +00:00
@classic
export default class Application extends JSONSerializer {
primaryKey = 'ID';
2017-09-19 14:47:10 +00:00
/**
A list of keys that are converted to empty arrays if their value is null.
arrayNullOverrides = ['Array'];
{ Array: null } => { Array: [] }
@property arrayNullOverrides
@type String[]
*/
arrayNullOverrides = null;
/**
A list of keys that are converted to empty objects if their value is null.
objectNullOverrides = ['Object'];
{ Object: null } => { Object: {} }
@property objectNullOverrides
@type String[]
*/
objectNullOverrides = null;
/**
A list of keys or objects to convert a map into an array of maps with the original map keys as Name properties.
mapToArray = ['Map'];
{ Map: { a: { x: 1 } } } => { Map: [ { Name: 'a', x: 1 }] }
mapToArray = [{ beforeName: 'M', afterName: 'Map' }];
{ M: { a: { x: 1 } } } => { Map: [ { Name: 'a', x: 1 }] }
@property mapToArray
@type (String|Object)[]
*/
mapToArray = null;
/**
A list of keys for nanosecond timestamps that will be split into two properties: `separateNanos = ['Time']` will
produce a `Time` property with a millisecond timestamp and `TimeNanos` with the nanoseconds alone.
separateNanos = ['Time'];
{ Time: 1607839992000100000 } => { Time: 1607839992000, TimeNanos: 100096 }
@property separateNanos
@type String[]
*/
separateNanos = null;
2017-09-19 14:47:10 +00:00
keyForAttribute(attr) {
return capitalize(camelize(attr));
}
2017-09-19 14:47:10 +00:00
keyForRelationship(attr, relationshipType) {
const key = `${capitalize(camelize(singularize(attr)))}ID`;
2019-03-26 04:24:11 +00:00
return relationshipType === 'hasMany' ? pluralize(key) : key;
}
2017-09-19 14:47:10 +00:00
// Modeled after the pushPayload for ember-data/serializers/rest
pushPayload(store, payload) {
const documentHash = {
data: [],
included: [],
};
2021-12-28 14:45:20 +00:00
Object.keys(payload).forEach((key) => {
2017-09-19 14:47:10 +00:00
const modelName = this.modelNameFromPayloadKey(key);
const serializer = store.serializerFor(modelName);
const type = store.modelFor(modelName);
2021-12-28 14:45:20 +00:00
makeArray(payload[key]).forEach((hash) => {
2017-09-19 14:47:10 +00:00
const { data, included } = serializer.normalize(type, hash, key);
documentHash.data.push(data);
if (included) {
documentHash.included.push(...included);
}
});
});
store.push(documentHash);
}
normalize(modelClass, hash) {
if (hash) {
if (this.arrayNullOverrides) {
2021-12-28 14:45:20 +00:00
this.arrayNullOverrides.forEach((key) => {
if (!hash[key]) {
hash[key] = [];
}
});
}
if (this.objectNullOverrides) {
2021-12-28 14:45:20 +00:00
this.objectNullOverrides.forEach((key) => {
if (!hash[key]) {
hash[key] = {};
}
});
}
if (this.mapToArray) {
2021-12-28 14:45:20 +00:00
this.mapToArray.forEach((conversion) => {
let apiKey, uiKey;
if (conversion.beforeName) {
apiKey = conversion.beforeName;
uiKey = conversion.afterName;
} else {
apiKey = conversion;
uiKey = conversion;
}
const map = hash[apiKey] || {};
hash[uiKey] = Object.keys(map)
.sort()
2021-12-28 14:45:20 +00:00
.map((mapKey) => {
const propertiesForKey = map[mapKey] || {};
const convertedMap = { Name: mapKey };
assign(convertedMap, propertiesForKey);
return convertedMap;
});
});
}
if (this.separateNanos) {
2021-12-28 14:45:20 +00:00
this.separateNanos.forEach((key) => {
const timeWithNanos = hash[key];
hash[`${key}Nanos`] = timeWithNanos % 1000000;
hash[key] = Math.floor(timeWithNanos / 1000000);
});
}
}
return super.normalize(modelClass, hash);
}
normalizeFindAllResponse(store, modelClass) {
const result = super.normalizeFindAllResponse(...arguments);
this.cullStore(store, modelClass.modelName, result.data);
return result;
}
2017-09-19 14:47:10 +00:00
// When records are removed server-side, and therefore don't show up in requests,
// the local copies of those records need to be unloaded from the store.
cullStore(store, type, records, storeFilter = () => true) {
2021-12-28 14:45:20 +00:00
const newRecords = copy(records).filter((record) => get(record, 'id'));
const oldRecords = store.peekAll(type);
oldRecords
2021-12-28 14:45:20 +00:00
.filter((record) => get(record, 'id'))
.filter(storeFilter)
2021-12-28 14:45:20 +00:00
.forEach((old) => {
2021-12-28 16:08:12 +00:00
const newRecord = newRecords.find(
(record) => get(record, 'id') === get(old, 'id')
);
if (!newRecord) {
removeRecord(store, old);
} else {
newRecords.removeObject(newRecord);
}
});
}
2017-09-19 14:47:10 +00:00
modelNameFromPayloadKey(key) {
return singularize(dasherize(key));
}
}