From eb6a6446ee703f689841b178a251642a2fd7e3ae Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Tue, 1 Jun 2021 16:49:03 +0200 Subject: [PATCH] FIX: Update non-Ember CLI ember-buffered-proxy (#13227) This version matches the one we have in discourse/package.json. Fixes an issue with saving topic name form w/o any changes. --- app/assets/javascripts/discourse-shims.js | 4 - .../app/controllers/reorder-categories.js | 5 +- .../discourse/app/mixins/buffered-content.js | 4 +- vendor/assets/javascripts/buffered-proxy.js | 151 ++++++++++++++---- 4 files changed, 120 insertions(+), 44 deletions(-) diff --git a/app/assets/javascripts/discourse-shims.js b/app/assets/javascripts/discourse-shims.js index bf4765a13f8..b63d4a6fce8 100644 --- a/app/assets/javascripts/discourse-shims.js +++ b/app/assets/javascripts/discourse-shims.js @@ -26,10 +26,6 @@ define("ember-buffered-proxy/proxy", ["exports"], function (__exports__) { __exports__.default = window.BufferedProxy; }); -define("ember-buffered-proxy/mixin", ["exports"], function (__exports__) { - __exports__.default = null; -}); - define("bootbox", ["exports"], function (__exports__) { __exports__.default = window.bootbox; }); diff --git a/app/assets/javascripts/discourse/app/controllers/reorder-categories.js b/app/assets/javascripts/discourse/app/controllers/reorder-categories.js index 333108eefd8..47da67b284a 100644 --- a/app/assets/javascripts/discourse/app/controllers/reorder-categories.js +++ b/app/assets/javascripts/discourse/app/controllers/reorder-categories.js @@ -1,8 +1,6 @@ import discourseComputed, { on } from "discourse-common/utils/decorators"; -import BufferedMixin from "ember-buffered-proxy/mixin"; import BufferedProxy from "ember-buffered-proxy/proxy"; import Controller from "@ember/controller"; -import EmberObjectProxy from "@ember/object/proxy"; import Evented from "@ember/object/evented"; import ModalFunctionality from "discourse/mixins/modal-functionality"; import { ajax } from "discourse/lib/ajax"; @@ -17,8 +15,7 @@ export default Controller.extend(ModalFunctionality, Evented, { @discourseComputed("site.categories.[]") categoriesBuffered(categories) { - const bufProxy = EmberObjectProxy.extend(BufferedMixin || BufferedProxy); - return (categories || []).map((c) => bufProxy.create({ content: c })); + return (categories || []).map((c) => BufferedProxy.create({ content: c })); }, categoriesOrdered: sort("categoriesBuffered", "categoriesSorting"), diff --git a/app/assets/javascripts/discourse/app/mixins/buffered-content.js b/app/assets/javascripts/discourse/app/mixins/buffered-content.js index 2b13f7d1d16..92b44f16fa8 100644 --- a/app/assets/javascripts/discourse/app/mixins/buffered-content.js +++ b/app/assets/javascripts/discourse/app/mixins/buffered-content.js @@ -1,13 +1,11 @@ -import BufferedMixin from "ember-buffered-proxy/mixin"; import BufferedProxy from "ember-buffered-proxy/proxy"; -import EmberObjectProxy from "@ember/object/proxy"; import Mixin from "@ember/object/mixin"; import { computed } from "@ember/object"; export function bufferedProperty(property) { const mixin = { buffered: computed(property, function () { - return EmberObjectProxy.extend(BufferedMixin || BufferedProxy).create({ + return BufferedProxy.create({ content: this.get(property), }); }), diff --git a/vendor/assets/javascripts/buffered-proxy.js b/vendor/assets/javascripts/buffered-proxy.js index a26ec632be3..24f8243043f 100644 --- a/vendor/assets/javascripts/buffered-proxy.js +++ b/vendor/assets/javascripts/buffered-proxy.js @@ -1,6 +1,12 @@ (function (global) { "use strict"; + function aliasMethod(methodName) { + return function() { + return this[methodName].apply(this, arguments); + }; + } + function empty(obj) { var key; for (key in obj) if (obj.hasOwnProperty(key)) return false; @@ -8,77 +14,156 @@ } var Ember = global.Ember, - get = Ember.get, set = Ember.set; + get = Ember.get, set = Ember.set, + isArray = Ember.isArray, getProperties = Ember.getProperties, + notifyPropertyChange = Ember.notifyPropertyChange, + meta = Ember.meta, defineProperty = Ember.defineProperty; - var BufferedProxy = Ember.Mixin.create({ + var hasOwnProp = Object.prototype.hasOwnProperty; + + var BufferedProxyMixin = Ember.Mixin.create({ buffer: null, - hasBufferedChanges: false, - unknownProperty: function (key) { - var buffer = this.buffer; - return buffer && buffer.hasOwnProperty(key) ? buffer[key] : this._super(key); + hasChanges: Ember.computed.readOnly('hasBufferedChanges'), + + applyChanges: function() { + return this.applyBufferedChanges(...arguments); }, - setUnknownProperty: function (key, value) { - if (!this.buffer) this.buffer = {}; + discardChanges: function() { + return this.discardBufferedChanges(...arguments); + }, - var buffer = this.buffer, - content = this.get('content'), - current = content && get(content, key), - previous = buffer.hasOwnProperty(key) ? buffer[key] : current; + init: function() { + this.initializeBuffer(); + set(this, 'hasBufferedChanges', false); + this._super(...arguments); + }, - if (previous === value) return; + initializeBuffer: function(onlyTheseKeys) { + if(isArray(onlyTheseKeys) && !empty(onlyTheseKeys)) { + onlyTheseKeys.forEach((key) => delete this.buffer[key]); + } + else { + set(this, 'buffer', Object.create(null)); + } + }, + + unknownProperty: function(key) { + var buffer = get(this, 'buffer'); + return (hasOwnProp.call(buffer, key)) ? buffer[key] : this._super(key); + }, + + setUnknownProperty: function(key, value) { + var m = meta(this); + + if (m.proto === this || (m.isInitializing && m.isInitializing())) { + defineProperty(this, key, null, value); + return value; + } + + var props = getProperties(this, ['buffer', 'content']), + buffer = props.buffer, + content = props.content, + current, + previous; + + if (content != null) { + current = get(content, key); + } + + previous = hasOwnProp.call(buffer, key) ? buffer[key] : current; + + if (previous === value) { + return; + } if (current === value) { delete buffer[key]; if (empty(buffer)) { - this.set('hasBufferedChanges', false); + set(this, 'hasBufferedChanges', false); } } else { buffer[key] = value; - this.set('hasBufferedChanges', true); + set(this, 'hasBufferedChanges', true); } - this.notifyPropertyChange(key); + notifyPropertyChange(content, key); + return value; }, - applyBufferedChanges: function() { - var buffer = this.buffer, - content = this.get('content'), + applyBufferedChanges: function(onlyTheseKeys) { + var props = getProperties(this, ['buffer', 'content']), + buffer = props.buffer, + content = props.content, key; - for (key in buffer) { - if (!buffer.hasOwnProperty(key)) continue; + + Object.keys(buffer).forEach((key) => { + if (isArray(onlyTheseKeys) && onlyTheseKeys.indexOf(key) === -1) { + return; + } + set(content, key, buffer[key]); + }); + + this.initializeBuffer(onlyTheseKeys); + + if (empty(get(this, 'buffer'))) { + set(this, 'hasBufferedChanges', false); } - this.buffer = {}; - this.set('hasBufferedChanges', false); }, - discardBufferedChanges: function() { - var buffer = this.buffer, - content = this.get('content'), + discardBufferedChanges: function(onlyTheseKeys) { + var props = getProperties(this, ['buffer', 'content']), + buffer = props.buffer, + content = props.content, key; - for (key in buffer) { - if (!buffer.hasOwnProperty(key)) continue; - delete buffer[key]; - this.notifyPropertyChange(key); + this.initializeBuffer(onlyTheseKeys); + + Object.keys(buffer).forEach((key) => { + if (isArray(onlyTheseKeys) && onlyTheseKeys.indexOf(key) === -1) { + return; + } + + notifyPropertyChange(content, key); + }); + + if (empty(get(this, 'buffer'))) { + set(this, 'hasBufferedChanges', false); } - this.set('hasBufferedChanges', false); + }, + + hasChanged: function(key) { + var props = getProperties(this, ['buffer', 'content']), + buffer = props.buffer, + content = props.content, + key; + + if (typeof key !== 'string' || typeof get(buffer, key) === 'undefined') { + return false; + } + + if (get(buffer, key) !== get(content, key)) { + return true; + } + + return false; } }); + var BufferedProxy = Ember.ObjectProxy.extend(BufferedProxyMixin); + // CommonJS module if (typeof module !== 'undefined' && module.exports) { module.exports = BufferedProxy; } else if (typeof define === "function" && define.amd) { - define("buffered-proxy", function (require, exports, module) { + define("ember-buffered-proxy/proxy", function (require, exports, module) { return BufferedProxy; }); } else { global.BufferedProxy = BufferedProxy; } - }(this));