From eb95157b52dc72d099cc336328439ad45e34432a Mon Sep 17 00:00:00 2001 From: Anna Headley Date: Mon, 29 Aug 2016 16:21:35 -0400 Subject: [PATCH] Add alphabetical sort to file manager, fix #929 --- .../curation_concerns/curation_concerns.js | 1 + .../file_manager/sorting.es6 | 49 ++++- .../base/_file_manager_actions.html.erb | 1 + .../file_manager_sorting_spec.coffee | 23 +++ spec/javascripts/fixtures/sortable.html | 182 ++++++++++++++++++ 5 files changed, 251 insertions(+), 5 deletions(-) create mode 100644 spec/javascripts/file_manager_sorting_spec.coffee create mode 100644 spec/javascripts/fixtures/sortable.html diff --git a/app/assets/javascripts/curation_concerns/curation_concerns.js b/app/assets/javascripts/curation_concerns/curation_concerns.js index e4bcd3b9f..cee79e7d8 100644 --- a/app/assets/javascripts/curation_concerns/curation_concerns.js +++ b/app/assets/javascripts/curation_concerns/curation_concerns.js @@ -10,6 +10,7 @@ //= require curation_concerns/collections //= require curation_concerns/file_manager //= require curation_concerns/boot +//= require babel/polyfill // Initialize plugins and Bootstrap dropdowns on jQuery's ready event as well as // Turbolinks's page change event. diff --git a/app/assets/javascripts/curation_concerns/file_manager/sorting.es6 b/app/assets/javascripts/curation_concerns/file_manager/sorting.es6 index 25aa34c7d..ceef1cbdb 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,38 @@ export default class SortManager { } ).toArray() } + + get alpha_sort_button() { + return $("*[data-action='alpha-sort-action']") + } + + initialize_alpha_sort_button() { + 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().get() + children.forEach(function(child) { + let title = $(child).find("input.title").val() + array.push( + { title: title, + element: child } + ) + }) + // sort array by title of each object + array.sort(function(o1, o2) { + let a = o1.title.toLowerCase() + let b = o2.title.toLowerCase() + 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" %> diff --git a/spec/javascripts/file_manager_sorting_spec.coffee b/spec/javascripts/file_manager_sorting_spec.coffee new file mode 100644 index 000000000..60b746dd2 --- /dev/null +++ b/spec/javascripts/file_manager_sorting_spec.coffee @@ -0,0 +1,23 @@ +describe "FileManagerSorting", -> + sortm = require('curation_concerns/file_manager/sorting') + sort_manager = null + save_manager = null + titles = null + beforeEach () -> + loadFixtures('sortable.html') + save_manager = { + push_changed: () -> {}, + mark_unchanged: () -> {} + } + sort_manager= new sortm(save_manager) + describe "sort_alpha", -> + it "sorts correctly, ignoring capitalization", -> + expect(sort_manager.order).toEqual(sort_manager.element.data("current-order")) + sort_manager.sort_alpha() + # order has changed + expect(sort_manager.order).not.toEqual(sort_manager.element.data("current-order")) + # order is now alphabetical + titles = $("input.title").map( -> + return $(@).val() + ).get() + expect(titles).toEqual([ 'child1', 'child2', 'CIMG1815.JPG', 'CIMG1816 copy.JPG', 'zeldogbeach2.jpg' ]) diff --git a/spec/javascripts/fixtures/sortable.html b/spec/javascripts/fixtures/sortable.html new file mode 100644 index 000000000..cf53963ac --- /dev/null +++ b/spec/javascripts/fixtures/sortable.html @@ -0,0 +1,182 @@ +