Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #23 from PolymerElements/tap-event
Browse files Browse the repository at this point in the history
Use tap event
  • Loading branch information
Yvonne Yip committed May 28, 2015
2 parents 6e46fc3 + 77c2ed6 commit a042954
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
16 changes: 11 additions & 5 deletions iron-selectable.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@
*
* @attribute activateEvent
* @type {string}
* @default 'click'
* @default 'tap'
*/
activateEvent: {
type: String,
value: 'click',
value: 'tap',
observer: '_activateEventChanged'
},

Expand Down Expand Up @@ -109,7 +109,6 @@
},

created: function() {
this._bindActivateHandler = this._activateHandler.bind(this);
this._bindFilterItem = this._filterItem.bind(this);
this._selection = new Polymer.IronSelection(this._applySelection.bind(this));
},
Expand Down Expand Up @@ -183,11 +182,13 @@
},

_addListener: function(eventName) {
this.addEventListener(eventName, this._bindActivateHandler);
this.listen(this, eventName, '_activateHandler');
},

_removeListener: function(eventName) {
this.removeEventListener(eventName, this._bindActivateHandler);
// There is no unlisten yet...
// https://github.com/Polymer/polymer/issues/1639
//this.removeEventListener(eventName, this._bindActivateHandler);
},

_activateEventChanged: function(eventName, old) {
Expand Down Expand Up @@ -276,6 +277,11 @@
},

_activateHandler: function(e) {
// TODO: remove this when https://github.com/Polymer/polymer/issues/1639 is fixed so we
// can just remove the old event listener.
if (e.type !== this.activateEvent) {
return;
}
var t = e.target;
var items = this.items;
while (t && t != this) {
Expand Down
16 changes: 8 additions & 8 deletions test/activate-event.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@
s = fixture('test');
});

test('activates on click', function() {
test('activates on tap', function() {
assert.equal(s.selected, '0');

// select Item 1
s.children[1].dispatchEvent(new CustomEvent('click', {bubbles: true}));
s.children[1].dispatchEvent(new CustomEvent('tap', {bubbles: true}));
assert.equal(s.selected, '1');
});

test('activates on click and fires iron-activate', function(done) {
test('activates on tap and fires iron-activate', function(done) {
assert.equal(s.selected, '0');

// attach iron-activate listener
Expand All @@ -71,10 +71,10 @@
});

// select Item 1
s.children[1].dispatchEvent(new CustomEvent('click', {bubbles: true}));
s.children[1].dispatchEvent(new CustomEvent('tap', {bubbles: true}));
});

test('click on already selected and fires iron-activate', function(done) {
test('tap on already selected and fires iron-activate', function(done) {
assert.equal(s.selected, '0');

// attach iron-activate listener
Expand All @@ -85,7 +85,7 @@
});

// select Item 0
s.children[0].dispatchEvent(new CustomEvent('click', {bubbles: true}));
s.children[0].dispatchEvent(new CustomEvent('tap', {bubbles: true}));
});

test('activates on mousedown', function() {
Expand Down Expand Up @@ -119,13 +119,13 @@
assert.equal(s.selected, '0');
});

test('activates on click and preventDefault', function() {
test('activates on tap and preventDefault', function() {
// attach iron-activate listener
s.addEventListener("iron-activate", function(event) {
event.preventDefault();
});
// select Item 2
s.children[2].dispatchEvent(new CustomEvent('click', {bubbles: true}));
s.children[2].dispatchEvent(new CustomEvent('tap', {bubbles: true}));
// shouldn't got selected since we preventDefault in iron-activate
assert.equal(s.selected, '0');
});
Expand Down
4 changes: 2 additions & 2 deletions test/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@
assert.isFalse(s1.multi);
});

test('to click as activateEvent', function() {
assert.equal(s1.activateEvent, 'click');
test('to tap as activateEvent', function() {
assert.equal(s1.activateEvent, 'tap');
});

test('to nothing as attrForSelected', function() {
Expand Down
4 changes: 2 additions & 2 deletions test/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@

test('activate event', function() {
var item = s1.querySelector('#item2');
item.dispatchEvent(new CustomEvent('click', {bubbles: true}));
item.dispatchEvent(new CustomEvent('tap', {bubbles: true}));
// check selected class
assert.isTrue(item.classList.contains('iron-selected'));
});
Expand Down Expand Up @@ -122,7 +122,7 @@

test('activate event', function() {
var item = s2.querySelector('#item2');
item.dispatchEvent(new CustomEvent('click', {bubbles: true}));
item.dispatchEvent(new CustomEvent('tap', {bubbles: true}));
// check selected class
assert.isTrue(item.classList.contains('iron-selected'));
});
Expand Down
8 changes: 4 additions & 4 deletions test/multi.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@

test('set multi-selection via tap', function() {
// set selectedValues
s.children[0].dispatchEvent(new CustomEvent('click', {bubbles: true}));
s.children[2].dispatchEvent(new CustomEvent('click', {bubbles: true}));
s.children[0].dispatchEvent(new CustomEvent('tap', {bubbles: true}));
s.children[2].dispatchEvent(new CustomEvent('tap', {bubbles: true}));
// check selected class
assert.isTrue(s.children[0].classList.contains('iron-selected'));
assert.isTrue(s.children[2].classList.contains('iron-selected'));
Expand All @@ -100,12 +100,12 @@
deselectEventCounter++;
});
// tap to select an item
s.children[0].dispatchEvent(new CustomEvent('click', {bubbles: true}));
s.children[0].dispatchEvent(new CustomEvent('tap', {bubbles: true}));
// check events
assert.equal(selectEventCounter, 1);
assert.equal(deselectEventCounter, 0);
// tap on already selected item should deselect it
s.children[0].dispatchEvent(new CustomEvent('click', {bubbles: true}));
s.children[0].dispatchEvent(new CustomEvent('tap', {bubbles: true}));
// check selectedValues
assert.equal(s.selectedValues.length, 0);
// check class
Expand Down

0 comments on commit a042954

Please sign in to comment.