Skip to content

Commit

Permalink
Merge pull request #171 from a15n/a15n/fix-eslint-order-warning
Browse files Browse the repository at this point in the history
A15n/fix eslint order warning
  • Loading branch information
Duncan Walker authored Jan 28, 2017
2 parents 2e01e9e + 1e5339a commit 595b608
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 74 deletions.
161 changes: 90 additions & 71 deletions addon/components/lazy-render-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {
warn,
} = Ember;

export const targetEventNameSpace = 'target-lazy-render-wrapper';
export const TARGET_EVENT_NAMESPACE = 'target-lazy-render-wrapper';

/* Beware: use of private API! :(
Expand Down Expand Up @@ -75,8 +75,24 @@ const PASSABLE_ACTIONS = [
const PASSABLE_OPTIONS = PASSABLE_PROPERTIES.concat(PASSABLE_ACTIONS);

export default Component.extend({

/* 1. Services */

/* 2. Defaults */

tagName: '',
layout,
enableLazyRendering: false,
event: 'hover', // Options are: hover, click, focus, none
_childView: null, // This is set during the childView's didRender and is needed for the hide action

_hasUserInteracted: false,
_hasRendered: false,
_shouldShowOnRender: false,

/* 3. Single line Computed Property */

/* 4. Multiline Computed Property */

