2013-06-25 16:23:09 +04:00
|
|
|
/*global window, document, Ghost, $, _, Backbone, JST */
|
2013-05-31 09:58:20 +04:00
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
2013-06-02 03:45:02 +04:00
|
|
|
var ContentList,
|
|
|
|
ContentItem,
|
2013-09-04 20:21:50 +04:00
|
|
|
PreviewContainer;
|
2013-06-02 03:45:02 +04:00
|
|
|
|
2013-05-31 09:58:20 +04:00
|
|
|
// Base view
|
|
|
|
// ----------
|
2013-06-04 16:38:45 +04:00
|
|
|
Ghost.Views.Blog = Ghost.View.extend({
|
2013-05-31 09:58:20 +04:00
|
|
|
initialize: function (options) {
|
2013-06-02 03:45:02 +04:00
|
|
|
this.addSubview(new PreviewContainer({ el: '.js-content-preview', collection: this.collection })).render();
|
|
|
|
this.addSubview(new ContentList({ el: '.js-content-list', collection: this.collection })).render();
|
2013-05-31 09:58:20 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Content list (sidebar)
|
|
|
|
// -----------------------
|
2013-06-04 16:38:45 +04:00
|
|
|
ContentList = Ghost.View.extend({
|
2013-06-02 03:45:02 +04:00
|
|
|
|
|
|
|
events: {
|
|
|
|
'click .content-list-content' : 'scrollHandler'
|
|
|
|
},
|
|
|
|
|
2013-05-31 09:58:20 +04:00
|
|
|
initialize: function (options) {
|
2013-09-04 20:21:50 +04:00
|
|
|
this.$('.content-list-content').scrollClass({target: '.content-list', offset: 10});
|
2013-06-02 03:45:02 +04:00
|
|
|
this.listenTo(this.collection, 'remove', this.showNext);
|
2013-05-31 09:58:20 +04:00
|
|
|
},
|
|
|
|
|
2013-06-02 03:45:02 +04:00
|
|
|
showNext: function () {
|
|
|
|
var id = this.collection.at(0).id;
|
|
|
|
if (id) {
|
|
|
|
Backbone.trigger('blog:activeItem', id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function () {
|
|
|
|
this.collection.each(function (model) {
|
|
|
|
this.$('ol').append(this.addSubview(new ContentItem({model: model})).render().el);
|
|
|
|
}, this);
|
|
|
|
this.showNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// Content Item
|
|
|
|
// -----------------------
|
2013-06-04 16:38:45 +04:00
|
|
|
ContentItem = Ghost.View.extend({
|
2013-06-02 03:45:02 +04:00
|
|
|
|
|
|
|
tagName: 'li',
|
|
|
|
|
2013-05-31 09:58:20 +04:00
|
|
|
events: {
|
2013-06-02 03:45:02 +04:00
|
|
|
'click a': 'setActiveItem'
|
|
|
|
},
|
|
|
|
|
|
|
|
active: false,
|
|
|
|
|
|
|
|
initialize: function () {
|
|
|
|
this.listenTo(Backbone, 'blog:activeItem', this.checkActive);
|
|
|
|
this.listenTo(this.model, 'destroy', this.removeItem);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeItem: function () {
|
2013-08-20 22:52:44 +04:00
|
|
|
var self = this;
|
2013-06-02 03:45:02 +04:00
|
|
|
$.when(this.$el.slideUp()).then(function () {
|
2013-08-20 22:52:44 +04:00
|
|
|
self.remove();
|
2013-06-02 03:45:02 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// If the current item isn't active, we trigger the event to
|
|
|
|
// notify a change in which item we're viewing.
|
|
|
|
setActiveItem: function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
if (this.active !== true) {
|
|
|
|
Backbone.trigger('blog:activeItem', this.model.id);
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Checks whether this item is active and doesn't match the current id.
|
|
|
|
checkActive: function (id) {
|
|
|
|
if (this.model.id !== id) {
|
|
|
|
if (this.active) {
|
|
|
|
this.active = false;
|
|
|
|
this.$el.removeClass('active');
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.active = true;
|
|
|
|
this.$el.addClass('active');
|
|
|
|
}
|
2013-05-31 09:58:20 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
showPreview: function (e) {
|
|
|
|
var item = $(e.currentTarget);
|
|
|
|
this.$('.content-list-content li').removeClass('active');
|
|
|
|
item.addClass('active');
|
2013-06-02 03:45:02 +04:00
|
|
|
Backbone.trigger('blog:activeItem', item.data('id'));
|
|
|
|
},
|
|
|
|
|
2013-06-22 19:50:59 +04:00
|
|
|
templateName: "list-item",
|
|
|
|
|
2013-08-02 02:33:06 +04:00
|
|
|
templateData: function () {
|
|
|
|
return _.extend({active: this.active}, this.model.toJSON());
|
2013-05-31 09:58:20 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Content preview
|
|
|
|
// ----------------
|
2013-06-04 16:38:45 +04:00
|
|
|
PreviewContainer = Ghost.View.extend({
|
2013-06-02 03:45:02 +04:00
|
|
|
|
|
|
|
activeId: null,
|
2013-05-31 09:58:20 +04:00
|
|
|
|
|
|
|
events: {
|
|
|
|
'click .post-controls .delete' : 'deletePost',
|
|
|
|
'click .post-controls .post-edit' : 'editPost'
|
|
|
|
},
|
|
|
|
|
2013-06-02 03:45:02 +04:00
|
|
|
initialize: function (options) {
|
|
|
|
this.listenTo(Backbone, 'blog:activeItem', this.setActivePreview);
|
|
|
|
},
|
|
|
|
|
|
|
|
setActivePreview: function (id) {
|
|
|
|
if (this.activeId !== id) {
|
|
|
|
this.activeId = id;
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-05-31 09:58:20 +04:00
|
|
|
deletePost: function (e) {
|
|
|
|
e.preventDefault();
|
2013-09-07 19:07:47 +04:00
|
|
|
var self = this;
|
2013-08-05 13:16:31 +04:00
|
|
|
this.addSubview(new Ghost.Views.Modal({
|
|
|
|
model: {
|
|
|
|
options: {
|
|
|
|
close: false,
|
|
|
|
confirm: {
|
|
|
|
accept: {
|
|
|
|
func: function () {
|
|
|
|
self.model.destroy({
|
|
|
|
wait: true
|
|
|
|
}).then(function () {
|
2013-08-06 11:55:47 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'success',
|
2013-09-07 19:07:47 +04:00
|
|
|
message: 'Your post has been deleted.',
|
2013-08-06 11:55:47 +04:00
|
|
|
status: 'passive'
|
|
|
|
});
|
2013-08-05 13:16:31 +04:00
|
|
|
}, function () {
|
2013-08-06 11:55:47 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'error',
|
2013-09-07 19:07:47 +04:00
|
|
|
message: 'Your post could not be deleted. Please try again.',
|
2013-08-06 11:55:47 +04:00
|
|
|
status: 'passive'
|
|
|
|
});
|
2013-08-05 13:16:31 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
text: "Yes"
|
|
|
|
},
|
|
|
|
reject: {
|
|
|
|
func: function () {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
text: "No"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
type: "action",
|
2013-09-06 18:36:16 +04:00
|
|
|
style: ["wide", "centered"],
|
2013-08-28 17:23:36 +04:00
|
|
|
animation: 'fade'
|
2013-08-05 13:16:31 +04:00
|
|
|
},
|
|
|
|
content: {
|
|
|
|
template: 'blank',
|
|
|
|
title: 'Are you sure you want to delete this post?'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
2013-05-31 09:58:20 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
editPost: function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
// for now this will disable "open in new tab", but when we have a Router implemented
|
|
|
|
// it can go back to being a normal link to '#/ghost/editor/X'
|
|
|
|
window.location = '/ghost/editor/' + this.model.get('id');
|
|
|
|
},
|
|
|
|
|
2013-06-22 19:50:59 +04:00
|
|
|
templateName: "preview",
|
|
|
|
|
2013-05-31 09:58:20 +04:00
|
|
|
render: function () {
|
2013-06-02 03:45:02 +04:00
|
|
|
if (this.activeId) {
|
|
|
|
this.model = this.collection.get(this.activeId);
|
2013-08-02 02:33:06 +04:00
|
|
|
this.$el.html(this.template(this.templateData()));
|
2013-06-02 03:45:02 +04:00
|
|
|
}
|
2013-09-04 20:21:50 +04:00
|
|
|
this.$('.content-preview-content').scrollClass({target: '.content-preview', offset: 10});
|
2013-06-02 03:45:02 +04:00
|
|
|
this.$('.wrapper').on('click', 'a', function (e) {
|
|
|
|
$(e.currentTarget).attr('target', '_blank');
|
|
|
|
});
|
|
|
|
Ghost.temporary.initToggles(this.$el);
|
|
|
|
return this;
|
2013-05-31 09:58:20 +04:00
|
|
|
}
|
|
|
|
|
2013-06-02 03:45:02 +04:00
|
|
|
});
|
2013-05-31 09:58:20 +04:00
|
|
|
|
2013-08-02 02:33:06 +04:00
|
|
|
}());
|