DEV: improves sk api (#9653)

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
diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.js b/app/assets/javascripts/discourse/app/lib/plugin-api.js
index ab04e0e..342dce0 100644
--- a/app/assets/javascripts/discourse/app/lib/plugin-api.js
+++ b/app/assets/javascripts/discourse/app/lib/plugin-api.js
@@ -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) {
diff --git a/app/assets/javascripts/select-kit/components/select-kit.js b/app/assets/javascripts/select-kit/components/select-kit.js
index 61e21be..84f0fab 100644
--- a/app/assets/javascripts/select-kit/components/select-kit.js
+++ b/app/assets/javascripts/select-kit/components/select-kit.js
@@ -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);
-      }
+      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(
diff --git a/app/assets/javascripts/select-kit/mixins/plugin-api.js b/app/assets/javascripts/select-kit/mixins/plugin-api.js
index 370f301..22f446d 100644
--- a/app/assets/javascripts/select-kit/mixins/plugin-api.js
+++ b/app/assets/javascripts/select-kit/mixins/plugin-api.js
@@ -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);
-}
-

[... diff too long, it was truncated ...]

GitHub sha: c99ecba6

This commit appears in #9653 which was approved by eviltrout. It was merged by jjaffeux.