DEV: Move select kit to an addon (#9797)

DEV: Move select kit to an addon (#9797)

diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js
index d7f9c0d..4445fd7 100644
--- a/app/assets/javascripts/application.js
+++ b/app/assets/javascripts/application.js
@@ -1,6 +1,6 @@
 //= require_tree ./discourse-common/addon
 //= require ./polyfills
-//= require_tree ./select-kit
+//= require_tree ./select-kit/app
 //= require ./discourse/app/app
 //= require ./app-boot
 
diff --git a/app/assets/javascripts/select-kit/app/.gitkeep b/app/assets/javascripts/select-kit/app/.gitkeep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/app/assets/javascripts/select-kit/app/.gitkeep
diff --git a/app/assets/javascripts/select-kit/app/components/admin-group-selector.js b/app/assets/javascripts/select-kit/app/components/admin-group-selector.js
new file mode 100644
index 0000000..1b02a45
--- /dev/null
+++ b/app/assets/javascripts/select-kit/app/components/admin-group-selector.js
@@ -0,0 +1,9 @@
+import MultiSelectComponent from "select-kit/components/multi-select";
+
+export default MultiSelectComponent.extend({
+  pluginApiIdentifiers: ["admin-group-selector"],
+  classNames: ["admin-group-selector"],
+  selectKitOptions: {
+    allowAny: false
+  }
+});
diff --git a/app/assets/javascripts/select-kit/app/components/categories-admin-dropdown.js b/app/assets/javascripts/select-kit/app/components/categories-admin-dropdown.js
new file mode 100644
index 0000000..d8ea039
--- /dev/null
+++ b/app/assets/javascripts/select-kit/app/components/categories-admin-dropdown.js
@@ -0,0 +1,40 @@
+import I18n from "I18n";
+import DropdownSelectBoxComponent from "select-kit/components/dropdown-select-box";
+import { computed } from "@ember/object";
+import { setting } from "discourse/lib/computed";
+
+export default DropdownSelectBoxComponent.extend({
+  pluginApiIdentifiers: ["categories-admin-dropdown"],
+  classNames: ["categories-admin-dropdown"],
+  fixedCateoryPositions: setting("fixed_category_positions"),
+
+  selectKitOptions: {
+    icon: "bars",
+    showFullTitle: false,
+    autoFilterable: false,
+    filterable: false,
+    none: "select_kit.components.categories_admin_dropdown.title"
+  },
+
+  content: computed(function() {
+    const items = [
+      {
+        id: "create",
+        name: I18n.t("category.create"),
+        description: I18n.t("category.create_long"),
+        icon: "plus"
+      }
+    ];
+
+    if (this.fixedCateoryPositions) {
+      items.push({
+        id: "reorder",
+        name: I18n.t("categories.reorder.title"),
+        description: I18n.t("categories.reorder.title_long"),
+        icon: "random"
+      });
+    }
+
+    return items;
+  })
+});
diff --git a/app/assets/javascripts/select-kit/app/components/category-chooser.js b/app/assets/javascripts/select-kit/app/components/category-chooser.js
new file mode 100644
index 0000000..f36e8be
--- /dev/null
+++ b/app/assets/javascripts/select-kit/app/components/category-chooser.js
@@ -0,0 +1,151 @@
+import I18n from "I18n";
+import ComboBoxComponent from "select-kit/components/combo-box";
+import PermissionType from "discourse/models/permission-type";
+import Category from "discourse/models/category";
+import { categoryBadgeHTML } from "discourse/helpers/category-link";
+import { computed, set } from "@ember/object";
+import { isNone } from "@ember/utils";
+import { setting } from "discourse/lib/computed";
+
+export default ComboBoxComponent.extend({
+  pluginApiIdentifiers: ["category-chooser"],
+  classNames: ["category-chooser"],
+  allowUncategorizedTopics: setting("allow_uncategorized_topics"),
+  fixedCategoryPositionsOnCreate: setting("fixed_category_positions_on_create"),
+
+  selectKitOptions: {
+    filterable: true,
+    allowUncategorized: false,
+    allowSubCategories: true,
+    permissionType: PermissionType.FULL,
+    excludeCategoryId: null,
+    scopedCategoryId: null
+  },
+
+  modifyComponentForRow() {
+    return "category-row";
+  },
+
+  modifyNoSelection() {
+    if (!isNone(this.selectKit.options.none)) {
+      const none = this.selectKit.options.none;
+      const isString = typeof none === "string";
+      return this.defaultItem(
+        null,
+        I18n.t(
+          isString ? this.selectKit.options.none : "category.none"
+        ).htmlSafe()
+      );
+    } else if (
+      this.allowUncategorizedTopics ||
+      this.selectKit.options.allowUncategorized
+    ) {
+      return Category.findUncategorized();
+    } else {
+      return this.defaultItem(null, I18n.t("category.choose").htmlSafe());
+    }
+  },
+
+  modifySelection(content) {
+    if (this.selectKit.hasSelection) {
+      const category = Category.findById(this.value);
+
+      set(
+        content,
+        "label",
+        categoryBadgeHTML(category, {
+          link: false,
+          hideParent: category ? !!category.parent_category_id : true,
+          allowUncategorized: true,
+          recursive: true
+        }).htmlSafe()
+      );
+    }
+
+    return content;
+  },
+
+  search(filter) {
+    if (filter) {
+      return this.content.filter(item => {
+        const category = Category.findById(this.getValue(item));
+        const categoryName = this.getName(item);
+
+        if (category && category.parentCategory) {
+          const parentCategoryName = this.getName(category.parentCategory);
+          return (
+            this._matchCategory(filter, categoryName) ||
+            this._matchCategory(filter, parentCategoryName)
+          );
+        } else {
+          return this._matchCategory(filter, categoryName);
+        }
+      });
+    } else {
+      return this.content;
+    }
+  },
+
+  content: computed(
+    "selectKit.filter",
+    "selectKit.options.scopedCategoryId",
+    function() {
+      if (!this.selectKit.filter && this.selectKit.options.scopedCategoryId) {
+        return this.categoriesByScope(this.selectKit.options.scopedCategoryId);
+      } else {
+        return this.categoriesByScope();
+      }
+    }
+  ),
+
+  categoriesByScope(scopedCategoryId = null) {
+    const categories = this.fixedCategoryPositionsOnCreate
+      ? Category.list()
+      : Category.listByActivity();
+
+    if (scopedCategoryId) {
+      const scopedCat = Category.findById(scopedCategoryId);
+      scopedCategoryId = scopedCat.parent_category_id || scopedCat.id;
+    }
+
+    const excludeCategoryId = this.selectKit.options.excludeCategoryId;
+
+    return categories.filter(category => {
+      const categoryId = this.getValue(category);
+
+      if (
+        scopedCategoryId &&
+        categoryId !== scopedCategoryId &&
+        category.parent_category_id !== scopedCategoryId
+      ) {
+        return false;
+      }
+
+      if (
+        this.selectKit.options.allowSubCategories === false &&
+        category.parentCategory
+      ) {
+        return false;
+      }
+
+      if (
+        (this.selectKit.options.allowUncategorized === false &&
+          category.isUncategorizedCategory) ||
+        excludeCategoryId === categoryId
+      ) {
+        return false;
+      }
+
+      const permissionType = this.selectKit.options.permissionType;
+      if (permissionType) {
+        return permissionType === category.permission;
+      }
+
+      return true;
+    });
+  },
+
+  _matchCategory(filter, categoryName) {
+    return this._normalize(categoryName).indexOf(filter) > -1;
+  }
+});
diff --git a/app/assets/javascripts/select-kit/app/components/category-drop.js b/app/assets/javascripts/select-kit/app/components/category-drop.js
new file mode 100644
index 0000000..ebe5eec
--- /dev/null
+++ b/app/assets/javascripts/select-kit/app/components/category-drop.js
@@ -0,0 +1,185 @@
+import I18n from "I18n";
+import { readOnly } from "@ember/object/computed";
+import { computed } from "@ember/object";
+import ComboBoxComponent from "select-kit/components/combo-box";
+import DiscourseURL from "discourse/lib/url";
+import Category from "discourse/models/category";

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

GitHub sha: db8e872b

1 Like

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