Skip to content

Commit

Permalink
Merge pull request #20 from bedrich-schindler/feature/SUI-BS-1
Browse files Browse the repository at this point in the history
Filterable event trigger change
  • Loading branch information
adamkudrna committed Jun 8, 2015
2 parents 5b82b7a + 0a33d7d commit 32166d4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
35 changes: 22 additions & 13 deletions src/js/filterable.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,27 +123,36 @@
};


// FILTERABLE DATA-API
// FILTERABLE DAT laA-API
// ===================

$(document).on('keyup.sui.filterable.data-api', '[data-toggle=filter]', function() {
var lastEventTarget;
var lastEventValue;

$(document).on('keyup.sui.filterable.data-api change.sui.filterable.data-api', '[data-toggle=filter]', function(e) {
var $filter = $(this).closest('form');
var filterData = [];

$filter.find(':input').each(function() {
var $this = $(this);
if ($this.val() !== '' && $this.val() !== null) {
filterData.push({
'filter-attrib': $this.data('filter-attrib'),
'filter-operator': $this.data('filter-operator'),
'filter-value': $this.val()
});
}
});
if(lastEventTarget !== e.target || (lastEventTarget === e.target && lastEventValue !== e.target.value)) {
$filter.find(':input').each(function() {
var $this = $(this);
if ($this.val() !== '' && $this.val() !== null) {
filterData.push({
'filter-attrib': $this.data('filter-attrib'),
'filter-operator': $this.data('filter-operator'),
'filter-value': $this.val()
});
}

Plugin.call($($filter.data('filter-target')), filterData);
Plugin.call($($filter.data('filter-target')), filterData);
});
}

lastEventTarget = e.target;
lastEventValue = e.target.value;
});


$(document).on('click.sui.filterable.data-api', '[data-toggle="filter-reset"]', function() {
var $form = $(this).closest('form');
$form[0].reset();
Expand Down
32 changes: 29 additions & 3 deletions src/js/tests/unit/filterable.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ $(function() {
////////////////////////////
// Data-api related tests //
////////////////////////////
test('should filter filterables by changing an element in the appropriate filter form', function() {
test('should filter filterables by changing an element in the appropriate filter form on key up', function() {
stop();

// Two forms are defined to ensue that the second one doesnt interfere
Expand All @@ -563,6 +563,32 @@ $(function() {
}, 100);
});

test('should filter filterables by changing an element in the appropriate filter form on change', function() {
stop();

// Two forms are defined to ensue that the second one doesnt interfere
$('#qunit-fixture').html('<div data-tag="tag1">Tag 1</div>' +
'<form data-filter-target="#qunit-fixture div[data-tag=tag1]">' +
'<input id="control" type="text" data-toggle="filter" data-filter-attrib="tag" data-filter-operator="intersect" />' +
'</form>' +
'<div data-tag="tag2">Tag 2</div>' +
'<form data-filter-target="#qunit-fixture div[data-tag=tag2]">' +
'<input type="text" data-toggle="filter" data-filter-attrib="tag" data-filter-operator="intersect" />' +
'</form>');

$(document).on('filtered.sui.filterable', function() {
ok($('#qunit-fixture div[data-tag="tag1"]').is(':hidden'), 'tag 1 was hidden');
ok($('#qunit-fixture div[data-tag="tag2"]').is(':visible'), 'tag 2 is visible');
});

$('#control').val('tag2').change();

setTimeout(function() {
$(document).off('filtered.sui.filterable');
start();
}, 100);
});

test('should reset filterables by clicking on reset element', function() {
stop();

Expand All @@ -578,7 +604,7 @@ $(function() {
'<button type="reset" data-toggle="filter-reset" />' +
'</form>');

$('#control-2').val('tag1').keyup();
$('#control-2').val('tag1').change();
$(document).on('filtered.sui.filterable', function() {
$(document).on('resetEnd.sui.filterable', function() {
ok($('#qunit-fixture div[data-tag="tag1"]').is(':visible'), 'tag 1 was shown again');
Expand Down Expand Up @@ -613,7 +639,7 @@ $(function() {
start();
});

$('#control-1').val('').keyup();
$('#control-1').val('').change();
});

});

0 comments on commit 32166d4

Please sign in to comment.