Skip to content

Commit

Permalink
WIP Add alphabetical sort to file manager
Browse files Browse the repository at this point in the history
  • Loading branch information
hackartisan committed Aug 29, 2016
1 parent 6a2630a commit c9cc685
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
53 changes: 48 additions & 5 deletions app/assets/javascripts/curation_concerns/file_manager/sorting.es6
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<div class="actions form-horizontal panel panel-default">
<%= 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" }%>
</div>
<%= render "file_manager_resource_form" %>

0 comments on commit c9cc685

Please sign in to comment.