-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from a15n/ember-popover
ember-popovers
- Loading branch information
Showing
26 changed files
with
1,441 additions
and
466 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import PopoverOnElementComponent from 'ember-tooltips/components/popover-on-element'; | ||
import { onComponentTarget } from 'ember-tooltips/utils'; | ||
|
||
export default PopoverOnElementComponent.extend({ | ||
|
||
target: onComponentTarget, | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import Ember from 'ember'; | ||
import TooltipAndPopoverComponent from 'ember-tooltips/components/tooltip-and-popover'; | ||
import layout from 'ember-tooltips/templates/components/popover-on-element'; | ||
|
||
const { $, run } = Ember; | ||
|
||
export default TooltipAndPopoverComponent.extend({ | ||
|
||
/* Options */ | ||
hideDelay: 250, | ||
|
||
/* Properties */ | ||
layout, | ||
classNames: ['ember-popover'], | ||
_isMouseInside: false, | ||
didInsertElement() { | ||
this._super(...arguments); | ||
|
||
const event = this.get('event'); | ||
const target = this.get('target'); | ||
const $target = $(target); | ||
const $popover = this.$(); | ||
|
||
if (event === 'none') { | ||
|
||
return; | ||
|
||
} else if (event === 'hover') { | ||
|
||
const _showOn = this.get('_showOn'); | ||
const _hideOn = this.get('_hideOn'); | ||
|
||
// _showOn == 'mouseenter' | ||
$target.on(_showOn, () => this.show()); | ||
|
||
// _hideOn == 'mouseleave' | ||
$target.add($popover).on(_hideOn, () => { | ||
run.later(() => { | ||
if (!this.get('_isMouseInside')) { | ||
this.hide(); | ||
} | ||
}, +this.get('hideDelay')); | ||
}); | ||
|
||
// we must use mouseover/mouseout because they correctly | ||
// register hover interactivity when spacing='0' | ||
$target.add($popover).on('mouseover', () => this.set('_isMouseInside', true)); | ||
$target.add($popover).on('mouseout', () => this.set('_isMouseInside', false)); | ||
|
||
} else if (event === 'click') { | ||
|
||
$(document).on(`click.${target}`, (event) => { | ||
// this lightweight, name-spaced click handler is necessary to determine | ||
// if a click is NOT on $target and NOT an ancestor of $target. | ||
// If so then it must be a click elsewhere and should close the popover | ||
// see... https://css-tricks.com/dangers-stopping-event-propagation/ | ||
const isClickedElementElsewhere = this._isElementElsewhere(event.target); | ||
const isPopoverShown = this.get('isShown'); | ||
|
||
if (isClickedElementElsewhere && isPopoverShown) { | ||
this.hide(); | ||
} | ||
}); | ||
|
||
// we use mousedown because it occurs before the focus event | ||
$target.on('mousedown', (event) => { | ||
// $target.on('mousedown') is called when the $popover is | ||
// clicked because the $popover is contained within the $target. | ||
// This will ignores those types of clicks. | ||
const isMouseDownElementInPopover = this._isElementInPopover(event.target); | ||
if (isMouseDownElementInPopover) { | ||
return; | ||
} | ||
this.toggle(); | ||
}); | ||
} | ||
|
||
$target.on('focus', () => this.show()); | ||
|
||
$popover.on('focusout', () => { | ||
// use a run.later() to allow the 'focusout' event to finish handling | ||
run.later(() => { | ||
const isFocusedElementElsewhere = this._isElementElsewhere(document.activeElement); | ||
if (isFocusedElementElsewhere) { | ||
this.hide(); | ||
} | ||
}); | ||
}); | ||
}, | ||
willDestroyElement() { | ||
this._super(...arguments); | ||
|
||
const target = this.get('target'); | ||
const $target = $(target); | ||
const $popover = this.$(); | ||
const _showOn = this.get('_showOn'); | ||
const _hideOn = this.get('_hideOn'); | ||
|
||
$target.add($popover).off(`${_showOn} mouseover ${_hideOn} mouseout mousedown focus focusout`); | ||
|
||
$(document).off(`click.${target}`); | ||
}, | ||
_isElementInPopover(newElement) { | ||
// determines if newElement is $popover or contained within $popover | ||
const $popover = this.$(); | ||
return $popover.is(newElement) || $popover.find(newElement).length; | ||
}, | ||
_isElementElsewhere(newElement) { | ||
// determines if newElement is not $target, not $popover, and not contained within either | ||
const $target = $(this.get('target')); | ||
|
||
const isNewElementOutsideTarget = !$target.is(newElement) && !$target.find(newElement).length; | ||
const isNewElementOutsidePopover = !this._isElementInPopover(newElement); | ||
|
||
return isNewElementOutsideTarget && isNewElementOutsidePopover; | ||
}, | ||
actions: { | ||
hide() { | ||
this.hide(); | ||
} | ||
}, | ||
}); |
Oops, something went wrong.