2013-08-28 17:23:36 +04:00
|
|
|
/*global window, document, setTimeout, Ghost, $, _, Backbone, JST, shortcut */
|
2013-07-11 23:02:18 +04:00
|
|
|
(function () {
|
|
|
|
"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-07-11 23:02:18 +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 () {
|
2013-08-16 03:22:08 +04:00
|
|
|
var children = this.subviews;
|
|
|
|
|
2013-07-11 23:02:18 +04:00
|
|
|
if (!children) {
|
|
|
|
return this;
|
|
|
|
}
|
2013-08-16 03:22:08 +04:00
|
|
|
|
|
|
|
_(children).invoke("remove");
|
|
|
|
|
2013-07-11 23:02:18 +04:00
|
|
|
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-08-20 22:52:44 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Ghost.Views.Utils = {
|
2013-08-16 03:22:08 +04:00
|
|
|
|
|
|
|
// Used in API request fail handlers to parse a standard api error
|
|
|
|
// response json for the message to display
|
|
|
|
getRequestErrorMessage: function (request) {
|
2013-09-16 12:16:42 +04:00
|
|
|
var message,
|
|
|
|
msgDetail;
|
2013-08-16 03:22:08 +04:00
|
|
|
|
|
|
|
// Can't really continue without a request
|
|
|
|
if (!request) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seems like a sensible default
|
|
|
|
message = request.statusText;
|
|
|
|
|
|
|
|
// If a non 200 response
|
|
|
|
if (request.status !== 200) {
|
|
|
|
try {
|
|
|
|
// Try to parse out the error, or default to "Unknown"
|
|
|
|
message = request.responseJSON.error || "Unknown Error";
|
|
|
|
} catch (e) {
|
2013-09-16 12:16:42 +04:00
|
|
|
msgDetail = request.status ? request.status + " - " + request.statusText : "Server was not available";
|
|
|
|
message = "The server returned an error (" + msgDetail + ").";
|
2013-08-16 03:22:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
2013-08-20 03:40:09 +04:00
|
|
|
},
|
2013-07-11 23:02:18 +04:00
|
|
|
|
2013-08-20 03:40:09 +04:00
|
|
|
// Getting URL vars
|
|
|
|
getUrlVariables: function () {
|
|
|
|
var vars = [],
|
|
|
|
hash,
|
|
|
|
hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'),
|
|
|
|
i;
|
|
|
|
|
|
|
|
for (i = 0; i < hashes.length; i += 1) {
|
|
|
|
hash = hashes[i].split('=');
|
|
|
|
vars.push(hash[0]);
|
|
|
|
vars[hash[0]] = hash[1];
|
|
|
|
}
|
|
|
|
return vars;
|
|
|
|
}
|
2013-08-20 22:52:44 +04:00
|
|
|
};
|
2013-07-11 23:02:18 +04:00
|
|
|
|
2013-07-19 03:53:08 +04:00
|
|
|
/**
|
|
|
|
* This is the view to generate the markup for the individual
|
2013-09-16 12:46:29 +04:00
|
|
|
* notification. Will be included into #notifications.
|
2013-07-19 03:53:08 +04:00
|
|
|
*
|
|
|
|
* 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({
|
2013-09-16 12:46:29 +04:00
|
|
|
el: '#notifications',
|
2013-07-22 14:46:23 +04:00
|
|
|
initialize: function () {
|
2013-08-10 04:12:54 +04:00
|
|
|
var self = this;
|
2013-07-19 03:53:08 +04:00
|
|
|
this.render();
|
2013-08-21 14:50:49 +04:00
|
|
|
Ghost.on('urlchange', function () {
|
2013-08-10 04:12:54 +04:00
|
|
|
self.clearEverything();
|
|
|
|
});
|
2013-07-19 03:53:08 +04:00
|
|
|
},
|
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) {
|
2013-08-29 14:06:16 +04:00
|
|
|
var itemView = new Ghost.Views.Notification({ model: item }),
|
|
|
|
height,
|
2013-09-17 07:43:18 +04:00
|
|
|
$notification = $(itemView.render().el);
|
|
|
|
|
|
|
|
this.$el.append($notification);
|
|
|
|
height = $notification.hide().outerHeight(true);
|
|
|
|
$notification.animate({height: height}, 250, function () {
|
|
|
|
$(this)
|
|
|
|
.css({height: "auto"})
|
|
|
|
.fadeIn(250);
|
2013-08-29 14:06:16 +04:00
|
|
|
});
|
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-08-10 04:12:54 +04:00
|
|
|
clearEverything: function () {
|
2013-08-29 14:06:16 +04:00
|
|
|
var height = this.$('.js-notification').outerHeight(true),
|
|
|
|
self = this;
|
|
|
|
this.$el.css({height: height});
|
|
|
|
this.$el.find('.js-notification.notification-passive').fadeOut(250, function () {
|
|
|
|
$(this).remove();
|
|
|
|
self.$el.slideUp(250, function () {
|
|
|
|
$(this).show().css({height: "auto"});
|
|
|
|
});
|
|
|
|
});
|
2013-08-10 04:12:54 +04:00
|
|
|
},
|
2013-07-22 14:46:23 +04:00
|
|
|
removeItem: function (e) {
|
|
|
|
e.preventDefault();
|
2013-08-29 14:06:16 +04:00
|
|
|
var self = e.currentTarget,
|
|
|
|
bbSelf = this;
|
2013-08-04 22:21:50 +04:00
|
|
|
if (self.className.indexOf('notification-persistent') !== -1) {
|
|
|
|
$.ajax({
|
|
|
|
type: "DELETE",
|
|
|
|
url: '/api/v0.1/notifications/' + $(self).find('.close').data('id')
|
|
|
|
}).done(function (result) {
|
2013-08-29 14:06:16 +04:00
|
|
|
bbSelf.$el.slideUp(250, function () {
|
|
|
|
$(this).show().css({height: "auto"});
|
|
|
|
$(self).remove();
|
|
|
|
});
|
2013-08-04 22:21:50 +04:00
|
|
|
});
|
|
|
|
} else {
|
2013-09-17 07:43:18 +04:00
|
|
|
$(self).slideUp(250, function () {
|
|
|
|
$(this)
|
|
|
|
.show()
|
|
|
|
.css({height: "auto"})
|
|
|
|
.remove();
|
2013-08-29 14:06:16 +04:00
|
|
|
});
|
2013-08-04 22:21:50 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
closePassive: function (e) {
|
2013-09-17 07:43:18 +04:00
|
|
|
$(e.currentTarget)
|
|
|
|
.parent()
|
|
|
|
.fadeOut(250)
|
|
|
|
.slideUp(250, function () {
|
|
|
|
$(this).remove();
|
2013-08-29 14:06:16 +04:00
|
|
|
});
|
2013-08-04 22:21:50 +04:00
|
|
|
},
|
|
|
|
closePersistent: function (e) {
|
2013-08-29 14:06:16 +04:00
|
|
|
var self = e.currentTarget,
|
|
|
|
bbSelf = this;
|
2013-08-04 22:21:50 +04:00
|
|
|
$.ajax({
|
|
|
|
type: "DELETE",
|
|
|
|
url: '/api/v0.1/notifications/' + $(self).data('id')
|
|
|
|
}).done(function (result) {
|
2013-08-29 14:06:16 +04:00
|
|
|
var height = bbSelf.$('.js-notification').outerHeight(true),
|
|
|
|
$parent = $(self).parent();
|
|
|
|
bbSelf.$el.css({height: height});
|
|
|
|
|
|
|
|
if ($parent.parent().hasClass('js-bb-notification')) {
|
|
|
|
$parent.parent().fadeOut(200, function () {
|
|
|
|
$(this).remove();
|
|
|
|
bbSelf.$el.slideUp(250, function () {
|
|
|
|
$(this).show().css({height: "auto"});
|
|
|
|
});
|
|
|
|
});
|
2013-08-04 22:21:50 +04:00
|
|
|
} else {
|
2013-08-29 14:06:16 +04:00
|
|
|
$parent.fadeOut(200, function () {
|
|
|
|
$(this).remove();
|
|
|
|
bbSelf.$el.slideUp(250, function () {
|
|
|
|
$(this).show().css({height: "auto"});
|
|
|
|
});
|
|
|
|
});
|
2013-08-04 22:21:50 +04:00
|
|
|
}
|
|
|
|
});
|
2013-07-19 03:53:08 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-08-13 14:53:49 +04:00
|
|
|
// ## Modals
|
2013-07-24 14:29:20 +04:00
|
|
|
Ghost.Views.Modal = Ghost.View.extend({
|
|
|
|
el: '#modal-container',
|
|
|
|
templateName: 'modal',
|
|
|
|
className: 'js-bb-modal',
|
2013-08-13 14:53:49 +04:00
|
|
|
// Render and manages modal dismissal
|
2013-07-24 14:29:20 +04:00
|
|
|
initialize: function () {
|
|
|
|
this.render();
|
2013-07-26 18:32:44 +04:00
|
|
|
var self = this;
|
|
|
|
if (!this.model.options.confirm) {
|
|
|
|
shortcut.add("ESC", function () {
|
2013-08-13 14:53:49 +04:00
|
|
|
self.removeElement();
|
2013-07-26 18:32:44 +04:00
|
|
|
});
|
|
|
|
$(document).on('click', '.modal-background', function (e) {
|
2013-08-13 14:53:49 +04:00
|
|
|
self.removeElement(e);
|
2013-07-26 18:32:44 +04:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Initiate functions for buttons here so models don't get tied up.
|
|
|
|
this.acceptModal = function () {
|
2013-08-31 23:10:17 +04:00
|
|
|
this.model.options.confirm.accept.func.call(this);
|
2013-08-13 14:53:49 +04:00
|
|
|
self.removeElement();
|
2013-07-26 18:32:44 +04:00
|
|
|
};
|
|
|
|
this.rejectModal = function () {
|
2013-08-31 23:10:17 +04:00
|
|
|
this.model.options.confirm.reject.func.call(this);
|
2013-08-13 14:53:49 +04:00
|
|
|
self.removeElement();
|
2013-07-26 18:32:44 +04:00
|
|
|
};
|
|
|
|
shortcut.remove("ESC");
|
|
|
|
$(document).off('click', '.modal-background');
|
|
|
|
}
|
2013-07-24 14:29:20 +04:00
|
|
|
},
|
2013-08-13 14:53:49 +04:00
|
|
|
templateData: function () {
|
|
|
|
return this.model;
|
2013-07-24 14:29:20 +04:00
|
|
|
},
|
|
|
|
events: {
|
2013-08-13 14:53:49 +04:00
|
|
|
'click .close': 'removeElement',
|
2013-07-26 18:32:44 +04:00
|
|
|
'click .js-button-accept': 'acceptModal',
|
|
|
|
'click .js-button-reject': 'rejectModal'
|
2013-07-24 14:29:20 +04:00
|
|
|
},
|
2013-08-13 14:53:49 +04:00
|
|
|
afterRender: function () {
|
2013-07-24 14:29:20 +04:00
|
|
|
this.$(".modal-content").html(this.addSubview(new Ghost.Views.Modal.ContentView({model: this.model})).render().el);
|
2013-08-20 14:55:44 +04:00
|
|
|
this.$el.children(".js-modal").center({animate: false}).css("max-height", $(window).height() - 120); // same as resize(), but the debounce causes init lag
|
2013-08-13 14:53:49 +04:00
|
|
|
this.$el.addClass("active dark");
|
|
|
|
|
2013-07-24 14:29:20 +04:00
|
|
|
if (document.body.style.webkitFilter !== undefined) { // Detect webkit filters
|
|
|
|
$("body").addClass("blur");
|
|
|
|
}
|
2013-08-31 23:10:17 +04:00
|
|
|
if (_.isFunction(this.model.options.afterRender)) {
|
|
|
|
this.model.options.afterRender.call(this);
|
|
|
|
}
|
2013-08-28 17:23:36 +04:00
|
|
|
if (this.model.options.animation) {
|
|
|
|
this.animate(this.$el.children(".js-modal"));
|
|
|
|
}
|
2013-08-13 14:53:49 +04:00
|
|
|
var self = this;
|
|
|
|
$(window).on('resize', self.resize);
|
|
|
|
|
2013-07-24 14:29:20 +04:00
|
|
|
},
|
2013-08-13 14:53:49 +04:00
|
|
|
// #### resize
|
|
|
|
// Center and resize modal based on window height
|
|
|
|
resize: _.debounce(function () {
|
|
|
|
$(".js-modal").center().css("max-height", $(window).height() - 120);
|
|
|
|
}, 50),
|
|
|
|
// #### remove
|
|
|
|
// Removes Backbone attachments from modals
|
2013-07-26 18:32:44 +04:00
|
|
|
remove: function () {
|
|
|
|
this.undelegateEvents();
|
|
|
|
this.$el.empty();
|
|
|
|
this.stopListening();
|
|
|
|
return this;
|
|
|
|
},
|
2013-08-13 14:53:49 +04:00
|
|
|
// #### removeElement
|
|
|
|
// Visually removes the modal from the user interface
|
|
|
|
removeElement: function (e) {
|
2013-07-26 18:32:44 +04:00
|
|
|
if (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
2013-08-13 14:53:49 +04:00
|
|
|
|
2013-08-28 17:23:36 +04:00
|
|
|
var self = this,
|
|
|
|
$jsModal = $('.js-modal'),
|
|
|
|
removeModalDelay = $jsModal.transitionDuration(),
|
|
|
|
removeBackgroundDelay = self.$el.transitionDuration();
|
|
|
|
|
|
|
|
$jsModal.removeClass('in');
|
|
|
|
|
|
|
|
if (!this.model.options.animation) {
|
|
|
|
removeModalDelay = removeBackgroundDelay = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
|
2013-07-24 14:29:20 +04:00
|
|
|
if (document.body.style.filter !== undefined) {
|
|
|
|
$("body").removeClass("blur");
|
|
|
|
}
|
2013-08-28 17:23:36 +04:00
|
|
|
self.$el.removeClass('dark');
|
2013-08-13 14:53:49 +04:00
|
|
|
|
2013-08-28 17:23:36 +04:00
|
|
|
setTimeout(function () {
|
|
|
|
self.remove();
|
|
|
|
self.$el.removeClass('active');
|
|
|
|
}, removeBackgroundDelay);
|
|
|
|
}, removeModalDelay);
|
|
|
|
|
|
|
|
},
|
|
|
|
// #### animate
|
|
|
|
// Animates the animation
|
|
|
|
animate: function (target) {
|
|
|
|
setTimeout(function () {
|
|
|
|
target.addClass('in');
|
|
|
|
}, target.transitionDuration());
|
2013-07-24 14:29:20 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-08-13 14:53:49 +04:00
|
|
|
// ## Modal Content
|
2013-07-24 14:29:20 +04:00
|
|
|
Ghost.Views.Modal.ContentView = Ghost.View.extend({
|
|
|
|
|
|
|
|
template: function (data) {
|
|
|
|
return JST['modals/' + this.model.content.template](data);
|
|
|
|
},
|
2013-08-13 14:53:49 +04:00
|
|
|
templateData: function () {
|
|
|
|
return this.model;
|
2013-07-24 14:29:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2013-07-11 23:02:18 +04:00
|
|
|
}());
|