Skip to content

Commit

Permalink
Minor factoring; ensure base properties set on instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Orvell committed Nov 20, 2015
1 parent 306cc81 commit da15ff0
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 87 deletions.
4 changes: 4 additions & 0 deletions polymer.html
Expand Up @@ -57,12 +57,16 @@
},

_initFeatures: function() {
// setup gestures
this._setupGestures();
// manage configuration
this._setupConfigure();
// setup style properties
this._setupStyleProperties();
// setup debouncers
this._setupDebouncers();
// setup shady
this._setupShady();
this._registerHost();
if (this._template) {
// manage local dom
Expand Down
1 change: 1 addition & 0 deletions src/lib/base.html
Expand Up @@ -33,6 +33,7 @@

createdCallback: function() {
Polymer.telemetry.instanceCount++;
this.isAttached = false;
this.root = this;
this._doBehavior('created'); // abstract
this._initFeatures(); // abstract
Expand Down
110 changes: 26 additions & 84 deletions src/lib/dom-api-shady.html
Expand Up @@ -15,6 +15,7 @@

var Settings = Polymer.Settings;
var DomApi = Polymer.DomApi;
var dom = DomApi.factory;
var TreeApi = Polymer.TreeApi;
var getInnerHTML = Polymer.domInnerHTML.getInnerHTML;
var CONTENT = DomApi.CONTENT;
Expand Down Expand Up @@ -69,7 +70,7 @@
throw Error('The ref_node to be inserted before is not a child ' +
'of this node');
}
this._addLogicalInfo(node, this.node, ref_node);
TreeApi.Logical.recordInsertBefore(node, this.node, ref_node);
}
this._addNodeToHost(node);
// if not distributing and not adding to host, do a fast path addition
Expand Down Expand Up @@ -101,7 +102,7 @@
This method also performs dom composition.
*/
removeChild: function(node) {
if (DomApi.factory(node).parentNode !== this.node) {
if (dom(node).parentNode !== this.node) {
console.warn('The node to be removed is not a child of this node',
node);
}
Expand Down Expand Up @@ -201,9 +202,9 @@
var added;
if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE &&
!node.__noContent) {
var c$ = DomApi.factory(node).querySelectorAll(CONTENT);
var c$ = dom(node).querySelectorAll(CONTENT);
for (var i=0, n, np, na; (i<c$.length) && (n=c$[i]); i++) {
np = DomApi.factory(n).parentNode;
np = dom(n).parentNode;
// don't allow node's parent to be fragment itself
if (np === node) {
np = parent;
Expand Down Expand Up @@ -231,12 +232,12 @@

_updateInsertionPoints: function(host) {
var i$ = host.shadyRoot._insertionPoints =
DomApi.factory(host.shadyRoot).querySelectorAll(CONTENT);
dom(host.shadyRoot).querySelectorAll(CONTENT);
// ensure <content>'s and their parents have logical dom info.
for (var i=0, c; i < i$.length; i++) {
c = i$[i];
TreeApi.Logical.saveChildNodes(c);
TreeApi.Logical.saveChildNodes(DomApi.factory(c).parentNode);
TreeApi.Logical.saveChildNodes(dom(c).parentNode);
}
},

Expand All @@ -248,9 +249,12 @@
_removeNodeFromParent: function(node) {
// note: we may need to notify and not have logical info so fallback
// to composed parentNode.

var parent = node.__parentNode || node.parentNode;
//var parent = dom(node).parentNode;
// TODO(sorvell): hasDomApi doesn't make sense now
if (parent && DomApi.hasApi(parent)) {
DomApi.factory(parent).notifyObserver();
dom(parent).notifyObserver();
}
this._removeNodeFromHost(node, true);
},
Expand All @@ -262,17 +266,18 @@
// to require distribution... both cases are handled here.
var hostNeedsDist;
var root;
// important that we want to do this only if the node has a logical parent
var parent = node.__parentNode;
if (parent) {
// distribute node's parent iff needed
DomApi.factory(node)._distributeParent();
dom(node)._distributeParent();
root = this._ownerShadyRootForNode(node);
// remove node from root and distribute it iff needed
if (root) {
root.host._elementRemove(node);
hostNeedsDist = this._removeDistributedChildren(root, node);
}
this._removeLogicalInfo(node, parent);
TreeApi.Logical.recordRemoveChild(node, parent);
}
this._removeOwnerShadyRoot(node);
if (root && hostNeedsDist) {
Expand All @@ -290,7 +295,7 @@
for (var i=0; i<ip$.length; i++) {
var content = ip$[i];
if (this._contains(container, content)) {
var dc$ = DomApi.factory(content).getDistributedNodes();
var dc$ = dom(content).getDistributedNodes();
for (var j=0; j<dc$.length; j++) {
hostNeedsDist = true;
var node = dc$[j];
Expand All @@ -310,7 +315,7 @@
if (node == container) {
return true;
}
node = DomApi.factory(node).parentNode;
node = dom(node).parentNode;
}
},

Expand All @@ -322,73 +327,10 @@
}
},

_addLogicalInfo: function(node, container, ref_node) {
container.__childNodes = null;
// handle document fragments
if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
// NOTE: the act of setting this info can affect patched nodes
// getters; therefore capture childNodes before patching.
var c$ = TreeApi.arrayCopyChildNodes(node);
for (var i=0, n; (i<c$.length) && (n=c$[i]); i++) {
this._linkNode(n, container, ref_node);
}
} else {
this._linkNode(node, container, ref_node);
}
},

_linkNode: function(node, container, ref_node) {
// update node <-> ref_node
node.__previousSibling = ref_node ? ref_node.__previousSibling :
container.__lastChild;
if (node.__previousSibling) {
node.__previousSibling.__nextSibling = node;
}
node.__nextSibling = ref_node;
if (ref_node) {
ref_node.__previousSibling = node;
}
// update node <-> container
node.__parentNode = container;
if (ref_node && ref_node === container.__firstChild) {
container.__firstChild = node;
} else {
container.__lastChild = node;
if (!container.__firstChild) {
container.__firstChild = node;
}
}
// remove caching of childNodes
container.__childNodes = null;
},

// NOTE: in general, we expect contents of the lists here to be small-ish
// and therefore indexOf to be nbd. Other optimizations can be made
// for larger lists (linked list)
_removeLogicalInfo: function(node, container) {
if (node === container.__firstChild) {
container.__firstChild = node.__nextSibling;
}
if (node === container.__lastChild) {
container.__lastChild = node.__previousSibling;
}
var p = node.__previousSibling;
var n = node.__nextSibling;
if (p) {
p.__nextSibling = n;
}
if (n) {
n.__previousSibling = p;
}
node.__parentNode = node.__previousSibling = node.__nextSibling = null;
// remove caching of childNodes
container.__childNodes = null;
},

_removeOwnerShadyRoot: function(node) {
// optimization: only reset the tree if node is actually in a root
if (this._hasCachedOwnerRoot(node)) {
var c$ = DomApi.factory(node).childNodes;
var c$ = dom(node).childNodes;
for (var i=0, l=c$.length, n; (i<l) && (n=c$[i]); i++) {
this._removeOwnerShadyRoot(n);
}
Expand All @@ -400,9 +342,9 @@
// question is pending; this is expected to be exceedingly rare, but if
// the issue comes up, we can force a flush in this case.
_firstComposedNode: function(content) {
var n$ = DomApi.factory(content).getDistributedNodes();
var n$ = dom(content).getDistributedNodes();
for (var i=0, l=n$.length, n, p$; (i<l) && (n=n$[i]); i++) {
p$ = DomApi.factory(n).getDestinationInsertionPoints();
p$ = dom(n).getDestinationInsertionPoints();
// means that we're composed to this spot.
if (p$[p$.length-1] === content) {
return n;
Expand All @@ -424,7 +366,7 @@
_query: function(matcher, node) {
node = node || this.node;
var list = [];
this._queryElements(DomApi.factory(node).childNodes, matcher, list);
this._queryElements(dom(node).childNodes, matcher, list);
return list;
},

Expand All @@ -440,7 +382,7 @@
if (matcher(node)) {
list.push(node);
}
this._queryElements(DomApi.factory(node).childNodes, matcher, list);
this._queryElements(dom(node).childNodes, matcher, list);
},

getDestinationInsertionPoints: function() {
Expand Down Expand Up @@ -477,9 +419,9 @@
var n = nativeCloneNode.call(this.node, false);
if (deep) {
var c$ = this.childNodes;
var d = DomApi.factory(n);
var d = dom(n);
for (var i=0, nc; i < c$.length; i++) {
nc = DomApi.factory(c$[i]).cloneNode(true);
nc = dom(c$[i]).cloneNode(true);
d.appendChild(nc);
}
}
Expand All @@ -492,10 +434,10 @@
this.node.ownerDocument;
var n = nativeImportNode.call(doc, externalNode, false);
if (deep) {
var c$ = DomApi.factory(externalNode).childNodes;
var d = DomApi.factory(n);
var c$ = dom(externalNode).childNodes;
var d = dom(n);
for (var i=0, nc; i < c$.length; i++) {
nc = DomApi.factory(doc).importNode(c$[i], true);
nc = dom(doc).importNode(c$[i], true);
d.appendChild(nc);
}
}
Expand Down
64 changes: 62 additions & 2 deletions src/lib/dom-tree-api.html
Expand Up @@ -22,7 +22,7 @@
* TreeApi is a dom manipulation library used by Shady/Polymer.dom to
* manipulate composed and logical trees.
*/
Polymer.TreeApi = {
var TreeApi = Polymer.TreeApi = {

// sad but faster than slice...
arrayCopyChildNodes: function(parent) {
Expand Down Expand Up @@ -93,7 +93,67 @@
n.__previousSibling = n.previousSibling;
}
}
}
},

recordInsertBefore: function(node, container, ref_node) {
container.__childNodes = null;
// handle document fragments
if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
// NOTE: the act of setting this info can affect patched nodes
// getters; therefore capture childNodes before patching.
var c$ = TreeApi.arrayCopyChildNodes(node);
for (var i=0, n; (i<c$.length) && (n=c$[i]); i++) {
this._linkNode(n, container, ref_node);
}
} else {
this._linkNode(node, container, ref_node);
}
},

_linkNode: function(node, container, ref_node) {
// update node <-> ref_node
node.__previousSibling = ref_node ? ref_node.__previousSibling :
container.__lastChild;
if (node.__previousSibling) {
node.__previousSibling.__nextSibling = node;
}
node.__nextSibling = ref_node;
if (ref_node) {
ref_node.__previousSibling = node;
}
// update node <-> container
node.__parentNode = container;
if (ref_node && ref_node === container.__firstChild) {
container.__firstChild = node;
} else {
container.__lastChild = node;
if (!container.__firstChild) {
container.__firstChild = node;
}
}
// remove caching of childNodes
container.__childNodes = null;
},

recordRemoveChild: function(node, container) {
if (node === container.__firstChild) {
container.__firstChild = node.__nextSibling;
}
if (node === container.__lastChild) {
container.__lastChild = node.__previousSibling;
}
var p = node.__previousSibling;
var n = node.__nextSibling;
if (p) {
p.__nextSibling = n;
}
if (n) {
n.__previousSibling = p;
}
node.__parentNode = node.__previousSibling = node.__nextSibling = null;
// remove caching of childNodes
container.__childNodes = null;
},

}

Expand Down
3 changes: 3 additions & 0 deletions src/mini/ready.html
Expand Up @@ -69,6 +69,8 @@
if (host && host._clients) {
host._clients.push(this);
}
this._clients = null;
this._clientsReadied = false;
},

// establish this element as the current hosting element (allows
Expand All @@ -86,6 +88,7 @@
},

_tryReady: function() {
this._readied = false;
if (this._canReady()) {
this._ready();
}
Expand Down
18 changes: 17 additions & 1 deletion src/mini/shady.html
Expand Up @@ -34,6 +34,22 @@
this._useContent = this._useContent || Boolean(this._template);
},

_setupShady: function() {
// object shaping...
this.shadyRoot = null;
if (!this.__domApi) {
this.__domApi = null;
}
if (!this._ownerShadyRoot) {
this._ownerShadyRoot = undefined;
}
// TODO(sorvell): these could be on the domApi object?
// there are a bunch of `__` properties that get put on the node
// from logicalizing. These could be on `__domApi`. Initializing
// them here is problematic because they may have been set prior
// to upgrading.
},

// called as part of content initialization, prior to template stamping
_poolContent: function() {
if (this._useContent) {
Expand Down Expand Up @@ -277,7 +293,7 @@
this._updateChildNodes(this, this._composeNode(this));
var p$ = this.shadyRoot._insertionPoints;
for (var i=0, l=p$.length, p, parent; (i<l) && (p=p$[i]); i++) {
parent = p.__parentNode || p.parentNode;
parent = Polymer.dom(p).parentNode;
if (!parent._useContent && (parent !== this) &&
(parent !== this.shadyRoot)) {
this._updateChildNodes(parent, this._composeNode(parent));
Expand Down
1 change: 1 addition & 0 deletions src/standard/configure.html
Expand Up @@ -47,6 +47,7 @@
_setupConfigure: function(initialConfig) {
this._config = {};
this._handlers = [];
this._aboveConfig = null;
if (initialConfig) {
// don't accept undefined values in intialConfig
for (var i in initialConfig) {
Expand Down

0 comments on commit da15ff0

Please sign in to comment.