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

Commit

Permalink
Use observeNodes in favor of MutationObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Joel committed Oct 16, 2015
1 parent dca7800 commit 88658f2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 34 deletions.
55 changes: 23 additions & 32 deletions iron-selectable.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

/**
* Returns the currently selected item.
*
*
* @type {?Object}
*/
selectedItem: {
Expand Down Expand Up @@ -106,6 +106,17 @@
value: null
},

/**
* The list of items from which a selection can be made.
*/
items: {
type: Array,
readOnly: true,
value: function() {
return [];
}
},

/**
* The set of excluded elements where the key is the `localName`
* of the element that will be ignored from the item list.
Expand Down Expand Up @@ -133,7 +144,7 @@

attached: function() {
this._observer = this._observeItems(this);
this._contentObserver = this._observeContent(this);
this._updateItems();
if (!this.selectedItem && this.selected) {
this._updateSelected(this.attrForSelected,this.selected)
}
Expand All @@ -142,25 +153,11 @@

detached: function() {
if (this._observer) {
this._observer.disconnect();
}
if (this._contentObserver) {
this._contentObserver.disconnect();
Polymer.dom(this).unobserveNodes(this._observer);
}
this._removeListener(this.activateEvent);
},

/**
* Returns an array of selectable items.
*
* @property items
* @type Array
*/
get items() {
var nodes = Polymer.dom(this).queryDistributedElements(this.selectable || '*');
return Array.prototype.filter.call(nodes, this._bindFilterItem);
},

/**
* Returns the index of the given item.
*
Expand Down Expand Up @@ -216,6 +213,12 @@
this._addListener(eventName);
},

_updateItems: function() {
var nodes = Polymer.dom(this).queryDistributedElements(this.selectable || '*');
nodes = Array.prototype.filter.call(nodes, this._bindFilterItem);
this._setItems(nodes);
},

_updateSelected: function() {
this._selectSelected(this.selected);
},
Expand Down Expand Up @@ -274,34 +277,22 @@
this._setSelectedItem(this._selection.get());
},

// observe content changes under the given node.
_observeContent: function(node) {
var content = node.querySelector('content');
if (content && content.parentElement === node) {
return this._observeItems(node.domHost);
}
},

// observe items change under the given node.
_observeItems: function(node) {
// TODO(cdata): Update this when we get distributed children changed.
var observer = new MutationObserver(function(mutations) {
return Polymer.dom(node).observeNodes(function(mutations) {
// Let other interested parties know about the change so that
// we don't have to recreate mutation observers everywher.
this.fire('iron-items-changed', mutations, {
bubbles: false,
cancelable: false
});

this._updateItems();

if (this.selected != null) {
this._updateSelected();
}
}.bind(this));
observer.observe(node, {
childList: true,
subtree: true
});
return observer;
},

_activateHandler: function(e) {
Expand Down
4 changes: 2 additions & 2 deletions test/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@
changeCount++;
});

s2.appendChild(newItem);
Polymer.dom(s2).appendChild(newItem);

Polymer.Base.async(function() {
s2.removeChild(newItem);
Polymer.dom(s2).removeChild(newItem);

Polymer.Base.async(function() {
expect(changeCount).to.be.equal(2);
Expand Down

0 comments on commit 88658f2

Please sign in to comment.