-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathLeftRightArranger.js
220 lines (202 loc) · 5.55 KB
/
LeftRightArranger.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* Contains the declaration for the {@link module:layout/LeftRightArranger~LeftRightArranger} kind.
* @module layout/LeftRightArranger
*/
var
kind = require('enyo/kind');
var
Arranger = require('./Arranger');
/**
* {@link module:layout/LeftRightArranger~LeftRightArranger} is a
* {@link module:layout/Arranger~Arranger} that displays the active control and
* some of the previous and next controls. The active control is centered
* horizontally in the container, and the previous and next controls are laid out
* to the left and right, respectively.
*
* Transitions between arrangements are handled by sliding the new control in
* from the right and sliding the active control out to the left.
*
* For more information, see the documentation on
* [Arrangers]{@linkplain $dev-guide/building-apps/layout/arrangers.html} in the
* Enyo Developer Guide.
*
* @class LeftRightArranger
* @extends module:layout/Arranger~Arranger
* @public
*/
module.exports = kind(
/** @lends module:layout/LeftRightArranger~LeftRightArranger.prototype */ {
/**
* @private
*/
name: 'enyo.LeftRightArranger',
/**
* @private
*/
kind: Arranger,
/**
* The margin width (i.e., how much of the previous and next controls
* are visible) in pixels.
*
* Note that this is imported from the container at construction time.
*
* @type {Number}
* @default 40
* @public
*/
margin: 40,
/**
* The axis along which the panels will animate.
*
* @type {String}
* @readOnly
* @default 'width'
* @protected
*/
axisSize: 'width',
/**
* The axis along which the panels will **not** animate.
*
* @type {String}
* @readOnly
* @default 'height'
* @protected
*/
offAxisSize: 'height',
/**
* The axis position at which the panel will animate.
*
* @type {String}
* @readOnly
* @default 'left'
* @protected
*/
axisPosition: 'left',
/**
* @method
* @private
*/
constructor: kind.inherit(function (sup) {
return function () {
sup.apply(this, arguments);
this.margin = this.container.margin != null ? this.container.margin : this.margin;
};
}),
/**
* Sizes the panels such that they fill [offAxisSize]{@link module:layout/LeftRightArranger~LeftRightArranger#offAxisSize}
* and yield [margin]{@link module:layout/LeftRightArranger~LeftRightArranger#margin} pixels on each side of
* [axisSize]{@link module:layout/LeftRightArranger~LeftRightArranger#axisSize}.
*
* @see {@link module:layout/Arranger~Arranger#size}
* @protected
*/
size: function () {
var c$ = this.container.getPanels();
var port = this.containerBounds[this.axisSize];
var box = port - this.margin -this.margin;
for (var i=0, b, c; (c=c$[i]); i++) {
b = {};
b[this.axisSize] = box;
b[this.offAxisSize] = '100%';
c.setBounds(b);
}
},
/**
* To prevent a panel that is switching sides (to maintain the balance) from overlapping
* the active panel during the animation, updates the `z-index` of the switching panel
* to ensure that it stays behind the other panels.
*
* @todo Could use some optimization in its `for` loop (e.g. .length lookup and calc)
* @see {@link module:layout/Arranger~Arranger#start}
* @method
* @protected
*/
start: kind.inherit(function (sup) {
return function () {
sup.apply(this, arguments);
var s = this.container.fromIndex;
var f = this.container.toIndex;
var c$ = this.getOrderedControls(f);
var o = Math.floor(c$.length/2);
for (var i=0, c; (c=c$[i]); i++) {
if (s > f){
if (i == (c$.length - o)){
c.applyStyle('z-index', 0);
} else {
c.applyStyle('z-index', 1);
}
} else {
if (i == (c$.length-1 - o)){
c.applyStyle('z-index', 0);
} else {
c.applyStyle('z-index', 1);
}
}
}
};
}),
/**
* Balances the panels laid out to each side of the active panel
* such that, for a set of `n` panels, `floor(n/2)` are before and `ceil(n/2)` are after
* the active panel.
*
* @protected
*/
arrange: function (controls, arrangement) {
var i,c,b;
if (this.container.getPanels().length==1){
b = {};
b[this.axisPosition] = this.margin;
this.arrangeControl(this.container.getPanels()[0], b);
return;
}
var o = Math.floor(this.container.getPanels().length/2);
var c$ = this.getOrderedControls(Math.floor(arrangement)-o);
var box = this.containerBounds[this.axisSize] - this.margin - this.margin;
var e = this.margin - box * o;
for (i=0; (c=c$[i]); i++) {
b = {};
b[this.axisPosition] = e;
this.arrangeControl(c, b);
e += box;
}
},
/**
* Calculates the difference along the
* [axisPosition]{@link module:layout/LeftRightArranger~LeftRightArranger#axisPosition} (e.g., `'left'`).
*
* @param {Number} inI0 - The initial layout setting.
* @param {Object} inA0 - The initial arrangement.
* @param {Number} inI1 - The target layout setting.
* @param {Object} inA1 - The target arrangement.
* @protected
*/
calcArrangementDifference: function (inI0, inA0, inI1, inA1) {
if (this.container.getPanels().length==1){
return 0;
}
var i = Math.abs(inI0 % this.c$.length);
//enyo.log(inI0, inI1);
return inA0[i][this.axisPosition] - inA1[i][this.axisPosition];
},
/**
* Resets the positioning and opacity of panels.
*
* @method
* @private
*/
destroy: kind.inherit(function (sup) {
return function () {
var c$ = this.container.getPanels();
for (var i=0, c; (c=c$[i]); i++) {
Arranger.positionControl(c, {left: null, top: null});
Arranger.opacifyControl(c, 1);
c.applyStyle('left', null);
c.applyStyle('top', null);
c.applyStyle('height', null);
c.applyStyle('width', null);
}
sup.apply(this, arguments);
};
})
});