From 1e7521f00688c533c27866871fbb64557ac87b3f Mon Sep 17 00:00:00 2001 From: fzngagan Date: Tue, 11 Aug 2020 12:42:02 +0530 Subject: [PATCH] FEATURE: Added a new field type Item Chooser --- .../wizard/components/item-chooser.js.es6 | 36 +++++++++++++++++++ .../wizard-field-item-chooser.js.es6 | 14 ++++++++ .../components/wizard-field-item-chooser.hbs | 1 + config/locales/client.en.yml | 1 + config/routes.rb | 1 + controllers/custom_wizard/item.rb | 12 +++++++ lib/custom_wizard/field.rb | 3 +- lib/custom_wizard/item.rb | 18 ++++++++++ plugin.rb | 2 ++ 9 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 assets/javascripts/wizard/components/item-chooser.js.es6 create mode 100644 assets/javascripts/wizard/components/wizard-field-item-chooser.js.es6 create mode 100644 assets/javascripts/wizard/templates/components/wizard-field-item-chooser.hbs create mode 100644 controllers/custom_wizard/item.rb create mode 100644 lib/custom_wizard/item.rb diff --git a/assets/javascripts/wizard/components/item-chooser.js.es6 b/assets/javascripts/wizard/components/item-chooser.js.es6 new file mode 100644 index 00000000..7fade8a7 --- /dev/null +++ b/assets/javascripts/wizard/components/item-chooser.js.es6 @@ -0,0 +1,36 @@ +import MiniTagChooser from "select-kit/components/mini-tag-chooser"; +import { ajax } from "discourse/lib/ajax"; +import { observes } from "discourse-common/utils/decorators"; +import { popupAjaxError } from "discourse/lib/ajax-error"; + +export default MiniTagChooser.extend({ + searchTags(url, data, callback){ + return ajax(url, { + data: { + name: data.name, + value: data.q, + } + }).then(result => callback(this, result)) + .catch(popupAjaxError); + }, + + search(filter){ + const data = { + q: filter, + name: this.get('item') + } + + return this.searchTags('/w/items/search', data, this._transformJson ); + + }, + + + _transformJson(obj, result){ + let x = result.map(item => { + return { id: item, name: item } + }) + + return x ; + }, + +}); diff --git a/assets/javascripts/wizard/components/wizard-field-item-chooser.js.es6 b/assets/javascripts/wizard/components/wizard-field-item-chooser.js.es6 new file mode 100644 index 00000000..22858b30 --- /dev/null +++ b/assets/javascripts/wizard/components/wizard-field-item-chooser.js.es6 @@ -0,0 +1,14 @@ +import Component from "@ember/component"; +import discourseComputed, { observes } from 'discourse-common/utils/decorators'; + +export default Component.extend({ + @observes('value') + updateValue(){ + this.set('field.value', this.value.join(',')); + }, + + @discourseComputed('field.label') + itemName(label){ + return label.replace(/(<([^>]+)>)/gi, ""); + } +}); diff --git a/assets/javascripts/wizard/templates/components/wizard-field-item-chooser.hbs b/assets/javascripts/wizard/templates/components/wizard-field-item-chooser.hbs new file mode 100644 index 00000000..cd2377e6 --- /dev/null +++ b/assets/javascripts/wizard/templates/components/wizard-field-item-chooser.hbs @@ -0,0 +1 @@ +{{item-chooser value=value item=itemName}} diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 65e31c96..02144b55 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -171,6 +171,7 @@ en: date: Date time: Time date_time: Date & Time + item_chooser: Item Chooser connector: and: "and" diff --git a/config/routes.rb b/config/routes.rb index 908e9656..ef9c7176 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ CustomWizard::Engine.routes.draw do get ':wizard_id/steps' => 'wizard#index' get ':wizard_id/steps/:step_id' => 'wizard#index' put ':wizard_id/steps/:step_id' => 'steps#update' + get 'items/search' => 'items#search' end Discourse::Application.routes.append do diff --git a/controllers/custom_wizard/item.rb b/controllers/custom_wizard/item.rb new file mode 100644 index 00000000..1590c2d6 --- /dev/null +++ b/controllers/custom_wizard/item.rb @@ -0,0 +1,12 @@ +class CustomWizard::ItemsController < ::ApplicationController + def search + search_params = { + name: params[:name], + value: params[:value] || '', + limit: params[:limit] || 5 + } + items = CustomWizard::Item.search(search_params) + + render json: MultiJson.dump(items) + end +end diff --git a/lib/custom_wizard/field.rb b/lib/custom_wizard/field.rb index 534e4824..dea1e0f2 100644 --- a/lib/custom_wizard/field.rb +++ b/lib/custom_wizard/field.rb @@ -49,7 +49,8 @@ class CustomWizard::Field prefill: nil, content: nil }, - user_selector: {} + user_selector: {}, + item_chooser: {} } end diff --git a/lib/custom_wizard/item.rb b/lib/custom_wizard/item.rb new file mode 100644 index 00000000..8a41afa8 --- /dev/null +++ b/lib/custom_wizard/item.rb @@ -0,0 +1,18 @@ +class CustomWizard::Item + WIZARD_ITEM = 'wizard_item'.freeze + + def initialize(name, value) + @name = name + @value = value + end + + def save + PluginStore.set(WIZARD_ITEM, @name, @value) + end + + def self.search(params) + items = PluginStore.get(WIZARD_ITEM, params[:name]) + items ? items.filter{ |string| string.downcase.include?(params[:value].downcase) }.take(params[:limit] || 5) + : [] + end +end diff --git a/plugin.rb b/plugin.rb index 6e26bcee..7a908a3e 100644 --- a/plugin.rb +++ b/plugin.rb @@ -53,6 +53,7 @@ after_initialize do ../controllers/custom_wizard/wizard.rb ../controllers/custom_wizard/steps.rb ../controllers/custom_wizard/transfer.rb + ../controllers/custom_wizard/item.rb ../jobs/clear_after_time_wizard.rb ../jobs/refresh_api_access_token.rb ../jobs/set_after_time_wizard.rb @@ -61,6 +62,7 @@ after_initialize do ../lib/custom_wizard/builder.rb ../lib/custom_wizard/field.rb ../lib/custom_wizard/mapper.rb + ../lib/custom_wizard/item.rb ../lib/custom_wizard/log.rb ../lib/custom_wizard/step_updater.rb ../lib/custom_wizard/validator.rb