2013-07-11 23:02:18 +04:00
|
|
|
/*global window, document, Ghost, $, _, Backbone, JST */
|
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var ContentList,
|
|
|
|
ContentItem,
|
|
|
|
PreviewContainer,
|
|
|
|
|
|
|
|
// Add shadow during scrolling
|
|
|
|
scrollShadow = function (target, e) {
|
|
|
|
if ($(e.currentTarget).scrollTop() > 10) {
|
|
|
|
$(target).addClass('scrolling');
|
|
|
|
} else {
|
|
|
|
$(target).removeClass('scrolling');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Base view
|
|
|
|
// ----------
|
|
|
|
Ghost.Views.Blog = Ghost.View.extend({
|
|
|
|
initialize: function (options) {
|
|
|
|
this.addSubview(new PreviewContainer({ el: '.js-content-preview', collection: this.collection })).render();
|
|
|
|
this.addSubview(new ContentList({ el: '.js-content-list', collection: this.collection })).render();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Content list (sidebar)
|
|
|
|
// -----------------------
|
|
|
|
ContentList = Ghost.View.extend({
|
|
|
|
|
|
|
|
events: {
|
|
|
|
'click .content-list-content' : 'scrollHandler'
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function (options) {
|
|
|
|
this.$('.content-list-content').on('scroll', _.bind(scrollShadow, null, '.content-list'));
|
|
|
|
this.listenTo(this.collection, 'remove', this.showNext);
|
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
|
// -----------------------
|
|
|
|
ContentItem = Ghost.View.extend({
|
|
|
|
|
|
|
|
tagName: 'li',
|
|
|
|
|
|
|
|
events: {
|
|
|
|
'click a': 'setActiveItem'
|
|
|
|
},
|
|
|
|
|
|
|
|
active: false,
|
|
|
|
|
|
|
|
initialize: function () {
|
|
|
|
this.listenTo(Backbone, 'blog:activeItem', this.checkActive);
|
|
|
|
this.listenTo(this.model, 'destroy', this.removeItem);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeItem: function () {
|
|
|
|
var view = this;
|
|
|
|
$.when(this.$el.slideUp()).then(function () {
|
|
|
|
view.remove();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
showPreview: function (e) {
|
|
|
|
var item = $(e.currentTarget);
|
|
|
|
this.$('.content-list-content li').removeClass('active');
|
|
|
|
item.addClass('active');
|
|
|
|
Backbone.trigger('blog:activeItem', item.data('id'));
|
|
|
|
},
|
|
|
|
|
|
|
|
templateName: "list-item",
|
|
|
|
|
2013-08-02 02:33:06 +04:00
|
|
|
templateData: function () {
|
|
|
|
return _.extend({active: this.active}, this.model.toJSON());
|
2013-07-11 23:02:18 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Content preview
|
|
|
|
// ----------------
|
|
|
|
PreviewContainer = Ghost.View.extend({
|
|
|
|
|
|
|
|
activeId: null,
|
|
|
|
|
|
|
|
events: {
|
|
|
|
'click .post-controls .delete' : 'deletePost',
|
|
|
|
'click .post-controls .post-edit' : 'editPost'
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function (options) {
|
|
|
|
this.listenTo(Backbone, 'blog:activeItem', this.setActivePreview);
|
|
|
|
this.$('.content-preview-content').on('scroll', _.bind(scrollShadow, null, '.content-preview'));
|
|
|
|
},
|
|
|
|
|
|
|
|
setActivePreview: function (id) {
|
|
|
|
if (this.activeId !== id) {
|
|
|
|
this.activeId = id;
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
deletePost: function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
if (window.confirm('Are you sure you want to delete this post?')) {
|
2013-07-22 14:27:01 +04:00
|
|
|
var self = this,
|
|
|
|
title = self.model.get('title');
|
2013-07-19 03:53:08 +04:00
|
|
|
|
|
|
|
self.model.destroy({
|
2013-07-11 23:02:18 +04:00
|
|
|
wait: true
|
2013-07-19 03:53:08 +04:00
|
|
|
}).then(function () {
|
|
|
|
self.addSubview(new Ghost.Views.NotificationCollection({
|
|
|
|
model: [{
|
|
|
|
type: 'success',
|
2013-07-22 14:27:01 +04:00
|
|
|
message: 'Your post: ' + title + ' has been deleted',
|
2013-07-19 03:53:08 +04:00
|
|
|
status: 'passive'
|
|
|
|
}]
|
|
|
|
}));
|
|
|
|
}, function () {
|
|
|
|
self.addSubview(new Ghost.Views.NotificationCollection({
|
|
|
|
model: [{
|
|
|
|
type: 'error',
|
2013-07-22 14:27:01 +04:00
|
|
|
message: 'Your post: ' + title + ' has not been deleted.',
|
2013-07-19 03:53:08 +04:00
|
|
|
status: 'passive'
|
|
|
|
}]
|
|
|
|
}));
|
2013-07-11 23:02:18 +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');
|
|
|
|
},
|
|
|
|
|
|
|
|
templateName: "preview",
|
|
|
|
|
|
|
|
render: function () {
|
|
|
|
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-07-11 23:02:18 +04:00
|
|
|
}
|
|
|
|
this.$('.wrapper').on('click', 'a', function (e) {
|
|
|
|
$(e.currentTarget).attr('target', '_blank');
|
|
|
|
});
|
|
|
|
Ghost.temporary.initToggles(this.$el);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-08-02 02:33:06 +04:00
|
|
|
}());
|