Skip to content

Commit

Permalink
Adds event normalization via Polymer.dom(event), api includes:
Browse files Browse the repository at this point in the history
* rootTarget: the original or root target before shadow retargeting (equivalent of event.path[0] or event.target under Shady DOM)
* localTarget: retargeted event target (equivalent of event.target under Shadow DOM)
* path: array of nodes through which event will pass (equivalent of event.path under Shadow DOM)
  • Loading branch information
Steven Orvell committed Mar 16, 2015
1 parent 269cdb3 commit 739de80
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 10 deletions.
12 changes: 10 additions & 2 deletions src/lib/dom-api.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="settings.html">
<link rel="import" href="event-api.html">
<script>

Polymer.DomApi = (function() {
Expand Down Expand Up @@ -355,7 +356,13 @@
return node.__domApi;
}

Polymer.dom = factory;
Polymer.dom = function(obj, patch) {
if (obj instanceof Event) {
return Polymer.EventApi.factory(obj);
} else {
return factory(obj, patch);
}
};

// make flush available directly.
Polymer.dom.flush = DomApi.prototype.flush;
Expand Down Expand Up @@ -391,7 +398,8 @@
return {
getLightChildren: getLightChildren,
saveLightChildrenIfNeeded: saveLightChildrenIfNeeded,
matchesSelector: matchesSelector
matchesSelector: matchesSelector,
factory: factory
};

})();
Expand Down
89 changes: 89 additions & 0 deletions src/lib/event-api.html
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
-->
<link rel="import" href="settings.html">
<script>

Polymer.EventApi = (function() {

var Settings = Polymer.Settings;

var EventApi = function(event) {
this.event = event;
};

if (Settings.useShadow) {

EventApi.prototype = {

get rootTarget() {
return this.event.path[0];
},

get localTarget() {
return this.event.target;
},

get path() {
return this.event.path;
}

};

} else {

EventApi.prototype = {

get rootTarget() {
return this.event.target;
},

get localTarget() {
var current = this.event.currentTarget;
var currentRoot = current && current._ownerShadyRoot;
var p$ = this.path;
for (var i=0; i < p$.length; i++) {
if (p$[i]._ownerShadyRoot === currentRoot) {
return p$[i];
}
}
},

// TODO(sorvell): should include <content> elements
get path() {
if (!this.event._path) {
var path = [];
var o = this.rootTarget;
while (o) {
path.push(o);
o = Polymer.dom(o).parentNode || o.host;
}
this.event._path = path;
}
return this.event._path;
}

};

}

var factory = function(event) {
if (!event.__eventApi) {
event.__eventApi = new EventApi(event);
}
return event.__eventApi;
}

return {
factory: factory
};

})();

</script>
4 changes: 2 additions & 2 deletions test/runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
'unit/ready-shadow.html',
'unit/configure.html',
'unit/shady.html',
'unit/projection.html',
'unit/projection-shadow.html',
'unit/polymer-dom.html',
'unit/polymer-dom-shadow.html',
'unit/bind.html',
'unit/notify-path.html',
'unit/utils.html',
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
<script src="../../../web-component-tester/browser.js"></script>
<script>Polymer = {dom: 'shadow'};</script>
<link rel="import" href="../../polymer.html">
<link rel="import" href="projection-elements.html">
<link rel="import" href="polymer-dom-elements.html">
</head>
<body>

<x-test></x-test>

<script src="projection.js"></script>
<script src="polymer-dom.js"></script>

</body>
</html>
4 changes: 2 additions & 2 deletions test/unit/projection.html → test/unit/polymer-dom.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
<script src="../../../web-component-tester/browser.js"></script>
<link rel="import" href="../../polymer.html">
<link rel="import" href="projection-elements.html">
<link rel="import" href="polymer-dom-elements.html">
</head>
<body>

<x-test></x-test>

<script src="projection.js"></script>
<script src="polymer-dom.js"></script>

</body>
</html>
31 changes: 29 additions & 2 deletions test/unit/projection.js → test/unit/polymer-dom.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
suite('projection', function() {
suite('Polymer.dom', function() {

test('querySelector (local)', function() {
var test = document.querySelector('x-test');
Expand Down Expand Up @@ -235,7 +235,7 @@ suite('projection', function() {
}
});

test('elementParent', function() {
test('parentNode', function() {
var test = document.querySelector('x-test');
var rere = Polymer.dom(test.root).querySelector('x-rereproject');
var projected = Polymer.dom(test.root).querySelector('#projected');
Expand Down Expand Up @@ -264,4 +264,31 @@ suite('projection', function() {
assert.notOk(projected);
});

test('Polymer.dom event', function() {
var test = document.querySelector('x-test');
var rere = Polymer.dom(test.root).querySelector('x-rereproject');
var re = Polymer.dom(rere.root).querySelector('x-reproject');
var p = Polymer.dom(re.root).querySelector('x-project');
var eventHandled = 0;
test.addEventListener('test-event', function(e) {
eventHandled++;
assert.equal(Polymer.dom(e).rootTarget, p);
assert.equal(Polymer.dom(e).localTarget, test);
var path = Polymer.dom(e).path;
assert.equal(path.length, 10);
assert.equal(path[0], p);
assert.equal(path[2], re);
assert.equal(path[4], rere);
assert.equal(path[6], test);
});

rere.addEventListener('test-event', function(e) {
eventHandled++;
assert.equal(Polymer.dom(e).localTarget, rere);
});

p.fire('test-event');
assert.equal(eventHandled, 2);
});

});

0 comments on commit 739de80

Please sign in to comment.