Skip to content
This repository has been archived by the owner on May 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #232 from pulibrary/merge_reorder_label
Browse files Browse the repository at this point in the history
Combine bulk label and reorder views.
  • Loading branch information
escowles committed Dec 7, 2015
2 parents b814875 + 17882d5 commit 194cd00
Show file tree
Hide file tree
Showing 19 changed files with 232 additions and 299 deletions.
222 changes: 125 additions & 97 deletions app/assets/javascripts/bulk_label.es6
Original file line number Diff line number Diff line change
@@ -1,119 +1,147 @@
{
jQuery(() => {
window.bulk_labeler = new BulkLabeler
})
/* exported BulkLabeler */
class BulkLabeler {
constructor() {
this.element = $("*[data-action=bulk-label]")
this.actions_element = new window.LabelerActionsManager(this.element.children(".actions"))
$("#foliate-settings").hide()
this.element.children("ul").selectable(
{
stop: this.stopped_label_select,
selecting: this.shift_enabled_selecting
}
)
this.element.find("li input[type=text]").change(this.input_value_changed)
this.element.find("li input[type=text]").on("focus", function() {
$(this).data("current-value", $(this).val())
})
this.track_radio_buttons()
this.setup_buttons()
this.flash = new window.Flash
}

class BulkLabeler {
constructor() {
this.element = $("*[data-action=bulk-label]")
this.actions_element = new window.LabelerActionsManager(this.element.children(".actions"))
$("#foliate-settings").hide()
this.element.children("ul").selectable(
{
stop: this.stopped_label_select,
selecting: this.shift_enabled_selecting
}
)
this.element.find("li input[type=text]").change(this.input_value_changed)
this.element.find("li input[type=text]").on("focus", function() {
$(this).data("current-value", $(this).val())
})
this.setup_buttons()
}
initialize_radio_buttons() {
this.element.find("li input[type=radio]:checked").each(function(id, element) {
element = $(element)
let parent = element.parents("div").first()
parent.attr("data-first-value", element.val())
})
}
track_radio_buttons() {
let master = this
this.initialize_radio_buttons()
this.element.find("li input[type=radio]").change(function() {
let parent = $(this).parents("div").first()
if(parent.attr("data-first-value") != $(this).val()) {
parent.attr("data-old-value", $(this).val())
} else {
parent.attr("data-old-value", null)
}
master.check_save_button()
})
}

setup_buttons() {
this.actions_element.on_apply(this.apply_labels)
this.actions_element.on_save(this.save_labels)
let master = this
this.element.find("form").on("ajax:success", function() {
let form_input = $(this)
form_input.find("input[data-old-title]").attr("data-old-title",null)
master.actions_element.save_button.prop("disabled", master.changed_members.length == 0)
})
}
setup_buttons() {
this.actions_element.on_apply(this.apply_labels)
this.actions_element.on_save(this.save_labels)
let master = this
this.element.find("form").on("ajax:success", function() {
let form_input = $(this)
window.form_input = form_input
form_input.find("*[data-old-value]").attr("data-old-value",null)
master.initialize_radio_buttons()
master.check_save_button()
})
}

get shift_enabled_selecting() {
let prev = -1
return function(e, ui) { // on select
let curr = $(ui.selecting.tagName, e.target).index(ui.selecting) // get selecting item index
if(e.shiftKey && prev > -1) { // if shift key was pressed and there is previous - select them all
$(ui.selecting.tagName, e.target).slice(Math.min(prev, curr), 1 + Math.max(prev, curr)).addClass("ui-selected")
} else {
prev = curr // othervise just save prev
}
get shift_enabled_selecting() {
let prev = -1
return function(e, ui) { // on select
let curr = $(ui.selecting.tagName, e.target).index(ui.selecting) // get selecting item index
if(e.shiftKey && prev > -1) { // if shift key was pressed and there is previous - select them all
$(ui.selecting.tagName, e.target).slice(Math.min(prev, curr), 1 + Math.max(prev, curr)).addClass("ui-selected")
} else {
prev = curr // othervise just save prev
}
}
}

get apply_labels() {
return (event) => {
event.preventDefault()
let generator = this.generator
let value = null
let title_field = null
for(let i of this.selected_elements.toArray()) {
i = $(i)
value = generator.next().value
title_field = i.find("input[name='file_set[title][]']")
this.title_field_changing(title_field, value)
}
get apply_labels() {
return (event) => {
event.preventDefault()
let generator = this.generator
let value = null
let title_field = null
for(let i of this.selected_elements.toArray()) {
i = $(i)
value = generator.next().value
title_field = i.find("input[name='file_set[title][]']")
this.field_changing(title_field, value)
}
}
}

title_field_changing(title_field, value) {
if(title_field.val() != value) {
if(!title_field.attr("data-old-title")) {
title_field.attr("data-old-title", title_field.val())
}
if(title_field.attr("data-old-title") == value) {
title_field.attr("data-old-title", null)
}
} else {
title_field.attr("data-old-title", null)
field_changing(field, value) {
if(field.val() != value) {
if(!field.attr("data-old-value")) {
field.attr("data-old-value", field.val())
}
if(field.attr("data-old-value") == value) {
field.attr("data-old-value", null)
}
title_field.val(value)
this.actions_element.save_button.prop("disabled", this.changed_members.length == 0)
} else {
field.attr("data-old-value", null)
}
field.val(value)
this.check_save_button()
}

get generator() {
return this.actions_element.generator
}
get generator() {
return this.actions_element.generator
}

get save_labels() {
return (event) => {
event.preventDefault()
this.changed_members.find("form").submit()
this.selected_elements.removeClass("ui-selected")
}
get save_labels() {
return (event) => {
event.preventDefault()
this.changed_members.find("form").submit()
this.selected_elements.removeClass("ui-selected")
}
}

get input_value_changed() {
let master = this
return function() {
let title_field = $(this)
let new_value = title_field.val()
title_field.val(title_field.data("current-value"))
master.title_field_changing(title_field, new_value)
}
get input_value_changed() {
let master = this
return function() {
let field = $(this)
let new_value = field.val()
field.val(field.data("current-value"))
master.field_changing(field, new_value)
}
}

get changed_members() {
return this.element.find("*[data-old-title]").parents("li")
}
get needs_saved() {
return this.changed_members.length != 0 || this.sorter.needs_saved
}

get selected_elements() {
return this.element.find("li.ui-selected")
}
check_save_button() {
this.actions_element.save_button.prop("disabled", !this.needs_saved)
}

get changed_members() {
return this.element.find("*[data-old-value]").parents("li")
}

get stopped_label_select() {
return () => {
let selected_count = this.selected_elements.length
if(selected_count > 0) {
this.actions_element.apply_button.enable()
this.actions_element.inputs.prop("disabled", false)
} else {
this.actions_element.apply_button.disable()
this.actions_element.inputs.prop("disabled", true)
}
get selected_elements() {
return this.element.find("li.ui-selected")
}

get stopped_label_select() {
return () => {
let selected_count = this.selected_elements.length
if(selected_count > 0) {
this.actions_element.apply_button.enable()
this.actions_element.inputs.prop("disabled", false)
} else {
this.actions_element.apply_button.disable()
this.actions_element.inputs.prop("disabled", true)
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions app/assets/javascripts/flash.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* exported Flash */
class Flash {
constructor() {
this.element = $("*[data-reorder-action='flash']")
this.element.children(".close").click(
() => {
this.element.hide()
}
)
}
reset_type() {
this.element.removeClass("alert-success")
this.element.removeClass("alert-danger")
}
set(type, message) {
this.reset_type()
this.element.addClass(`alert-${type}`)
this.element.children(".text").text(message)
this.element.show()
this.element.removeClass("hidden")
}
}
Loading

0 comments on commit 194cd00

Please sign in to comment.