-
Notifications
You must be signed in to change notification settings - Fork 16
/
polymer-date-picker-positioning.html
64 lines (62 loc) · 2.26 KB
/
polymer-date-picker-positioning.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
<polymer-element name="polymer-date-picker-positioning" attributes="relatedTarget opened halign">
<script>
Polymer("polymer-date-picker-positioning", {
ready: function() {
var self = this;
window.addEventListener('scroll', function() {
self.reposition();
});
window.addEventListener('resize', function() {
self.reposition();
});
this.async(self.reposition);
},
closeCanceled: false,
openedChanged: function(oldVal, newVal) {
var picker = this;
if (!this.clickListener) {
this.clickListener = function(e) {
var bound = picker.target.getBoundingClientRect();
picker.async(function() {if (!picker.closeCanceled) picker.close(); else picker.closeCanceled = false});
};
this.targetClickListener = function(e) {
picker.closeCanceled = true;
}
}
if (newVal == "true") {
this.reposition();
this.style.display = "";
this.async(function() {
window.addEventListener('click', picker.clickListener, false);
picker.target.addEventListener('click', picker.targetClickListener, false);
}, 100);
} else {
window.removeEventListener('click', picker.clickListener, false);
window.removeEventListener('click', picker.targetClickListener, false);
picker.style.display = "none";
}
},
reposition: function() {
if (this.opened == "true") {
var picker = this;
var bound = this.relatedTarget.getBoundingClientRect();
picker.style.position = "fixed";
picker.style.top = (bound.bottom) + "px";
if (this.halign == "right") picker.style.left = (bound.right) + "px";
else if (this.halign == "left") picker.style.left = (bound.left) + "px";
else picker.style.left = ((bound.left + bound.right) / 2) + "px";
}
},
toggle: function() {
if (this.opened == "true") this.opened = "false";
else this.opened = "true";
},
open: function() {
this.opened = "true";
},
close: function() {
this.opened = "false";
}
});
</script>
</polymer-element>