pixl-xyapp 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/js/popover.js ADDED
@@ -0,0 +1,158 @@
1
+
2
+ var Popover = {
3
+
4
+ enabled: false,
5
+ onDetach: null,
6
+ isSafari: !!(navigator.userAgent.match(/Safari/) && !navigator.userAgent.match(/(Chrome|Opera|Edge)/)),
7
+
8
+ attach: function(elem, html, shrinkwrap) {
9
+ if (this.enabled) this.detach();
10
+ var $elem = $(elem);
11
+ var rect = this.isSafari ? this.getBoundingClientRect($elem[0]) : $elem[0].getBoundingClientRect();
12
+ var win = get_inner_window_size();
13
+
14
+ var $box = $('<div class="arrow_box"></div>').html(html).css({
15
+ left: '-9999px'
16
+ });
17
+ if (!shrinkwrap) $box.css('width', '' + Math.floor( rect.width ) + 'px');
18
+
19
+ $('body').append( $box );
20
+
21
+ this.$box = $box;
22
+ this.rect = rect;
23
+ this.enabled = true;
24
+ this.$anchor = $elem;
25
+
26
+ setTimeout( function() {
27
+ var width = $box.width();
28
+ var height = $box.height();
29
+
30
+ if (height + 16 < rect.top) {
31
+ $box.css('top', '' + Math.floor( rect.top - height - 16 ) + 'px');
32
+ $box.addClass('bottom');
33
+ }
34
+ else {
35
+ $box.css('top', '' + Math.floor( rect.bottom + 16 ) + 'px');
36
+ $box.addClass('top');
37
+ }
38
+
39
+ if (shrinkwrap) {
40
+ var left = Math.floor( (rect.left + (rect.width / 2)) - (width / 2) );
41
+ var adj = 0;
42
+ if (left < 20) {
43
+ adj = 20 - left;
44
+ left += adj;
45
+ }
46
+ else if (left + width > win.width - 20) {
47
+ adj = (win.width - 20) - (left + width);
48
+ left += adj;
49
+ adj++;
50
+ }
51
+ $box.css('left', '' + left + 'px');
52
+ if (adj) $box.css('--arrow-left', 'calc(50% - ' + adj + 'px)');
53
+ }
54
+ else $box.css('left', '' + Math.floor( rect.left ) + 'px');
55
+
56
+ if ($('#popoverlay').length) {
57
+ $('#popoverlay').stop().remove();
58
+ }
59
+
60
+ var $overlay = $('<div id="popoverlay"></div>').css('opacity', 0);
61
+ $('body').append($overlay);
62
+ $overlay.fadeTo( 500, 0.5 ).click(function() {
63
+ Popover.detach();
64
+ });
65
+
66
+ if (!Dialog.active) unscroll();
67
+ }, 1 );
68
+ },
69
+
70
+ detach: function() {
71
+ if (this.enabled) {
72
+ this.$box.remove();
73
+ this.enabled = false;
74
+ delete this.$anchor;
75
+ $('#popoverlay').stop().fadeOut( 300, function() { $(this).remove(); } );
76
+ if (!Dialog.active) unscroll.reset();
77
+
78
+ if (this.onDetach) {
79
+ // one time hook for detach
80
+ var callback = this.onDetach;
81
+ this.onDetach = null;
82
+ callback();
83
+ }
84
+ this.onKeyDown = null;
85
+ }
86
+ },
87
+
88
+ reposition: function() {
89
+ // box has changed size, so reposition it
90
+ if (this.$box.hasClass('bottom')) {
91
+ var height = this.$box.height();
92
+ this.$box.css('top', '' + Math.floor( this.rect.top - height - 16 ) + 'px');
93
+ }
94
+ },
95
+
96
+ handleKeyDown: function(event) {
97
+ // intercept keydown for custom actions
98
+ if (!this.enabled) return;
99
+
100
+ if (this.onKeyDown) {
101
+ this.onKeyDown(event);
102
+ }
103
+ else if (event.keyCode == 27) {
104
+ event.preventDefault();
105
+ this.detach();
106
+ }
107
+ },
108
+
109
+ getBoundingClientRect: function(el) {
110
+ // custom version of getBoundingClientRect that works in safari
111
+ // supports zoom, fixed and scrolling elements
112
+ if (!(el instanceof Element)) throw new TypeError("Expected an Element");
113
+
114
+ let x = 0, y = 0;
115
+ let current = el;
116
+ let encounteredFixed = false;
117
+ let scale = 1;
118
+
119
+ while (current) {
120
+ const style = window.getComputedStyle(current);
121
+ const zoom = parseFloat(style.zoom) || 1;
122
+ const position = style.position;
123
+
124
+ if (position === 'fixed') {
125
+ const rect = current.getBoundingClientRect();
126
+ x += rect.left * zoom;
127
+ y += rect.top * zoom;
128
+ scale *= zoom;
129
+ encounteredFixed = true;
130
+ break;
131
+ }
132
+
133
+ x = (x + current.offsetLeft - current.scrollLeft) * zoom;
134
+ y = (y + current.offsetTop - current.scrollTop) * zoom;
135
+ scale *= zoom;
136
+
137
+ current = current.offsetParent;
138
+ }
139
+
140
+ if (!encounteredFixed) {
141
+ x -= window.pageXOffset;
142
+ y -= window.pageYOffset;
143
+ }
144
+
145
+ const width = el.offsetWidth * scale;
146
+ const height = el.offsetHeight * scale;
147
+
148
+ return {
149
+ x, y,
150
+ width, height,
151
+ top: y,
152
+ left: x,
153
+ right: x + width,
154
+ bottom: y + height
155
+ };
156
+ }
157
+
158
+ };