2013-07-26 18:32:44 +04:00
|
|
|
/*global window, document, Ghost, $, _, Backbone, JST, shortcut */
|
2013-06-25 16:23:09 +04:00
|
|
|
(function () {
|
2013-06-08 06:16:28 +04:00
|
|
|
"use strict";
|
|
|
|
|
2013-08-02 02:33:06 +04:00
|
|
|
Ghost.TemplateView = Backbone.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 () {
|
|
|
|
if (_.isFunction(this.beforeRender)) {
|
|
|
|
this.beforeRender();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$el.html(this.template(this.templateData()));
|
|
|
|
|
|
|
|
if (_.isFunction(this.afterRender)) {
|
|
|
|
this.afterRender();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Ghost.View = Ghost.TemplateView.extend({
|
2013-06-08 06:16:28 +04:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
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',
|
2013-08-04 22:21:50 +04:00
|
|
|
'MSAnimationEnd .js-notification': 'removeItem',
|
|
|
|
'click .js-notification.notification-passive .close': 'closePassive',
|
|
|
|
'click .js-notification.notification-persistent .close': 'closePersistent'
|
2013-07-22 14:46:23 +04:00
|
|
|
},
|
|
|
|
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
|
|
|
},
|
2013-08-06 11:55:47 +04:00
|
|
|
addItem: function (item) {
|
|
|
|
this.model.push(item);
|
|
|
|
this.renderItem(item);
|
|
|
|
},
|
2013-07-22 14:46:23 +04:00
|
|
|
removeItem: function (e) {
|
|
|
|
e.preventDefault();
|
2013-08-04 22:21:50 +04:00
|
|
|
var self = e.currentTarget;
|
|
|
|
if (self.className.indexOf('notification-persistent') !== -1) {
|
|
|
|
$.ajax({
|
|
|
|
type: "DELETE",
|
|
|
|
url: '/api/v0.1/notifications/' + $(self).find('.close').data('id')
|
|
|
|
}).done(function (result) {
|
|
|
|
$(e.currentTarget).remove();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$(e.currentTarget).remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
closePassive: function (e) {
|
|
|
|
$(e.currentTarget).parent().fadeOut(200, function () { $(this).remove(); });
|
|
|
|
},
|
|
|
|
closePersistent: function (e) {
|
|
|
|
var self = e.currentTarget;
|
|
|
|
$.ajax({
|
|
|
|
type: "DELETE",
|
|
|
|
url: '/api/v0.1/notifications/' + $(self).data('id')
|
|
|
|
}).done(function (result) {
|
|
|
|
if ($(self).parent().parent().hasClass('js-bb-notification')) {
|
|
|
|
$(self).parent().parent().fadeOut(200, function () { $(self).remove(); });
|
|
|
|
} else {
|
|
|
|
$(self).parent().fadeOut(200, function () { $(self).remove(); });
|
|
|
|
}
|
|
|
|
});
|
2013-07-19 03:53:08 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-07-24 14:29:20 +04:00
|
|
|
/**
|
|
|
|
* This is the view to generate the markup for the individual
|
|
|
|
* modal. Will be included into #modals.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Types can be
|
|
|
|
* - (empty)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
Ghost.Views.Modal = Ghost.View.extend({
|
|
|
|
el: '#modal-container',
|
|
|
|
templateName: 'modal',
|
|
|
|
className: 'js-bb-modal',
|
|
|
|
initialize: function () {
|
|
|
|
this.render();
|
2013-07-26 18:32:44 +04:00
|
|
|
var self = this;
|
|
|
|
if (!this.model.options.confirm) {
|
|
|
|
shortcut.add("ESC", function () {
|
|
|
|
self.removeItem();
|
|
|
|
});
|
|
|
|
$(document).on('click', '.modal-background', function (e) {
|
|
|
|
self.removeItem(e);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Initiate functions for buttons here so models don't get tied up.
|
|
|
|
this.acceptModal = function () {
|
|
|
|
this.model.options.confirm.accept.func();
|
2013-08-05 13:16:31 +04:00
|
|
|
self.removeItem();
|
2013-07-26 18:32:44 +04:00
|
|
|
};
|
|
|
|
this.rejectModal = function () {
|
|
|
|
this.model.options.confirm.reject.func();
|
2013-08-05 13:16:31 +04:00
|
|
|
self.removeItem();
|
2013-07-26 18:32:44 +04:00
|
|
|
};
|
|
|
|
shortcut.remove("ESC");
|
|
|
|
$(document).off('click', '.modal-background');
|
|
|
|
}
|
2013-07-24 14:29:20 +04:00
|
|
|
},
|
|
|
|
template: function (data) {
|
|
|
|
return JST[this.templateName](data);
|
|
|
|
},
|
|
|
|
events: {
|
2013-07-26 18:32:44 +04:00
|
|
|
'click .close': 'removeItem',
|
|
|
|
'click .js-button-accept': 'acceptModal',
|
|
|
|
'click .js-button-reject': 'rejectModal'
|
2013-07-24 14:29:20 +04:00
|
|
|
},
|
|
|
|
render: function () {
|
|
|
|
this.$el.html(this.template(this.model));
|
|
|
|
this.$(".modal-content").html(this.addSubview(new Ghost.Views.Modal.ContentView({model: this.model})).render().el);
|
|
|
|
this.$el.children(".js-modal").center();
|
|
|
|
this.$el.addClass("active");
|
|
|
|
if (document.body.style.webkitFilter !== undefined) { // Detect webkit filters
|
|
|
|
$("body").addClass("blur");
|
|
|
|
} else {
|
|
|
|
this.$el.addClass("dark");
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
2013-07-26 18:32:44 +04:00
|
|
|
remove: function () {
|
|
|
|
this.undelegateEvents();
|
|
|
|
this.$el.empty();
|
|
|
|
this.stopListening();
|
|
|
|
return this;
|
|
|
|
},
|
2013-07-24 14:29:20 +04:00
|
|
|
removeItem: function (e) {
|
2013-07-26 18:32:44 +04:00
|
|
|
if (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
$('.js-modal').fadeOut(300, function () {
|
2013-07-24 14:29:20 +04:00
|
|
|
$(this).remove();
|
|
|
|
$("#modal-container").removeClass('active dark');
|
|
|
|
if (document.body.style.filter !== undefined) {
|
|
|
|
$("body").removeClass("blur");
|
|
|
|
}
|
|
|
|
});
|
2013-07-26 18:32:44 +04:00
|
|
|
this.remove();
|
2013-07-24 14:29:20 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modal Content
|
|
|
|
* @type {*}
|
|
|
|
*/
|
|
|
|
Ghost.Views.Modal.ContentView = Ghost.View.extend({
|
|
|
|
|
|
|
|
template: function (data) {
|
|
|
|
return JST['modals/' + this.model.content.template](data);
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function () {
|
|
|
|
this.$el.html(this.template(this.model));
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2013-06-25 16:23:09 +04:00
|
|
|
}());
|