Skip to content

Commit da15ff0

Browse files
author
Steven Orvell
committed
Minor factoring; ensure base properties set on instance.
1 parent 306cc81 commit da15ff0

9 files changed

+124
-87
lines changed

polymer.html

+4
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@
5757
},
5858

5959
_initFeatures: function() {
60+
// setup gestures
61+
this._setupGestures();
6062
// manage configuration
6163
this._setupConfigure();
6264
// setup style properties
6365
this._setupStyleProperties();
6466
// setup debouncers
6567
this._setupDebouncers();
68+
// setup shady
69+
this._setupShady();
6670
this._registerHost();
6771
if (this._template) {
6872
// manage local dom

src/lib/base.html

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
createdCallback: function() {
3535
Polymer.telemetry.instanceCount++;
36+
this.isAttached = false;
3637
this.root = this;
3738
this._doBehavior('created'); // abstract
3839
this._initFeatures(); // abstract

src/lib/dom-api-shady.html

+26-84
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
var Settings = Polymer.Settings;
1717
var DomApi = Polymer.DomApi;
18+
var dom = DomApi.factory;
1819
var TreeApi = Polymer.TreeApi;
1920
var getInnerHTML = Polymer.domInnerHTML.getInnerHTML;
2021
var CONTENT = DomApi.CONTENT;
@@ -69,7 +70,7 @@
6970
throw Error('The ref_node to be inserted before is not a child ' +
7071
'of this node');
7172
}
72-
this._addLogicalInfo(node, this.node, ref_node);
73+
TreeApi.Logical.recordInsertBefore(node, this.node, ref_node);
7374
}
7475
this._addNodeToHost(node);
7576
// if not distributing and not adding to host, do a fast path addition
@@ -101,7 +102,7 @@
101102
This method also performs dom composition.
102103
*/
103104
removeChild: function(node) {
104-
if (DomApi.factory(node).parentNode !== this.node) {
105+
if (dom(node).parentNode !== this.node) {
105106
console.warn('The node to be removed is not a child of this node',
106107
node);
107108
}
@@ -201,9 +202,9 @@
201202
var added;
202203
if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE &&
203204
!node.__noContent) {
204-
var c$ = DomApi.factory(node).querySelectorAll(CONTENT);
205+
var c$ = dom(node).querySelectorAll(CONTENT);
205206
for (var i=0, n, np, na; (i<c$.length) && (n=c$[i]); i++) {
206-
np = DomApi.factory(n).parentNode;
207+
np = dom(n).parentNode;
207208
// don't allow node's parent to be fragment itself
208209
if (np === node) {
209210
np = parent;
@@ -231,12 +232,12 @@
231232

232233
_updateInsertionPoints: function(host) {
233234
var i$ = host.shadyRoot._insertionPoints =
234-
DomApi.factory(host.shadyRoot).querySelectorAll(CONTENT);
235+
dom(host.shadyRoot).querySelectorAll(CONTENT);
235236
// ensure <content>'s and their parents have logical dom info.
236237
for (var i=0, c; i < i$.length; i++) {
237238
c = i$[i];
238239
TreeApi.Logical.saveChildNodes(c);
239-
TreeApi.Logical.saveChildNodes(DomApi.factory(c).parentNode);
240+
TreeApi.Logical.saveChildNodes(dom(c).parentNode);
240241
}
241242
},
242243

@@ -248,9 +249,12 @@
248249
_removeNodeFromParent: function(node) {
249250
// note: we may need to notify and not have logical info so fallback
250251
// to composed parentNode.
252+
251253
var parent = node.__parentNode || node.parentNode;
254+
//var parent = dom(node).parentNode;
255+
// TODO(sorvell): hasDomApi doesn't make sense now
252256
if (parent && DomApi.hasApi(parent)) {
253-
DomApi.factory(parent).notifyObserver();
257+
dom(parent).notifyObserver();
254258
}
255259
this._removeNodeFromHost(node, true);
256260
},
@@ -262,17 +266,18 @@
262266
// to require distribution... both cases are handled here.
263267
var hostNeedsDist;
264268
var root;
269+
// important that we want to do this only if the node has a logical parent
265270
var parent = node.__parentNode;
266271
if (parent) {
267272
// distribute node's parent iff needed
268-
DomApi.factory(node)._distributeParent();
273+
dom(node)._distributeParent();
269274
root = this._ownerShadyRootForNode(node);
270275
// remove node from root and distribute it iff needed
271276
if (root) {
272277
root.host._elementRemove(node);
273278
hostNeedsDist = this._removeDistributedChildren(root, node);
274279
}
275-
this._removeLogicalInfo(node, parent);
280+
TreeApi.Logical.recordRemoveChild(node, parent);
276281
}
277282
this._removeOwnerShadyRoot(node);
278283
if (root && hostNeedsDist) {
@@ -290,7 +295,7 @@
290295
for (var i=0; i<ip$.length; i++) {
291296
var content = ip$[i];
292297
if (this._contains(container, content)) {
293-
var dc$ = DomApi.factory(content).getDistributedNodes();
298+
var dc$ = dom(content).getDistributedNodes();
294299
for (var j=0; j<dc$.length; j++) {
295300
hostNeedsDist = true;
296301
var node = dc$[j];
@@ -310,7 +315,7 @@
310315
if (node == container) {
311316
return true;
312317
}
313-
node = DomApi.factory(node).parentNode;
318+
node = dom(node).parentNode;
314319
}
315320
},
316321

@@ -322,73 +327,10 @@
322327
}
323328
},
324329

325-
_addLogicalInfo: function(node, container, ref_node) {
326-
container.__childNodes = null;
327-
// handle document fragments
328-
if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
329-
// NOTE: the act of setting this info can affect patched nodes
330-
// getters; therefore capture childNodes before patching.
331-
var c$ = TreeApi.arrayCopyChildNodes(node);
332-
for (var i=0, n; (i<c$.length) && (n=c$[i]); i++) {
333-
this._linkNode(n, container, ref_node);
334-
}
335-
} else {
336-
this._linkNode(node, container, ref_node);
337-
}
338-
},
339-
340-
_linkNode: function(node, container, ref_node) {
341-
// update node <-> ref_node
342-
node.__previousSibling = ref_node ? ref_node.__previousSibling :
343-
container.__lastChild;
344-
if (node.__previousSibling) {
345-
node.__previousSibling.__nextSibling = node;
346-
}
347-
node.__nextSibling = ref_node;
348-
if (ref_node) {
349-
ref_node.__previousSibling = node;
350-
}
351-
// update node <-> container
352-
node.__parentNode = container;
353-
if (ref_node && ref_node === container.__firstChild) {
354-
container.__firstChild = node;
355-
} else {
356-
container.__lastChild = node;
357-
if (!container.__firstChild) {
358-
container.__firstChild = node;
359-
}
360-
}
361-
// remove caching of childNodes
362-
container.__childNodes = null;
363-
},
364-
365-
// NOTE: in general, we expect contents of the lists here to be small-ish
366-
// and therefore indexOf to be nbd. Other optimizations can be made
367-
// for larger lists (linked list)
368-
_removeLogicalInfo: function(node, container) {
369-
if (node === container.__firstChild) {
370-
container.__firstChild = node.__nextSibling;
371-
}
372-
if (node === container.__lastChild) {
373-
container.__lastChild = node.__previousSibling;
374-
}
375-
var p = node.__previousSibling;
376-
var n = node.__nextSibling;
377-
if (p) {
378-
p.__nextSibling = n;
379-
}
380-
if (n) {
381-
n.__previousSibling = p;
382-
}
383-
node.__parentNode = node.__previousSibling = node.__nextSibling = null;
384-
// remove caching of childNodes
385-
container.__childNodes = null;
386-
},
387-
388330
_removeOwnerShadyRoot: function(node) {
389331
// optimization: only reset the tree if node is actually in a root
390332
if (this._hasCachedOwnerRoot(node)) {
391-
var c$ = DomApi.factory(node).childNodes;
333+
var c$ = dom(node).childNodes;
392334
for (var i=0, l=c$.length, n; (i<l) && (n=c$[i]); i++) {
393335
this._removeOwnerShadyRoot(n);
394336
}
@@ -400,9 +342,9 @@
400342
// question is pending; this is expected to be exceedingly rare, but if
401343
// the issue comes up, we can force a flush in this case.
402344
_firstComposedNode: function(content) {
403-
var n$ = DomApi.factory(content).getDistributedNodes();
345+
var n$ = dom(content).getDistributedNodes();
404346
for (var i=0, l=n$.length, n, p$; (i<l) && (n=n$[i]); i++) {
405-
p$ = DomApi.factory(n).getDestinationInsertionPoints();
347+
p$ = dom(n).getDestinationInsertionPoints();
406348
// means that we're composed to this spot.
407349
if (p$[p$.length-1] === content) {
408350
return n;
@@ -424,7 +366,7 @@
424366
_query: function(matcher, node) {
425367
node = node || this.node;
426368
var list = [];
427-
this._queryElements(DomApi.factory(node).childNodes, matcher, list);
369+
this._queryElements(dom(node).childNodes, matcher, list);
428370
return list;
429371
},
430372

@@ -440,7 +382,7 @@
440382
if (matcher(node)) {
441383
list.push(node);
442384
}
443-
this._queryElements(DomApi.factory(node).childNodes, matcher, list);
385+
this._queryElements(dom(node).childNodes, matcher, list);
444386
},
445387

446388
getDestinationInsertionPoints: function() {
@@ -477,9 +419,9 @@
477419
var n = nativeCloneNode.call(this.node, false);
478420
if (deep) {
479421
var c$ = this.childNodes;
480-
var d = DomApi.factory(n);
422+
var d = dom(n);
481423
for (var i=0, nc; i < c$.length; i++) {
482-
nc = DomApi.factory(c$[i]).cloneNode(true);
424+
nc = dom(c$[i]).cloneNode(true);
483425
d.appendChild(nc);
484426
}
485427
}
@@ -492,10 +434,10 @@
492434
this.node.ownerDocument;
493435
var n = nativeImportNode.call(doc, externalNode, false);
494436
if (deep) {
495-
var c$ = DomApi.factory(externalNode).childNodes;
496-
var d = DomApi.factory(n);
437+
var c$ = dom(externalNode).childNodes;
438+
var d = dom(n);
497439
for (var i=0, nc; i < c$.length; i++) {
498-
nc = DomApi.factory(doc).importNode(c$[i], true);
440+
nc = dom(doc).importNode(c$[i], true);
499441
d.appendChild(nc);
500442
}
501443
}

src/lib/dom-tree-api.html

+62-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* TreeApi is a dom manipulation library used by Shady/Polymer.dom to
2323
* manipulate composed and logical trees.
2424
*/
25-
Polymer.TreeApi = {
25+
var TreeApi = Polymer.TreeApi = {
2626

2727
// sad but faster than slice...
2828
arrayCopyChildNodes: function(parent) {
@@ -93,7 +93,67 @@
9393
n.__previousSibling = n.previousSibling;
9494
}
9595
}
96-
}
96+
},
97+
98+
recordInsertBefore: function(node, container, ref_node) {
99+
container.__childNodes = null;
100+
// handle document fragments
101+
if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
102+
// NOTE: the act of setting this info can affect patched nodes
103+
// getters; therefore capture childNodes before patching.
104+
var c$ = TreeApi.arrayCopyChildNodes(node);
105+
for (var i=0, n; (i<c$.length) && (n=c$[i]); i++) {
106+
this._linkNode(n, container, ref_node);
107+
}
108+
} else {
109+
this._linkNode(node, container, ref_node);
110+
}
111+
},
112+
113+
_linkNode: function(node, container, ref_node) {
114+
// update node <-> ref_node
115+
node.__previousSibling = ref_node ? ref_node.__previousSibling :
116+
container.__lastChild;
117+
if (node.__previousSibling) {
118+
node.__previousSibling.__nextSibling = node;
119+
}
120+
node.__nextSibling = ref_node;
121+
if (ref_node) {
122+
ref_node.__previousSibling = node;
123+
}
124+
// update node <-> container
125+
node.__parentNode = container;
126+
if (ref_node && ref_node === container.__firstChild) {
127+
container.__firstChild = node;
128+
} else {
129+
container.__lastChild = node;
130+
if (!container.__firstChild) {
131+
container.__firstChild = node;
132+
}
133+
}
134+
// remove caching of childNodes
135+
container.__childNodes = null;
136+
},
137+
138+
recordRemoveChild: function(node, container) {
139+
if (node === container.__firstChild) {
140+
container.__firstChild = node.__nextSibling;
141+
}
142+
if (node === container.__lastChild) {
143+
container.__lastChild = node.__previousSibling;
144+
}
145+
var p = node.__previousSibling;
146+
var n = node.__nextSibling;
147+
if (p) {
148+
p.__nextSibling = n;
149+
}
150+
if (n) {
151+
n.__previousSibling = p;
152+
}
153+
node.__parentNode = node.__previousSibling = node.__nextSibling = null;
154+
// remove caching of childNodes
155+
container.__childNodes = null;
156+
},
97157

98158
}
99159

src/mini/ready.html

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
if (host && host._clients) {
7070
host._clients.push(this);
7171
}
72+
this._clients = null;
73+
this._clientsReadied = false;
7274
},
7375

7476
// establish this element as the current hosting element (allows
@@ -86,6 +88,7 @@
8688
},
8789

8890
_tryReady: function() {
91+
this._readied = false;
8992
if (this._canReady()) {
9093
this._ready();
9194
}

src/mini/shady.html

+17-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@
3434
this._useContent = this._useContent || Boolean(this._template);
3535
},
3636

37+
_setupShady: function() {
38+
// object shaping...
39+
this.shadyRoot = null;
40+
if (!this.__domApi) {
41+
this.__domApi = null;
42+
}
43+
if (!this._ownerShadyRoot) {
44+
this._ownerShadyRoot = undefined;
45+
}
46+
// TODO(sorvell): these could be on the domApi object?
47+
// there are a bunch of `__` properties that get put on the node
48+
// from logicalizing. These could be on `__domApi`. Initializing
49+
// them here is problematic because they may have been set prior
50+
// to upgrading.
51+
},
52+
3753
// called as part of content initialization, prior to template stamping
3854
_poolContent: function() {
3955
if (this._useContent) {
@@ -277,7 +293,7 @@
277293
this._updateChildNodes(this, this._composeNode(this));
278294
var p$ = this.shadyRoot._insertionPoints;
279295
for (var i=0, l=p$.length, p, parent; (i<l) && (p=p$[i]); i++) {
280-
parent = p.__parentNode || p.parentNode;
296+
parent = Polymer.dom(p).parentNode;
281297
if (!parent._useContent && (parent !== this) &&
282298
(parent !== this.shadyRoot)) {
283299
this._updateChildNodes(parent, this._composeNode(parent));

src/standard/configure.html

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
_setupConfigure: function(initialConfig) {
4848
this._config = {};
4949
this._handlers = [];
50+
this._aboveConfig = null;
5051
if (initialConfig) {
5152
// don't accept undefined values in intialConfig
5253
for (var i in initialConfig) {

0 commit comments

Comments
 (0)