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

autocheck parents in post.categories

parent 75d256b7
No related branches found
No related tags found
No related merge requests found
//= require_self
//= require ./menu_items
//= require ./posts
//= require ./websites
window.osuny.communication = {};
window.osuny.communication.posts = {
init: function () {
'use strict';
var i;
this.fieldset = document.querySelector('form fieldset.communication_website_post_categories');
if (this.fieldset === null) {
return;
}
this.checkboxes = this.fieldset.querySelectorAll('input[type="checkbox"]');
for (i = 0; i < this.checkboxes.length; i += 1) {
this.checkboxes[i].addEventListener('change', this.onCheckboxChange.bind(this));
}
},
onCheckboxChange: function (event) {
'use strict';
var checkbox = event.currentTarget,
parentId = checkbox.dataset.parent,
parentCheckbox;
if (!checkbox.checked || typeof parentId === 'undefined') {
return;
}
parentCheckbox = this.fieldset.querySelector('input[type="checkbox"][value="' + parentId + '"]');
if (parentCheckbox !== null) {
this.triggerCheck(parentCheckbox);
}
},
triggerCheck: function (checkbox) {
'use strict';
var evt;
checkbox.checked = true;
if (typeof document.createEvent !== 'undefined') {
evt = document.createEvent('HTMLEvents');
evt.initEvent('change', false, true);
checkbox.dispatchEvent(evt);
} else {
checkbox.fireEvent('onchange');
}
},
invoke: function () {
'use strict';
return {
init: this.init.bind(this)
};
}
}.invoke();
window.addEventListener('DOMContentLoaded', function () {
'use strict';
if (document.body.classList.contains('posts-new') || document.body.classList.contains('posts-edit')) {
window.osuny.communication.posts.init();
}
});
......@@ -21,7 +21,7 @@ module WithTree
def self_and_children(level)
elements = []
label = "&nbsp;&nbsp;&nbsp;" * level + self.to_s
elements << { label: label, id: self.id }
elements << { label: label, id: self.id, parent_id: self.parent_id }
children.each do |child|
elements.concat(child.self_and_children(level + 1))
end
......
......@@ -29,9 +29,11 @@
<%= f.association :author, collection: @website.authors.ordered if @website.authors.any? %>
<%= f.association :categories,
as: :check_boxes,
collection: @website.list_of_categories,
label_method: ->(p) { sanitize p[:label] },
value_method: ->(p) { p[:id] } if @website.categories.any? %>
collection: @website.list_of_categories.map { |category| [
sanitize(category[:label]),
category[:id],
{ data: { parent: category[:parent_id] } }
] } if @website.categories.any? %>
</div>
</div>
<div class="card flex-fill w-100">
......
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