-
Notifications
You must be signed in to change notification settings - Fork 1
/
paper-fab-menu.html
398 lines (351 loc) · 12.1 KB
/
paper-fab-menu.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../neon-animation/neon-animation-runner-behavior.html" />
<link rel="import" href="../neon-animation/neon-animations.html" />
<link rel="import" href="../paper-tooltip/paper-tooltip.html" />
<link rel="import" href="../iron-media-query/iron-media-query.html" />
<link rel="import" href="../iron-overlay-behavior/iron-overlay-behavior.html" />
<link rel="import" href="../iron-flex-layout/classes/iron-flex-layout.html" />
<!-- <link rel="import" href="../iron-flex-layout/iron-flex-layout-classes.html" /> -->
<!--
This element provides a FAB Speed Dial menu.
Example:
<paper-fab-menu>
<paper-fab></paper-fab>
<paper-fab></paper-fab>
</paper-fab-menu>
@demo
-->
<dom-module id="paper-fab-menu">
<template>
<!--
Uncomment to use newer iron-flex-layout-classes instead of classes/iron-flex-layout.html, see imports
<style is="custom-style" include="iron-flex iron-flex-reverse iron-flex-alignment">
-->
<style>
:host {
position: fixed;
bottom: 1.5em;
right: 1.5em;
transition: 100ms ease-in-out;
}
.hidden {
visibility: hidden;
}
div.tooltip-hidden {
display: none;
}
div.main-fab-tooltip,div.option-fab-tooltip {
background-color: #616161;
opacity: 0.9;
color: white;
font-size: 10px;
line-height: 1;
padding: 8px;
border-radius: 2px;
position: absolute;
white-space: nowrap;
}
div.main-fab-tooltip {
right: 64px;
}
div.option-fab-tooltip {
right: 56px;
}
#fab_options_container ::content > paper-fab {
margin-bottom: 0.5em;
}
#fab_container ::content > paper-fab > iron-icon {
transition: 150ms ease-in-out;
}
</style>
<iron-media-query query="(max-device-width: 1024px) and (orientation: landscape), (orientation: portrait)" query-matches="{{isDevice}}"></iron-media-query>
<div id="fab_container" class="layout vertical-reverse center"
on-mouseenter="_showOptionFabs"
on-mouseleave="_hideOptionFabs">
<content id="main_fab_content"
select="paper-fab-menu > paper-fab:first-child, div#fab_container > paper-fab:first-child">
</content>
<div id="fab_options_container"
class="layout vertical-reverse center hidden">
<content id="option_fabs_content" select="paper-fab,div.tooltip"></content>
</div>
</div>
</template>
<script>
Polymer ( {
is : "paper-fab-menu",
behaviors: [
Polymer.NeonAnimationRunnerBehavior
],
properties : {
/**
* The name of the icon to show when menu is not active.
* Defaults to "icons:add"
*/
mainIconName: {
type: String,
value: 'add'
},
/**
* The name of the icon when menu is active.
* Not necessary to set since it's taken from the first FAB on init.
*/
mainIconNameActive: {
type: String
},
/**
* The config for the animations.
* Unknown behavior if this is altered for now..
*/
animationConfig: {
type: Object,
value: function(){return {'entry':[],'exit':[]};}
},
/**
* Indicates if the menu is currently open.
*/
opened: {
type: Boolean,
value: false,
readonly: true,
observer: '_openedChanged'
},
/**
* Set to true if no menu behavior should be applied.
* Commonly used for debugging.
*/
singleOption: {
type: Boolean,
value: false
},
isDevice: {
type: Boolean,
value: false
}
},
listeners: {
'neon-animation-finish': '_onNeonAnimationFinish'
},
ready : function( ) {
var clickEvent = ('ontouchstart' in window) ? 'touchstart' : 'mousedown';
document.addEventListener(clickEvent, this._onCaptureClick.bind(this), true);
var initFabs = this._initFabs;
var mainFab = this._getMainFab();
this.mainIconNameActive = mainFab.getAttribute("icon");
if(!this.singleOption) {
mainFab.setAttribute("icon", this.mainIconName);
}
Polymer.dom(this.$.main_fab_content).observeNodes(function(info) {
this._initFabs(info.addedNodes,true);
}.bind(this));
Polymer.dom(this.$.option_fabs_content).observeNodes(function(info) {
this._initFabs(info.addedNodes,false).then(function (tooltips) {
this._configureAnimations(info.addedNodes,tooltips);
}.bind(this));
}.bind(this));
},
_openedChanged: function(opened) {
this.toggleClass('hidden', !opened, this.$.fab_options_container);
if(this.isDevice && !this.singleOption) {
var action = this.opened ? this._onFabHover.bind(this) : this._onFabUnhover.bind(this);
var e;
var mainFab = this._getMainFab();
e = {
target: mainFab
};
action(e);
var optionFabs = this._getOptionFabs();
for (var i = 0; i < optionFabs.length; i++) {
if (optionFabs[i].style.display.toLowerCase()!=='none') {
e = {target:optionFabs[i]};
action(e);
}
}
}
},
_initFabs: function (fabArray, isMain) {
return new Promise(function(resolve, reject) {
var tooltips = {};
for (var i = 0; i < fabArray.length; i++) {
var fab = fabArray[i];
if(!this.isDevice){
this.listen(fab,"mouseenter", '_onFabHover');
this.listen(fab,'mouseleave','_onFabUnhover');
this.listen(fab,'tap', '_onFabTap');
} else {
this.listen(fab,'tap', '_onTap');
}
//Make sure the FAB always have an id
if (fab.id === "") {
fab.id = "paper-fab-menu_fab" + (isMain ? "main" : "") + i;
}
if(fab.title && fab.title!==""){
var tooltip = document.createElement('div');
this.toggleClass('tooltip',true,tooltip);
tooltip.innerHTML = fab.title;
fab.title = "";
tooltip.id = "pfm_tooltip_" + fab.id;
// Distance to bottom of div for tooltip
// = (main fab height - tooltip height)/2 = (56px - 26px)/2 = 15px [if main fab]
// = main fab height + fab gap + (fab height - tooltip height)/2 + (fab height + fab gap)*(fab list number)
// = 56px + 8px + (40px - 26px)/2 + (40px + 8px)*i = (69 + 48*i)px [if option fab #i]
tooltip.style.bottom = (isMain ? "15" : (69+i*48)) + "px";
Polymer.dom(fab.parentNode).appendChild(tooltip);
this.toggleClass('tooltip-hidden', true, tooltip);
this.toggleClass('main-fab-tooltip', isMain, tooltip);
this.toggleClass('option-fab-tooltip', !isMain, tooltip);
tooltips[fab.id] = tooltip;
}
if(!isMain) {
fab.setAttribute("mini",true);
}
}
resolve(tooltips);
}.bind(this));
},
_configureAnimations : function (optionFabs, tooltips) {
var entryAnimation = [];
var exitAnimation = [];
for (var i = 0; i < optionFabs.length; i++) {
this.animationConfig.entry.push({
name: 'scale-up-animation',
node: optionFabs[i],
timing: {duration: 100, delay: 50*i}
});
this.animationConfig.exit.push({
name: 'scale-down-animation',
node: optionFabs[i],
timing: {duration: 100, delay: 50*(optionFabs.length-i)}
});
if(tooltips[optionFabs[i].id]) {
this.animationConfig.entry.push({
name: 'scale-up-animation',
node: tooltips[optionFabs[i].id],
timing: {duration: 100, delay: 50*i}
});
this.animationConfig.exit.push({
name: 'scale-down-animation',
node: tooltips[optionFabs[i].id],
timing: {duration: 100, delay: 50*(optionFabs.length-i)}
});
}
}
},
_getMainFab : function() {
var fab = Polymer.dom(this.$.main_fab_content).getDistributedNodes()[0];
return fab;
},
_getMainFabIcon : function() {
return this._getMainFab().getElementsByTagName('iron-icon')[0];
},
_getOptionFabs : function() {
return Polymer.dom(this.$.option_fabs_content).getDistributedNodes();
},
_onCaptureClick: function (e) {
if(this.isDevice && this.opened) {
var p;
var t = e.target;
while(!p) {
if (t.tagName.toLowerCase() === 'paper-fab' || t.tagName.toLowerCase() === 'body') {
p = t.tagName.toLowerCase() === 'body' ? 'body' : 'paper-fab';
} else {
t = t.parentNode;
}
}
if(p === 'body') {
this._hideOptionFabs(e);
}
}
},
_showOptionFabs : function (e) {
if(this.isDevice && e && e.type === "mouseenter") {
return;
}
this.cancelAnimation();
this.opened = true;
if(!this.singleOption) {
var iconElement = this._getMainFabIcon();
iconElement.style.transform = "rotate(360deg) scale(0.5,0.5)";
setTimeout(
function () {
iconElement.icon = this.mainIconNameActive;
iconElement.style.transform = "rotate(360deg) scale(1,1)";
}.bind(this), 75
);
}
this.playAnimation('entry');
},
_hideOptionFabs : function (e) {
if(this.isDevice && e && e.type === "mouseleave") {
return;
}
this.cancelAnimation();
if(!this.singleOption) {
var iconElement = this._getMainFabIcon();
iconElement.style.transform = "rotate(0deg) scale(0.5,0.5)";
setTimeout(
function () {
iconElement.icon = this.mainIconName;
iconElement.style.transform = "rotate(0deg) scale(1,1)";
}.bind(this), 75
);
}
this.playAnimation('exit', function () {
this.opened = false;
}.bind(this));
},
_onNeonAnimationFinish: function(event, fun) {
if (typeof fun === "function") {
fun();
}
},
_onFabHover: function(e) {
this._setFabHoverStatus(e.target, true);
},
_onFabUnhover: function(e) {
this._setFabHoverStatus(e.target, false);
},
_setFabHoverStatus: function(target, hovering) {
target.setAttribute("elevation", hovering ? "3": "1");
this.toggleClass('tooltip-hidden', !hovering, Polymer.dom(target.parentNode).querySelector('#pfm_tooltip_' + target.id));
},
_onTap: function (e) {
var t = e.target;
var fab, pfm;
while (!(pfm && fab)) {
fab = t.tagName.toLowerCase() === 'paper-fab' ? t : fab;
if(t.parentNode.tagName.toLowerCase() === 'paper-fab-menu') {
pfm = t.parentNode;
} else {
t = t.parentNode;
}
}
if(pfm.isDevice && !pfm.singleOption) {
if(pfm.opened) {
if(e.type === 'tap') {
this._onFabTap(e);
pfm._hideOptionFabs(e);
}
} else {
pfm._showOptionFabs(e);
}
} else {
this._onFabTap(e);
}
},
// Element Behavior
/**
* The `fab-tap` event is fired whenever one FAB is tapped.
*
* @event fab-tap
* @detail {{event: tap-event}}
*/
_onFabTap: function (e) {
e.target.fire('fab-tap', e);
},
_translateX: function(x) {
this.transform("translateX(-" + x + "px)");
}
} );
</script>
</dom-module>