4 var webkitStyles
= '-webkit-transition' in document
.documentElement
.style
5 var TRANSITION_CSSNAME
= webkitStyles
? '-webkit-transition' : 'transition';
6 var TRANSFORM_CSSNAME
= webkitStyles
? '-webkit-transform' : 'transform';
7 var TRANSITION_NAME
= webkitStyles
? 'webkitTransition' : 'transition';
8 var TRANSFORM_NAME
= webkitStyles
? 'webkitTransform' : 'transform';
10 var hasShadowDOMPolyfill
= window
.ShadowDOMPolyfill
;
12 Polymer('hero-transition',{
14 go: function(scope
, options
) {
22 var duration
= options
&& options
.duration
||
23 (CoreStyle
.g
.transitions
.heroDuration
||
24 CoreStyle
.g
.transitions
.duration
);
26 scope
._heroes
.forEach(function(h
) {
27 var d
= h
.h0
.hasAttribute('hero-delayed') ? CoreStyle
.g
.transitions
.heroDelay
: '';
29 props
.forEach(function(p
) {
30 wt
.push(p
+ ' ' + duration
+ ' ' + options
.easing
+ ' ' + d
);
33 h
.h1
.style
[TRANSITION_NAME
] = wt
.join(', ');
34 h
.h1
.style
.borderRadius
= h
.r1
;
35 h
.h1
.style
[TRANSFORM_NAME
] = '';
38 this.super(arguments
);
40 if (!scope
._heroes
.length
) {
41 this.completed
= true;
45 prepare: function(scope
, options
) {
46 this.super(arguments
);
47 var src
= options
.src
, dst
= options
.dst
;
49 if (scope
._heroes
&& scope
._heroes
.length
) {
50 this.ensureComplete(scope
);
55 // FIXME(yvonne): basic support for nested pages.
56 // Look for heroes in the light DOM and one level of shadow DOM of the src and dst,
57 // and also in src.selectedItem or dst.selectedItem, then transform the dst hero to src
59 var h
$ = this.findAllInShadows(src
, ss
);
60 if (src
.selectedItem
) {
61 hs
$ = this.findAllInShadows(src
.selectedItem
, ss
);
64 Array
.prototype.forEach
.call(hs
$, function(hs
) {
65 if (h
$.indexOf(hs
) === -1) {
72 for (var i
=0, h0
; h0
=h
$[i
]; i
++) {
73 var v
= h0
.getAttribute('hero-id');
74 var ds
= '[hero][hero-id="' + v
+ '"]';
75 var h1
= this.findInShadows(dst
, ds
);
77 if (!h1
&& dst
.selectedItem
) {
78 h1
= this.findInShadows(dst
.selectedItem
, ds
);
81 // console.log('src', src);
82 // console.log('dst', dst, dst.selectedItem);
83 // console.log(v, h0, h1);
85 var c0
= getComputedStyle(h0
);
86 var c1
= getComputedStyle(h1
);
89 b0
: h0
.getBoundingClientRect(),
92 b1
: h1
.getBoundingClientRect(),
96 var dl
= h
.b0
.left
- h
.b1
.left
;
97 var dt
= h
.b0
.top
- h
.b1
.top
;
98 var sw
= h
.b0
.width
/ h
.b1
.width
;
99 var sh
= h
.b0
.height
/ h
.b1
.height
;
101 // h.scaley = h.h0.hasAttribute('scaley');
102 // if (!h.scaley && (sw !== 1 || sh !== 1)) {
104 // h.h1.style.width = h.b0.width + 'px';
105 // h.h1.style.height = h.b0.height + 'px';
108 // Also animate the border-radius for the circle-to-square transition
110 h
.h1
.style
.borderRadius
= h
.r0
;
115 h
.h1
.style
[TRANSFORM_NAME
] = 'translate(' + dl
+ 'px,' + dt
+ 'px)' + ' scale(' + sw
+ ',' + sh
+ ')';
116 h
.h1
.style
[TRANSFORM_NAME
+ 'Origin'] = '0 0';
118 scope
._heroes
.push(h
);
124 // carefully look into ::shadow with polyfill specific hack
125 findInShadows: function(node
, selector
) {
126 return node
.querySelector(selector
) || (hasShadowDOMPolyfill
?
127 queryAllShadows(node
, selector
) :
128 node
.querySelector('::shadow ' + selector
));
131 findAllInShadows: function(node
, selector
) {
132 if (hasShadowDOMPolyfill
) {
133 var nodes
= node
.querySelectorAll(selector
).array();
134 var shadowNodes
= queryAllShadows(node
, selector
, true);
135 return nodes
.concat(shadowNodes
);
137 return node
.querySelectorAll(selector
).array().concat(node
.shadowRoot
? node
.shadowRoot
.querySelectorAll(selector
).array() : []);
141 ensureComplete: function(scope
) {
142 this.super(arguments
);
144 scope
._heroes
.forEach(function(h
) {
145 h
.h1
.style
[TRANSITION_NAME
] = '';
146 h
.h1
.style
[TRANSFORM_NAME
] = '';
152 complete: function(scope
, e
) {
153 // if (e.propertyName === TRANSFORM_CSSNAME) {
155 scope
._heroes
.forEach(function(h
) {
156 if (h
.h1
=== e
.path
[0]) {
162 this.super(arguments
);
170 // utility method for searching through shadowRoots.
171 function queryShadow(node
, selector
) {
172 var m
, el
= node
.firstElementChild
;
175 sr
= node
.shadowRoot
;
178 sr
= sr
.olderShadowRoot
;
180 for(i
= shadows
.length
- 1; i
>= 0; i
--) {
181 m
= shadows
[i
].querySelector(selector
);
187 m
= queryShadow(el
, selector
);
191 el
= el
.nextElementSibling
;
196 function _queryAllShadows(node
, selector
, results
) {
197 var el
= node
.firstElementChild
;
198 var temp
, sr
, shadows
, i
, j
;
200 sr
= node
.shadowRoot
;
203 sr
= sr
.olderShadowRoot
;
205 for (i
= shadows
.length
- 1; i
>= 0; i
--) {
206 temp
= shadows
[i
].querySelectorAll(selector
);
207 for(j
= 0; j
< temp
.length
; j
++) {
208 results
.push(temp
[j
]);
212 _queryAllShadows(el
, selector
, results
);
213 el
= el
.nextElementSibling
;
218 queryAllShadows = function(node
, selector
, all
) {
220 return _queryAllShadows(node
, selector
, []);
222 return queryShadow(node
, selector
);