Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
platform-less wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sorvell committed Aug 1, 2014
1 parent 4745a8a commit 704b651
Show file tree
Hide file tree
Showing 13 changed files with 633 additions and 28 deletions.
27 changes: 27 additions & 0 deletions polymer.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@
<link rel="import" href="layout.html">
<script src="src/polymer.js"></script>
<script src="src/boot.js"></script>

<!-- Do not load in the presence of platform.js -->
<scripts>
<script>
(function() {
// evacipate all scripts in parent!
if (window.Platform) {
var scripts = document._currentScript.parentNode;
scripts.parentNode.removeChild(scripts);
}
})();
</script>
<script src="src/platform/compat.js"></script>
<script src="src/platform/microtask.js"></script>
<script src="src/platform/module.js"></script>
<script src="src/platform/unresolved.js"></script>
<script src="../HTMLImports/src/base.js"></script>
</scripts>

<!-- move from Platform to polymer! -->
<script src="src/platform/url.js"></script>
<script src="src/platform/loader.js"></script>
<script src="src/platform/styleloader.js"></script>
<script src="../observe-js/src/observe.js"></script>
<script src="../NodeBind/src/NodeBind.js"></script>
<script src="../TemplateBinding/src/TemplateBinding.js"></script>

<script src="src/lib/lang.js"></script>
<script src="src/lib/job.js"></script>
<script src="src/lib/dom.js"></script>
Expand Down
2 changes: 1 addition & 1 deletion src/declaration/mdv.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
templateContent: function() {
var template = this.fetchTemplate();
return template && Platform.templateContent(template);
return template && template.content;
},
installBindingDelegate: function(template) {
if (template) {
Expand Down
11 changes: 7 additions & 4 deletions src/declaration/polymer.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,13 @@
// document. Platform collects those calls until we can process them, which
// we do here.

var declarations = Platform.deliverDeclarations();
if (declarations) {
for (var i=0, l=declarations.length, d; (i<l) && (d=declarations[i]); i++) {
element.apply(null, d);
if (Platform.deliverDeclarations) {

var declarations = Platform.deliverDeclarations();
if (declarations) {
for (var i=0, l=declarations.length, d; (i<l) && (d=declarations[i]); i++) {
element.apply(null, d);
}
}
}

Expand Down
42 changes: 26 additions & 16 deletions src/declaration/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,7 @@

ready: function() {
this.flush();
// TODO(sorvell): As an optimization, turn off CE polyfill upgrading
// while registering. This way we avoid having to upgrade each document
// piecemeal per registration and can instead register all elements
// and upgrade once in a batch. Without this optimization, upgrade time
// degrades significantly when SD polyfill is used. This is mainly because
// querying the document tree for elements is slow under the SD polyfill.
if (CustomElements.ready === false) {
CustomElements.upgradeDocumentTree(document);
CustomElements.ready = true;
}
Platform.flush();
readyPolyfill();
requestAnimationFrame(this.flushReadyCallbacks);
},

Expand Down Expand Up @@ -180,14 +170,34 @@

var polymerReadied = false;

document.addEventListener('WebComponentsReady', function() {
CustomElements.ready = false;
});
var customElementsPolyfill = !CustomElements.useNative;

if (!customElementsPolyfill) {
document.addEventListener('WebComponentsReady', function() {
CustomElements.ready = false;
});
}

function readyPolyfill() {
// TODO(sorvell): As an optimization, turn off CE polyfill upgrading
// while registering. This way we avoid having to upgrade each document
// piecemeal per registration and can instead register all elements
// and upgrade once in a batch. Without this optimization, upgrade time
// degrades significantly when SD polyfill is used. This is mainly because
// querying the document tree for elements is slow under the SD polyfill.
if (customElementsPolyfill && CustomElements.ready === false) {
CustomElements.upgradeDocumentTree(document);
CustomElements.ready = true;
}
Platform.flush();
}

function whenPolymerReady(callback) {
queue.waitToReady = true;
CustomElements.ready = false;
HTMLImports.whenImportsReady(function() {
if (customElementsPolyfill) {
CustomElements.ready = false;
}
HTMLImports.whenReady(function() {
queue.addReadyCallback(callback);
queue.waitToReady = false;
queue.check();
Expand Down
65 changes: 59 additions & 6 deletions src/lib/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,63 @@
originalStopPropagation.apply(this, arguments);
};

// TODO(sorvell): remove when we're sure imports does not need
// to load stylesheets
/*
HTMLImports.importer.preloadSelectors +=
', polymer-element link[rel=stylesheet]';
*/

// polyfill DOMTokenList
// * add/remove: allow these methods to take multiple classNames
// * toggle: add a 2nd argument which forces the given state rather
// than toggling.

var add = DOMTokenList.prototype.add;
var remove = DOMTokenList.prototype.remove;
DOMTokenList.prototype.add = function() {
for (var i = 0; i < arguments.length; i++) {
add.call(this, arguments[i]);
}
};
DOMTokenList.prototype.remove = function() {
for (var i = 0; i < arguments.length; i++) {
remove.call(this, arguments[i]);
}
};
DOMTokenList.prototype.toggle = function(name, bool) {
if (arguments.length == 1) {
bool = !this.contains(name);
}
bool ? this.add(name) : this.remove(name);
};
DOMTokenList.prototype.switch = function(oldName, newName) {
oldName && this.remove(oldName);
newName && this.add(newName);
};

// add array() to NodeList, NamedNodeMap, HTMLCollection

var ArraySlice = function() {
return Array.prototype.slice.call(this);
};

var namedNodeMap = (window.NamedNodeMap || window.MozNamedAttrMap || {});

NodeList.prototype.array = ArraySlice;
namedNodeMap.prototype.array = ArraySlice;
HTMLCollection.prototype.array = ArraySlice;

// utility

function createDOM(inTagOrNode, inHTML, inAttrs) {
var dom = typeof inTagOrNode == 'string' ?
document.createElement(inTagOrNode) : inTagOrNode.cloneNode(true);
dom.innerHTML = inHTML;
if (inAttrs) {
for (var n in inAttrs) {
dom.setAttribute(n, inAttrs[n]);
}
}
return dom;
}

// exports

scope.createDOM = createDOM;

})(Polymer);
36 changes: 35 additions & 1 deletion src/lib/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,43 @@
}
return prototype;
}



// mixin

// copy all properties from inProps (et al) to inObj
function mixin(inObj/*, inProps, inMoreProps, ...*/) {
var obj = inObj || {};
for (var i = 1; i < arguments.length; i++) {
var p = arguments[i];
try {
for (var n in p) {
copyProperty(n, p, obj);
}
} catch(x) {
}
}
return obj;
}

// copy property inName from inSource object to inTarget object
function copyProperty(inName, inSource, inTarget) {
var pd = getPropertyDescriptor(inSource, inName);
Object.defineProperty(inTarget, inName, pd);
}

// get property descriptor for inName on inObject, even if
// inName exists on some link in inObject's prototype chain
function getPropertyDescriptor(inObject, inName) {
if (inObject) {
var pd = Object.getOwnPropertyDescriptor(inObject, inName);
return pd || getPropertyDescriptor(Object.getPrototypeOf(inObject), inName);
}
}

// exports

scope.extend = extend;
scope.mixin = mixin;

})(Polymer);
38 changes: 38 additions & 0 deletions src/platform/compat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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
*/

/*
On supported platforms, platform.js is not needed. To retain compatibility
with the polyfills, we stub out minimal functionality.
*/
if (!window.Platfrom) {

Platform = {
flush: function() {}
};

CustomElements = {
useNative: true,
takeRecords: function() {},
instanceof: function(obj, base) {
return obj instanceof base;
}
};

HTMLImports = {
useNative: true
};

// ShadowDOM
ShadowDOMPolyfill = null;
wrap = unwrap = function(n){
return n;
};

}
114 changes: 114 additions & 0 deletions src/platform/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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
*/

(function(scope) {
var endOfMicrotask = scope.endOfMicrotask;

// Generic url loader
function Loader(regex) {
this.cache = Object.create(null);
this.map = Object.create(null);
this.requests = 0;
this.regex = regex;
}
Loader.prototype = {

// TODO(dfreedm): there may be a better factoring here
// extract absolute urls from the text (full of relative urls)
extractUrls: function(text, base) {
var matches = [];
var matched, u;
while ((matched = this.regex.exec(text))) {
u = new URL(matched[1], base);
matches.push({matched: matched[0], url: u.href});
}
return matches;
},
// take a text blob, a root url, and a callback and load all the urls found within the text
// returns a map of absolute url to text
process: function(text, root, callback) {
var matches = this.extractUrls(text, root);

// every call to process returns all the text this loader has ever received
var done = callback.bind(null, this.map);
this.fetch(matches, done);
},
// build a mapping of url -> text from matches
fetch: function(matches, callback) {
var inflight = matches.length;

// return early if there is no fetching to be done
if (!inflight) {
return callback();
}

// wait for all subrequests to return
var done = function() {
if (--inflight === 0) {
callback();
}
};

// start fetching all subrequests
var m, req, url;
for (var i = 0; i < inflight; i++) {
m = matches[i];
url = m.url;
req = this.cache[url];
// if this url has already been requested, skip requesting it again
if (!req) {
req = this.xhr(url);
req.match = m;
this.cache[url] = req;
}
// wait for the request to process its subrequests
req.wait(done);
}
},
handleXhr: function(request) {
var match = request.match;
var url = match.url;

// handle errors with an empty string
var response = request.response || request.responseText || '';
this.map[url] = response;
this.fetch(this.extractUrls(response, url), request.resolve);
},
xhr: function(url) {
this.requests++;
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.send();
request.onerror = request.onload = this.handleXhr.bind(this, request);

// queue of tasks to run after XHR returns
request.pending = [];
request.resolve = function() {
var pending = request.pending;
for(var i = 0; i < pending.length; i++) {
pending[i]();
}
request.pending = null;
};

// if we have already resolved, pending is null, async call the callback
request.wait = function(fn) {
if (request.pending) {
request.pending.push(fn);
} else {
endOfMicrotask(fn);
}
};

return request;
}
};

scope.Loader = Loader;
})(window.Platform);
Loading

0 comments on commit 704b651

Please sign in to comment.