-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-stack.html
More file actions
131 lines (111 loc) · 2.92 KB
/
basic-stack.html
File metadata and controls
131 lines (111 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<!--
Stacks its child elements on top of each other, taking on the maximum height
and width of the child elements.
@element basic-stack
-->
<link rel="import" href="../basic-aspect/basic-aspect.html">
<link rel="import" href="../basic-children-content/basic-children-content.html">
<link rel="import" href="../basic-shared/basic-shared.html">
<dom-module id="basic-stack">
<template>
<style>
:host {
box-sizing: border-box;
display: inline-block;
position: relative;
}
#stackContainer {
display: block;
left: 0;
position: relative;
top: 0;
width: 100%;
}
#stackContainer ::content > * {
position: absolute;
}
</style>
<basic-children-content id="stackContainer">
<content id="content"></content>
</basic-children-content>
</template>
</dom-module>
<script>
Polymer({
behaviors: [
Basic.Aspect,
Basic.Resized
],
contribute: {
contentChanged: function() {
this._listenForLoadEvents('img');
this._listenForLoadEvents('iframe');
setTimeout(function() {
this.recalc();
}.bind(this));
}
},
is: 'basic-stack',
properties: {
target: {
value: 'shadow'
}
},
/**
* Force the component to recalculate its size.
*
* @method recalc
*/
recalc: function() {
var contentNodes = this.collective.content;
if (!contentNodes) {
return;
}
var maxWidth = 0;
var maxHeight = 0;
Array.prototype.forEach.call(contentNodes, function(node) {
// We use getBoundingClientRect so we can get fractional sizes.
var rect = node.getBoundingClientRect();
maxWidth = Math.max(maxWidth, rect.width);
maxHeight = Math.max(maxHeight, rect.height);
});
maxWidth = Math.ceil(maxWidth); // Round up when hard-coding width.
var width = maxWidth ?
maxWidth + 'px' :
null;
var height = maxHeight ?
maxHeight + 'px' :
null;
this.$.stackContainer.style.width = width;
this.$.stackContainer.style.height = height;
var event = new CustomEvent('basic-layout', {
bubbles: true,
detail: {
count: contentNodes.length,
height: parseInt(height),
width: parseInt(width)
}
});
this.dispatchEvent(event);
},
resized: function(event) {
if (this.$ && this.$.stackContainer) {
var foo = this.$.stackContainer.style;
this.$.stackContainer.style.width = '100%';
this.recalc();
}
},
// Listen for load events on children of the specified tag type.
// TODO: Share this with basic-framed-content.
_listenForLoadEvents: function(tag) {
var childNodes = Basic.ContentHelpers.flattenChildNodes(this);
Array.prototype.forEach.call(childNodes, function(child) {
if (child.localName === tag) {
child.addEventListener('load', function() {
this.recalc();
}.bind(this));
}
}.bind(this));
}
});
</script>