Skip to content

Commit

Permalink
General cleanup of the flash object and component
Browse files Browse the repository at this point in the history
  • Loading branch information
poteto committed Oct 14, 2015
1 parent 6233088 commit 2367d1d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
23 changes: 11 additions & 12 deletions addon/components/flash-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ import layout from '../templates/components/flash-message';
import computed from 'ember-new-computed';

const {
Handlebars,
String: { classify, htmlSafe },
Component,
getWithDefault,
warn,
run,
on,
get,
set,
String: { classify },
Handlebars: { SafeString }
} = Ember;
const {
escapeExpression
} = Handlebars.Utils;
readOnly,
bool
} = computed;

export default Ember.Component.extend({
export default Component.extend({
layout,
classNameBindings: [ 'alertType', 'active', 'exiting' ],
classNameBindings: ['alertType', 'active', 'exiting'],
active: false,
messageStyle: 'bootstrap',
showProgressBar: computed.readOnly('flash.showProgress'),
exiting: computed.readOnly('flash.exiting'),
showProgressBar: readOnly('flash.showProgress'),
exiting: readOnly('flash.exiting'),

alertType: computed('flash.type', {
get() {
Expand Down Expand Up @@ -72,9 +72,8 @@ export default Ember.Component.extend({
}

const duration = getWithDefault(this, 'flash.timeout', 0);
const escapedCSS = escapeExpression(`transition-duration: ${duration}ms`);

return new SafeString(escapedCSS);
return htmlSafe(`transition-duration: ${duration}ms`);
},

set() {
Expand All @@ -100,5 +99,5 @@ export default Ember.Component.extend({
}
},

hasBlock: computed.bool('template').readOnly()
hasBlock: bool('template').readOnly()
});
39 changes: 21 additions & 18 deletions addon/flash/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,34 @@ import customComputed from '../utils/computed';
import computed from 'ember-new-computed';

const {
Object: EmberObject,
run: { later, cancel },
Evented,
on,
run,
get,
set
} = Ember;
const {
readOnly
} = computed;

export default Ember.Object.extend(Evented, {
queue: computed.readOnly('flashService.queue'),
export default EmberObject.extend(Evented, {
queue: readOnly('flashService.queue'),
totalTimeout: customComputed.add('timeout', 'extendedTimeout').readOnly(),
timer: null,
exitTimer: null,
exiting: false,

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

if (get(this, 'sticky')) {
return;
}

this._setTimer('exitTimer', 'exitMessage', get(this, 'timeout'));
this._setTimer('timer', 'destroyMessage', get(this, 'totalTimeout'));
},

destroyMessage() {
const queue = get(this, 'queue');

Expand All @@ -36,7 +50,7 @@ export default Ember.Object.extend(Evented, {
},

willDestroy() {
const timers = [ 'timer', 'exitTimer' ];
const timers = ['timer', 'exitTimer'];

timers.forEach((timer) => {
this._cancelTimer(timer);
Expand All @@ -48,26 +62,15 @@ export default Ember.Object.extend(Evented, {
// private
_guid: customComputed.guidFor('message').readOnly(),

_setInitialState: on('init', function() {
if (get(this, 'sticky')) {
return;
}

this._setTimer('exitTimer', 'exitMessage', get(this, 'timeout'));
this._setTimer('timer', 'destroyMessage', get(this, 'totalTimeout'));
}),

_setTimer(name, methodName, timeout) {
const timer = run.later(this, methodName, timeout);

set(this, name, timer);
set(this, name, later(this, methodName, timeout));
},

_cancelTimer(name) {
const timer = get(this, name);

if (timer) {
run.cancel(timer);
cancel(timer);
set(this, name, null);
}
}
Expand Down
3 changes: 1 addition & 2 deletions app/initializers/flash-messages.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import config from '../config/environment';

export function initialize() {
let application = arguments[1] || arguments[0];

const application = arguments[1] || arguments[0];
const { flashMessageDefaults } = config;
const { injectionFactories } = flashMessageDefaults;

Expand Down

0 comments on commit 2367d1d

Please sign in to comment.