diff --git a/app/assets/javascripts/curation_concerns/file_manager/sorting.es6 b/app/assets/javascripts/curation_concerns/file_manager/sorting.es6 index 25aa34c7d..ab46e6f76 100644 --- a/app/assets/javascripts/curation_concerns/file_manager/sorting.es6 +++ b/app/assets/javascripts/curation_concerns/file_manager/sorting.es6 @@ -5,6 +5,7 @@ export default class SortManager { this.initialize_sort() this.element.data("current-order", this.order) this.save_manager = save_manager + this.initialize_alpha_sort_button() } initialize_sort() { @@ -42,17 +43,21 @@ export default class SortManager { return this.element.children().index(item) } + register_order_change() { + if(this.order.toString() != this.element.data("current-order").toString()) { + this.save_manager.push_changed(this) + } else { + this.save_manager.mark_unchanged(this) + } + } + get stopped_sorting() { return (event, ui) => { this.sorting_info.end = this.get_sort_position($(ui.item)) if(this.sorting_info.end == this.sorting_info.start) { return } - if(this.order.toString() != this.element.data("current-order").toString()) { - this.save_manager.push_changed(this) - } else { - this.save_manager.mark_unchanged(this) - } + this.register_order_change() } } @@ -82,4 +87,42 @@ export default class SortManager { } ).toArray() } + + get alpha_sort_button() { + return $("*[data-action='alpha-sort-action']") + } + + initialize_alpha_sort_button() { + Blacklight.onLoad(() => { + let that = this + this.alpha_sort_button.click(function() { that.sort_alpha() } ) + }) + } + + sort_alpha() { + // create array of { title: element } objects + let array = [] + let children = this.element.children() + for (let child of children) { + let title = $(child).find("input.title").val() + let id = $(child).data('reorder-id') + array.push( + { title: title, + element: child, + id: id } + ) + } + // sort array by title of each object + array.sort(function(o1, o2) { + let a = o1.title + let b = o2.title + return a < b ? -1 : (a > b ? 1 : 0); + }); + // replace contents of #sortable with elements from the array + this.element.empty() + for (let child of array) { + this.element.append(child.element) + } + this.register_order_change() + } } diff --git a/app/views/curation_concerns/base/_file_manager_actions.html.erb b/app/views/curation_concerns/base/_file_manager_actions.html.erb index 23b78247d..fd98f2540 100644 --- a/app/views/curation_concerns/base/_file_manager_actions.html.erb +++ b/app/views/curation_concerns/base/_file_manager_actions.html.erb @@ -1,4 +1,5 @@
<%= button_tag "Save", class: "btn btn-primary disabled", data: { action: "save-actions" }%> + <%= button_tag "Sort alphabetically", class: "btn btn-primary", data: { action: "alpha-sort-action" }%>
<%= render "file_manager_resource_form" %>