Skip to content

Commit

Permalink
v.1.0.1 contd
Browse files Browse the repository at this point in the history
* position popup to maximize visible area
* popup.hide() method
* other changes
  • Loading branch information
foo123 committed Mar 17, 2022
1 parent cf94a8b commit 6dac8df
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 18 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A simple JavaScript class to show info popups easily for various combinations of items and events (Desktop and Mobile)

**version 1.0.1** (5 kB minified)
**version 1.0.1** (6 kB minified)


[Live Example](https://foo123.github.io/examples/infopopup/)
Expand All @@ -16,7 +16,7 @@ A simple JavaScript class to show info popups easily for various combinations of
```

```javascript
InfoPopup({
let infoPopup = InfoPopup({
// custom class for custom styling of popup
infoClass: 'title-info',
// custom class for custom styling of focused element
Expand All @@ -33,9 +33,13 @@ InfoPopup({
// position popup differently on Y axis depending on item
// options: 'bottom', 'center', 'top' (default)
atItemY: (item) => item.href === '#bar' ? 'bottom' : 'top',
// close popup after 1 sec
closeDelay: 1000
// hide popup after 1 sec
hideDelay: 1000
});

// programmatically show/hide popup
infoPopup.show(document.querySelector('[href="#foo"]'));
infoPopup.hide();
```

[![interactive map with InfoPopup](/screenshot.png)](https://foo123.github.io/examples/infopopup/)
72 changes: 60 additions & 12 deletions src/InfoPopup.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function InfoPopup(options)
};
removeInfoPopup = function() {
clearTimer();
timer = setTimeout(removePopup, +(self.options.closeDelay || 0));
timer = setTimeout(removePopup, +(self.options.hideDelay || 0));
};
handler2 = function handler2(evt) {
if ('mouseleave' === evt.type)
Expand All @@ -149,7 +149,7 @@ function InfoPopup(options)
};
handler = function(evt) {
var item = evt.target && closest(evt.target, self.options.item || '.info-item');
if (item && ('function' === typeof self.options.content))
if (item && is_function(self.options.content))
{
self.show(item, evt);
if ('click' !== self.options.trigger)
Expand Down Expand Up @@ -181,10 +181,46 @@ function InfoPopup(options)
case 'left':
x = itemRect.left;
addClass(infoPopup, 'left');
if (0 > x)
{
if (itemRect.right - infoRect.width > x)
{
x = itemRect.right - infoRect.width;
removeClass(infoPopup, 'left');
addClass(infoPopup, 'right');
}
}
else if (x + infoRect.width > vw)
{
if (itemRect.right < x + infoRect.width)
{
x = itemRect.right - infoRect.width;
removeClass(infoPopup, 'left');
addClass(infoPopup, 'right');
}
}
break;
case 'right':
x = itemRect.right - infoRect.width;
addClass(infoPopup, 'right');
if (0 > x)
{
if (itemRect.left > x)
{
x = itemRect.left;
removeClass(infoPopup, 'right');
addClass(infoPopup, 'left');
}
}
else if (itemRect.right > vw)
{
if (itemRect.left + infoRect.width < itemRect.right)
{
x = itemRect.left;
removeClass(infoPopup, 'right');
addClass(infoPopup, 'left');
}
}
break;
case 'center':
default:
Expand Down Expand Up @@ -231,13 +267,14 @@ function InfoPopup(options)
break;
case 'bottom':
y = itemRect.bottom;
addClass(infoPopup, 'below');
if (y + infoRect.height > vh)
{
y = itemRect.top - infoRect.height;
}
else
{
addClass(infoPopup, 'below');
if (infoRect.height - itemRect.top < y + infoRect.height - vh)
{
y = itemRect.top - infoRect.height;
removeClass(infoPopup, 'below');
}
}
break;
case 'top':
Expand All @@ -251,7 +288,7 @@ function InfoPopup(options)
sy = document.body.scrollTop;
y = 0;
}
else
else if (vh - itemRect.bottom - infoRect.height > y)
{
y = itemRect.bottom;
addClass(infoPopup, 'below');
Expand Down Expand Up @@ -279,10 +316,15 @@ function InfoPopup(options)
removeEvent(document.body, 'touchstart', handler, {capture:true, passive:false});
}
};
self.hide = function() {
clearTimer();
removePopup();
};
self.show = function(item, evt) {
if (!item || !is_function(self.options.content)) return;
var infoContent, imgs, loaded = 0;

infoContent = self.options.content(item);
infoContent = self.options.content(item, self);
if (null == infoContent || false === infoContent)
{
return;
Expand All @@ -303,8 +345,8 @@ function InfoPopup(options)

clearCurrent();

addClass(item, self.options.focusedClass || 'focused');
current = item;
addClass(current, self.options.focusedClass || 'focused');

document.body.appendChild(infoPopup);

Expand All @@ -318,7 +360,12 @@ function InfoPopup(options)
removeEvent(img, 'error', load);
removeEvent(img, 'load', load);
++loaded;
if ((imgs.length === loaded) && infoPopup.parentNode && (infoPopup === ancestor(img, infoPopup)))
if (
(imgs.length === loaded)
&& (item === current)
&& infoPopup.parentNode
&& (infoPopup === ancestor(img, infoPopup))
)
{
// re-position popup
positionAt(item, self.options.atItemX, self.options.atItemY);
Expand Down Expand Up @@ -384,7 +431,8 @@ InfoPopup.prototype = {
constructor: InfoPopup,
options: null,
dispose: null,
show: null
show: null,
hide: null
};
InfoPopup.VERSION = VERSION;
return InfoPopup;
Expand Down
2 changes: 1 addition & 1 deletion src/InfoPopup.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ <h1 class="title">Interactive Map of Areas of Greece w/ InfoPopup v.<span id="ve
// different Y position per item
return -1 !== ['argosaronikos','egeo','dodekanisa','sporades','kyklades','ionio'].indexOf(g.id) ? 'center' : 'top';
},
closeDelay: 1000
hideDelay: 1000
});
</script>
</body>
Expand Down

0 comments on commit 6dac8df

Please sign in to comment.