2013-06-25 16:23:09 +04:00
|
|
|
/*global window, document, Ghost, $, _, Backbone */
|
2013-06-08 09:05:40 +04:00
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Settings = {};
|
|
|
|
|
|
|
|
// Base view
|
|
|
|
// ----------
|
|
|
|
Ghost.Views.Settings = Ghost.View.extend({
|
|
|
|
initialize: function (options) {
|
2013-07-15 21:09:10 +04:00
|
|
|
$(".settings-content").removeClass('active');
|
2013-06-08 09:05:40 +04:00
|
|
|
this.addSubview(new Settings.Sidebar({
|
|
|
|
el: '.settings-sidebar',
|
|
|
|
pane: options.pane,
|
|
|
|
model: this.model
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Sidebar (tabs)
|
|
|
|
// ---------------
|
|
|
|
Settings.Sidebar = Ghost.View.extend({
|
|
|
|
initialize: function (options) {
|
2013-08-02 02:33:06 +04:00
|
|
|
this.render();
|
2013-06-08 09:05:40 +04:00
|
|
|
this.menu = this.$('.settings-menu');
|
|
|
|
this.showContent(options.pane || 'general');
|
|
|
|
},
|
|
|
|
|
2013-08-05 21:26:44 +04:00
|
|
|
models: {},
|
|
|
|
|
2013-06-08 09:05:40 +04:00
|
|
|
events: {
|
|
|
|
'click .settings-menu li' : 'switchPane'
|
|
|
|
},
|
|
|
|
|
|
|
|
switchPane: function (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var item = $(e.currentTarget),
|
|
|
|
id = item.find('a').attr('href').substring(1);
|
|
|
|
this.showContent(id);
|
|
|
|
},
|
|
|
|
|
|
|
|
showContent: function (id) {
|
2013-08-05 21:26:44 +04:00
|
|
|
var self = this,
|
|
|
|
model;
|
|
|
|
|
2013-06-08 09:05:40 +04:00
|
|
|
Backbone.history.navigate('/settings/' + id);
|
2013-08-02 02:33:06 +04:00
|
|
|
if (this.pane && id === this.pane.el.id) {
|
2013-06-08 09:05:40 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_.result(this.pane, 'destroy');
|
|
|
|
this.setActive(id);
|
2013-08-05 21:26:44 +04:00
|
|
|
this.pane = new Settings[id]({ el: '.settings-content'});
|
|
|
|
|
|
|
|
if (!this.models.hasOwnProperty(this.pane.options.modelType)) {
|
|
|
|
model = this.models[this.pane.options.modelType] = new Ghost.Models[this.pane.options.modelType]();
|
|
|
|
model.fetch().then(function () {
|
|
|
|
self.renderPane(model);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
model = this.models[this.pane.options.modelType];
|
|
|
|
self.renderPane(model);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
renderPane: function (model) {
|
|
|
|
this.pane.model = model;
|
2013-06-08 09:05:40 +04:00
|
|
|
this.pane.render();
|
|
|
|
},
|
|
|
|
|
|
|
|
setActive: function (id) {
|
|
|
|
this.menu.find('li').removeClass('active');
|
|
|
|
this.menu.find('a[href=#' + id + ']').parent().addClass('active');
|
2013-08-02 02:33:06 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
templateName: 'settings/sidebar'
|
2013-06-08 09:05:40 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
// Content panes
|
|
|
|
// --------------
|
|
|
|
Settings.Pane = Ghost.View.extend({
|
2013-08-05 21:26:44 +04:00
|
|
|
options: {
|
|
|
|
modelType: 'Settings'
|
|
|
|
},
|
2013-06-08 09:05:40 +04:00
|
|
|
destroy: function () {
|
|
|
|
this.$el.removeClass('active');
|
2013-06-24 01:25:34 +04:00
|
|
|
this.undelegateEvents();
|
2013-06-08 09:05:40 +04:00
|
|
|
},
|
|
|
|
|
2013-08-02 02:33:06 +04:00
|
|
|
afterRender: function () {
|
|
|
|
this.$el.attr('id', this.id);
|
2013-06-08 09:05:40 +04:00
|
|
|
this.$el.addClass('active');
|
2013-08-06 23:27:56 +04:00
|
|
|
|
|
|
|
this.$('input').iCheck({
|
|
|
|
checkboxClass: 'icheckbox_ghost'
|
|
|
|
});
|
2013-08-05 21:26:44 +04:00
|
|
|
},
|
|
|
|
saveSuccess: function () {
|
2013-08-06 11:55:47 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'success',
|
|
|
|
message: 'Saved',
|
|
|
|
status: 'passive'
|
|
|
|
});
|
2013-08-05 21:26:44 +04:00
|
|
|
|
|
|
|
},
|
|
|
|
saveError: function () {
|
2013-08-06 11:55:47 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'error',
|
|
|
|
message: 'Something went wrong, not saved :(',
|
|
|
|
status: 'passive'
|
|
|
|
});
|
2013-06-08 09:05:40 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: use some kind of data-binding for forms
|
|
|
|
|
|
|
|
// ### General settings
|
|
|
|
Settings.general = Settings.Pane.extend({
|
2013-08-02 02:33:06 +04:00
|
|
|
id: "general",
|
|
|
|
|
2013-06-08 09:05:40 +04:00
|
|
|
events: {
|
|
|
|
'click .button-save': 'saveSettings'
|
|
|
|
},
|
|
|
|
|
|
|
|
saveSettings: function () {
|
|
|
|
this.model.save({
|
|
|
|
title: this.$('#blog-title').val(),
|
2013-08-05 21:31:29 +04:00
|
|
|
email: this.$('#email-address').val(),
|
|
|
|
logo: this.$('#logo').attr("src"),
|
|
|
|
icon: this.$('#icon').attr("src")
|
|
|
|
|
2013-06-08 09:05:40 +04:00
|
|
|
}, {
|
2013-08-05 21:26:44 +04:00
|
|
|
success: this.saveSuccess,
|
|
|
|
error: this.saveError
|
2013-06-08 09:05:40 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-02 02:33:06 +04:00
|
|
|
templateName: 'settings/general',
|
|
|
|
|
|
|
|
beforeRender: function () {
|
2013-06-08 09:05:40 +04:00
|
|
|
var settings = this.model.toJSON();
|
|
|
|
this.$('#blog-title').val(settings.title);
|
|
|
|
this.$('#email-address').val(settings.email);
|
2013-08-05 21:31:29 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
afterRender: function () {
|
|
|
|
this.$('.js-drop-zone').upload();
|
2013-08-06 23:27:56 +04:00
|
|
|
Settings.Pane.prototype.afterRender.call(this);
|
2013-06-08 09:05:40 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// ### Content settings
|
|
|
|
Settings.content = Settings.Pane.extend({
|
2013-08-02 02:33:06 +04:00
|
|
|
id: 'content',
|
2013-06-08 09:05:40 +04:00
|
|
|
events: {
|
2013-06-17 01:47:10 +04:00
|
|
|
'click .button-save': 'saveSettings'
|
|
|
|
},
|
|
|
|
saveSettings: function () {
|
|
|
|
this.model.save({
|
|
|
|
description: this.$('#blog-description').val()
|
|
|
|
}, {
|
2013-08-05 21:26:44 +04:00
|
|
|
success: this.saveSuccess,
|
|
|
|
error: this.saveError
|
2013-06-17 01:47:10 +04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-02 02:33:06 +04:00
|
|
|
templateName: 'settings/content',
|
|
|
|
|
|
|
|
beforeRender: function () {
|
2013-06-17 01:47:10 +04:00
|
|
|
var settings = this.model.toJSON();
|
|
|
|
this.$('#blog-description').val(settings.description);
|
2013-06-08 09:05:40 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-08-05 21:26:44 +04:00
|
|
|
// ### User profile
|
|
|
|
Settings.user = Settings.Pane.extend({
|
|
|
|
id: 'user',
|
|
|
|
|
|
|
|
options: {
|
|
|
|
modelType: 'User'
|
|
|
|
},
|
|
|
|
|
|
|
|
events: {
|
2013-08-06 03:49:06 +04:00
|
|
|
'click .button-save': 'saveUser',
|
|
|
|
'click .button-change-password': 'changePassword'
|
2013-08-05 21:26:44 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
saveUser: function () {
|
|
|
|
this.model.save({
|
|
|
|
'full_name': this.$('#user-name').val(),
|
|
|
|
'email_address': this.$('#user-email').val(),
|
|
|
|
'location': this.$('#user-location').val(),
|
|
|
|
'url': this.$('#user-website').val(),
|
|
|
|
'bio': this.$('#user-bio').val(),
|
|
|
|
'profile_picture': this.$('#user-profile-picture').attr('src'),
|
|
|
|
'cover_picture': this.$('#user-cover-picture').attr('src')
|
|
|
|
}, {
|
|
|
|
success: this.saveSuccess,
|
|
|
|
error: this.saveError
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-06 03:49:06 +04:00
|
|
|
changePassword: function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
var self = this,
|
|
|
|
email = this.$('#user-email').val(),
|
|
|
|
oldPassword = this.$('#user-password-old').val(),
|
|
|
|
newPassword = this.$('#user-password-new').val(),
|
|
|
|
ne2Password = this.$('#user-new-password-verification').val();
|
|
|
|
|
|
|
|
if (newPassword !== ne2Password || newPassword.length < 6 || oldPassword.length < 6) {
|
|
|
|
this.saveError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: '/ghost/changepw/',
|
|
|
|
type: 'POST',
|
|
|
|
data: {
|
|
|
|
email: email,
|
|
|
|
password: oldPassword,
|
|
|
|
newpassword: newPassword,
|
|
|
|
ne2password: ne2Password
|
|
|
|
},
|
|
|
|
success: function (msg) {
|
2013-08-06 23:27:56 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'success',
|
|
|
|
message: msg.msg,
|
|
|
|
status: 'passive',
|
|
|
|
id: 'success-98'
|
|
|
|
});
|
2013-08-06 03:49:06 +04:00
|
|
|
self.$('#user-password-old').val('');
|
|
|
|
self.$('#user-password-new').val('');
|
|
|
|
self.$('#user-new-password-verification').val('');
|
|
|
|
},
|
|
|
|
error: function (obj, string, status) {
|
2013-08-06 23:27:56 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'error',
|
|
|
|
message: 'Invalid username or password',
|
|
|
|
status: 'passive'
|
|
|
|
});
|
2013-08-06 03:49:06 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-08-05 21:26:44 +04:00
|
|
|
templateName: 'settings/user-profile',
|
|
|
|
|
|
|
|
beforeRender: function () {
|
|
|
|
var user = this.model.toJSON();
|
|
|
|
this.$('#user-name').val(user.full_name);
|
|
|
|
this.$('#user-email').val(user.email_address);
|
|
|
|
this.$('#user-location').val(user.location);
|
|
|
|
this.$('#user-website').val(user.url);
|
|
|
|
this.$('#user-bio').val(user.bio);
|
|
|
|
this.$('#user-profile-picture').attr('src', user.profile_picture);
|
|
|
|
this.$('#user-cover-picture').attr('src', user.cover_picture);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-06-08 09:05:40 +04:00
|
|
|
// ### User settings
|
|
|
|
Settings.users = Settings.Pane.extend({
|
2013-08-05 21:26:44 +04:00
|
|
|
id: 'users',
|
2013-06-08 09:05:40 +04:00
|
|
|
events: {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// ### Appearance settings
|
|
|
|
Settings.appearance = Settings.Pane.extend({
|
2013-08-05 21:26:44 +04:00
|
|
|
id: 'appearance',
|
2013-06-08 09:05:40 +04:00
|
|
|
events: {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// ### Services settings
|
|
|
|
Settings.services = Settings.Pane.extend({
|
2013-08-05 21:26:44 +04:00
|
|
|
id: 'services',
|
2013-06-08 09:05:40 +04:00
|
|
|
events: {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// ### Plugins settings
|
|
|
|
Settings.plugins = Settings.Pane.extend({
|
2013-08-05 21:26:44 +04:00
|
|
|
id: 'plugins',
|
2013-06-08 09:05:40 +04:00
|
|
|
events: {
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-07-19 03:53:08 +04:00
|
|
|
}());
|