-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…mium/issues/detail?id=516550 by adding Polymer.RenderStatus.whenReady and using it to defer `attached`
- Loading branch information
Steven Orvell
committed
Aug 5, 2015
1 parent
eafa3e5
commit 2bffc4c
Showing
10 changed files
with
209 additions
and
60 deletions.
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
This file was deleted.
Oops, something went wrong.
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,65 @@ | ||
<!-- | ||
@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> | ||
/* | ||
* Helper for determining when first render occurs. | ||
* Call `Polymer.RenderStatus.whenReady(callback)` to be notified when | ||
* first render occurs or immediately if it has already occured. | ||
* Note that since HTML Imports are designed to load before rendering, | ||
* this call can also be used to guarantee that imports have loaded. | ||
* This behavior is normalized to function correctly with the HTMLImports | ||
* polyfill which does not otherwise maintain this rendering guarantee. | ||
* Querying style and layout data before first render is currently | ||
* problematic on some browsers (Blink/Webkit) so this helper can be used | ||
* to prevent doing so until a safe time. | ||
*/ | ||
Polymer.RenderStatus = { | ||
|
||
_ready: false, | ||
|
||
_callbacks: [], | ||
|
||
whenReady: function(cb) { | ||
if (this._ready) { | ||
cb(); | ||
} else { | ||
this._callbacks.push(cb); | ||
} | ||
}, | ||
|
||
_makeReady: function() { | ||
this._ready = true; | ||
this._callbacks.forEach(function(cb) { | ||
cb(); | ||
}); | ||
this._callbacks = []; | ||
}, | ||
|
||
_catchFirstRender: function() { | ||
requestAnimationFrame(function() { | ||
Polymer.RenderStatus._makeReady(); | ||
}); | ||
} | ||
}; | ||
|
||
if (window.HTMLImports) { | ||
HTMLImports.whenReady(function() { | ||
Polymer.RenderStatus._catchFirstRender(); | ||
}); | ||
} else { | ||
Polymer.RenderStatus._catchFirstRender(); | ||
} | ||
|
||
// NOTE: for bc. | ||
Polymer.ImportStatus = Polymer.RenderStatus; | ||
Polymer.ImportStatus.whenLoaded = Polymer.ImportStatus.whenReady; | ||
|
||
</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
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
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
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,36 @@ | ||
<!-- | ||
@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="../../polymer.html"> | ||
|
||
<dom-module id="some-thing"> | ||
<script> | ||
Polymer({ | ||
|
||
is: 'some-thing', | ||
|
||
created: function() { | ||
console.warn(this.localName, 'created'); | ||
}, | ||
|
||
attached: function() { | ||
this.report(); | ||
}, | ||
|
||
report: function() { | ||
console.group(this.localName, 'report'); | ||
console.log('offsetParent is', this.offsetParent); | ||
console.log('parentNode', this.parentNode, 'has position', | ||
window.getComputedStyle(this.parentNode).position); | ||
console.groupEnd(this.localName); | ||
} | ||
|
||
}); | ||
</script> | ||
</dom-module> |
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
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,32 @@ | ||
<!-- | ||
@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 | ||
--> | ||
<dom-module id="attached-style"> | ||
<style> | ||
:host { | ||
display: block; | ||
box-sizing: border-box; | ||
border: 1px solid black; | ||
height: 100px; | ||
} | ||
</style> | ||
<template>Hi</template> | ||
<script> | ||
Polymer({ | ||
|
||
is: 'attached-style', | ||
|
||
attached: function() { | ||
this.offsetParentAtAttachedTime = this.offsetParent; | ||
this.offsetHeightAtAttachedTime = this.offsetHeight; | ||
} | ||
|
||
}); | ||
</script> | ||
</dom-module> |
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,62 @@ | ||
<!DOCTYPE html> | ||
<!-- | ||
@license | ||
Copyright (c) 2015 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 | ||
--> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
|
||
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script> | ||
<script> | ||
addEventListener('WebComponentsReady', function() { | ||
window.elementsReady = true; | ||
// Record layout data at WebComponentsReady time for later testing | ||
var el = document.querySelector('attached-style'); | ||
el.offsetParentAtWebComponentsReadyTime = el.offsetParent; | ||
el.offsetHeightAtWebComponentsReadyTime = el.offsetHeight; | ||
}); | ||
</script> | ||
<script src="../../../web-component-tester/browser.js"></script> | ||
|
||
<link rel="import" href="../../polymer.html"> | ||
<link rel="import" href="attached-style-elements.html"> | ||
</head> | ||
<body> | ||
|
||
<attached-style></attached-style> | ||
|
||
<script> | ||
|
||
suite('Attached can get computed style data', function() { | ||
|
||
var el = document.querySelector('attached-style'); | ||
|
||
test('style data available at attached time', function() { | ||
assert.equal(el.offsetParentAtAttachedTime, document.body); | ||
assert.equal(el.offsetHeightAtAttachedTime, 100); | ||
}); | ||
|
||
test('style data available at WebComponentsReady time', function(done) { | ||
function finish() { | ||
assert.equal(el.offsetParentAtWebComponentsReadyTime, document.body); | ||
assert.equal(el.offsetHeightAtWebComponentsReadyTime, 100); | ||
done(); | ||
} | ||
if (!window.elementsReady) { | ||
addEventListener('WebComponentsReady', finish); | ||
} else { | ||
finish(); | ||
} | ||
}) | ||
|
||
}); | ||
</script> | ||
|
||
</body> | ||
</html> |