Skip to content

Commit

Permalink
dialog focus, tests and bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelcobain committed Mar 25, 2016
1 parent ef4dedd commit 05608bf
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 75 deletions.
16 changes: 13 additions & 3 deletions addon/components/paper-dialog-inner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@ import Ember from 'ember';
import Translate3dMixin from '../mixins/translate3d-mixin';
import PaperDialog from './paper-dialog';

const { Component, computed } = Ember;
const { Component, computed, $ } = Ember;

export default Component.extend(Translate3dMixin, {
tagName: 'md-dialog',
classNames: ['md-default-theme'],
classNameBindings: ['contentOverflow:md-content-overflow', 'fullscreen:md-dialog-fullscreen'],

onTranslateDestroy(origin) {
origin.focus();
onTranslateFromEnd() {
if (this.get('focusOnOpen')) {
let toFocus = this.$('[autofocus]').last();
if (toFocus.length === 0) {
toFocus = this.$('md-dialog-actions button').last();
}
toFocus.focus();
}
},

onTranslateToEnd() {
$(this.get('origin')).focus();
},

click(ev) {
Expand Down
36 changes: 28 additions & 8 deletions addon/components/paper-dialog.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@
import Ember from 'ember';
const { Component, computed, $ } = Ember;
const { Component, computed, $, inject: { service } } = Ember;

export default Component.extend({
tagName: '',

escapeToClose: true,
focusOnOpen: true,

destination: computed('parent', function() {
return this.get('parent') ? this.get('parent') : 'paper-wormhole';
}),

defaultParent: 'body',

hashedParent: computed('destination', function() {
let parent = this.get('destination');
return parent ? `#${parent}` : null;
}),

parentElementSelector: computed.or('hashedParent', 'defaultParent'),

constants: service(),

didInsertElement() {
$(window).on('keyup', (e) => {
if (27 === e.keyCode && this.get('onClose')) {
this.get('onClose')();
}
});
if (this.get('escapeToClose')) {
let parent = this.get('parentElementSelector');
$(parent).on(`keydown.${this.elementId}`, (e) => {
if (e.keyCode === this.get('constants.KEYCODE.ESCAPE') && this.get('onClose')) {
this.get('onClose')();
}
});
}
},

willDestroyElement() {
$(window).off('keyup');
if (this.get('escapeToClose')) {
let parent = this.get('parentElementSelector');
$(parent).off(`keydown.${this.elementId}`);
}
},

actions: {
outsideClicked() {
if (this.get('clickOutsideToClose') && this.get('onClose')) {
return this.get('onClose')();
this.get('onClose')();
}
}
}
Expand Down
31 changes: 17 additions & 14 deletions addon/mixins/translate3d-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ export default Mixin.create({
attributeBindings: ['translateStyle:style'],
classNameBindings: ['transformIn:md-transition-in'],

defaultParent: 'body',

parentElement: computed.or('hashedParent', 'defaultParent'),
hashedParent: computed('parent', function() {
let parent = this.get('parent');
return parent ? `#${parent}` : null;
}),

fromElement: computed.or('openFrom', 'origin'),

toElement: computed.or('closeTo', 'origin'),
Expand All @@ -50,6 +42,14 @@ export default Mixin.create({
}
}),

init() {
this._super(...arguments);
this.TRANSITIONEND = this.get('constants').get('CSS').TRANSITIONEND;
},

onTranslateFromEnd: K,
onTranslateToEnd: K,

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

Expand All @@ -59,14 +59,17 @@ export default Mixin.create({
// Wait while CSS takes affect
// Set the `main` styles and run the transition-in styles
window.requestAnimationFrame(() => {
this.set('transformStyleApply', 'main');
this.set('transformIn', true);
Ember.run(() => {
this.waitTransitionEnd(this.element).then(() => {
this.onTranslateFromEnd();
});
this.set('transformStyleApply', 'main');
this.set('transformIn', true);
});
});
});
},

onTranslateDestroy: K,

/**
* Specific reversal of the request translate animation above...
*
Expand All @@ -92,7 +95,7 @@ export default Mixin.create({

this.waitTransitionEnd(dialogClone).then(() => {
containerClone.remove();
this.onTranslateDestroy(toElement);
this.onTranslateToEnd(toElement);
});
});

Expand All @@ -112,7 +115,7 @@ export default Mixin.create({

// Upon timeout or transitionEnd, reject or resolve (respectively) this promise.
// NOTE: Make sure this transitionEnd didn't bubble up from a child
element.on(this.get('constants').get('CSS').TRANSITIONEND, function(ev) {
$(element).on(this.TRANSITIONEND, function(ev) {
if (ev) {
resolve();
}
Expand Down
1 change: 0 additions & 1 deletion app/templates/components/paper-dialog-container.hbs

This file was deleted.

1 change: 0 additions & 1 deletion app/templates/components/paper-dialog-content.hbs

This file was deleted.

3 changes: 2 additions & 1 deletion app/templates/components/paper-dialog.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
}}
{{#paper-dialog-container outsideClicked=(action "outsideClicked")}}
{{#paper-dialog-inner
parent=parent
parentElement=parentElementSelector
origin=origin
openFrom=openFrom
closeTo=closeTo
fullscreen=fullscreen
clickOutsideToClose=clickOutsideToClose
focusOnOpen=focusOnOpen
}}
{{yield}}
{{/paper-dialog-inner}}
Expand Down
22 changes: 11 additions & 11 deletions tests/dummy/app/templates/dialog.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@
</div>
\{{/paper-dialog-content}}

<div class="md-actions" layout="row">
\{{#paper-dialog-actions class="layout-row"}}
<span flex></span>
\{{#paper-button primary=true action=(action "closeDialogWithParent" "cancel")}}Cancel\{{/paper-button}}
\{{#paper-button primary=true action=(action "closeDialogWithParent" "ok")}}OK\{{/paper-button}}
</div>
\{{/paper-dialog-actions}}
</form>
\{{/paper-dialog}}
\{{/if}}
Expand All @@ -73,11 +73,11 @@
</div>
\{{/paper-dialog-content}}

<div class="md-actions" layout="row">
\{{#paper-dialog-actions class="layout-row"}}
<span flex></span>
\{{#paper-button primary=true action=(action "closeDialog" "cancel")}}Cancel\{{/paper-button}}
\{{#paper-button primary=true action=(action "closeDialog" "ok")}}OK\{{/paper-button}}
</div>
\{{/paper-dialog-actions}}
\{{/paper-dialog}}
\{{/if}}

Expand All @@ -93,11 +93,11 @@
</div>
\{{/paper-dialog-content}}

<div class="md-actions" layout="row">
\{{#paper-dialog-actions class="layout-row"}}
<span flex></span>
\{{#paper-button primary=true action=(action "closePromptDialog" "cancel")}}I'm a cat person\{{/paper-button}}
\{{#paper-button primary=true action=(action "closePromptDialog" "ok" dogName)}}Okay!\{{/paper-button}}
</div>
\{{/paper-dialog-actions}}
\{{/paper-dialog}}
\{{/if}}
{{/code-block}}
Expand Down Expand Up @@ -190,11 +190,11 @@
</div>
{{/paper-dialog-content}}

<div class="md-actions" layout="row">
{{#paper-dialog-actions class="layout-row"}}
<span flex></span>
{{#paper-button action=(action "closeDialogWithParent" "cancel")}}Cancel{{/paper-button}}
{{#paper-button action=(action "closeDialogWithParent" "ok")}}OK{{/paper-button}}
</div>
{{/paper-dialog-actions}}
</form>
{{/paper-dialog}}
{{/if}}
Expand All @@ -218,11 +218,11 @@
</div>
{{/paper-dialog-content}}

<div class="md-actions" layout="row">
{{#paper-dialog-actions class="layout-row"}}
<span flex></span>
{{#paper-button action=(action "closeDialog" "cancel")}}Cancel{{/paper-button}}
{{#paper-button action=(action "closeDialog" "ok")}}OK{{/paper-button}}
</div>
{{/paper-dialog-actions}}
{{/paper-dialog}}
{{/if}}

Expand All @@ -232,7 +232,7 @@
{{#paper-dialog-content}}
<h2 class="md-title">What would you name your dog?</h2>
<p>Bowser is a common name.</p>
{{paper-input placeholder="dog name" value=dogName}}
{{paper-input placeholder="dog name" autofocus=true value=dogName}}
<div>
{{#paper-checkbox checked=fullscreen onchange=(action (mut fullscreen))}}Display fullscreen at small window size breakpoints{{/paper-checkbox}}
</div>
Expand Down
Loading

0 comments on commit 05608bf

Please sign in to comment.