Skip to content

Commit effedcb

Browse files
author
Steven Orvell
committed
Factor dom-api's into separate helpers.
1 parent 1ca065f commit effedcb

File tree

8 files changed

+587
-260
lines changed

8 files changed

+587
-260
lines changed

src/lib/dom-api-classlist.html

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!--
2+
@license
3+
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
4+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7+
Code distributed by Google as part of the polymer project is also
8+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9+
-->
10+
<script>
11+
12+
(function() {
13+
'use strict';
14+
15+
var DomApi = Polymer.DomApi.ctor;
16+
17+
Object.defineProperty(DomApi.prototype, 'classList', {
18+
get: function() {
19+
if (!this._classList) {
20+
this._classList = new DomApi.ClassList(this);
21+
}
22+
return this._classList;
23+
},
24+
configurable: true
25+
});
26+
27+
DomApi.ClassList = function(host) {
28+
this.domApi = host;
29+
this.node = host.node;
30+
}
31+
32+
DomApi.ClassList.prototype = {
33+
add: function() {
34+
this.node.classList.add.apply(this.node.classList, arguments);
35+
this.domApi._distributeParent();
36+
},
37+
38+
remove: function() {
39+
this.node.classList.remove.apply(this.node.classList, arguments);
40+
this.domApi._distributeParent();
41+
},
42+
43+
toggle: function() {
44+
this.node.classList.toggle.apply(this.node.classList, arguments);
45+
this.domApi._distributeParent();
46+
},
47+
contains: function() {
48+
return this.node.classList.contains.apply(this.node.classList,
49+
arguments);
50+
}
51+
}
52+
53+
})();
54+
</script>

src/lib/dom-api-event.html

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<!--
2+
@license
3+
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
4+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7+
Code distributed by Google as part of the polymer project is also
8+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9+
-->
10+
<link rel="import" href="settings.html">
11+
<script>
12+
Polymer.EventApi = (function() {
13+
'use strict';
14+
15+
var DomApi = Polymer.DomApi.ctor;
16+
var Settings = Polymer.Settings;
17+
18+
DomApi.Event = function(event) {
19+
this.event = event;
20+
};
21+
22+
if (Settings.useShadow) {
23+
24+
DomApi.Event.prototype = {
25+
26+
get rootTarget() {
27+
return this.event.path[0];
28+
},
29+
30+
get localTarget() {
31+
return this.event.target;
32+
},
33+
34+
get path() {
35+
return this.event.path;
36+
}
37+
38+
};
39+
40+
} else {
41+
42+
DomApi.Event.prototype = {
43+
44+
get rootTarget() {
45+
return this.event.target;
46+
},
47+
48+
get localTarget() {
49+
var current = this.event.currentTarget;
50+
var currentRoot = current && Polymer.dom(current).getOwnerRoot();
51+
var p$ = this.path;
52+
for (var i=0; i < p$.length; i++) {
53+
if (Polymer.dom(p$[i]).getOwnerRoot() === currentRoot) {
54+
return p$[i];
55+
}
56+
}
57+
},
58+
59+
// TODO(sorvell): simulate event.path. This probably incorrect for
60+
// non-bubbling events.
61+
get path() {
62+
if (!this.event._path) {
63+
var path = [];
64+
var o = this.rootTarget;
65+
while (o) {
66+
path.push(o);
67+
o = Polymer.dom(o).parentNode || o.host;
68+
}
69+
// event path includes window in most recent native implementations
70+
path.push(window);
71+
this.event._path = path;
72+
}
73+
return this.event._path;
74+
}
75+
76+
};
77+
78+
}
79+
80+
var factory = function(event) {
81+
if (!event.__eventApi) {
82+
event.__eventApi = new DomApi.Event(event);
83+
}
84+
return event.__eventApi;
85+
};
86+
87+
return {
88+
factory: factory
89+
};
90+
91+
})();
92+
93+
</script>

src/lib/dom-api-flush.html

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!--
2+
@license
3+
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
4+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7+
Code distributed by Google as part of the polymer project is also
8+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9+
-->
10+
<script>
11+
12+
// add Polymer.dom flush api...
13+
Polymer.Base.extend(Polymer.dom, {
14+
15+
_flushGuard: 0,
16+
_FLUSH_MAX: 100,
17+
_needsTakeRecords: !Polymer.Settings.useNativeCustomElements,
18+
_debouncers: [],
19+
_finishDebouncer: null,
20+
21+
// flush and debounce exposed as statics on Polymer.dom
22+
flush: function() {
23+
// flush debouncers
24+
for (var i=0; i < this._debouncers.length; i++) {
25+
this._debouncers[i].complete();
26+
}
27+
// clear the list of debouncers
28+
if (this._finishDebouncer) {
29+
this._finishDebouncer.complete();
30+
}
31+
// again make any pending CE mutations that might trigger debouncer
32+
// additions go...
33+
this._flushPolyfills();
34+
// flush again if there are now any debouncers to process
35+
if (this._debouncers.length && this._flushGuard < this._FLUSH_MAX) {
36+
this._flushGuard++;
37+
this.flush();
38+
} else {
39+
if (this._flushGuard >= this._FLUSH_MAX) {
40+
console.warn('Polymer.dom.flush aborted. Flush may not be complete.')
41+
}
42+
this._flushGuard = 0;
43+
}
44+
},
45+
46+
// TODO(sorvell): There is currently not a good way
47+
// to process all custom elements mutations under SD polyfill because
48+
// these mutations may be inside shadowRoots.
49+
_flushPolyfills: function() {
50+
if (this._needsTakeRecords) {
51+
CustomElements.takeRecords();
52+
}
53+
},
54+
55+
addDebouncer: function(debouncer) {
56+
this._debouncers.push(debouncer);
57+
// ensure the list of active debouncers is cleared when done.
58+
this._finishDebouncer = Polymer.Debounce(this._finishDebouncer,
59+
this._finishFlush);
60+
},
61+
62+
_finishFlush: function() {
63+
Polymer.dom._debouncers = [];
64+
}
65+
66+
});
67+
68+
</script>

