Skip to content
Snippets Groups Projects
Unverified Commit 63088f9f authored by Sébastien Gaya's avatar Sébastien Gaya
Browse files

Merge branch 'master' of github.com:noesya/osuny

parents 3d4125f8 1121da76
No related branches found
No related tags found
No related merge requests found
/* global $ */
window.osuny.communication.menuItems = {
init: function () {
'use strict';
this.kindInput = document.querySelector('form #communication_website_menu_item_kind');
this.kindInput = document.querySelector('form .js-kind-input');
if (this.kindInput === null) {
return;
}
this.elementsForKindUrl = document.querySelectorAll('.kind-url');
this.requiredInputsForKindUrl = document.querySelectorAll('.kind-url .required select, .kind-url .required input');
this.elementsForKindPage = document.querySelectorAll('.kind-page');
this.requiredInputsForKindPage = document.querySelectorAll('.kind-page .required select, .kind-page .required input');
this.switchUrl = this.kindInput.dataset.url;
this.kindInput.addEventListener('change', this.onKindChange.bind(this));
this.onKindChange();
},
onKindChange: function () {
'use strict';
var kind = this.kindInput.value;
if (kind === 'url') {
this.showElements(this.elementsForKindUrl, this.requiredInputsForKindUrl);
this.hideElements(this.elementsForKindPage, this.requiredInputsForKindPage);
} else if (kind === 'page') {
this.showElements(this.elementsForKindPage, this.requiredInputsForKindPage);
this.hideElements(this.elementsForKindUrl, this.requiredInputsForKindUrl);
} else if (kind === 'blank') {
this.hideElements(this.elementsForKindPage, this.requiredInputsForKindPage);
this.hideElements(this.elementsForKindUrl, this.requiredInputsForKindUrl);
}
},
showElements: function (elements, requiredInputs) {
'use strict';
var i;
for (i = 0; i < elements.length; i += 1) {
elements[i].classList.remove('d-none');
}
for (i = 0; i < requiredInputs.length; i += 1) {
requiredInputs[i].setAttribute('required', 'required');
}
},
hideElements: function (elements, requiredInputs) {
'use strict';
var i;
for (i = 0; i < elements.length; i += 1) {
elements[i].classList.add('d-none');
}
for (i = 0; i < requiredInputs.length; i += 1) {
requiredInputs[i].removeAttribute('required');
}
$.ajax(this.switchUrl, {
method: 'GET',
data: 'kind=' + kind,
processData: false,
contentType: false
});
},
invoke: function () {
......
......@@ -25,6 +25,12 @@ class Admin::Communication::Website::Menu::ItemsController < Admin::Communicatio
@children = @item.children.ordered
end
def kind_switch
return unless request.xhr?
@kind = params[:kind]
return if @kind.blank?
end
def new
@item.menu = @menu
@item.website = @website
......
......@@ -80,6 +80,10 @@ class Communication::Website::Menu::Item < ApplicationRecord
}
end
def has_about?
kind_page? || kind_program? || kind_news_category? || kind_news_article
end
protected
def set_position
......
......@@ -11,13 +11,28 @@
<%= f.input :kind,
include_blank: false,
label_method: -> (p) { I18n.t(p[1], scope: 'enums.communication/website/menu/item.kind') },
class: 'js-kind-input' %>
<div class="kind-url">
input_html: {
class: 'js-kind-input',
data: { url: kind_switch_admin_communication_website_menu_items_path }
} %>
<div class="<%= 'd-none' unless item.url? %> js-url-field">
<%= f.input :url, as: :string %>
</div>
<div class="kind-page">
<%= f.input :about_type, as: :hidden, input_html: { value: 'Communication::Website::Page' } %>
<%= f.association :about, collection: @website.list_of_pages, label_method: ->(p) { sanitize p[:label] }, value_method: ->(p) { p[:id] } %>
<div class="<%= 'd-none' unless item.has_about? %> js-about-fields">
<%
if item.has_about?
if item.kind_page?
about_collection = @website.list_of_pages
elsif item.kind_program?
elsif item.kind_news_category?
elsif item.kind_news_article?
end
else
about_collection = nil
end
%>
<%= f.input :about_type, as: :hidden, input_html: { class: 'js-about-type' } %>
<%= f.association :about, collection: about_collection, label_method: ->(p) { sanitize p[:label] }, value_method: ->(p) { p[:id] }, input_html: { class: 'js-about-id' } %>
</div>
</div>
</div>
......
function displayAbout(kind, options) {
$('.js-url-field').addClass('d-none');
$('.js-about-fields').removeClass('d-none');
$('.js-about-type').val(kind);
$('.js-about-id').html(options);
}
function hideAbout() {
$('.js-about-fields').addClass('d-none');
}
<% if @kind == 'url' %>
$('.js-url-field').removeClass('d-none');
hideAbout();
<% elsif @kind == 'page' %>
<%
options = ['<option value="" label=" "></option>']
@website.list_of_pages.each do |page|
options << "<option value=\"#{page[:id]}\">#{page[:label]}</option>"
end
%>
displayAbout('Communication::Website::Page', "<%= j(raw(options.join(''))) %>");
<% elsif @kind == 'program' %>
displayAbout();
<% elsif @kind == 'news_category' %>
displayAbout();
<% elsif @kind == 'news_article' %>
displayAbout();
<% else %>
$('.js-url-field').addClass('d-none');
hideAbout();
<% end %>
......@@ -39,6 +39,7 @@ namespace :communication do
resources :menus, controller: 'website/menus' do
resources :items, controller: 'website/menu/items', except: :index do
collection do
get :kind_switch
post :reorder
end
member do
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment