This repository was archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathg-flow-panel-transition.html
151 lines (146 loc) · 5.43 KB
/
g-flow-panel-transition.html
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!--
/*
* Copyright 2012 The Polymer Authors. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
-->
<!--
/**
* @module Polymer Elements
*/
-->
<polymer-element name="g-flow-panel-transition" extends="g-panel-transition">
<template>
<style scoped id="flowsheet">
g-panels[transition='flow'] > .right {
left: auto;
right: 0;
}
g-panels[transition='flow'] > .narrow-layout {
width: 100%;
box-shadow: none;
}
g-panels[transition='flow'] > .narrow-layout.custom-panel {
width: auto;
}
</style>
</template>
<script>
(function() {
var WEBKIT_END_EVENT = 'webkitTransitionEnd';
var END_EVENT = 'transitionEnd';
Polymer('g-flow-panel-transition', {
highlander: false,
narrowMedia: '(max-width: 800px)',
setup: function() {
this.super(arguments);
this.listenForMediaMatch();
// TODO(sorvell): redo when reference combinators arrive
//this.panels.appendChild(this.$.flowsheet);
this.boundResizeHandler = this.resizeHandler.bind(this);
window.addEventListener('resize', this.boundResizeHandler);
this.panels.addEventListener(END_EVENT, this.finishListener);
this.panels.addEventListener(WEBKIT_END_EVENT, this.finishListener);
},
teardown: function() {
window.removeEventListener('resize', this.boundResizeHandler);
// TODO(sorvell): how do we remove a node from lightDOM?
// Need a ref to the node in lightDOM; workaround until fixed.
var sheet = this.panels.querySelector(this.$.flowsheet.id);
if (sheet) {
this.panels.removeChild(sheet);
}
this.super();
this.panels.removeEventListener(END_EVENT, this.finishListener);
this.panels.removeEventListener(WEBKIT_END_EVENT, this.finishListener);
},
listenForMediaMatch: function() {
var mq = window.matchMedia(this.narrowMedia);
mq.addListener(this.layoutChange.bind(this));
this.layoutChange(mq);
},
layoutChange: function(inQuery) {
this.narrowLayout = inQuery.matches;
this.panels.getPanels().forEach(function(p) {
p.classList.toggle('narrow-layout', this.narrowLayout);
}, this);
this.refresh();
},
enableTransitions: function(inEnable) {
this.panels.getPanels().forEach(function(p) {
p.style.webkitTransition = p.style.transition = inEnable ? null : 'none';
});
},
resizeHandler: function(e) {
if (this.narrowLayout && !this.resizing) {
this.resizing = this.asyncMethod('resize', null, 50);
}
},
resize: function() {
clearTimeout(this.resizing);
this.resizing = null;
this.refresh();
},
refresh: function() {
this.enableTransitions(false);
this.to = this.panels.getSelectedPanel();
if (this.canTransition()) {
this.begin();
}
requestAnimationFrame(function() {
this.enableTransitions(true);
}.bind(this));
},
begin: function() {
var i = this.panels.indexOf(this.to);
if (this.panelIsRightAligned(this.to)) {
this.applyRightLayout(this.to, i);
} else {
this.applyLeftLayout(this.to, i);
}
},
// inIndex appears at the left with subsequent nodes
// moved out of the way to the right; right aligned nodes are ignored
applyLeftLayout: function(inTo, inIndex) {
if (!inTo) {
return;
}
var l, count = this.count;
this.panels.getPanels().forEach(function(p, i) {
var x = !l ? 0 : l.offsetWidth + 'px';
var right = this.panelIsRightAligned(p);
p.style.zIndex = right ? -i : null;
p.style.webkitTransform = p.style.transform = 'translate3d(' + (right ? 0 : x) + ',0,0)';
if (i >= inIndex) {
l = p;
}
}, this);
},
// inIndex appears at the right with previous nodes
// moved out of the way to the left; note z-index used to get proper stacking
applyRightLayout: function(inTo, inIndex) {
var p$ = this.panels.getPanels(), right=true, l;
for (var i=p$.length-1, p; p=p$[i]; i--) {
var x = l ? -l.offsetWidth : 0;
p.style.zIndex = right ? p$.length - i : null;
p.style.webkitTransform = p.style.transform = 'translate3d(' + x + 'px,0,0)';
if (i <= inIndex) {
l = p;
}
// record where we stop being right aligned so we can stop z-stacking
right = right && this.panelIsRightAligned(p);
}
},
// TODO(sorvell): it's unsatisfying that we need a specific class to
// determine right alignment because this defeats applying rightness via
// selectors (e.g. :last-child or media queries). We could look for
// computed style right == 0 but this seems more brittle.
panelIsRightAligned: function(inNode) {
return !this.panels.narrowLayout && inNode &&
inNode.classList.contains('right');
}
});
})();
</script>
</polymer-element>