DEV: Remove ember-addons (#9559)

We weren't using this very much and introduces a dependency between
discourse-common and discourse which makes moving to yarn workspaces
more difficult.

In the future we might user ember-addons properly but for now it's
easier to move the code into discourse-common.

Note the old folder is still there because at least one plugin was still
requiring the old files. It will be removed in the future.
This commit is contained in:
Robin Ward 2020-04-28 10:14:49 -04:00 committed by GitHub
parent 84be92c067
commit c1cc2f2a05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 167 additions and 175 deletions

View File

@ -26,9 +26,6 @@ acc5cbdf8ecb9293a0fa9474ee73baf499c02428
# Rename discourse-common es6 -> js
167503ca4824e37a2e93d74b3f50271556d0ba8e
# Rename ember-addons es6 -> js
16ba50bce362c1eefe1881f86c67bec66f493abb
# Rename some root files
11938d58d4b1bea1ff43306450da7b24f05db0a

View File

@ -1,8 +1,4 @@
//= require_tree ./ember-addons/utils
//= require_tree ./discourse-common
//= require ./ember-addons/decorator-alias
//= require ./ember-addons/macro-alias
//= require ./ember-addons/fmt
//= require ./polyfills
//= require_tree ./select-kit
//= require ./discourse/app/app

View File

@ -0,0 +1,22 @@
import extractValue from "discourse-common/utils/extract-value";
export default function decoratorAlias(fn, errorMessage) {
return function(...params) {
// determine if user called as @discourseComputed('blah', 'blah') or @discourseComputed
if (params.length === 0) {
throw new Error(errorMessage);
} else {
return function(target, key, desc) {
return {
enumerable: desc.enumerable,
configurable: desc.configurable,
writable: desc.writable,
initializer: function() {
var value = extractValue(desc);
return fn.apply(null, params.concat(value));
}
};
};
}
};
}

View File

@ -1,6 +1,8 @@
import handleDescriptor from "ember-addons/utils/handle-descriptor";
import isDescriptor from "ember-addons/utils/is-descriptor";
import extractValue from "ember-addons/utils/extract-value";
import handleDescriptor from "discourse-common/utils/handle-descriptor";
import isDescriptor from "discourse-common/utils/is-descriptor";
import extractValue from "discourse-common/utils/extract-value";
import decoratorAlias from "discourse-common/utils/decorator-alias";
import macroAlias from "discourse-common/utils/macro-alias";
import { schedule, next } from "@ember/runloop";
export default function discourseComputedDecorator(...params) {
@ -39,8 +41,6 @@ export function readOnly(target, name, desc) {
};
}
import decoratorAlias from "ember-addons/decorator-alias";
/* eslint-disable */
export var on = decoratorAlias(Ember.on, "Can not `on` without event names");
export var observes = decoratorAlias(
@ -48,8 +48,6 @@ export var observes = decoratorAlias(
"Can not `observe` without property names"
);
import macroAlias from "ember-addons/macro-alias";
export var alias = macroAlias(Ember.computed.alias);
export var and = macroAlias(Ember.computed.and);
export var bool = macroAlias(Ember.computed.bool);

View File

@ -0,0 +1,5 @@
export default function extractValue(desc) {
return (
desc.value || (typeof desc.initializer === "function" && desc.initializer())
);
}

View File

@ -0,0 +1,71 @@
import { computed, get } from "@ember/object";
import extractValue from "./extract-value";
export default function handleDescriptor(target, key, desc, params = []) {
return {
enumerable: desc.enumerable,
configurable: desc.configurable,
writeable: desc.writeable,
initializer: function() {
let computedDescriptor;
if (desc.writable) {
var val = extractValue(desc);
if (typeof val === "object") {
let value = {};
if (val.get) {
value.get = callUserSuppliedGet(params, val.get);
}
if (val.set) {
value.set = callUserSuppliedSet(params, val.set);
}
computedDescriptor = value;
} else {
computedDescriptor = callUserSuppliedGet(params, val);
}
} else {
throw new Error(
"ember-computed-decorators does not support using getters and setters"
);
}
return computed.apply(null, params.concat(computedDescriptor));
}
};
}
function niceAttr(attr) {
const parts = attr.split(".");
let i;
for (i = 0; i < parts.length; i++) {
if (
parts[i] === "@each" ||
parts[i] === "[]" ||
parts[i].indexOf("{") !== -1
) {
break;
}
}
return parts.slice(0, i).join(".");
}
function callUserSuppliedGet(params, func) {
params = params.map(niceAttr);
return function() {
let paramValues = params.map(p => get(this, p));
return func.apply(this, paramValues);
};
}
function callUserSuppliedSet(params, func) {
params = params.map(niceAttr);
return function(key, value) {
let paramValues = params.map(p => get(this, p));
paramValues.unshift(value);
return func.apply(this, paramValues);
};
}

View File

@ -0,0 +1,9 @@
export default function isDescriptor(item) {
return (
item &&
typeof item === "object" &&
"writable" in item &&
"enumerable" in item &&
"configurable" in item
);
}

View File

@ -0,0 +1,24 @@
import isDescriptor from "discourse-common/utils/is-descriptor";
function handleDescriptor(target, property, desc, fn, params = []) {
return {
enumerable: desc.enumerable,
configurable: desc.configurable,
writable: desc.writable,
initializer: function() {
return fn(...params);
}
};
}
export default function macroAlias(fn) {
return function(...params) {
if (isDescriptor(params[params.length - 1])) {
return handleDescriptor(...params, fn);
} else {
return function(target, property, desc) {
return handleDescriptor(target, property, desc, fn, params);
};
}
};
}

View File

@ -1,7 +1,31 @@
import { computed } from "@ember/object";
import addonFmt from "ember-addons/fmt";
import { htmlSafe as htmlSafeTemplateHelper } from "@ember/template";
function addonFmt(str, formats) {
let cachedFormats = formats;
if (!_.isArray(cachedFormats) || arguments.length > 2) {
cachedFormats = new Array(arguments.length - 1);
for (let i = 1, l = arguments.length; i < l; i++) {
cachedFormats[i - 1] = arguments[i];
}
}
// first, replace any ORDERED replacements.
let idx = 0; // the current index for non-numerical replacements
return str.replace(/%@([0-9]+)?/g, function(s, argIndex) {
argIndex = argIndex ? parseInt(argIndex, 10) - 1 : idx++;
s = cachedFormats[argIndex];
return typeof s === "string"
? s
: s === null
? "(null)"
: s === undefined
? ""
: "" + s;
});
}
/**
Returns whether two properties are equal to each other.

View File

@ -1,22 +1 @@
import extractValue from "./utils/extract-value";
export default function decoratorAlias(fn, errorMessage) {
return function(...params) {
// determine if user called as @discourseComputed('blah', 'blah') or @discourseComputed
if (params.length === 0) {
throw new Error(errorMessage);
} else {
return function(target, key, desc) {
return {
enumerable: desc.enumerable,
configurable: desc.configurable,
writable: desc.writable,
initializer: function() {
var value = extractValue(desc);
return fn.apply(null, params.concat(value));
}
};
};
}
};
}
// left empty for backwards compatibility. Should be removed in the future

View File

@ -1,25 +1 @@
export default function(str, formats) {
let cachedFormats = formats;
if (!_.isArray(cachedFormats) || arguments.length > 2) {
cachedFormats = new Array(arguments.length - 1);
for (let i = 1, l = arguments.length; i < l; i++) {
cachedFormats[i - 1] = arguments[i];
}
}
// first, replace any ORDERED replacements.
let idx = 0; // the current index for non-numerical replacements
return str.replace(/%@([0-9]+)?/g, function(s, argIndex) {
argIndex = argIndex ? parseInt(argIndex, 10) - 1 : idx++;
s = cachedFormats[argIndex];
return typeof s === "string"
? s
: s === null
? "(null)"
: s === undefined
? ""
: "" + s;
});
}
// left empty for backwards compatibility. Should be removed in the future

View File

@ -1,24 +1 @@
import isDescriptor from "./utils/is-descriptor";
function handleDescriptor(target, property, desc, fn, params = []) {
return {
enumerable: desc.enumerable,
configurable: desc.configurable,
writable: desc.writable,
initializer: function() {
return fn(...params);
}
};
}
export default function macroAlias(fn) {
return function(...params) {
if (isDescriptor(params[params.length - 1])) {
return handleDescriptor(...params, fn);
} else {
return function(target, property, desc) {
return handleDescriptor(target, property, desc, fn, params);
};
}
};
}
// left empty for backwards compatibility. Should be removed in the future

View File

@ -1,5 +1 @@
export default function extractValue(desc) {
return (
desc.value || (typeof desc.initializer === "function" && desc.initializer())
);
}
// left for backwards compatibility. remove in the future.

View File

@ -1,71 +1 @@
import { computed, get } from "@ember/object";
import extractValue from "./extract-value";
export default function handleDescriptor(target, key, desc, params = []) {
return {
enumerable: desc.enumerable,
configurable: desc.configurable,
writeable: desc.writeable,
initializer: function() {
let computedDescriptor;
if (desc.writable) {
var val = extractValue(desc);
if (typeof val === "object") {
let value = {};
if (val.get) {
value.get = callUserSuppliedGet(params, val.get);
}
if (val.set) {
value.set = callUserSuppliedSet(params, val.set);
}
computedDescriptor = value;
} else {
computedDescriptor = callUserSuppliedGet(params, val);
}
} else {
throw new Error(
"ember-computed-decorators does not support using getters and setters"
);
}
return computed.apply(null, params.concat(computedDescriptor));
}
};
}
function niceAttr(attr) {
const parts = attr.split(".");
let i;
for (i = 0; i < parts.length; i++) {
if (
parts[i] === "@each" ||
parts[i] === "[]" ||
parts[i].indexOf("{") !== -1
) {
break;
}
}
return parts.slice(0, i).join(".");
}
function callUserSuppliedGet(params, func) {
params = params.map(niceAttr);
return function() {
let paramValues = params.map(p => get(this, p));
return func.apply(this, paramValues);
};
}
function callUserSuppliedSet(params, func) {
params = params.map(niceAttr);
return function(key, value) {
let paramValues = params.map(p => get(this, p));
paramValues.unshift(value);
return func.apply(this, paramValues);
};
}
// left for backwards compatibility. remove in the future.

View File

@ -1,9 +1 @@
export default function isDescriptor(item) {
return (
item &&
typeof item === "object" &&
"writable" in item &&
"enumerable" in item &&
"configurable" in item
);
}
// left for backwards compatibility. remove in the future.

View File

@ -1,4 +1,3 @@
//= require_tree ./ember-addons
//= require admin/models/user-field
//= require admin/models/site-setting
//= require admin/models/screened-ip-address

View File

@ -1,6 +1,3 @@
//= require_tree ./ember-addons/utils
//= require ./ember-addons/decorator-alias
//= require ./ember-addons/macro-alias
//= require ./discourse-common/utils/decorators
//= require_tree ./discourse-common
//= require i18n-patches