Refactor notifications.

This commit is contained in:
Lars Jung 2015-04-29 00:57:58 +02:00
parent 6500b624f5
commit 73496c6b7c
6 changed files with 40 additions and 39 deletions

View file

@ -1,4 +1,4 @@
#notify { #notification {
position: fixed; position: fixed;
left: 50%; left: 50%;
width: 200px; width: 200px;
@ -9,4 +9,5 @@
background: rgba(0,0,0,0.2); background: rgba(0,0,0,0.2);
border-radius: 0 0 4px 4px; border-radius: 0 0 4px 4px;
text-align: center; text-align: center;
overflow: hidden;
} }

View file

@ -1,4 +1,4 @@
modulejs.define('core/location', ['_', 'modernizr', 'core/event', 'core/notify', 'core/settings'], function (_, modernizr, event, notify, allsettings) { modulejs.define('core/location', ['_', 'modernizr', 'core/event', 'core/settings', 'view/notification'], function (_, modernizr, event, allsettings, notification) {
var settings = _.extend({ var settings = _.extend({
fastBrowsing: true, fastBrowsing: true,
@ -135,10 +135,10 @@ modulejs.define('core/location', ['_', 'modernizr', 'core/event', 'core/notify',
event.pub('location.changed', item); event.pub('location.changed', item);
refresh(); refresh();
} else { } else {
notify.set('loading...'); notification.set('loading...');
load(function () { load(function () {
item.isLoaded = true; item.isLoaded = true;
notify.set(); notification.set();
event.pub('location.changed', item); event.pub('location.changed', item);
}); });
} }

View file

@ -1,19 +0,0 @@
modulejs.define('core/notify', ['$'], function ($) {
var template = '<div id="notify"/>';
function set(content) {
if (content) {
$('#notify').stop(true, true).html(content).fadeIn(400);
} else {
$('#notify').stop(true, true).fadeOut(400);
}
}
$(template).hide().appendTo('body');
return {
set: set
};
});

View file

@ -0,0 +1,19 @@
modulejs.define('view/notification', ['$'], function ($) {
var template = '<div id="notification"/>';
function set(content) {
if (content) {
$('#notification').stop(true, true).html(content).fadeIn(400);
} else {
$('#notification').stop(true, true).fadeOut(400);
}
}
$(template).hide().appendTo('body');
return {
set: set
};
});

View file

@ -2,7 +2,7 @@
'use strict'; 'use strict';
var ID = 'core/location'; var ID = 'core/location';
var DEPS = ['_', 'modernizr', 'core/event', 'core/notify', 'core/settings']; var DEPS = ['_', 'modernizr', 'core/event', 'core/settings', 'view/notification'];
describe('module \'' + ID + '\'', function () { describe('module \'' + ID + '\'', function () {
@ -21,16 +21,16 @@ describe('module \'' + ID + '\'', function () {
pub: sinon.stub(), pub: sinon.stub(),
sub: sinon.stub() sub: sinon.stub()
}; };
this.xNotify = { this.xNotification = {
set: sinon.stub() set: sinon.stub()
}; };
this.applyFn = function () { this.applyFn = function () {
this.xEvent.pub.reset(); this.xEvent.pub.reset();
this.xEvent.sub.reset(); this.xEvent.sub.reset();
this.xNotify.set.reset(); this.xNotification.set.reset();
return this.definition.fn(_, this.xModernizr, this.xEvent, this.xNotify, this.xSettings); return this.definition.fn(_, this.xModernizr, this.xEvent, this.xSettings, this.xNotification);
}; };
}); });

View file

@ -1,7 +1,7 @@
(function () { (function () {
'use strict'; 'use strict';
var ID = 'core/notify'; var ID = 'view/notification';
var DEPS = ['$']; var DEPS = ['$'];
describe('module \'' + ID + '\'', function () { describe('module \'' + ID + '\'', function () {
@ -71,9 +71,9 @@ describe('module \'' + ID + '\'', function () {
it('adds HTML', function () { it('adds HTML', function () {
this.applyFn(); this.applyFn();
assert.lengthOf($('#notify'), 1); assert.lengthOf($('#notification'), 1);
assert.lengthOf($('#notify:visible'), 0); assert.lengthOf($('#notification:visible'), 0);
assert.strictEqual($('#notify').text(), ''); assert.strictEqual($('#notification').text(), '');
}); });
}); });
@ -90,20 +90,20 @@ describe('module \'' + ID + '\'', function () {
var instance = this.applyFn(); var instance = this.applyFn();
instance.set(); instance.set();
assert.lengthOf($('#notify:visible'), 0); assert.lengthOf($('#notification:visible'), 0);
assert.strictEqual($('#notify').text(), ''); assert.strictEqual($('#notification').text(), '');
instance.set('hello'); instance.set('hello');
assert.lengthOf($('#notify:visible'), 1); assert.lengthOf($('#notification:visible'), 1);
assert.strictEqual($('#notify').text(), 'hello'); assert.strictEqual($('#notification').text(), 'hello');
instance.set('world'); instance.set('world');
assert.lengthOf($('#notify:visible'), 1); assert.lengthOf($('#notification:visible'), 1);
assert.strictEqual($('#notify').text(), 'world'); assert.strictEqual($('#notification').text(), 'world');
instance.set(); instance.set();
// assert.lengthOf($('#notify:visible'), 0); // assert.lengthOf($('#notification:visible'), 0);
assert.strictEqual($('#notify').text(), 'world'); assert.strictEqual($('#notification').text(), 'world');
}); });
}); });
}); });