- Added SearchableAjaxSelect.
- Refactored search_controller.
This commit is contained in:
parent
f2b1dd2f26
commit
804e9ddcfe
6 changed files with 178 additions and 109 deletions
|
@ -1431,11 +1431,22 @@ class InputsRef extends App.ControllerContent
|
||||||
# selectable search
|
# selectable search
|
||||||
searchableSelectObject = new App.SearchableSelect
|
searchableSelectObject = new App.SearchableSelect
|
||||||
attribute:
|
attribute:
|
||||||
name: 'project-name'
|
name: 'project-name'
|
||||||
id: 'project-name-123'
|
id: 'project-name-123'
|
||||||
placeholder: 'Enter Project Name'
|
placeholder: 'Enter Project Name'
|
||||||
options: [{"value":0,"name":"Apple"},{"value":1,"name":"Microsoft","selected":true},{"value":2,"name":"Google"},{"value":3,"name":"Deutsche Bahn"},{"value":4,"name":"Sparkasse"},{"value":5,"name":"Deutsche Post"},{"value":6,"name":"Mitfahrzentrale"},{"value":7,"name":"Starbucks"},{"value":8,"name":"Mac Donalds"},{"value":9,"name":"Flixbus"},{"value":10,"name":"Betahaus"},{"value":11,"name":"Bruno Banani"},{"value":12,"name":"Alpina"},{"value":13,"name":"Samsung"},{"value":14,"name":"ChariTea"},{"value":15,"name":"fritz-kola"},{"value":16,"name":"Vitamin Water"},{"value":17,"name":"Znuny"},{"value":18,"name":"Max & Moritz"}]
|
options: [{"value":0,"name":"Apple"},{"value":1,"name":"Microsoft","selected":true},{"value":2,"name":"Google"},{"value":3,"name":"Deutsche Bahn"},{"value":4,"name":"Sparkasse"},{"value":5,"name":"Deutsche Post"},{"value":6,"name":"Mitfahrzentrale"},{"value":7,"name":"Starbucks"},{"value":8,"name":"Mac Donalds"},{"value":9,"name":"Flixbus"},{"value":10,"name":"Betahaus"},{"value":11,"name":"Bruno Banani"},{"value":12,"name":"Alpina"},{"value":13,"name":"Samsung"},{"value":14,"name":"ChariTea"},{"value":15,"name":"fritz-kola"},{"value":16,"name":"Vitamin Water"},{"value":17,"name":"Znuny"},{"value":18,"name":"Max & Moritz"}]
|
||||||
@$('.searchableSelectPlaceholder').replaceWith( searchableSelectObject.el )
|
@$('.searchableSelectPlaceholder').replaceWith( searchableSelectObject.element() )
|
||||||
|
|
||||||
|
# selectable search
|
||||||
|
searchableAjaxSelectObject = new App.SearchableAjaxSelect
|
||||||
|
attribute:
|
||||||
|
name: 'user'
|
||||||
|
id: 'user-123'
|
||||||
|
placeholder: 'Enter User'
|
||||||
|
limt: 10
|
||||||
|
object: 'User'
|
||||||
|
|
||||||
|
@$('.searchableAjaxSelectPlaceholder').replaceWith( searchableAjaxSelectObject.element() )
|
||||||
|
|
||||||
# time and timeframe
|
# time and timeframe
|
||||||
@$('.time').timepicker()
|
@$('.time').timepicker()
|
||||||
|
|
|
@ -14,7 +14,7 @@ class App.UserOrganizationAutocompletion extends App.Controller
|
||||||
@key = Math.floor( Math.random() * 999999 ).toString()
|
@key = Math.floor( Math.random() * 999999 ).toString()
|
||||||
|
|
||||||
if !@attribute.source
|
if !@attribute.source
|
||||||
@attribute.source = @apiPath + '/search_user_org'
|
@attribute.source = @apiPath + '/search/user-organization'
|
||||||
@build()
|
@build()
|
||||||
|
|
||||||
element: =>
|
element: =>
|
||||||
|
|
|
@ -3,12 +3,81 @@ class App.SearchableAjaxSelect extends App.SearchableSelect
|
||||||
onInput: (event) =>
|
onInput: (event) =>
|
||||||
super
|
super
|
||||||
|
|
||||||
# send ajax request @query
|
# convert requested object
|
||||||
|
# e.g. Ticket to ticket or AnotherObject to another_object
|
||||||
|
objectString = underscored( @options.attribute.object )
|
||||||
|
|
||||||
onAjaxResponse: (data) =>
|
# create common accessors
|
||||||
|
@apiPath = App.Config.get('api_path')
|
||||||
|
|
||||||
|
# create cache and cache key
|
||||||
|
@searchResultCache = @searchResultCache || {}
|
||||||
|
|
||||||
|
@cacheKey = "#{objectString}+#{@query}"
|
||||||
|
|
||||||
|
# use cache for search result
|
||||||
|
if @searchResultCache[@cacheKey]
|
||||||
|
return @onAjaxResponse( @searchResultCache[@cacheKey] )
|
||||||
|
|
||||||
|
# add timout for loader icon
|
||||||
|
clearTimeout @loaderTimeoutId
|
||||||
|
@loaderTimeoutId = setTimeout @showLoader, 1000
|
||||||
|
|
||||||
|
# start search request and update options
|
||||||
|
App.Ajax.request(
|
||||||
|
id: @options.attribute.id
|
||||||
|
type: 'GET'
|
||||||
|
url: "#{@apiPath}/search/#{objectString}"
|
||||||
|
data:
|
||||||
|
query: @query
|
||||||
|
limit: @options.attribute.limit
|
||||||
|
processData: true
|
||||||
|
success: @onAjaxResponse
|
||||||
|
)
|
||||||
|
|
||||||
|
onAjaxResponse: (data, status, xhr) =>
|
||||||
|
|
||||||
|
# clear timout and remove loader icon
|
||||||
|
clearTimeout @loaderTimeoutId
|
||||||
|
@el.removeClass('is-loading')
|
||||||
|
|
||||||
|
# cache search result
|
||||||
|
@searchResultCache[@cacheKey] = data
|
||||||
|
|
||||||
|
# load assets
|
||||||
|
App.Collection.loadAssets( data.assets )
|
||||||
|
|
||||||
|
# get options from search result
|
||||||
|
options = []
|
||||||
|
for object in data.result
|
||||||
|
if object.type is 'Ticket'
|
||||||
|
ticket = App.Ticket.find( object.id )
|
||||||
|
data =
|
||||||
|
name: "##{ticket.number} - #{ticket.title}"
|
||||||
|
value: ticket.id
|
||||||
|
options.push data
|
||||||
|
else if object.type is 'User'
|
||||||
|
user = App.User.find( object.id )
|
||||||
|
data =
|
||||||
|
name: "#{user.displayName()}"
|
||||||
|
value: user.id
|
||||||
|
options.push data
|
||||||
|
else if object.type is 'Organization'
|
||||||
|
organization = App.Organization.find( object.id )
|
||||||
|
data =
|
||||||
|
name: "#{organization.displayName()}"
|
||||||
|
value: organization.id
|
||||||
|
options.push data
|
||||||
|
|
||||||
|
# fill template with gathered options
|
||||||
@optionsList.html App.view('generic/searchable_select_options')
|
@optionsList.html App.view('generic/searchable_select_options')
|
||||||
options: data
|
options: options
|
||||||
|
|
||||||
|
# refresh elements
|
||||||
@refreshElements()
|
@refreshElements()
|
||||||
|
|
||||||
|
# execute filter
|
||||||
@filterByQuery @query
|
@filterByQuery @query
|
||||||
|
|
||||||
|
showLoader: =>
|
||||||
|
@el.addClass('is-loading')
|
||||||
|
|
|
@ -66,6 +66,10 @@
|
||||||
<div class="searchableSelectPlaceholder"></div>
|
<div class="searchableSelectPlaceholder"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="select form-group">
|
||||||
|
<label for="b">Users (searchable ajax)</label>
|
||||||
|
<div class="searchableAjaxSelectPlaceholder"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<h2>Checkbox</h2>
|
<h2>Checkbox</h2>
|
||||||
<div class="checkbox form-group">
|
<div class="checkbox form-group">
|
||||||
|
|
|
@ -3,8 +3,9 @@
|
||||||
class SearchController < ApplicationController
|
class SearchController < ApplicationController
|
||||||
before_action :authentication_check
|
before_action :authentication_check
|
||||||
|
|
||||||
# GET /api/v1/search_user_org
|
# GET|POST /api/v1/search/:objects
|
||||||
def search_user_org
|
|
||||||
|
def search_generic
|
||||||
|
|
||||||
# enable search only for agents and admins
|
# enable search only for agents and admins
|
||||||
if !current_user.role?(Z_ROLENAME_AGENT) && !current_user.role?(Z_ROLENAME_ADMIN)
|
if !current_user.role?(Z_ROLENAME_AGENT) && !current_user.role?(Z_ROLENAME_ADMIN)
|
||||||
|
@ -16,11 +17,17 @@ class SearchController < ApplicationController
|
||||||
query = params[:query]
|
query = params[:query]
|
||||||
limit = params[:limit] || 10
|
limit = params[:limit] || 10
|
||||||
|
|
||||||
|
# convert objects string into array of class names
|
||||||
|
# e.g. user-ticket-another_object = %w( User Ticket AnotherObject )
|
||||||
|
objects = params[:objects].split('-').map { |object|
|
||||||
|
object.camelize
|
||||||
|
}
|
||||||
|
|
||||||
# try search index backend
|
# try search index backend
|
||||||
assets = {}
|
assets = {}
|
||||||
result = []
|
result = []
|
||||||
if SearchIndexBackend.enabled?
|
if SearchIndexBackend.enabled?
|
||||||
items = SearchIndexBackend.search( query, limit, %w(User Organization) )
|
items = SearchIndexBackend.search( query, limit, objects )
|
||||||
items.each { |item|
|
items.each { |item|
|
||||||
require item[:type].to_filename
|
require item[:type].to_filename
|
||||||
record = Kernel.const_get( item[:type] ).find( item[:id] )
|
record = Kernel.const_get( item[:type] ).find( item[:id] )
|
||||||
|
@ -29,34 +36,23 @@ class SearchController < ApplicationController
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
# do query
|
# do query
|
||||||
users = User.search(
|
objects.each { |object|
|
||||||
query: query,
|
|
||||||
limit: limit,
|
|
||||||
current_user: current_user,
|
|
||||||
)
|
|
||||||
users.each do |user|
|
|
||||||
item = {
|
|
||||||
id: user.id,
|
|
||||||
type: user.class.to_s
|
|
||||||
}
|
|
||||||
result.push item
|
|
||||||
assets = user.assets(assets)
|
|
||||||
end
|
|
||||||
|
|
||||||
organizations = Organization.search(
|
found_objects = object.constantize.search(
|
||||||
query: query,
|
query: query,
|
||||||
limit: limit,
|
limit: limit,
|
||||||
current_user: current_user,
|
current_user: current_user,
|
||||||
)
|
)
|
||||||
|
|
||||||
organizations.each do |organization|
|
found_objects.each do |found_object|
|
||||||
item = {
|
item = {
|
||||||
id: organization.id,
|
id: found_object.id,
|
||||||
type: organization.class.to_s
|
type: found_object.class.to_s
|
||||||
}
|
}
|
||||||
result.push item
|
result.push item
|
||||||
assets = organization.assets(assets)
|
assets = found_object.assets(assets)
|
||||||
end
|
end
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
render json: {
|
render json: {
|
||||||
|
@ -68,64 +64,54 @@ class SearchController < ApplicationController
|
||||||
# GET /api/v1/search
|
# GET /api/v1/search
|
||||||
def search
|
def search
|
||||||
|
|
||||||
# build result list
|
assets = {}
|
||||||
tickets = Ticket.search(
|
result = []
|
||||||
limit: params[:limit],
|
objects = %w( Ticket User Organization )
|
||||||
query: params[:term],
|
if SearchIndexBackend.enabled?
|
||||||
current_user: current_user,
|
|
||||||
)
|
|
||||||
assets = {}
|
|
||||||
ticket_result = []
|
|
||||||
tickets.each do |ticket|
|
|
||||||
assets = ticket.assets(assets)
|
|
||||||
ticket_result.push ticket.id
|
|
||||||
end
|
|
||||||
|
|
||||||
# do query
|
found_objects = {}
|
||||||
users = User.search(
|
items = SearchIndexBackend.search( params[:term], params[:limit], objects )
|
||||||
query: params[:term],
|
items.each { |item|
|
||||||
limit: params[:limit],
|
require item[:type].to_filename
|
||||||
current_user: current_user,
|
record = Kernel.const_get( item[:type] ).find( item[:id] )
|
||||||
)
|
assets = record.assets(assets)
|
||||||
user_result = []
|
|
||||||
users.each do |user|
|
|
||||||
user_result.push user.id
|
|
||||||
assets = user.assets(assets)
|
|
||||||
end
|
|
||||||
|
|
||||||
organizations = Organization.search(
|
found_objects[ item[:type] ] ||= []
|
||||||
query: params[:term],
|
found_objects[ item[:type] ].push item[:id]
|
||||||
limit: params[:limit],
|
|
||||||
current_user: current_user,
|
|
||||||
)
|
|
||||||
|
|
||||||
organization_result = []
|
|
||||||
organizations.each do |organization|
|
|
||||||
organization_result.push organization.id
|
|
||||||
assets = organization.assets(assets)
|
|
||||||
end
|
|
||||||
|
|
||||||
result = []
|
|
||||||
if ticket_result[0]
|
|
||||||
data = {
|
|
||||||
name: 'Ticket',
|
|
||||||
ids: ticket_result,
|
|
||||||
}
|
}
|
||||||
result.push data
|
|
||||||
end
|
found_objects.each { |object, object_ids|
|
||||||
if user_result[0]
|
|
||||||
data = {
|
data = {
|
||||||
name: 'User',
|
name: object,
|
||||||
ids: user_result,
|
ids: object_ids,
|
||||||
|
}
|
||||||
|
result.push data
|
||||||
}
|
}
|
||||||
result.push data
|
else
|
||||||
end
|
|
||||||
if organization_result[0]
|
objects.each { |object|
|
||||||
data = {
|
|
||||||
name: 'Organization',
|
found_objects = object.constantize.search(
|
||||||
ids: organization_result,
|
query: params[:term],
|
||||||
|
limit: params[:limit],
|
||||||
|
current_user: current_user,
|
||||||
|
)
|
||||||
|
|
||||||
|
object_ids = []
|
||||||
|
found_objects.each do |found_object|
|
||||||
|
object_ids.push found_object.id
|
||||||
|
assets = found_object.assets(assets)
|
||||||
|
end
|
||||||
|
|
||||||
|
next if object_ids.empty?
|
||||||
|
|
||||||
|
data = {
|
||||||
|
name: object,
|
||||||
|
ids: object_ids,
|
||||||
|
}
|
||||||
|
result.push data
|
||||||
}
|
}
|
||||||
result.push data
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# return result
|
# return result
|
||||||
|
|
|
@ -2,9 +2,8 @@ Zammad::Application.routes.draw do
|
||||||
api_path = Rails.configuration.api_path
|
api_path = Rails.configuration.api_path
|
||||||
|
|
||||||
# search
|
# search
|
||||||
match api_path + '/search', to: 'search#search', via: [:get, :post]
|
match api_path + '/search', to: 'search#search', via: [:get, :post]
|
||||||
|
|
||||||
# search_user_org
|
|
||||||
match api_path + '/search_user_org', to: 'search#search_user_org', via: [:get, :post]
|
|
||||||
|
|
||||||
|
# search_generic
|
||||||
|
match api_path + '/search/:objects', to: 'search#search_generic', via: [:get, :post]
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue