-
-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ResourceLink as a page part (#1372)
- Loading branch information
Showing
14 changed files
with
164 additions
and
59 deletions.
There are no files selected for viewing
39 changes: 2 additions & 37 deletions
39
app/assets/javascripts/spina/controllers/page_select_controller.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 |
---|---|---|
@@ -1,38 +1,3 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
import SelectController from 'controllers/select_controller' | ||
|
||
export default class extends Controller { | ||
|
||
static get targets() { | ||
return ['input', 'label', 'search'] | ||
} | ||
|
||
connect() { | ||
// Show placeholder if there is no page selected yet | ||
if (this.labelTarget.querySelector("turbo-frame") == undefined) { | ||
this.clear() | ||
} | ||
} | ||
|
||
select(event) { | ||
let button = event.currentTarget | ||
|
||
this.inputTarget.value = button.dataset.id | ||
this.labelTarget.innerText = button.dataset.title | ||
} | ||
|
||
clear() { | ||
this.inputTarget.value = "" | ||
this.labelTarget.innerHTML = ` | ||
<span class="text-gray-400"> | ||
${this.element.dataset.placeholder} | ||
</span> | ||
` | ||
} | ||
|
||
autofocus() { | ||
setTimeout(function() { | ||
this.searchTarget.focus() | ||
}.bind(this), 100) | ||
} | ||
|
||
} | ||
export default class extends SelectController { } |
38 changes: 38 additions & 0 deletions
38
app/assets/javascripts/spina/controllers/select_controller.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,38 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
export default class extends Controller { | ||
|
||
static get targets() { | ||
return ['input', 'label', 'search'] | ||
} | ||
|
||
connect() { | ||
// Show placeholder if there is no page selected yet | ||
if (this.labelTarget.querySelector("turbo-frame") == undefined) { | ||
this.clear() | ||
} | ||
} | ||
|
||
select(event) { | ||
let button = event.currentTarget | ||
|
||
this.inputTarget.value = button.dataset.id | ||
this.labelTarget.innerText = button.dataset.title | ||
} | ||
|
||
clear() { | ||
this.inputTarget.value = "" | ||
this.labelTarget.innerHTML = ` | ||
<span class="text-gray-400"> | ||
${this.element.dataset.placeholder} | ||
</span> | ||
` | ||
} | ||
|
||
autofocus() { | ||
setTimeout(function () { | ||
this.searchTarget.focus() | ||
}.bind(this), 100) | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
app/controllers/spina/admin/resource_select_options_controller.rb
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,20 @@ | ||
module Spina | ||
module Admin | ||
class ResourceSelectOptionsController < AdminController | ||
|
||
def show | ||
@resource = Resource.find(params[:id]) | ||
end | ||
|
||
def index | ||
end | ||
|
||
def search | ||
@resources ||= Resource.all | ||
@resources = @resources.where("name ILIKE :query OR label ILIKE :query", query: "%#{params[:search]}%").order(created_at: :desc).distinct.page(params[:page]).per(20) | ||
render :index | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Spina | ||
module Parts | ||
class ResourceLink < Base | ||
attr_json :resource_id, :integer, default: nil | ||
|
||
attr_accessor :options | ||
|
||
def content | ||
::Spina::Resource.find_by(id: resource_id) | ||
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
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,32 @@ | ||
<div class="mt-6"> | ||
<label class="block text-sm leading-5 font-medium text-gray-700"><%= f.object.title %></label> | ||
<div class="text-gray-400 text-sm"><%= f.object.hint %></div> | ||
<div data-controller="select reveal" data-placeholder="<%=t "spina.resources.select_resource" %>" class="relative mt-1" data-reveal-away-value> | ||
<%= f.hidden_field :resource_id, data: {select_target: "input"} %> | ||
<button type="button" class="btn btn-default px-3 inline-flex items-center text-sm font-medium" data-action="reveal#toggle select#autofocus"> | ||
<%= heroicon("link", style: :mini, class: "w-4 h-4 mr-1 text-gray-600") %> | ||
<div data-select-target="label"> | ||
<% if f.object.resource_id.present? %> | ||
<%= turbo_frame_tag :resource_title, src: spina.admin_resource_select_option_path(f.object.resource_id) %> | ||
<% end %> | ||
</div> | ||
</button> | ||
<div class="relative mt-1"> | ||
<div data-reveal data-transition hidden class="absolute shadow-lg border border-gray-200 origin-top-right rounded-md z-10 top-0"> | ||
<div class="rounded-md bg-white shadow-xs"> | ||
<%= form_with url: spina.search_admin_resource_select_options_path, data: {turbo_frame: "resource_select_options_#{f.object.object_id}", controller: "form", debounce_time: 100} do |ff| %> | ||
<%= ff.hidden_field :object_id, value: f.object.object_id %> | ||
<%= ff.hidden_field :resource, value: f.object.options&.dig(:resource) %> | ||
<div class="p-2"> | ||
<%= ff.search_field :search, placeholder: t("spina.search"), class: "form-input sticky top-0 text-sm w-80", data: {action: "input->form#submit focus->form#submit", select_target: "search"} %> | ||
</div> | ||
<% end %> | ||
<div class="overflow-scroll max-h-80"> | ||
<%= turbo_frame_tag "resource_select_options_#{f.object.object_id}" do %> | ||
<% end %> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
13 changes: 13 additions & 0 deletions
13
app/views/spina/admin/resource_select_options/index.html.erb
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,13 @@ | ||
<%= turbo_frame_tag "resource_select_options_#{params[:object_id]}" do %> | ||
<% if params[:search].blank? %> | ||
<button type="button" data-action="select#clear reveal#hide" class="block hover:bg-gray-100 w-full text-left px-5 py-2 font-medium leading-5 text-gray-700 hover:text-gray-900 text-sm border-t border-gray-150"> | ||
– | ||
</button> | ||
<% end %> | ||
<% @resources.each do |resource| %> | ||
<button type="button" data-action="select#select reveal#hide" data-title="<%= resource.label %>" data-id="<%= resource.id %>" class="block hover:bg-gray-100 w-full text-left px-5 py-2 font-medium leading-5 text-gray-700 hover:text-gray-900 text-sm border-t border-gray-150"> | ||
<%= resource.label %> | ||
</button> | ||
<% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<%= turbo_frame_tag :resource_title do %> | ||
<%= @resource.label %> | ||
<% 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
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,16 @@ | ||
# ResourceLink | ||
|
||
An ResourceLink is a part where you can link to or reference a resource. This is useful for displaying a list of pages contained within a resource. | ||
|
||
## Theme configuration | ||
|
||
``` | ||
config.parts = [ | ||
# ... | ||
{ | ||
name: "content_group", | ||
title: "Content Group", | ||
part_type: "Spina::Parts::ResourceLink" | ||
} | ||
] | ||
``` |
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