Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: document and viewport dom service additions #4994

Merged
merged 1 commit into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions ui-v2/app/components/hashicorp-consul.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { get, set } from '@ember/object';
import { inject as service } from '@ember/service';
export default Component.extend({
dom: service('dom'),
// TODO: could this be dom.viewport() ?
win: window,
isDropdownVisible: false,
didInsertElement: function() {
get(this, 'dom')
Expand All @@ -19,11 +17,12 @@ export default Component.extend({
},
change: function(e) {
const dom = get(this, 'dom');
const win = dom.viewport();
const $root = dom.root();
const $body = dom.element('body');
if (e.target.checked) {
$root.classList.add('template-with-vertical-menu');
$body.style.height = $root.style.height = get(this, 'win').innerHeight + 'px';
$body.style.height = $root.style.height = win.innerHeight + 'px';
} else {
$root.classList.remove('template-with-vertical-menu');
$body.style.height = $root.style.height = null;
Expand Down
16 changes: 11 additions & 5 deletions ui-v2/app/mixins/click-outside.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import Mixin from '@ember/object/mixin';

import { inject as service } from '@ember/service';
import { next } from '@ember/runloop';
import { get } from '@ember/object';
const isOutside = function(element, e) {

// TODO: Potentially move this to dom service
const isOutside = function(element, e, doc = document) {
if (element) {
const isRemoved = !e.target || !document.contains(e.target);
const isRemoved = !e.target || !doc.contains(e.target);
const isInside = element === e.target || element.contains(e.target);
return !isRemoved && !isInside;
} else {
return false;
}
};

const handler = function(e) {
const el = get(this, 'element');
if (isOutside(el, e)) {
this.onblur(e);
}
};
export default Mixin.create({
dom: service('dom'),
init: function() {
this._super(...arguments);
this.handler = handler.bind(this);
Expand All @@ -26,12 +30,14 @@ export default Mixin.create({
onblur: function() {},
didInsertElement: function() {
this._super(...arguments);
const doc = get(this, 'dom').document();
next(this, () => {
document.addEventListener('click', this.handler);
doc.addEventListener('click', this.handler);
});
},
willDestroyElement: function() {
this._super(...arguments);
document.removeEventListener('click', this.handler);
const doc = get(this, 'dom').document();
doc.removeEventListener('click', this.handler);
},
});
13 changes: 9 additions & 4 deletions ui-v2/app/mixins/with-resizing.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Mixin from '@ember/object/mixin';
import { inject as service } from '@ember/service';
import { get } from '@ember/object';
import { assert } from '@ember/debug';
export default Mixin.create({
dom: service('dom'),
resize: function(e) {
assert('with-resizing.resize needs to be overridden', false);
},
win: window,
init: function() {
this._super(...arguments);
this.handler = e => {
Expand All @@ -17,14 +18,18 @@ export default Mixin.create({
},
didInsertElement: function() {
this._super(...arguments);
get(this, 'win').addEventListener('resize', this.handler, false);
get(this, 'dom')
.viewport()
.addEventListener('resize', this.handler, false);
this.didAppear();
},
didAppear: function() {
this.handler({ target: get(this, 'win') });
this.handler({ target: get(this, 'dom').viewport() });
},
willDestroyElement: function() {
get(this, 'win').removeEventListener('resize', this.handler, false);
get(this, 'dom')
.viewport()
.removeEventListener('resize', this.handler, false);
this._super(...arguments);
},
});
7 changes: 7 additions & 0 deletions ui-v2/app/services/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ let $_;
const clickFirstAnchor = clickFirstAnchorFactory(closest);
export default Service.extend({
doc: document,
win: window,
init: function() {
this._super(...arguments);
$_ = getComponentFactory(getOwner(this));
},
document: function() {
return get(this, 'doc');
},
viewport: function() {
return get(this, 'win');
},
// TODO: should this be here? Needs a better name at least
clickFirstAnchor: clickFirstAnchor,
closest: closest,
Expand Down
7 changes: 6 additions & 1 deletion ui-v2/tests/integration/mixins/with-resizing-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ module('Integration | Mixin | with-resizing', function(hooks) {
addEventListener: this.stub(),
removeEventListener: this.stub(),
};
const dom = {
viewport: function() {
return win;
},
};
const subject = EmberObject.extend(Mixin, {
win: win,
dom: dom,
}).create();
const resize = this.stub(subject, 'resize');
subject.didInsertElement();
Expand Down