-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
delete multiple images with select all button
- Loading branch information
Showing
11 changed files
with
236 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ruby-2.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,61 @@ | ||
(function($) { | ||
$.PMX.MultiImageDestroyer = function(el, options) { | ||
$.PMX.WatchImageSelections = function($el, options) { | ||
var base = this; | ||
|
||
base.$el = $(el); | ||
base.xhr = null; | ||
base.$el = $el; | ||
|
||
base.defaultOptions = { | ||
allSelector: 'input#all', | ||
observerSelector: 'input[type=checkbox]', | ||
submitSelector: 'button[type=submit]', | ||
deleteCheckbox: '.images input[type=checkbox]' | ||
}; | ||
|
||
base.init = function () { | ||
base.options = $.extend({}, base.defaultOptions, options); | ||
base.bindEvents(); | ||
}; | ||
}; | ||
|
||
$.fn.imageActions = function(options) { | ||
return this.each(function() { | ||
(new $.PMX.MultiImageDestroyer(this, options)).init(); | ||
}); | ||
base.bindEvents = function() { | ||
base.$el.on('click', base.options.submitSelector, base.submitForm); | ||
base.$el.on('change', base.options.allSelector, base.toggleCheckboxes); | ||
base.$el.on('change', base.options.observerSelector, base.submitState); | ||
}; | ||
|
||
base.submitForm = function(e) { | ||
var submitButton = $(e.currentTarget); | ||
|
||
if (submitButton.hasClass('disabled')) { | ||
e.preventDefault(); | ||
} | ||
}; | ||
|
||
base.submitState = function(e) { | ||
var checked = base.$el.find(base.options.deleteCheckbox).filter(':checked'), | ||
submitButton = base.$el.find(base.options.submitSelector); | ||
|
||
if (checked.length > 0) { | ||
submitButton.removeClass('disabled'); | ||
} else { | ||
submitButton.addClass('disabled'); | ||
} | ||
}; | ||
|
||
base.toggleCheckboxes = function(e) { | ||
var $target = $(e.currentTarget); | ||
|
||
if($target.is(':checked')) { | ||
base.$el.find(base.options.deleteCheckbox).prop('checked', true); | ||
} else { | ||
base.$el.find(base.options.deleteCheckbox).prop('checked', false); | ||
} | ||
}; | ||
}; | ||
|
||
$.fn.imageActions = function(options) { | ||
return this.each(function() { | ||
(new $.PMX.WatchImageSelections($(this), options)).init(); | ||
}); | ||
}; | ||
|
||
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#images_flow | ||
%section.image | ||
= button_tag 'Remove Selected', type: 'submit', class: 'button-negative submit-button disabled' | ||
.select-all | ||
.label | ||
Select All | ||
.styled-check | ||
= check_box_tag 'all', 1 | ||
%label{ :for => 'all' } | ||
%ul.images | ||
%li | ||
.actions | ||
.styled-check | ||
= check_box_tag "delete[1]", 1 | ||
%label{ :for => "delete_1" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
describe('$.fn.imageActions', function() { | ||
var subject; | ||
|
||
beforeEach(function() { | ||
fixture.load('images.html'); | ||
subject = new $.PMX.WatchImageSelections($('#images_flow section.image')); | ||
subject.init(); | ||
}); | ||
|
||
describe('clicking "Select All" ', function() { | ||
describe('when not checked', function() { | ||
it('check all of the delete image checkboxes', function() { | ||
var selectAll = $('input#all'); | ||
selectAll.prop('checked', false); | ||
|
||
selectAll.click(); | ||
expect($('.images input[type=checkbox]').filter(':checked').length).toEqual(1); | ||
}); | ||
|
||
it('enables the submit button', function() { | ||
var selectAll = $('input#all'); | ||
selectAll.prop('checked', false); | ||
|
||
selectAll.click(); | ||
expect($('button[type=submit]').hasClass('disabled')).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('when checked', function() { | ||
it('uncheck all of the delete image checkboxes', function() { | ||
var selectAll = $('input#all'), | ||
allBoxes = $('.images input[type=checkbox]'); | ||
|
||
selectAll.prop('checked', true); | ||
allBoxes.prop('checked', true); | ||
|
||
selectAll.click(); | ||
expect($('.images input[type=checkbox]').filter(':checked').length).toEqual(0); | ||
}); | ||
|
||
it('disables the submit button', function() { | ||
var selectAll = $('input#all'); | ||
selectAll.prop('checked', true); | ||
|
||
selectAll.click(); | ||
expect($('button[type=submit]').first().hasClass('disabled')).toBeTruthy(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('clicking an image checkbox', function() { | ||
describe('when not checked', function() { | ||
it('enables the submit button', function() { | ||
var checkbox = $('.images input[type=checkbox]').first(), | ||
submitButton = $('button[type=submit]'); | ||
|
||
checkbox.prop('checked', false); | ||
|
||
checkbox.click(); | ||
expect(submitButton.hasClass('disabled')).toBeFalsy() | ||
}); | ||
}); | ||
|
||
describe('when checked', function() { | ||
it('disables the submit button', function() { | ||
var checkbox = $('.images input[type=checkbox]').first(), | ||
submitButton = $('button[type=submit]'); | ||
|
||
checkbox.prop('checked', true); | ||
|
||
checkbox.click(); | ||
expect(submitButton.hasClass('disabled')).toBeTruthy() | ||
}); | ||
}); | ||
}); | ||
|
||
describe('clicking "Remove Selected"', function() { | ||
it ('prevents default when disabled', function() { | ||
var submitButton = $('button[type=submit]'), | ||
clickEvent = $.Event('click'); | ||
|
||
submitButton.addClass('disabled'); | ||
submitButton.trigger(clickEvent); | ||
expect(clickEvent.isDefaultPrevented()).toBeTruthy(); | ||
}); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters