0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-22 09:20:29 +01:00

REFACTOR - remove _.debounce and _.isEqual

Lodash is being removed from core Discourse.
Dieser Commit ist enthalten in:
jbrw 2020-09-29 12:57:32 -04:00
Ursprung 662c7432ef
Commit 866926501f
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: AF11336DC5C6EC71
2 geänderte Dateien mit 26 neuen und 24 gelöschten Zeilen

Datei anzeigen

@ -3,14 +3,15 @@ import { default as wizardSchema } from '../lib/wizard-schema';
import { set, get } from "@ember/object"; import { set, get } from "@ember/object";
import Mixin from "@ember/object/mixin"; import Mixin from "@ember/object/mixin";
import { observes } from 'discourse-common/utils/decorators'; import { observes } from 'discourse-common/utils/decorators';
import { deepEqual } from 'discourse-common/lib/object';
export default Mixin.create({ export default Mixin.create({
didInsertElement() { didInsertElement() {
this._super(...arguments); this._super(...arguments);
this.setupObservers(); this.setupObservers();
const obj = this.get(this.componentType); const obj = this.get(this.componentType);
this.setProperties({ this.setProperties({
originalObject: JSON.parse(JSON.stringify(obj)), originalObject: JSON.parse(JSON.stringify(obj)),
undoIcon: obj.isNew ? 'times' : 'undo', undoIcon: obj.isNew ? 'times' : 'undo',
@ -22,11 +23,11 @@ export default Mixin.create({
this._super(...arguments); this._super(...arguments);
this.removeObservers(); this.removeObservers();
}, },
removeObservers(objType=null) { removeObservers(objType=null) {
const componentType = this.componentType; const componentType = this.componentType;
const obj = this.get(componentType); const obj = this.get(componentType);
let opts = { let opts = {
objectType: objType || obj.type objectType: objType || obj.type
} }
@ -35,44 +36,44 @@ export default Mixin.create({
obj.removeObserver(property, this, this.toggleUndo); obj.removeObserver(property, this, this.toggleUndo);
}); });
}, },
setupObservers(objType=null) { setupObservers(objType=null) {
const componentType = this.componentType; const componentType = this.componentType;
const obj = this.get(componentType); const obj = this.get(componentType);
let opts = { let opts = {
objectType: objType || obj.type objectType: objType || obj.type
} }
listProperties(componentType, opts).forEach(property => { listProperties(componentType, opts).forEach(property => {
obj.addObserver(property, this, this.toggleUndo); obj.addObserver(property, this, this.toggleUndo);
}); });
}, },
revertToOriginal(revertBasic=false) { revertToOriginal(revertBasic=false) {
const original = JSON.parse(JSON.stringify(this.originalObject)); const original = JSON.parse(JSON.stringify(this.originalObject));
const componentType = this.componentType; const componentType = this.componentType;
const obj = this.get(componentType); const obj = this.get(componentType);
const objSchema = wizardSchema[componentType]; const objSchema = wizardSchema[componentType];
const basicDefaults = objSchema.basic; const basicDefaults = objSchema.basic;
if (revertBasic) { if (revertBasic) {
Object.keys(basicDefaults).forEach(property => { Object.keys(basicDefaults).forEach(property => {
let value; let value;
if (original.hasOwnProperty(property)) { if (original.hasOwnProperty(property)) {
value = get(original, property); value = get(original, property);
} else if (basicDefaults.hasOwnProperty(property)) { } else if (basicDefaults.hasOwnProperty(property)) {
value = get(basicDefaults, property); value = get(basicDefaults, property);
} }
set(obj, property, value); set(obj, property, value);
}); });
} }
if (objSchema.types && obj.type) { if (objSchema.types && obj.type) {
let typeDefaults = objSchema.types[obj.type]; let typeDefaults = objSchema.types[obj.type];
Object.keys(typeDefaults).forEach(property => { Object.keys(typeDefaults).forEach(property => {
let value; let value;
@ -81,45 +82,45 @@ export default Mixin.create({
} else if (typeDefaults.hasOwnProperty(property)) { } else if (typeDefaults.hasOwnProperty(property)) {
value = get(typeDefaults, property); value = get(typeDefaults, property);
} }
set(obj, property, value); set(obj, property, value);
}); });
} }
}, },
toggleUndo() { toggleUndo() {
const current = this.get(this.componentType); const current = this.get(this.componentType);
const original = this.originalObject; const original = this.originalObject;
this.set('showUndo', !_.isEqual(current, original)); this.set('showUndo', !deepEqual(current, original));
}, },
actions: { actions: {
undoChanges() { undoChanges() {
const componentType = this.componentType; const componentType = this.componentType;
const original = this.get('originalObject'); const original = this.get('originalObject');
const obj = this.get(componentType); const obj = this.get(componentType);
this.removeObservers(obj.type); this.removeObservers(obj.type);
this.revertToOriginal(true); this.revertToOriginal(true);
this.set('showUndo', false); this.set('showUndo', false);
this.setupObservers(this.get(componentType).type); this.setupObservers(this.get(componentType).type);
}, },
changeType(type) { changeType(type) {
const componentType = this.componentType; const componentType = this.componentType;
const original = this.get('originalObject'); const original = this.get('originalObject');
const obj = this.get(componentType); const obj = this.get(componentType);
this.removeObservers(obj.type); this.removeObservers(obj.type);
obj.set('type', type); obj.set('type', type);
this.revertToOriginal(); this.revertToOriginal();
this.set('showUndo', type !== original.type); this.set('showUndo', type !== original.type);
this.setupObservers(type); this.setupObservers(type);
}, },
mappedFieldUpdated(property, mappedComponent, type) { mappedFieldUpdated(property, mappedComponent, type) {
const obj = this.get(this.componentType); const obj = this.get(this.componentType);
obj.notifyPropertyChange(property); obj.notifyPropertyChange(property);
} }
} }
}) })

Datei anzeigen

@ -1,5 +1,6 @@
import { CANCELLED_STATUS } from 'discourse/lib/autocomplete'; import { CANCELLED_STATUS } from 'discourse/lib/autocomplete';
import getUrl from 'discourse-common/lib/get-url'; import getUrl from 'discourse-common/lib/get-url';
import discourseDebounce from "discourse/lib/debounce";
var cache = {}, var cache = {},
cacheTopicId, cacheTopicId,
@ -39,7 +40,7 @@ function performSearch(term, topicId, includeGroups, includeMentionableGroups, i
}); });
} }
var debouncedSearch = _.debounce(performSearch, 300); var debouncedSearch = discourseDebounce(performSearch, 300);
function organizeResults(r, options) { function organizeResults(r, options) {
if (r === CANCELLED_STATUS) { return r; } if (r === CANCELLED_STATUS) { return r; }