Skip to content

Commit

Permalink
Merge pull request #1 from Envoc/feat/close
Browse files Browse the repository at this point in the history
Feat/close
  • Loading branch information
Neil Smith committed Nov 26, 2014
2 parents 9d9a339 + 98f1464 commit 5740517
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 12 deletions.
6 changes: 4 additions & 2 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ <h1> Hide on click </h1>
ns-popover-template="close"
ns-popover-trigger="click"
ns-popover-theme="ns-popover-tooltip-theme"
ns-popover-hide-on-click="false"
ns-popover-timeout="1">
ns-popover-timeout="1000"
ns-popover-hide-on-inside-click="false"
ns-popover-hide-on-outside-click="true"
ns-popover-hide-on-button-click="true">
Click Me
</button>
</article>
Expand Down
56 changes: 51 additions & 5 deletions example/nsPopover.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
container: 'body',
placement: 'bottom|left',
timeout: 1.5,
hideOnClick: 'true',
hideOnInsideClick: false,
hideOnOutsideClick: true,
hideOnButtonClick: true,
mouseRelative: '',
popupDelay: 0
};
Expand Down Expand Up @@ -53,7 +55,9 @@
container: attrs.nsPopoverContainer || defaults.container,
placement: attrs.nsPopoverPlacement || defaults.placement,
timeout: attrs.nsPopoverTimeout || defaults.timeout,
hideOnClick: toBoolean(attrs.nsPopoverHideOnClick || defaults.hideOnClick),
hideOnInsideClick: toBoolean(attrs.nsPopoverHideOnInsideClick || defaults.hideOnInsideClick),
hideOnOutsideClick: toBoolean(attrs.nsPopoverHideOnOutsideClick || defaults.hideOnOutsideClick),
hideOnButtonClick: toBoolean(attrs.nsPopoverHideOnButtonClick || defaults.hideOnButtonClick),
mouseRelative: attrs.nsPopoverMouseRelative,
popupDelay: attrs.nsPopoverPopupDelay || defaults.popupDelay
};
Expand All @@ -74,6 +78,7 @@
}

displayer_.id_ = $timeout(function() {
$popover.isOpen = true;
$popover.css('display', 'block');

// position the popover accordingly to the defined placement around the
Expand All @@ -88,10 +93,50 @@

move($popover, placement_, align_, elmRect, $triangle);

if (options.hideOnClick) {
// Hide the popover without delay on click events.
if (options.hideOnInsideClick) {
// Hide the popover without delay on the popover click events.
$popover.on('click', function () {
hider_.hide($popover, 0);
if ($popover.isOpen) {
hider_.hide($popover, 0);
}
});
}
if (options.hideOnOutsideClick) {
// Hide the popover without delay on outside click events.
$document.on('click', function (e) {
if ($popover.isOpen && e.target !== elm[0]) {
var id = $popover[0].id;
if (!isInPopover(e.target)) {
hider_.hide($popover, 0);
}
}

function isInPopover(el) {
if (el.id === id) {
return true;
}

var parent = angular.element(el).parent()[0];

if (!parent) {
return false;
}

if (parent.id === id) {
return true;
}
else {
return isInPopover(parent);
}
}
});
}
if (options.hideOnButtonClick) {
// Hide the popover without delay on the button click events.
elm.on('click', function () {
if ($popover.isOpen) {
hider_.hide($popover, 0);
}
});
}
}, delay*1000);
Expand Down Expand Up @@ -122,6 +167,7 @@
}

hider_.id_ = $timeout(function() {
$popover.isOpen = false;
displayer_.cancel();
popover.css('display', 'none');
}, delay*1000);
Expand Down
56 changes: 51 additions & 5 deletions src/nsPopover.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
container: 'body',
placement: 'bottom|left',
timeout: 1.5,
hideOnClick: 'true',
hideOnInsideClick: false,
hideOnOutsideClick: true,
hideOnButtonClick: true,
mouseRelative: '',
popupDelay: 0
};
Expand Down Expand Up @@ -53,7 +55,9 @@
container: attrs.nsPopoverContainer || defaults.container,
placement: attrs.nsPopoverPlacement || defaults.placement,
timeout: attrs.nsPopoverTimeout || defaults.timeout,
hideOnClick: toBoolean(attrs.nsPopoverHideOnClick || defaults.hideOnClick),
hideOnInsideClick: toBoolean(attrs.nsPopoverHideOnInsideClick || defaults.hideOnInsideClick),
hideOnOutsideClick: toBoolean(attrs.nsPopoverHideOnOutsideClick || defaults.hideOnOutsideClick),
hideOnButtonClick: toBoolean(attrs.nsPopoverHideOnButtonClick || defaults.hideOnButtonClick),
mouseRelative: attrs.nsPopoverMouseRelative,
popupDelay: attrs.nsPopoverPopupDelay || defaults.popupDelay
};
Expand All @@ -74,6 +78,7 @@
}

displayer_.id_ = $timeout(function() {
$popover.isOpen = true;
$popover.css('display', 'block');

// position the popover accordingly to the defined placement around the
Expand All @@ -88,10 +93,50 @@

move($popover, placement_, align_, elmRect, $triangle);

if (options.hideOnClick) {
// Hide the popover without delay on click events.
if (options.hideOnInsideClick) {
// Hide the popover without delay on the popover click events.
$popover.on('click', function () {
hider_.hide($popover, 0);
if ($popover.isOpen) {
hider_.hide($popover, 0);
}
});
}
if (options.hideOnOutsideClick) {
// Hide the popover without delay on outside click events.
$document.on('click', function (e) {
if ($popover.isOpen && e.target !== elm[0]) {
var id = $popover[0].id;
if (!isInPopover(e.target)) {
hider_.hide($popover, 0);
}
}

function isInPopover(el) {
if (el.id === id) {
return true;
}

var parent = angular.element(el).parent()[0];

if (!parent) {
return false;
}

if (parent.id === id) {
return true;
}
else {
return isInPopover(parent);
}
}
});
}
if (options.hideOnButtonClick) {
// Hide the popover without delay on the button click events.
elm.on('click', function () {
if ($popover.isOpen) {
hider_.hide($popover, 0);
}
});
}
}, delay*1000);
Expand Down Expand Up @@ -122,6 +167,7 @@
}

hider_.id_ = $timeout(function() {
$popover.isOpen = false;
displayer_.cancel();
popover.css('display', 'none');
}, delay*1000);
Expand Down

0 comments on commit 5740517

Please sign in to comment.