-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Effective children #2548
Merged
Merged
Effective children #2548
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
f34fb45
Adds `getEffectiveChildNodes`, `getEffectiveChildren`, `getEffectiveT…
6499e83
Adds `Polymer.dom(element).observeChildren(callback)` api
1ca065f
Merge branch 'master' into effective-children
effedcb
Factor dom-api's into separate helpers.
393ba40
Merge branch 'master' into effective-children
b11f86b
Add mutation tracking for distributedNodes.
07261e4
Add `Polymer.dom().notifyObservers` method to 'kick' observers, for e…
65abbd0
Merge branch 'master' into effective-children
19aa6eb
Merge branch 'master' into effective-children
1774f57
Merge branch 'master' into effective-children
8242a98
Add optional attribute tracking to support better distributed node no…
4099342
Merge branch 'master' into effective-children
ff2c088
Merge branch 'master' into effective-children
c09296e
Merge branch 'master' into effective-children
bd90b57
add `observeNodes` tests.
9ff2ee4
make tests work on polyfill.
b40060a
Merge branch 'master' into effective-children
d021039
Merge branch 'master' into effective-children
669acaa
Simplify change tracking by always dirty checking at the observer lev…
e11a4f3
Merge branch 'master' into effective-children
54911a7
Make shadow attribute tracking automatic based on detecting a <conten…
0ede79a
Add docs
8b1face
Add <content>.getDistributedNodes observation. Refactor flush.
344f5cc
ensure distribution observers see all changes that can come from attr…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<!-- | ||
@license | ||
Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
--> | ||
<script> | ||
|
||
(function() { | ||
'use strict'; | ||
|
||
var DomApi = Polymer.DomApi.ctor; | ||
|
||
/** | ||
* DomApi.classList allows maniuplation of `classList` compatible with | ||
* Polymer.dom. The general usage is | ||
* `Polymer.dom(node).classList.method(arguments)` where methods and arguments | ||
* match native DOM. | ||
*/ | ||
Object.defineProperty(DomApi.prototype, 'classList', { | ||
get: function() { | ||
if (!this._classList) { | ||
this._classList = new DomApi.ClassList(this); | ||
} | ||
return this._classList; | ||
}, | ||
configurable: true | ||
}); | ||
|
||
DomApi.ClassList = function(host) { | ||
this.domApi = host; | ||
this.node = host.node; | ||
} | ||
|
||
DomApi.ClassList.prototype = { | ||
add: function() { | ||
this.node.classList.add.apply(this.node.classList, arguments); | ||
this.domApi._distributeParent(); | ||
}, | ||
|
||
remove: function() { | ||
this.node.classList.remove.apply(this.node.classList, arguments); | ||
this.domApi._distributeParent(); | ||
}, | ||
|
||
toggle: function() { | ||
this.node.classList.toggle.apply(this.node.classList, arguments); | ||
this.domApi._distributeParent(); | ||
}, | ||
contains: function() { | ||
return this.node.classList.contains.apply(this.node.classList, | ||
arguments); | ||
} | ||
} | ||
|
||
})(); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<!-- | ||
@license | ||
Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
--> | ||
<link rel="import" href="settings.html"> | ||
<script> | ||
Polymer.EventApi = (function() { | ||
'use strict'; | ||
|
||
var DomApi = Polymer.DomApi.ctor; | ||
var Settings = Polymer.Settings; | ||
|
||
|
||
/** | ||
* DomApi.Event allows maniuplation of events compatible with | ||
* the scoping concepts in Shadow DOM and compatible with both Shady DOM | ||
* and Shadow DOM. The general usage is | ||
* `Polymer.dom(event).property`. The `path` property returns `event.path` | ||
* matching Shadow DOM. The `rootTarget` property returns the first node | ||
* in the `path` and is the original event target. The `localTarget` property | ||
* matches event.target under Shadow DOM and is the scoped event target. | ||
*/ | ||
DomApi.Event = function(event) { | ||
this.event = event; | ||
}; | ||
|
||
if (Settings.useShadow) { | ||
|
||
DomApi.Event.prototype = { | ||
|
||
get rootTarget() { | ||
return this.event.path[0]; | ||
}, | ||
|
||
get localTarget() { | ||
return this.event.target; | ||
}, | ||
|
||
get path() { | ||
return this.event.path; | ||
} | ||
|
||
}; | ||
|
||
} else { | ||
|
||
DomApi.Event.prototype = { | ||
|
||
get rootTarget() { | ||
return this.event.target; | ||
}, | ||
|
||
get localTarget() { | ||
var current = this.event.currentTarget; | ||
var currentRoot = current && Polymer.dom(current).getOwnerRoot(); | ||
var p$ = this.path; | ||
for (var i=0; i < p$.length; i++) { | ||
if (Polymer.dom(p$[i]).getOwnerRoot() === currentRoot) { | ||
return p$[i]; | ||
} | ||
} | ||
}, | ||
|
||
// TODO(sorvell): simulate event.path. This probably incorrect for | ||
// non-bubbling events. | ||
get path() { | ||
if (!this.event._path) { | ||
var path = []; | ||
var o = this.rootTarget; | ||
while (o) { | ||
path.push(o); | ||
o = Polymer.dom(o).parentNode || o.host; | ||
} | ||
// event path includes window in most recent native implementations | ||
path.push(window); | ||
this.event._path = path; | ||
} | ||
return this.event._path; | ||
} | ||
|
||
}; | ||
|
||
} | ||
|
||
var factory = function(event) { | ||
if (!event.__eventApi) { | ||
event.__eventApi = new DomApi.Event(event); | ||
} | ||
return event.__eventApi; | ||
}; | ||
|
||
return { | ||
factory: factory | ||
}; | ||
|
||
})(); | ||
|
||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<!-- | ||
@license | ||
Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
--> | ||
<script> | ||
|
||
/** | ||
* `Polymer.dom.flush()` causes any asynchronously queued actions to be | ||
* flushed synchronously. It should be used sparingly as calling it frequently | ||
* can negatively impact performance since work is often deferred for | ||
* efficiency. Calling `Polymer.dom.flush()` is useful, for example, when | ||
* an element has to measure itself and is unsure about the state of its | ||
* internal or compoased DOM. | ||
*/ | ||
Polymer.Base.extend(Polymer.dom, { | ||
|
||
_flushGuard: 0, | ||
_FLUSH_MAX: 100, | ||
_needsTakeRecords: !Polymer.Settings.useNativeCustomElements, | ||
_debouncers: [], | ||
_staticFlushList: [], | ||
_finishDebouncer: null, | ||
|
||
// flush and debounce exposed as statics on Polymer.dom | ||
flush: function() { | ||
this._flushGuard = 0; | ||
this._prepareFlush(); | ||
while (this._debouncers.length && this._flushGuard < this._FLUSH_MAX) { | ||
for (var i=0; i < this._debouncers.length; i++) { | ||
this._debouncers[i].complete(); | ||
} | ||
// clear the list of debouncers | ||
if (this._finishDebouncer) { | ||
this._finishDebouncer.complete(); | ||
} | ||
this._prepareFlush(); | ||
this._flushGuard++; | ||
} | ||
if (this._flushGuard >= this._FLUSH_MAX) { | ||
console.warn('Polymer.dom.flush aborted. Flush may not be complete.') | ||
} | ||
}, | ||
|
||
_prepareFlush: function() { | ||
// TODO(sorvell): There is currently not a good way | ||
// to process all custom elements mutations under SD polyfill because | ||
// these mutations may be inside shadowRoots. | ||
// again make any pending CE mutations that might trigger debouncer | ||
// additions go... | ||
if (this._needsTakeRecords) { | ||
CustomElements.takeRecords(); | ||
} | ||
for (var i=0; i < this._staticFlushList.length; i++) { | ||
this._staticFlushList[i](); | ||
} | ||
}, | ||
|
||
// add to the static list of methods to call when flushing | ||
addStaticFlush: function(fn) { | ||
this._staticFlushList.push(fn); | ||
}, | ||
|
||
// remove a function from the static list of methods to call when flushing | ||
removeStaticFlush: function(fn) { | ||
var i = this._staticFlushList.indexOf(fn); | ||
if (i >= 0) { | ||
this._staticFlushList.splice(i, 1); | ||
} | ||
}, | ||
|
||
addDebouncer: function(debouncer) { | ||
this._debouncers.push(debouncer); | ||
// ensure the list of active debouncers is cleared when done. | ||
this._finishDebouncer = Polymer.Debounce(this._finishDebouncer, | ||
this._finishFlush); | ||
}, | ||
|
||
_finishFlush: function() { | ||
Polymer.dom._debouncers = []; | ||
} | ||
|
||
}); | ||
|
||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<!-- | ||
@license | ||
Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
--> | ||
<link rel="import" href="settings.html"> | ||
<script> | ||
(function() { | ||
'use strict'; | ||
|
||
var DomApi = Polymer.DomApi.ctor; | ||
var Settings = Polymer.Settings; | ||
|
||
/** | ||
* DomApi.ObserveDistributedNodes notifies when the list returned by | ||
* a <content> element's `getDistributedNodes()` may have changed. | ||
* It is not meant to be used directly; it is used by | ||
* `Polymer.dom(node).observeNodes(callback)` to observe changes to | ||
* `<content>.getDistributedNodes()`. | ||
*/ | ||
DomApi.ObserveDistributedNodes = function(domApi) { | ||
DomApi.ObserveNodes.call(this, domApi); | ||
}; | ||
|
||
DomApi.ObserveDistributedNodes.prototype = | ||
Object.create(DomApi.ObserveNodes.prototype); | ||
|
||
Polymer.Base.extend(DomApi.ObserveDistributedNodes.prototype, { | ||
|
||
// NOTE: ShadyDOM distribute provokes notification of these observers | ||
// so no setup is required. | ||
_setup: function() {}, | ||
|
||
_cleanup: function() {}, | ||
|
||
// no need to update sub-elements since <content> does not nest | ||
// (but note that <slot> will) | ||
_beforeCallListeners: function() {}, | ||
|
||
_getEffectiveNodes: function() { | ||
return this.domApi.getDistributedNodes(); | ||
} | ||
|
||
}); | ||
|
||
if (Settings.useShadow) { | ||
|
||
Polymer.Base.extend(DomApi.ObserveDistributedNodes.prototype, { | ||
|
||
// NOTE: Under ShadowDOM we must observe the host element for | ||
// changes. | ||
_setup: function() { | ||
if (!this._observer) { | ||
var root = this.domApi.getOwnerRoot(); | ||
var host = root && root.host; | ||
if (host) { | ||
this._observer = Polymer.dom(host).observeNodes( | ||
this._scheduleNotify.bind(this)); | ||
if (this._hasAttrSelect()) { | ||
Polymer.dom(host).observer.enableShadowAttributeTracking(); | ||
this._observer._alwaysCallListener = true; | ||
} | ||
} | ||
} | ||
}, | ||
|
||
_hasAttrSelect: function() { | ||
var select = this.node.getAttribute('select'); | ||
return select && select.match(/[[.]+/); | ||
}, | ||
|
||
_cleanup: function() { | ||
var root = this.domApi.getOwnerRoot(); | ||
var host = root && root.host; | ||
if (host) { | ||
Polymer.dom(host).unobserveNodes(this._observer); | ||
} | ||
this._observer = null; | ||
} | ||
|
||
}); | ||
|
||
} | ||
|
||
})(); | ||
|
||
</script> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_alwaysCallListener
needs to get passed up viaenableShadowTracking
?