passedPropertiesObject: computed(...PASSABLE_OPTIONS, function() {
return PASSABLE_OPTIONS.reduce((passablePropertiesObject, key) => {
Expand Down Expand Up @@ -105,9 +121,53 @@ export default Component.extend({
}, {});
}),

enableLazyRendering: false,
_hasUserInteracted: false,
_hasRendered: false,
/**
* A jQuery element that the _lazyRenderEvents will be
* attached to during didInsertElement and
* removed from during willDestroyElement
*
* @public
* @property $target
* @type jQuery element
* @default the parent jQuery element
*/

$target: computed('target', 'tetherComponentName', function() {
const target = this.get('target'); // #some-id
let $target;

if (target) {
$target = $(target);
} else if (this.get('tetherComponentName').indexOf('-on-component') >= 0) {

/* TODO(Andrew)
Refactor this once we've gotten rid of the -on-component approach
share the functionality with `onComponentTarget`
*/

const targetView = this.get('parentView');

if (!targetView) {
warn('No targetView found');

return null;
} else if (!targetView.get('elementId')) {
warn('No targetView.elementId');

return null;
}

const targetViewElementId = targetView.get('elementId');

$target = $(`#${targetViewElementId}`);
} else {
$target = getParent(this);
}

return $target;
}),

_shouldRender: computed('isShown', 'tooltipIsVisible', 'enableLazyRendering', '_hasUserInteracted', function() {

/* If isShown, tooltipIsVisible, !enableLazyRendering, or _hasUserInteracted then
Expand Down Expand Up @@ -136,9 +196,7 @@ export default Component.extend({

return false;
}),
_shouldShowOnRender: false,

event: 'hover', // Options are: hover, click, focus, none
_lazyRenderEvents: computed('event', function() {

/* The lazy-render wrapper will only render the tooltip when
Expand All @@ -164,52 +222,9 @@ export default Component.extend({
return _lazyRenderEvents;
}),

/**
* A jQuery element that the _lazyRenderEvents will be
* attached to during didInsertElement and
* removed from during willDestroyElement
*
* @public
* @property $target
* @type jQuery element
* @default the parent jQuery element
*/

$target: computed('target', 'tetherComponentName', function() {
const target = this.get('target'); // #some-id
let $target;

if (target) {
$target = $(target);
} else if (this.get('tetherComponentName').indexOf('-on-component') >= 0) {

/* TODO(Andrew)
Refactor this once we've gotten rid of the -on-component approach
share the functionality with `onComponentTarget`
*/

const targetView = this.get('parentView');

if (!targetView) {
warn('No targetView found');

return null;
} else if (!targetView.get('elementId')) {
warn('No targetView.elementId');

return null;
}

const targetViewElementId = targetView.get('elementId');

$target = $(`#${targetViewElementId}`);
} else {
$target = getParent(this);
}
/* 5. Observers */

return $target;
}),
/* 6. Lifecycle Hooks */

didInsertElement() {
this._super(...arguments);
Expand All @@ -232,15 +247,15 @@ export default Component.extend({
if the user has mouseenter and not mouseleave immediately afterwards.
*/

$target.on(`mouseleave.${targetEventNameSpace}`, () => {
$target.on(`mouseleave.${TARGET_EVENT_NAMESPACE}`, () => {
this.set('_shouldShowOnRender', false);
});
}

this.get('_lazyRenderEvents').forEach((entryInteractionEvent) => {
$target.on(`${entryInteractionEvent}.${targetEventNameSpace}`, () => {
this.get('_lazyRenderEvents').forEach((_lazyRenderEvent) => {
$target.on(`${_lazyRenderEvent}.${TARGET_EVENT_NAMESPACE}`, () => {
if (this.get('_hasUserInteracted')) {
$target.off(`${entryInteractionEvent}.${targetEventNameSpace}`);
$target.off(`${_lazyRenderEvent}.${TARGET_EVENT_NAMESPACE}`);
} else {
this.set('_hasUserInteracted', true);
this.set('_shouldShowOnRender', true);
Expand All @@ -250,30 +265,34 @@ export default Component.extend({
});
},

childView: null, // This is set during the childView's didRender and is needed for the hide action
willDestroyElement() {
this._super(...arguments);

const $target = this.get('$target');

this.get('_lazyRenderEvents').forEach((_lazyRenderEvent) => {
$target.off(`${_lazyRenderEvent}.${TARGET_EVENT_NAMESPACE}`);
});

$target.off(`mouseleave.${TARGET_EVENT_NAMESPACE}`);
},

/* 7. All actions */

actions: {
hide() {
const childView = this.get('childView');
const _childView = this.get('_childView');

/* The childView.actions property is not available in Ember 1.13
We will use childView._actions until we drop support for Ember 1.13
/* The _childView.actions property is not available in Ember 1.13
We will use _childView._actions until we drop support for Ember 1.13
*/

if (childView && childView._actions && childView._actions.hide) {
childView.send('hide');
if (_childView && _childView._actions && _childView._actions.hide) {
_childView.send('hide');
}
},
},

willDestroyElement() {
this._super(...arguments);

const $target = this.get('$target');
/* 8. Custom / private methods */

this.get('_lazyRenderEvents').forEach((entryInteractionEvent) => {
$target.off(`${entryInteractionEvent}.${targetEventNameSpace}`);
});

$target.off(`mouseleave.${targetEventNameSpace}`);
},
});
2 changes: 1 addition & 1 deletion addon/components/tether-popover-on-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default TooltipAndPopoverComponent.extend({
const parentView = this.get('parentView');

if (parentView) {
parentView.set('childView', this);
parentView.set('_childView', this);
}
},
didInsertElement() {
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/components/render-events-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ember from 'ember';
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import { targetEventNameSpace } from 'ember-tooltips/components/lazy-render-wrapper';
import { TARGET_EVENT_NAMESPACE } from 'ember-tooltips/components/lazy-render-wrapper';

const { $, run } = Ember;

Expand Down Expand Up @@ -34,7 +34,7 @@ function assertTargetHasLazyRenderEvents(assert, $target, eventType = 'hover') {
assert.equal(eventHandler.origType, event,
`the eventHandler's origType property should equal ${event}`);

assert.ok(eventHandler.namespace.indexOf(targetEventNameSpace) >= 0,
assert.ok(eventHandler.namespace.indexOf(TARGET_EVENT_NAMESPACE) >= 0,
'the eventHandler\'s namespace property be unique to ember-tooltips');
}

Expand Down

0 comments on commit 595b608

Please sign in to comment.