src/lib/dom-api-mutation-content.html

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<!--
2+
@license
3+
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
4+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7+
Code distributed by Google as part of the polymer project is also
8+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9+
-->
10+
<link rel="import" href="settings.html">
11+
<script>
12+
(function() {
13+
'use strict';
14+
15+
var DomApi = Polymer.DomApi.ctor;
16+
var Settings = Polymer.Settings;
17+
18+
DomApi.prototype.observeDistributedNodes = function(callback) {
19+
if (!this._observer) {
20+
this._observer = new DomApi.Mutation(this);
21+
}
22+
return this._observer.addObserver(callback);
23+
};
24+
25+
DomApi.prototype.unobserveDistributedNodes = function(handle) {
26+
if (this._observer) {
27+
this._observer.removeObserver(handle);
28+
}
29+
}
30+
31+
DomApi.MutationContent = function(domApi) {
32+
this.domApi = domApi;
33+
this.node = this.domApi.node;
34+
this._observers = [];
35+
this._addedNodes = [];
36+
this._removedNodes = [];
37+
};
38+
39+
DomApi.MutationContent.prototype = {
40+
41+
// addObserver: function(callback) {
42+
// return this._observers.push(callback);
43+
// },
44+
45+
// removeObserver: function(handle) {
46+
// this._observers.splice(handle - 1, 1);
47+
// },
48+
49+
// hasObservers: function() {
50+
// return Boolean(this._observers.length);
51+
// },
52+
53+
// _scheduleMutationNotify: function() {
54+
// this._mutationDebouncer = Polymer.Debounce(this._mutationDebouncer,
55+
// this._notifyObservers);
56+
// this._mutationDebouncer.context = this;
57+
// Polymer.dom.addDebouncer(this._mutationDebouncer);
58+
// },
59+
60+
// _notifyObservers: function(mxns) {
61+
// var info = {
62+
// target: this.node,
63+
// addedNodes: this._addedNodes,
64+
// removedNodes: this._removedNodes
65+
// }
66+
// var o$ = this._observers;
67+
// for (var i=0, o; (i < o$.length) && (o=o$[i]); i++) {
68+
// o.call(null, info);
69+
// }
70+
// this._addedNodes = [];
71+
// this._removedNodes = [];
72+
// }
73+
74+
// observeDistributedNodes: function(callback) {
75+
// if (this.node.localName !== 'content') {
76+
// console.warn('Must call `observeDistributedNodes` on a <content> element.');
77+
// return;
78+
// }
79+
// // setup <content>.getDistributedNodes observation
80+
// if (!this.hasObservers()) {
81+
// this._startObserveDistributedNodes();
82+
// }
83+
// this._addObserver(callback);
84+
// },
85+
86+
// unobserveDistributedNodes: function(handle) {
87+
// if (this.node.localName !== 'content') {
88+
// return;
89+
// }
90+
// this._removeObserver(handle);
91+
// if (!this.hasObservers()) {
92+
// this._stopObservingDistributedNodes();
93+
// }
94+
// },
95+
96+
// _startObservingDistributedNodes: function() {
97+
// this._distributedObservers = [];
98+
// var root = this.getOwnerRoot();
99+
// var host = root && root.host;
100+
// if (host) {
101+
// var h = Polymer.dom(host)
102+
// .observeChildren(this._scheduleDistributedNodesNotify);
103+
// this._distributedObservers.push(h);
104+
// }
105+
// },
106+
107+
// _stopObservingDistributedNodes: function() {
108+
109+
// },
110+
111+
// _scheduleDistributedNodesNotify: function() {
112+
// this._distributedNodesDebouncer =
113+
// Polymer.Debounce(this._distributedNodesDebouncer,
114+
// this._notifyObservers);
115+
// this._distributedNodesDebouncer.context = this.node;
116+
// Polymer.dom.addDebouncer(this._distributedNodesDebouncer);
117+
// },
118+
119+
};
120+
121+
if (Settings.useShadow) {
122+
123+
124+
125+
}
126+
127+
})();
128+
129+
</script>

0 commit comments

Comments
 (0)