2013-09-05 13:00:30 +04:00
|
|
|
/*global window, document, Ghost, $, _, Backbone, Countable */
|
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-09-02 07:54:19 +04:00
|
|
|
|
|
|
|
this.sidebar = new Settings.Sidebar({
|
2013-06-08 09:05:40 +04:00
|
|
|
el: '.settings-sidebar',
|
|
|
|
pane: options.pane,
|
|
|
|
model: this.model
|
2013-09-02 07:54:19 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
this.addSubview(this.sidebar);
|
|
|
|
|
|
|
|
this.listenTo(Ghost.router, "route:settings", this.changePane);
|
|
|
|
},
|
|
|
|
|
|
|
|
changePane: function (pane) {
|
|
|
|
if (!pane) {
|
|
|
|
// Can happen when trying to load /settings with no pane specified
|
|
|
|
// let the router navigate itself to /settings/general
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.sidebar.showContent(pane);
|
2013-06-08 09:05:40 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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');
|
2013-09-02 07:54:19 +04:00
|
|
|
this.showContent(options.pane);
|
2013-06-08 09:05:40 +04:00
|
|
|
},
|
|
|
|
|
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);
|
2013-09-02 07:54:19 +04:00
|
|
|
|
2013-06-08 09:05:40 +04:00
|
|
|
this.showContent(id);
|
|
|
|
},
|
|
|
|
|
|
|
|
showContent: function (id) {
|
2013-08-05 21:26:44 +04:00
|
|
|
var self = this,
|
2013-09-15 20:03:31 +04:00
|
|
|
model;
|
2013-08-05 21:26:44 +04:00
|
|
|
|
2013-09-15 15:13:06 +04:00
|
|
|
Ghost.router.navigate('/settings/' + id + '/');
|
2013-08-21 14:50:49 +04:00
|
|
|
Ghost.trigger('urlchange');
|
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]();
|
2013-09-15 20:03:31 +04:00
|
|
|
model.fetch().then(function () {
|
|
|
|
self.renderPane(model);
|
2013-08-05 21:26:44 +04:00
|
|
|
});
|
|
|
|
} 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-20 23:27:54 +04:00
|
|
|
render: function () {
|
|
|
|
this.$el.hide();
|
|
|
|
Ghost.View.prototype.render.call(this);
|
|
|
|
this.$el.fadeIn(300);
|
|
|
|
},
|
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
|
|
|
},
|
2013-08-20 22:52:44 +04:00
|
|
|
saveSuccess: function (model, response, options) {
|
2013-09-18 05:45:36 +04:00
|
|
|
Ghost.notifications.clearEverything();
|
2013-08-20 22:52:44 +04:00
|
|
|
// TODO: better messaging here?
|
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
|
|
|
},
|
2013-08-20 22:52:44 +04:00
|
|
|
saveError: function (model, xhr) {
|
2013-09-18 05:45:36 +04:00
|
|
|
Ghost.notifications.clearEverything();
|
2013-08-20 22:52:44 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'error',
|
|
|
|
message: Ghost.Views.Utils.getRequestErrorMessage(xhr),
|
|
|
|
status: 'passive'
|
|
|
|
});
|
|
|
|
},
|
|
|
|
validationError: function (message) {
|
2013-09-18 05:45:36 +04:00
|
|
|
Ghost.notifications.clearEverything();
|
2013-08-06 11:55:47 +04:00
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'error',
|
2013-08-19 01:50:42 +04:00
|
|
|
message: message,
|
2013-08-06 11:55:47 +04:00
|
|
|
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: {
|
2013-09-04 21:42:01 +04:00
|
|
|
'click .button-save': 'saveSettings',
|
|
|
|
'click .js-modal-logo': 'showLogo',
|
2013-09-09 20:40:20 +04:00
|
|
|
'click .js-modal-cover': 'showCover'
|
2013-06-08 09:05:40 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
saveSettings: function () {
|
2013-09-15 20:03:31 +04:00
|
|
|
var title = this.$('#blog-title').val(),
|
2013-09-13 00:03:18 +04:00
|
|
|
description = this.$('#blog-description').val(),
|
|
|
|
email = this.$('#email-address').val(),
|
|
|
|
postsPerPage = this.$('#postsPerPage').val();
|
|
|
|
|
|
|
|
Ghost.Validate._errors = [];
|
|
|
|
Ghost.Validate
|
|
|
|
.check(title, {message: "Title is too long", el: $('#blog-title')})
|
|
|
|
.len(0, 150);
|
|
|
|
Ghost.Validate
|
|
|
|
.check(description, {message: "Description is too long", el: $('#blog-description')})
|
|
|
|
.len(0, 200);
|
|
|
|
Ghost.Validate
|
|
|
|
.check(email, {message: "Please supply a valid email address", el: $('#email-address')})
|
|
|
|
.isEmail().len(0, 254);
|
|
|
|
Ghost.Validate
|
2013-09-19 10:55:37 +04:00
|
|
|
.check(postsPerPage, {message: "Please use a number less than 1000", el: $('postsPerPage')})
|
|
|
|
.isInt().max(1000);
|
2013-09-13 00:03:18 +04:00
|
|
|
|
|
|
|
if (Ghost.Validate._errors.length > 0) {
|
|
|
|
Ghost.Validate.handleErrors();
|
|
|
|
} else {
|
|
|
|
this.model.save({
|
|
|
|
title: title,
|
|
|
|
description: description,
|
|
|
|
email: email,
|
|
|
|
postsPerPage: postsPerPage,
|
|
|
|
activeTheme: this.$('#activeTheme').val()
|
|
|
|
}, {
|
|
|
|
success: this.saveSuccess,
|
|
|
|
error: this.saveError
|
|
|
|
});
|
|
|
|
}
|
2013-06-08 09:05:40 +04:00
|
|
|
},
|
2013-09-15 19:35:53 +04:00
|
|
|
showLogo: function (e) {
|
|
|
|
e.preventDefault();
|
2013-09-04 21:42:01 +04:00
|
|
|
var settings = this.model.toJSON();
|
2013-09-15 18:54:12 +04:00
|
|
|
this.showUpload('logo', settings.logo);
|
2013-09-04 21:42:01 +04:00
|
|
|
},
|
2013-09-15 19:35:53 +04:00
|
|
|
showCover: function (e) {
|
|
|
|
e.preventDefault();
|
2013-09-04 21:42:01 +04:00
|
|
|
var settings = this.model.toJSON();
|
2013-09-15 18:54:12 +04:00
|
|
|
this.showUpload('cover', settings.cover);
|
2013-09-04 21:42:01 +04:00
|
|
|
},
|
2013-09-15 18:54:12 +04:00
|
|
|
showUpload: function (key, src) {
|
2013-10-02 13:39:34 +04:00
|
|
|
var self = this, upload = new Ghost.Models.uploadModal({'key': key, 'src': src, 'id': this.id, 'accept': {
|
2013-09-08 20:17:16 +04:00
|
|
|
func: function () { // The function called on acceptance
|
2013-09-15 20:03:31 +04:00
|
|
|
var data = {};
|
2013-09-17 00:04:23 +04:00
|
|
|
if (this.$('#uploadurl').val()) {
|
|
|
|
data[key] = this.$('#uploadurl').val();
|
|
|
|
} else {
|
|
|
|
data[key] = this.$('.js-upload-target').attr('src');
|
|
|
|
}
|
|
|
|
|
2013-09-08 20:17:16 +04:00
|
|
|
self.model.save(data, {
|
|
|
|
success: self.saveSuccess,
|
|
|
|
error: self.saveError
|
|
|
|
});
|
|
|
|
self.render();
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
buttonClass: "button-save right",
|
|
|
|
text: "Save" // The accept button text
|
|
|
|
}});
|
|
|
|
|
2013-09-04 21:42:01 +04:00
|
|
|
this.addSubview(new Ghost.Views.Modal({
|
2013-09-08 20:17:16 +04:00
|
|
|
model: upload
|
2013-09-04 21:42:01 +04:00
|
|
|
}));
|
|
|
|
},
|
2013-08-02 02:33:06 +04:00
|
|
|
templateName: 'settings/general',
|
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-09-08 20:17:16 +04:00
|
|
|
// ### User profile
|
2013-08-05 21:26:44 +04:00
|
|
|
Settings.user = Settings.Pane.extend({
|
|
|
|
id: 'user',
|
|
|
|
|
|
|
|
options: {
|
|
|
|
modelType: 'User'
|
|
|
|
},
|
|
|
|
|
|
|
|
events: {
|
2013-08-06 03:49:06 +04:00
|
|
|
'click .button-save': 'saveUser',
|
2013-09-08 20:17:16 +04:00
|
|
|
'click .button-change-password': 'changePassword',
|
2013-09-12 02:04:49 +04:00
|
|
|
'click .js-modal-cover': 'showCover',
|
|
|
|
'click .js-modal-image': 'showImage'
|
2013-09-08 20:17:16 +04:00
|
|
|
},
|
2013-09-15 19:35:53 +04:00
|
|
|
showCover: function (e) {
|
|
|
|
e.preventDefault();
|
2013-09-08 20:17:16 +04:00
|
|
|
var user = this.model.toJSON();
|
2013-09-15 18:54:12 +04:00
|
|
|
this.showUpload('cover', user.cover);
|
2013-09-08 20:17:16 +04:00
|
|
|
},
|
2013-09-12 02:04:49 +04:00
|
|
|
showImage: function (e) {
|
2013-09-08 20:17:16 +04:00
|
|
|
e.preventDefault();
|
|
|
|
var user = this.model.toJSON();
|
2013-09-15 18:54:12 +04:00
|
|
|
this.showUpload('image', user.image);
|
2013-09-08 20:17:16 +04:00
|
|
|
},
|
2013-09-15 18:54:12 +04:00
|
|
|
showUpload: function (key, src) {
|
2013-10-02 13:39:34 +04:00
|
|
|
var self = this, upload = new Ghost.Models.uploadModal({'key': key, 'src': src, 'id': this.id, 'accept': {
|
2013-09-08 20:17:16 +04:00
|
|
|
func: function () { // The function called on acceptance
|
|
|
|
var data = {};
|
2013-09-17 00:04:23 +04:00
|
|
|
if (this.$('#uploadurl').val()) {
|
|
|
|
data[key] = this.$('#uploadurl').val();
|
|
|
|
} else {
|
|
|
|
data[key] = this.$('.js-upload-target').attr('src');
|
|
|
|
}
|
2013-09-08 20:17:16 +04:00
|
|
|
self.model.save(data, {
|
|
|
|
success: self.saveSuccess,
|
|
|
|
error: self.saveError
|
|
|
|
});
|
|
|
|
self.render();
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
buttonClass: "button-save right",
|
|
|
|
text: "Save" // The accept button text
|
|
|
|
}});
|
|
|
|
|
|
|
|
this.addSubview(new Ghost.Views.Modal({
|
|
|
|
model: upload
|
|
|
|
}));
|
2013-08-05 21:26:44 +04:00
|
|
|
},
|
|
|
|
|
2013-08-09 05:22:49 +04:00
|
|
|
|
2013-08-05 21:26:44 +04:00
|
|
|
saveUser: function () {
|
2013-09-13 00:03:18 +04:00
|
|
|
var userName = this.$('#user-name').val(),
|
|
|
|
userEmail = this.$('#user-email').val(),
|
|
|
|
userLocation = this.$('#user-location').val(),
|
|
|
|
userWebsite = this.$('#user-website').val(),
|
|
|
|
userBio = this.$('#user-bio').val();
|
|
|
|
|
|
|
|
Ghost.Validate._errors = [];
|
|
|
|
Ghost.Validate
|
|
|
|
.check(userName, {message: "Name is too long", el: $('#user-name')})
|
|
|
|
.len(0, 150);
|
|
|
|
Ghost.Validate
|
|
|
|
.check(userBio, {message: "Bio is too long", el: $('#user-bio')})
|
|
|
|
.len(0, 200);
|
|
|
|
Ghost.Validate
|
|
|
|
.check(userEmail, {message: "Please supply a valid email address", el: $('#user-email')})
|
|
|
|
.isEmail();
|
|
|
|
Ghost.Validate
|
|
|
|
.check(userLocation, {message: "Location is too long", el: $('#user-location')})
|
|
|
|
.len(0, 150);
|
|
|
|
if (userWebsite.length > 0) {
|
|
|
|
Ghost.Validate
|
|
|
|
.check(userWebsite, {message: "Please use a valid url", el: $('#user-website')})
|
|
|
|
.isUrl()
|
|
|
|
.len(0, 2000);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Ghost.Validate._errors.length > 0) {
|
|
|
|
Ghost.Validate.handleErrors();
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.model.save({
|
2013-09-12 02:04:49 +04:00
|
|
|
'name': userName,
|
|
|
|
'email': userEmail,
|
2013-09-13 00:03:18 +04:00
|
|
|
'location': userLocation,
|
2013-09-12 02:04:49 +04:00
|
|
|
'website': userWebsite,
|
2013-09-18 18:54:52 +04:00
|
|
|
'bio': userBio
|
2013-09-13 00:03:18 +04:00
|
|
|
}, {
|
|
|
|
success: this.saveSuccess,
|
|
|
|
error: this.saveError
|
|
|
|
});
|
|
|
|
}
|
2013-08-05 21:26:44 +04:00
|
|
|
},
|
|
|
|
|
2013-08-06 03:49:06 +04:00
|
|
|
changePassword: function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
var self = this,
|
|
|
|
oldPassword = this.$('#user-password-old').val(),
|
|
|
|
newPassword = this.$('#user-password-new').val(),
|
|
|
|
ne2Password = this.$('#user-new-password-verification').val();
|
|
|
|
|
2013-09-13 00:03:18 +04:00
|
|
|
Ghost.Validate._errors = [];
|
|
|
|
Ghost.Validate.check(newPassword, {message: 'Your new passwords do not match'}).equals(ne2Password);
|
2013-09-19 06:51:56 +04:00
|
|
|
Ghost.Validate.check(newPassword, {message: 'Your password is not long enough. It must be at least 8 characters long.'}).len(8);
|
2013-08-19 01:50:42 +04:00
|
|
|
|
2013-09-13 00:03:18 +04:00
|
|
|
if (Ghost.Validate._errors.length > 0) {
|
|
|
|
Ghost.Validate.handleErrors();
|
|
|
|
} else {
|
2013-08-06 03:49:06 +04:00
|
|
|
|
2013-09-13 00:03:18 +04:00
|
|
|
$.ajax({
|
|
|
|
url: '/ghost/changepw/',
|
|
|
|
type: 'POST',
|
|
|
|
data: {
|
|
|
|
password: oldPassword,
|
|
|
|
newpassword: newPassword,
|
|
|
|
ne2password: ne2Password
|
|
|
|
},
|
|
|
|
success: function (msg) {
|
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'success',
|
|
|
|
message: msg.msg,
|
|
|
|
status: 'passive',
|
|
|
|
id: 'success-98'
|
|
|
|
});
|
|
|
|
self.$('#user-password-old, #user-password-new, #user-new-password-verification').val('');
|
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
Ghost.notifications.addItem({
|
|
|
|
type: 'error',
|
|
|
|
message: Ghost.Views.Utils.getRequestErrorMessage(xhr),
|
|
|
|
status: 'passive'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-08-06 03:49:06 +04:00
|
|
|
},
|
|
|
|
|
2013-08-05 21:26:44 +04:00
|
|
|
templateName: 'settings/user-profile',
|
|
|
|
|
2013-09-05 13:00:30 +04:00
|
|
|
afterRender: function () {
|
|
|
|
var self = this;
|
|
|
|
Countable.live(document.getElementById('user-bio'), function (counter) {
|
|
|
|
if (counter.all > 180) {
|
|
|
|
self.$('.bio-container .word-count').css({color: "#e25440"});
|
|
|
|
} else {
|
|
|
|
self.$('.bio-container .word-count').css({color: "#9E9D95"});
|
|
|
|
}
|
|
|
|
|
|
|
|
self.$('.bio-container .word-count').text(200 - counter.all);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
Settings.Pane.prototype.afterRender.call(this);
|
2013-08-05 21:26:44 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-07-19 03:53:08 +04:00
|
|
|
}());
|