Skip to content
New issue

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

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

Already on GitHub? # to your account

paper-toaster: make duration and position configurable application wide #834

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
28 changes: 21 additions & 7 deletions addon/services/paper-toaster.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
import Ember from 'ember';
const { computed, assign, run, A, Service, tryInvoke, Object: EObject } = Ember;

const DEFAULT_PROPS = {
duration: 3000,
position: 'bottom left'
};
const { computed, assign, run, A, Service, tryInvoke, Object: EObject, getOwner } = Ember;

export default Service.extend({
queue: A(),

activeToast: computed.reads('queue.firstObject'),

defaultsOptions: {
duration: 3000,
position: 'bottom left'
},

init() {
this._super(...arguments);
let ENV = getOwner(this).factoryFor('config:environment').class;
if (ENV['ember-paper'] && ENV['ember-paper']['paper-toaster']) {
let defaultsAppOptions = ENV['ember-paper']['paper-toaster'];
['duration', 'position'].forEach((optKey) => {
let value = defaultsAppOptions[optKey];
if (value) {
this.set(`defaultsOptions.${optKey}`, value);
}
});
}
},

show(text, options) {
let t = EObject.create(assign({ text, show: true }, this.buildOptions(options)));
this.get('queue').pushObject(t);
Expand Down Expand Up @@ -38,6 +52,6 @@ export default Service.extend({
},

buildOptions(options) {
return assign({}, DEFAULT_PROPS, options);
return assign({}, this.get('defaultsOptions'), options);
}
});
6 changes: 6 additions & 0 deletions tests/dummy/app/templates/demo/toast.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@
)
}}

<h3>Application defaults</h3>

<p>You can define a default <code>duration</code> and <code>position</code> for your application by adding in your <code>config/environment.js</code> file: </p>

{{code-snippet name="paper-toaster-defaults.js"}}

{{/paper-card-content}}
</div>

Expand Down
6 changes: 6 additions & 0 deletions tests/dummy/snippets/paper-toaster-defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ENV['ember-paper'] = {
'paper-toaster': {
position: 'bottom right',
duration: 5000
}
};