Skip to content

Commit

Permalink
Factor dom-api's into separate helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Orvell committed Aug 10, 2015
1 parent 1ca065f commit effedcb
Show file tree
Hide file tree
Showing 8 changed files with 587 additions and 260 deletions.
54 changes: 54 additions & 0 deletions src/lib/dom-api-classlist.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!--
@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;

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>
93 changes: 93 additions & 0 deletions src/lib/dom-api-event.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<!--
@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 = 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>
68 changes: 68 additions & 0 deletions src/lib/dom-api-flush.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<!--
@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>

// add Polymer.dom flush api...
Polymer.Base.extend(Polymer.dom, {

_flushGuard: 0,
_FLUSH_MAX: 100,
_needsTakeRecords: !Polymer.Settings.useNativeCustomElements,
_debouncers: [],
_finishDebouncer: null,

// flush and debounce exposed as statics on Polymer.dom
flush: function() {
// flush debouncers
for (var i=0; i < this._debouncers.length; i++) {
this._debouncers[i].complete();
}
// clear the list of debouncers
if (this._finishDebouncer) {
this._finishDebouncer.complete();
}
// again make any pending CE mutations that might trigger debouncer
// additions go...
this._flushPolyfills();
// flush again if there are now any debouncers to process
if (this._debouncers.length && this._flushGuard < this._FLUSH_MAX) {
this._flushGuard++;
this.flush();
} else {
if (this._flushGuard >= this._FLUSH_MAX) {
console.warn('Polymer.dom.flush aborted. Flush may not be complete.')
}
this._flushGuard = 0;
}
},

// 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.
_flushPolyfills: function() {
if (this._needsTakeRecords) {
CustomElements.takeRecords();
}
},

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>
129 changes: 129 additions & 0 deletions src/lib/dom-api-mutation-content.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<!--
@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.prototype.observeDistributedNodes = function(callback) {
if (!this._observer) {
this._observer = new DomApi.Mutation(this);
}
return this._observer.addObserver(callback);
};

DomApi.prototype.unobserveDistributedNodes = function(handle) {
if (this._observer) {
this._observer.removeObserver(handle);
}
}

DomApi.MutationContent = function(domApi) {
this.domApi = domApi;
this.node = this.domApi.node;
this._observers = [];
this._addedNodes = [];
this._removedNodes = [];
};

DomApi.MutationContent.prototype = {

// addObserver: function(callback) {
// return this._observers.push(callback);
// },

// removeObserver: function(handle) {
// this._observers.splice(handle - 1, 1);
// },

// hasObservers: function() {
// return Boolean(this._observers.length);
// },

// _scheduleMutationNotify: function() {
// this._mutationDebouncer = Polymer.Debounce(this._mutationDebouncer,
// this._notifyObservers);
// this._mutationDebouncer.context = this;
// Polymer.dom.addDebouncer(this._mutationDebouncer);
// },

// _notifyObservers: function(mxns) {
// var info = {
// target: this.node,
// addedNodes: this._addedNodes,
// removedNodes: this._removedNodes
// }
// var o$ = this._observers;
// for (var i=0, o; (i < o$.length) && (o=o$[i]); i++) {
// o.call(null, info);
// }
// this._addedNodes = [];
// this._removedNodes = [];
// }

// observeDistributedNodes: function(callback) {
// if (this.node.localName !== 'content') {
// console.warn('Must call `observeDistributedNodes` on a <content> element.');
// return;
// }
// // setup <content>.getDistributedNodes observation
// if (!this.hasObservers()) {
// this._startObserveDistributedNodes();
// }
// this._addObserver(callback);
// },

// unobserveDistributedNodes: function(handle) {
// if (this.node.localName !== 'content') {
// return;
// }
// this._removeObserver(handle);
// if (!this.hasObservers()) {
// this._stopObservingDistributedNodes();
// }
// },

// _startObservingDistributedNodes: function() {
// this._distributedObservers = [];
// var root = this.getOwnerRoot();
// var host = root && root.host;
// if (host) {
// var h = Polymer.dom(host)
// .observeChildren(this._scheduleDistributedNodesNotify);
// this._distributedObservers.push(h);
// }
// },

// _stopObservingDistributedNodes: function() {

// },

// _scheduleDistributedNodesNotify: function() {
// this._distributedNodesDebouncer =
// Polymer.Debounce(this._distributedNodesDebouncer,
// this._notifyObservers);
// this._distributedNodesDebouncer.context = this.node;
// Polymer.dom.addDebouncer(this._distributedNodesDebouncer);
// },

};

if (Settings.useShadow) {



}

})();

</script>
Loading

0 comments on commit effedcb

Please sign in to comment.