DEV: improves sk api (#9653)

- reduces the API to 3 actions for now: appendContent/prependContent/onChange
- well tested
- removes all previous APIS which were only half supported or too dangerous as they could collide with other plugins or core behaviors
- this plugins also puts every sk test helpers in one file
This commit is contained in:
Joffrey JAFFEUX 2020-05-06 17:16:20 +02:00 committed by GitHub
parent 70bf1669be
commit c99ecba68f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 197 additions and 340 deletions

View File

@ -54,7 +54,7 @@ import { on } from "@ember/object/evented";
import KeyboardShortcuts from "discourse/lib/keyboard-shortcuts";
// If you add any methods to the API ensure you bump up this number
const PLUGIN_API_VERSION = "0.8.42";
const PLUGIN_API_VERSION = "0.8.43";
class PluginApi {
constructor(version, container) {

View File

@ -16,12 +16,8 @@ import {
} from "@ember/runloop";
import { Promise } from "rsvp";
import {
applyHeaderContentPluginApiCallbacks,
applyModifyNoSelectionPluginApiCallbacks,
applyContentPluginApiCallbacks,
applyOnOpenPluginApiCallbacks,
applyOnClosePluginApiCallbacks,
applyOnInputPluginApiCallbacks
applyOnChangePluginApiCallbacks
} from "select-kit/mixins/plugin-api";
export const MAIN_COLLECTION = "MAIN_COLLECTION";
@ -379,15 +375,7 @@ export default Component.extend(
cancel(this._searchPromise);
}
const input = applyOnInputPluginApiCallbacks(
this.pluginApiIdentifiers,
event,
this.selectKit
);
if (input) {
debounce(this, this._debouncedInput, event.target.value, 200);
}
},
_debouncedInput(filter) {
@ -430,6 +418,9 @@ export default Component.extend(
}
this._boundaryActionHandler("onChange", value, items);
applyOnChangePluginApiCallbacks(value, items, this);
resolve(items);
}).finally(() => {
if (!this.isDestroying && !this.isDestroyed) {
@ -448,11 +439,7 @@ export default Component.extend(
_modifyContentWrapper(content) {
content = this.modifyContent(content);
return applyContentPluginApiCallbacks(
this.pluginApiIdentifiers,
content,
this.selectKit
);
return applyContentPluginApiCallbacks(content, this);
},
modifyContent(content) {
@ -460,13 +447,7 @@ export default Component.extend(
},
_modifyNoSelectionWrapper() {
let none = this.modifyNoSelection();
return applyModifyNoSelectionPluginApiCallbacks(
this.pluginApiIdentifiers,
none,
this.selectKit
);
return this.modifyNoSelection();
},
modifyNoSelection() {
@ -498,12 +479,6 @@ export default Component.extend(
},
_modifySelectionWrapper(item) {
applyHeaderContentPluginApiCallbacks(
this.pluginApiIdentifiers,
item,
this.selectKit
);
return this.modifySelection(item);
},
@ -731,30 +706,14 @@ export default Component.extend(
this.selectKit.change(null, null);
},
_onOpenWrapper(event) {
let boundaryAction = this._boundaryActionHandler("onOpen");
boundaryAction = applyOnOpenPluginApiCallbacks(
this.pluginApiIdentifiers,
this.selectKit,
event
);
return boundaryAction;
_onOpenWrapper() {
return this._boundaryActionHandler("onOpen");
},
_onCloseWrapper(event) {
_onCloseWrapper() {
this.set("selectKit.highlighted", null);
let boundaryAction = this._boundaryActionHandler("onClose");
boundaryAction = applyOnClosePluginApiCallbacks(
this.pluginApiIdentifiers,
this.selectKit,
event
);
return boundaryAction;
return this._boundaryActionHandler("onClose");
},
_toggle(event) {
@ -772,9 +731,7 @@ export default Component.extend(
this.clearErrors();
if (!this.selectKit.onClose(event)) {
return;
}
this.selectKit.onClose(event);
this.selectKit.setProperties({
isExpanded: false,
@ -789,9 +746,7 @@ export default Component.extend(
this.clearErrors();
if (!this.selectKit.onOpen(event)) {
return;
}
this.selectKit.onOpen(event);
if (!this.popper) {
const anchor = document.querySelector(

View File

@ -12,257 +12,55 @@ function appendContent(pluginApiIdentifiers, contentFunction) {
}
let _prependContentCallbacks = {};
function prependContent(pluginApiIdentifiers, contentFunction) {
if (isNone(_prependContentCallbacks[pluginApiIdentifiers])) {
_prependContentCallbacks[pluginApiIdentifiers] = [];
function prependContent(targetedIdentifier, contentFunction) {
if (isNone(_prependContentCallbacks[targetedIdentifier])) {
_prependContentCallbacks[targetedIdentifier] = [];
}
_prependContentCallbacks[pluginApiIdentifiers].push(contentFunction);
_prependContentCallbacks[targetedIdentifier].push(contentFunction);
}
let _filterContentCallbacks = {};
function filterContent(pluginApiIdentifiers, contentFunction) {
if (isNone(_filterContentCallbacks[pluginApiIdentifiers])) {
_filterContentCallbacks[pluginApiIdentifiers] = [];
let _onChangeCallbacks = {};
function onChange(pluginApiIdentifiers, mutationFunction) {
if (isNone(_onChangeCallbacks[pluginApiIdentifiers])) {
_onChangeCallbacks[pluginApiIdentifiers] = [];
}
_filterContentCallbacks[pluginApiIdentifiers].push(contentFunction);
_onChangeCallbacks[pluginApiIdentifiers].push(mutationFunction);
}
let _modifyContentCallbacks = {};
function modifyContent(pluginApiIdentifiers, contentFunction) {
if (isNone(_modifyContentCallbacks[pluginApiIdentifiers])) {
_modifyContentCallbacks[pluginApiIdentifiers] = [];
}
_modifyContentCallbacks[pluginApiIdentifiers].push(contentFunction);
}
let _modifyHeaderComputedContentCallbacks = {};
function modifyHeaderComputedContent(pluginApiIdentifiers, contentFunction) {
if (isNone(_modifyHeaderComputedContentCallbacks[pluginApiIdentifiers])) {
_modifyHeaderComputedContentCallbacks[pluginApiIdentifiers] = [];
}
_modifyHeaderComputedContentCallbacks[pluginApiIdentifiers].push(
contentFunction
);
}
let _modifyNoSelectionCallbacks = {};
function modifyNoSelection(pluginApiIdentifiers, contentFunction) {
if (isNone(_modifyNoSelectionCallbacks[pluginApiIdentifiers])) {
_modifyNoSelectionCallbacks[pluginApiIdentifiers] = [];
}
_modifyNoSelectionCallbacks[pluginApiIdentifiers].push(contentFunction);
}
let _modifyCollectionHeaderCallbacks = {};
function modifyCollectionHeader(pluginApiIdentifiers, contentFunction) {
if (isNone(_modifyCollectionHeaderCallbacks[pluginApiIdentifiers])) {
_modifyCollectionHeaderCallbacks[pluginApiIdentifiers] = [];
}
_modifyCollectionHeaderCallbacks[pluginApiIdentifiers].push(contentFunction);
}
let _onSelectCallbacks = {};
function onSelect(pluginApiIdentifiers, mutationFunction) {
if (isNone(_onSelectCallbacks[pluginApiIdentifiers])) {
_onSelectCallbacks[pluginApiIdentifiers] = [];
}
_onSelectCallbacks[pluginApiIdentifiers].push(mutationFunction);
}
let _onOpenCallbacks = {};
function onOpen(pluginApiIdentifiers, mutationFunction) {
if (isNone(_onOpenCallbacks[pluginApiIdentifiers])) {
_onOpenCallbacks[pluginApiIdentifiers] = [];
}
_onOpenCallbacks[pluginApiIdentifiers].push(mutationFunction);
}
let _onCloseCallbacks = {};
function onClose(pluginApiIdentifiers, mutationFunction) {
if (isNone(_onCloseCallbacks[pluginApiIdentifiers])) {
_onCloseCallbacks[pluginApiIdentifiers] = [];
}
_onCloseCallbacks[pluginApiIdentifiers].push(mutationFunction);
}
let _onInputCallbacks = {};
function onInput(pluginApiIdentifiers, mutationFunction) {
if (isNone(_onInputCallbacks[pluginApiIdentifiers])) {
_onInputCallbacks[pluginApiIdentifiers] = [];
}
_onInputCallbacks[pluginApiIdentifiers].push(mutationFunction);
}
export function applyContentPluginApiCallbacks(
identifiers,
content,
selectKit
) {
identifiers.forEach(key => {
export function applyContentPluginApiCallbacks(content, component) {
makeArray(component.pluginApiIdentifiers).forEach(key => {
(_prependContentCallbacks[key] || []).forEach(c => {
content = makeArray(c(selectKit, content)).concat(content);
content = makeArray(c(component, content)).concat(content);
});
(_appendContentCallbacks[key] || []).forEach(c => {
content = content.concat(makeArray(c(selectKit, content)));
});
const filterCallbacks = _filterContentCallbacks[key] || [];
if (filterCallbacks.length) {
content = content.filter(c => {
let kept = true;
filterCallbacks.forEach(cb => {
kept = cb(selectKit, c);
});
return kept;
});
}
(_modifyContentCallbacks[key] || []).forEach(c => {
content = c(selectKit, content);
content = content.concat(makeArray(c(component, content)));
});
});
return content;
}
export function applyHeaderContentPluginApiCallbacks(
identifiers,
content,
context
) {
identifiers.forEach(key => {
(_modifyHeaderComputedContentCallbacks[key] || []).forEach(c => {
content = c(context, content);
});
});
return content;
}
export function applyModifyNoSelectionPluginApiCallbacks(
identifiers,
content,
context
) {
identifiers.forEach(key => {
(_modifyNoSelectionCallbacks[key] || []).forEach(c => {
content = c(context, content);
});
});
return content;
}
export function applyCollectionHeaderCallbacks(
identifiers,
content,
selectKit
) {
identifiers.forEach(key => {
(_modifyCollectionHeaderCallbacks[key] || []).forEach(c => {
content = c(selectKit, content);
});
});
return content;
}
export function applyOnSelectPluginApiCallbacks(identifiers, val, selectKit) {
identifiers.forEach(key => {
(_onSelectCallbacks[key] || []).forEach(c => c(selectKit, val));
export function applyOnChangePluginApiCallbacks(value, items, component) {
makeArray(component.pluginApiIdentifiers).forEach(key => {
(_onChangeCallbacks[key] || []).forEach(c => c(component, value, items));
});
}
export function applyOnOpenPluginApiCallbacks(identifiers, selectKit, event) {
let keepBubbling = true;
identifiers.forEach(key => {
(_onOpenCallbacks[key] || []).forEach(
c => (keepBubbling = c(selectKit, event))
);
});
return keepBubbling;
}
export function applyOnClosePluginApiCallbacks(identifiers, selectKit, event) {
let keepBubbling = true;
identifiers.forEach(key => {
(_onCloseCallbacks[key] || []).forEach(
c => (keepBubbling = c(selectKit, event))
);
});
return keepBubbling;
}
export function applyOnInputPluginApiCallbacks(identifiers, event, selectKit) {
let keepBubbling = true;
identifiers.forEach(key => {
(_onInputCallbacks[key] || []).forEach(
c => (keepBubbling = c(selectKit, event))
);
});
return keepBubbling;
}
export function modifySelectKit(pluginApiIdentifiers) {
export function modifySelectKit(targetedIdentifier) {
return {
appendContent: content => {
appendContent(pluginApiIdentifiers, () => {
return content;
});
return modifySelectKit(pluginApiIdentifiers);
appendContent: callback => {
appendContent(targetedIdentifier, callback);
return modifySelectKit(targetedIdentifier);
},
prependContent: content => {
prependContent(pluginApiIdentifiers, () => {
return content;
});
return modifySelectKit(pluginApiIdentifiers);
prependContent: callback => {
prependContent(targetedIdentifier, callback);
return modifySelectKit(targetedIdentifier);
},
filterContent: filterFunction => {
filterContent(pluginApiIdentifiers, filterFunction);
return modifySelectKit(pluginApiIdentifiers);
},
modifyContent: callback => {
modifyContent(pluginApiIdentifiers, callback);
return modifySelectKit(pluginApiIdentifiers);
},
modifyHeaderComputedContent: callback => {
modifyHeaderComputedContent(pluginApiIdentifiers, callback);
return modifySelectKit(pluginApiIdentifiers);
},
modifySelection: callback => {
modifyHeaderComputedContent(pluginApiIdentifiers, callback);
return modifySelectKit(pluginApiIdentifiers);
},
modifyNoSelection: callback => {
modifyNoSelection(pluginApiIdentifiers, callback);
return modifySelectKit(pluginApiIdentifiers);
},
modifyCollectionHeader: callback => {
modifyCollectionHeader(pluginApiIdentifiers, callback);
return modifySelectKit(pluginApiIdentifiers);
},
onSelect: callback => {
onSelect(pluginApiIdentifiers, callback);
return modifySelectKit(pluginApiIdentifiers);
},
onClose: callback => {
onClose(pluginApiIdentifiers, callback);
return modifySelectKit(pluginApiIdentifiers);
},
onOpen: callback => {
onOpen(pluginApiIdentifiers, callback);
return modifySelectKit(pluginApiIdentifiers);
},
onInput: callback => {
onInput(pluginApiIdentifiers, callback);
return modifySelectKit(pluginApiIdentifiers);
onChange: callback => {
onChange(targetedIdentifier, callback);
return modifySelectKit(targetedIdentifier);
}
};
}
@ -270,15 +68,7 @@ export function modifySelectKit(pluginApiIdentifiers) {
export function clearCallbacks() {
_appendContentCallbacks = {};
_prependContentCallbacks = {};
_filterContentCallbacks = {};
_modifyNoSelectionCallbacks = {};
_modifyContentCallbacks = {};
_modifyHeaderComputedContentCallbacks = {};
_modifyCollectionHeaderCallbacks = {};
_onSelectCallbacks = {};
_onCloseCallbacks = {};
_onOpenCallbacks = {};
_onInputCallbacks = {};
_onChangeCallbacks = {};
}
const EMPTY_ARRAY = Object.freeze([]);

View File

@ -0,0 +1,109 @@
import componentTest from "helpers/component-test";
import selectKit, {
testSelectKitModule,
setDefaultState,
DEFAULT_CONTENT
} from "helpers/select-kit-helper";
import { withPluginApi } from "discourse/lib/plugin-api";
import { clearCallbacks } from "select-kit/mixins/plugin-api";
testSelectKitModule("select-kit:api", {
beforeEach() {
this.setProperties({
comboBox: selectKit(".combo-box"),
singleSelect: selectKit(".single-select:not(.combo-box)")
});
},
afterEach() {
clearCallbacks();
}
});
componentTest("modifySelectKit(identifier).appendContent", {
template: `
{{combo-box value=value content=content onChange=onChange}}
{{single-select value=value content=content onChange=onChange}}
`,
beforeEach() {
setDefaultState(this, null, { content: DEFAULT_CONTENT });
withPluginApi("0.8.43", api => {
api.modifySelectKit("combo-box").appendContent(() => {
return {
id: "alpaca",
name: "Alpaca"
};
});
});
},
async test(assert) {
await this.comboBox.expand();
const row = this.comboBox.rowByIndex(3);
assert.ok(row.exists());
assert.equal(row.value(), "alpaca");
await this.comboBox.collapse();
assert.notOk(this.singleSelect.rowByValue("alpaca").exists());
}
});
componentTest("modifySelectKit(identifier).prependContent", {
template: `
{{combo-box value=value content=content onChange=onChange}}
{{single-select value=value content=content onChange=onChange}}
`,
beforeEach() {
setDefaultState(this, null, { content: DEFAULT_CONTENT });
withPluginApi("0.8.43", api => {
api.modifySelectKit("combo-box").prependContent(() => {
return {
id: "alpaca",
name: "Alpaca"
};
});
});
},
afterEach() {},
async test(assert) {
await this.comboBox.expand();
const row = this.comboBox.rowByIndex(0);
assert.ok(row.exists());
assert.equal(row.value(), "alpaca");
await this.comboBox.collapse();
assert.notOk(this.singleSelect.rowByValue("alpaca").exists());
}
});
componentTest("modifySelectKit(identifier).onChange", {
template: `
<div id="test"></div>
{{combo-box value=value content=content onChange=onChange}}
`,
beforeEach() {
setDefaultState(this, null, { content: DEFAULT_CONTENT });
withPluginApi("0.8.43", api => {
api.modifySelectKit("combo-box").onChange((component, value, item) => {
find("#test").text(item.name);
});
});
},
async test(assert) {
await this.comboBox.expand();
await this.comboBox.selectRowByIndex(0);
assert.equal(find("#test").text(), "foo");
}
});

View File

@ -1,5 +1,5 @@
import componentTest from "helpers/component-test";
import { testSelectKitModule } from "./select-kit-test-helper";
import { testSelectKitModule } from "helpers/select-kit-helper";
testSelectKitModule("category-chooser");

View File

@ -1,7 +1,7 @@
import DiscourseURL from "discourse/lib/url";
import Category from "discourse/models/category";
import componentTest from "helpers/component-test";
import { testSelectKitModule } from "./select-kit-test-helper";
import { testSelectKitModule } from "helpers/select-kit-helper";
import {
NO_CATEGORIES_ID,
ALL_CATEGORIES_ID

View File

@ -1,5 +1,5 @@
import componentTest from "helpers/component-test";
import { testSelectKitModule } from "./select-kit-test-helper";
import { testSelectKitModule } from "helpers/select-kit-helper";
testSelectKitModule("list-setting");

View File

@ -1,5 +1,5 @@
import componentTest from "helpers/component-test";
import { testSelectKitModule } from "./select-kit-test-helper";
import { testSelectKitModule } from "helpers/select-kit-helper";
testSelectKitModule("mini-tag-chooser");

View File

@ -1,5 +1,5 @@
import componentTest from "helpers/component-test";
import { testSelectKitModule } from "./select-kit-test-helper";
import { testSelectKitModule } from "helpers/select-kit-helper";
testSelectKitModule("multi-select");

View File

@ -1,5 +1,8 @@
import componentTest from "helpers/component-test";
import { testSelectKitModule, setDefaultState } from "./select-kit-test-helper";
import {
testSelectKitModule,
setDefaultState
} from "helpers/select-kit-helper";
testSelectKitModule("notifications-button");

View File

@ -1,35 +0,0 @@
import selectKit from "helpers/select-kit-helper";
export function testSelectKitModule(moduleName, options = {}) {
moduleForComponent(`select-kit/${moduleName}`, {
integration: true,
beforeEach() {
this.set("subject", selectKit());
options.beforeEach && options.beforeEach.call(this);
},
afterEach() {
options.afterEach && options.afterEach.call(this);
}
});
}
export const DEFAULT_CONTENT = [
{ id: 1, name: "foo" },
{ id: 2, name: "bar" },
{ id: 3, name: "baz" }
];
export function setDefaultState(ctx, value, options = {}) {
const properties = Object.assign(
{
onChange: v => {
this.set("value", v);
}
},
options || {}
);
ctx.setProperties(properties);
}

View File

@ -1,5 +1,5 @@
import componentTest from "helpers/component-test";
import { testSelectKitModule } from "./select-kit-test-helper";
import { testSelectKitModule } from "helpers/select-kit-helper";
testSelectKitModule("single-select");

View File

@ -1,5 +1,5 @@
import componentTest from "helpers/component-test";
import { testSelectKitModule } from "./select-kit-test-helper";
import { testSelectKitModule } from "helpers/select-kit-helper";
import Site from "discourse/models/site";
import { set } from "@ember/object";
import pretender from "helpers/create-pretender";

View File

@ -1,5 +1,5 @@
import componentTest from "helpers/component-test";
import { testSelectKitModule } from "./select-kit-test-helper";
import { testSelectKitModule } from "helpers/select-kit-helper";
import pretender from "helpers/create-pretender";
testSelectKitModule("user-chooser");

View File

@ -276,3 +276,38 @@ export default function selectKit(selector) {
}
};
}
export function testSelectKitModule(moduleName, options = {}) {
moduleForComponent(`select-kit/${moduleName}`, {
integration: true,
beforeEach() {
this.set("subject", selectKit());
options.beforeEach && options.beforeEach.call(this);
},
afterEach() {
options.afterEach && options.afterEach.call(this);
}
});
}
export const DEFAULT_CONTENT = [
{ id: 1, name: "foo" },
{ id: 2, name: "bar" },
{ id: 3, name: "baz" }
];
export function setDefaultState(ctx, value, options = {}) {
const properties = Object.assign(
{
value,
onChange: v => {
ctx.set("value", v);
}
},
options || {}
);
ctx.setProperties(properties);
}