-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
132 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.attachment-select--attachment { | ||
display: flex; | ||
align-items: center; | ||
height: 21px; | ||
|
||
.icon { | ||
margin: 0 $default-margin 0 0; | ||
|
||
.select2-highlighted & { | ||
fill: $white; | ||
} | ||
} | ||
} | ||
|
||
.attachment-select--attachment-name { | ||
text-overflow: ellipsis; | ||
overflow: hidden; | ||
white-space: nowrap; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Alchemy | ||
module Admin | ||
class AttachmentSelect < ViewComponent::Base | ||
delegate :alchemy, to: :helpers | ||
|
||
def initialize(attachment = nil, url: nil, placeholder: Alchemy.t("Please choose"), query_params: nil) | ||
@attachment = attachment | ||
@url = url | ||
@placeholder = placeholder | ||
@query_params = query_params | ||
end | ||
|
||
def call | ||
content_tag("alchemy-attachment-select", content, attributes) | ||
end | ||
|
||
private | ||
|
||
def attributes | ||
options = { | ||
"allow-clear": true, | ||
placeholder: @placeholder, | ||
url: @url || alchemy.api_attachments_path | ||
} | ||
|
||
if @query_params | ||
options[:"query-params"] = @query_params.to_json | ||
end | ||
|
||
if @attachment | ||
selection = ActiveModelSerializers::SerializableResource.new(@attachment) | ||
options[:selection] = selection.to_json | ||
end | ||
|
||
options | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
class Api::AttachmentsController < Api::BaseController | ||
def index | ||
authorize! :index, Attachment | ||
|
||
@attachments = Attachment.all | ||
@attachments = @attachments.ransack(params[:q]).result | ||
|
||
if params[:page] | ||
@attachments = @attachments.page(params[:page]).per(params[:per_page]) | ||
end | ||
|
||
render json: @attachments, adapter: :json, root: "data", meta: meta_data | ||
end | ||
|
||
private | ||
|
||
def meta_data | ||
{ | ||
total_count: total_count_value, | ||
per_page: per_page_value, | ||
page: page_value | ||
} | ||
end | ||
|
||
def total_count_value | ||
params[:page] ? @attachments.total_count : @attachments.size | ||
end | ||
|
||
def per_page_value | ||
if params[:page] | ||
(params[:per_page] || Kaminari.config.default_per_page).to_i | ||
else | ||
@attachments.size | ||
end | ||
end | ||
|
||
def page_value | ||
params[:page] ? params[:page].to_i : 1 | ||
end | ||
end | ||
end |
24 changes: 24 additions & 0 deletions
24
app/javascript/alchemy_admin/components/attachment_select.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { RemoteSelect } from "alchemy_admin/components/remote_select" | ||
|
||
class AttachmentSelect extends RemoteSelect { | ||
_renderResult(item) { | ||
return this._renderListEntry(item) | ||
} | ||
|
||
/** | ||
* html template for each list entry | ||
* @param {object} page | ||
* @returns {string} | ||
* @private | ||
*/ | ||
_renderListEntry(attachment) { | ||
return ` | ||
<div class="attachment-select--attachment"> | ||
<alchemy-icon name="${attachment.icon_css_class}"></alchemy-icon> | ||
<span class="attachment-select--attachment-name">${attachment.name}</span> | ||
</div> | ||
` | ||
} | ||
} | ||
|
||
customElements.define("alchemy-attachment-select", AttachmentSelect) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters