2021-03-28 11:06:49 +02:00
|
|
|
import { listProperties } from "../lib/wizard";
|
|
|
|
import { default as wizardSchema } from "../lib/wizard-schema";
|
2021-04-12 07:10:02 +02:00
|
|
|
import { get, set } from "@ember/object";
|
2020-04-20 11:41:13 +02:00
|
|
|
import Mixin from "@ember/object/mixin";
|
2021-03-28 11:06:49 +02:00
|
|
|
import { deepEqual } from "discourse-common/lib/object";
|
2020-04-20 11:41:13 +02:00
|
|
|
|
2023-03-15 10:38:26 +01:00
|
|
|
let observedCache = [];
|
2023-03-15 09:22:09 +01:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
export default Mixin.create({
|
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.setupObservers();
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
const obj = this.get(this.componentType);
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
this.setProperties({
|
|
|
|
originalObject: JSON.parse(JSON.stringify(obj)),
|
2021-03-28 11:06:49 +02:00
|
|
|
undoIcon: obj.isNew ? "times" : "undo",
|
|
|
|
undoKey: `admin.wizard.${obj.isNew ? "clear" : "undo"}`,
|
|
|
|
});
|
2020-04-20 11:41:13 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.removeObservers();
|
|
|
|
},
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2021-03-28 11:06:49 +02:00
|
|
|
removeObservers(objType = null) {
|
2020-04-20 11:41:13 +02:00
|
|
|
const componentType = this.componentType;
|
|
|
|
const obj = this.get(componentType);
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
let opts = {
|
2021-03-28 11:06:49 +02:00
|
|
|
objectType: objType || obj.type,
|
|
|
|
};
|
2020-04-20 11:41:13 +02:00
|
|
|
|
2021-03-28 11:06:49 +02:00
|
|
|
listProperties(componentType, opts).forEach((property) => {
|
2023-03-15 09:22:09 +01:00
|
|
|
if (observedCache.includes(property)) {
|
|
|
|
obj.removeObserver(property, this, this.toggleUndo);
|
2023-03-15 10:38:26 +01:00
|
|
|
observedCache = observedCache.filter((p) => p !== property);
|
2023-03-15 09:22:09 +01:00
|
|
|
}
|
2020-04-20 11:41:13 +02:00
|
|
|
});
|
|
|
|
},
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2021-03-28 11:06:49 +02:00
|
|
|
setupObservers(objType = null) {
|
2020-04-20 11:41:13 +02:00
|
|
|
const componentType = this.componentType;
|
|
|
|
const obj = this.get(componentType);
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
let opts = {
|
2021-03-28 11:06:49 +02:00
|
|
|
objectType: objType || obj.type,
|
|
|
|
};
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2021-03-28 11:06:49 +02:00
|
|
|
listProperties(componentType, opts).forEach((property) => {
|
2023-03-15 09:22:09 +01:00
|
|
|
if (observedCache.indexOf(property) === -1) {
|
|
|
|
observedCache.push(property);
|
|
|
|
}
|
2020-04-20 11:41:13 +02:00
|
|
|
obj.addObserver(property, this, this.toggleUndo);
|
|
|
|
});
|
|
|
|
},
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2021-03-28 11:06:49 +02:00
|
|
|
revertToOriginal(revertBasic = false) {
|
2020-04-20 11:41:13 +02:00
|
|
|
const original = JSON.parse(JSON.stringify(this.originalObject));
|
|
|
|
const componentType = this.componentType;
|
|
|
|
const obj = this.get(componentType);
|
|
|
|
const objSchema = wizardSchema[componentType];
|
|
|
|
const basicDefaults = objSchema.basic;
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
if (revertBasic) {
|
2021-03-28 11:06:49 +02:00
|
|
|
Object.keys(basicDefaults).forEach((property) => {
|
2020-04-20 11:41:13 +02:00
|
|
|
let value;
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
if (original.hasOwnProperty(property)) {
|
|
|
|
value = get(original, property);
|
|
|
|
} else if (basicDefaults.hasOwnProperty(property)) {
|
|
|
|
value = get(basicDefaults, property);
|
|
|
|
}
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
set(obj, property, value);
|
|
|
|
});
|
|
|
|
}
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
if (objSchema.types && obj.type) {
|
|
|
|
let typeDefaults = objSchema.types[obj.type];
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2021-03-28 11:06:49 +02:00
|
|
|
Object.keys(typeDefaults).forEach((property) => {
|
2020-04-20 11:41:13 +02:00
|
|
|
let value;
|
|
|
|
|
|
|
|
if (original.type === obj.type && original.hasOwnProperty(property)) {
|
|
|
|
value = get(original, property);
|
|
|
|
} else if (typeDefaults.hasOwnProperty(property)) {
|
|
|
|
value = get(typeDefaults, property);
|
|
|
|
}
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
set(obj, property, value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
toggleUndo() {
|
|
|
|
const current = this.get(this.componentType);
|
|
|
|
const original = this.originalObject;
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("showUndo", !deepEqual(current, original));
|
2020-04-20 11:41:13 +02:00
|
|
|
},
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
actions: {
|
|
|
|
undoChanges() {
|
|
|
|
const componentType = this.componentType;
|
|
|
|
const obj = this.get(componentType);
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
this.removeObservers(obj.type);
|
|
|
|
this.revertToOriginal(true);
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("showUndo", false);
|
2020-04-20 11:41:13 +02:00
|
|
|
this.setupObservers(this.get(componentType).type);
|
|
|
|
},
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
changeType(type) {
|
|
|
|
const componentType = this.componentType;
|
2021-03-28 11:06:49 +02:00
|
|
|
const original = this.get("originalObject");
|
2020-04-20 11:41:13 +02:00
|
|
|
const obj = this.get(componentType);
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2020-04-20 11:41:13 +02:00
|
|
|
this.removeObservers(obj.type);
|
2021-03-28 11:06:49 +02:00
|
|
|
obj.set("type", type);
|
2020-04-20 11:41:13 +02:00
|
|
|
this.revertToOriginal();
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("showUndo", type !== original.type);
|
2020-04-20 11:41:13 +02:00
|
|
|
this.setupObservers(type);
|
|
|
|
},
|
2020-09-29 18:57:32 +02:00
|
|
|
|
2021-04-12 07:44:47 +02:00
|
|
|
// eslint-disable-next-line
|
2020-04-29 03:42:39 +02:00
|
|
|
mappedFieldUpdated(property, mappedComponent, type) {
|
|
|
|
const obj = this.get(this.componentType);
|
2020-09-29 18:57:32 +02:00
|
|
|
obj.notifyPropertyChange(property);
|
2021-03-28 11:06:49 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|