2013-07-11 23:02:18 +04:00
|
|
|
/*global window, document, Ghost, $, _, Backbone, JST */
|
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
Ghost.View = Backbone.View.extend({
|
|
|
|
|
|
|
|
// Adds a subview to the current view, which will
|
|
|
|
// ensure its removal when this view is removed,
|
|
|
|
// or when view.removeSubviews is called
|
|
|
|
addSubview: function (view) {
|
|
|
|
if (!(view instanceof Backbone.View)) {
|
|
|
|
throw new Error("Subview must be a Backbone.View");
|
|
|
|
}
|
|
|
|
this.subviews = this.subviews || [];
|
|
|
|
this.subviews.push(view);
|
|
|
|
return view;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Removes any subviews associated with this view
|
|
|
|
// by `addSubview`, which will in-turn remove any
|
|
|
|
// children of those views, and so on.
|
|
|
|
removeSubviews: function () {
|
|
|
|
var i, l, children = this.subviews;
|
|
|
|
if (!children) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
for (i = 0, l = children.length; i < l; i += 1) {
|
|
|
|
children[i].remove();
|
|
|
|
}
|
|
|
|
this.subviews = [];
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Extends the view's remove, by calling `removeSubviews`
|
|
|
|
// if any subviews exist.
|
|
|
|
remove: function () {
|
|
|
|
if (this.subviews) {
|
|
|
|
this.removeSubviews();
|
|
|
|
}
|
|
|
|
return Backbone.View.prototype.remove.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
Ghost.TemplateView = Ghost.View.extend({
|
|
|
|
templateName: "widget",
|
|
|
|
|
|
|
|
template: function (data) {
|
|
|
|
return JST[this.templateName](data);
|
|
|
|
},
|
|
|
|
|
|
|
|
templateData: function () {
|
|
|
|
if (this.model) {
|
|
|
|
return this.model.toJSON();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.collection) {
|
|
|
|
return this.collection.toJSON();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function () {
|
|
|
|
this.$el.html(this.template(this.templateData()));
|
|
|
|
|
|
|
|
if (_.isFunction(this.afterRender)) {
|
|
|
|
this.afterRender();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-07-19 03:53:08 +04:00
|
|
|
/**
|
|
|
|
* This is the view to generate the markup for the individual
|
|
|
|
* notification. Will be included into #flashbar.
|
|
|
|
*
|
|
|
|
* States can be
|
|
|
|
* - persistent
|
|
|
|
* - passive
|
|
|
|
*
|
|
|
|
* Types can be
|
|
|
|
* - error
|
|
|
|
* - success
|
|
|
|
* - alert
|
|
|
|
* - (empty)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
Ghost.Views.Notification = Ghost.View.extend({
|
|
|
|
templateName: 'notification',
|
|
|
|
className: 'js-bb-notification',
|
|
|
|
template: function (data) {
|
|
|
|
return JST[this.templateName](data);
|
|
|
|
},
|
2013-07-22 14:46:23 +04:00
|
|
|
render: function () {
|
2013-07-19 03:53:08 +04:00
|
|
|
var html = this.template(this.model);
|
|
|
|
this.$el.html(html);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This handles Notification groups
|
|
|
|
*/
|
|
|
|
Ghost.Views.NotificationCollection = Ghost.View.extend({
|
|
|
|
el: '#flashbar',
|
2013-07-22 14:46:23 +04:00
|
|
|
initialize: function () {
|
2013-07-19 03:53:08 +04:00
|
|
|
this.render();
|
|
|
|
},
|
2013-07-22 14:46:23 +04:00
|
|
|
events: {
|
|
|
|
'animationend .js-notification': 'removeItem',
|
|
|
|
'webkitAnimationEnd .js-notification': 'removeItem',
|
|
|
|
'oanimationend .js-notification': 'removeItem',
|
|
|
|
'MSAnimationEnd .js-notification': 'removeItem'
|
|
|
|
},
|
|
|
|
render: function () {
|
2013-07-19 03:53:08 +04:00
|
|
|
_.each(this.model, function (item) {
|
|
|
|
this.renderItem(item);
|
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
renderItem: function (item) {
|
|
|
|
var itemView = new Ghost.Views.Notification({ model: item });
|
|
|
|
this.$el.html(itemView.render().el);
|
2013-07-22 14:46:23 +04:00
|
|
|
},
|
|
|
|
removeItem: function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
$(e.currentTarget).remove();
|
2013-07-19 03:53:08 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-07-11 23:02:18 +04:00
|
|
|
}());
|