$ = jQuery
$.fn.batchEdit = (args) ->
$elem = this
$("[data-behavior='batch-tools']", $elem).removeClass('hidden')
window.batch_edits_options = {} if typeof window.batch_edits_options is "undefined"
default_options =
checked_label: "Selected",
unchecked_label: "Select",
css_class: "batch_toggle"
options = $.extend({}, default_options, window.batch_edits_options)
# complete the override by overriding update_state_for
update_state_for = (check, state, label, form) ->
check.prop "checked", state
label.toggleClass "checked", state
if state
form.find("input[name=_method]").val "delete"
$('span', label).text(options.progress_label)
else
form.find("input[name=_method]").val "put"
$('span', label).text(options.progress_label)
for obj in $("[data-behavior='batch-add-form']", $elem)
form = $(obj)
form.children().hide()
# We're going to use the existing form to actually send our add/removes
# This works conveneintly because the exact same action href is used
# for both bookmarks/$doc_id. But let's take out the irrelevant parts
# of the form to avoid any future confusion.
form.find("input[type=submit]").remove()
form.addClass('form-inline')
# View needs to set data-doc-id so we know a unique value
# for making DOM id
unique_id = form.attr("data-doc-id") || Math.random()
# if form is currently using method delete to change state,
# then checkbox is currently checked
checked = (form.find("input[name=_method][value=delete]").size() != 0)
checkbox = $('')
.addClass( options.css_class )
.attr("id", options.css_class + "_" + unique_id)
label = $('