From 716a09a452259026bead7490add0707c48126b4a Mon Sep 17 00:00:00 2001 From: Peter Szel Date: Tue, 8 Apr 2014 00:01:46 +0200 Subject: [PATCH] Ported logic to settings: Debug Tab closes #2424 - Added file upload component - Added import client logic - Added e-mail sending client logic - Added settings model --- core/client/components/file-upload.js | 23 ++++++++++++ core/client/components/gh-form.js | 13 +++++++ core/client/controllers/settings/debug.js | 37 +++++++++++++++++++ core/client/models/base.js | 2 + core/client/models/post.js | 2 +- core/client/models/settings.js | 30 ++++++++++++++- core/client/router.js | 1 + core/client/routes/debug.js | 7 ++++ core/client/routes/settings/debug.js | 11 ++++++ .../templates/components/file-upload.hbs | 2 + core/client/templates/settings/debug.hbs | 21 +++++++---- 11 files changed, 139 insertions(+), 10 deletions(-) create mode 100644 core/client/components/file-upload.js create mode 100644 core/client/components/gh-form.js create mode 100644 core/client/controllers/settings/debug.js create mode 100644 core/client/routes/debug.js create mode 100644 core/client/routes/settings/debug.js create mode 100644 core/client/templates/components/file-upload.hbs diff --git a/core/client/components/file-upload.js b/core/client/components/file-upload.js new file mode 100644 index 0000000000..98d3e8c5a6 --- /dev/null +++ b/core/client/components/file-upload.js @@ -0,0 +1,23 @@ +var FileUpload = Ember.Component.extend({ + _file: null, + uploadButtonText: 'Text', + uploadButtonDisabled: true, + change: function (event) { + this.set('uploadButtonDisabled', false); + this.sendAction('onAdd'); + this._file = event.target.files[0]; + }, + actions: { + upload: function () { + var self = this; + if (!this.uploadButtonDisabled && self._file) { + self.sendAction('onUpload', self._file); + } + + // Prevent double post by disabling the button. + this.set('uploadButtonDisabled', true); + } + } +}); + +export default FileUpload; diff --git a/core/client/components/gh-form.js b/core/client/components/gh-form.js new file mode 100644 index 0000000000..5b6dde63ee --- /dev/null +++ b/core/client/components/gh-form.js @@ -0,0 +1,13 @@ +export default Ember.View.extend({ + tagName: 'form', + attributeBindings: ['enctype'], + reset: function () { + this.$().get(0).reset(); + }, + didInsertElement: function () { + this.get('controller').on('reset', this, this.reset); + }, + willClearRender: function () { + this.get('controller').off('reset', this, this.reset); + } +}); diff --git a/core/client/controllers/settings/debug.js b/core/client/controllers/settings/debug.js new file mode 100644 index 0000000000..875e44d1e4 --- /dev/null +++ b/core/client/controllers/settings/debug.js @@ -0,0 +1,37 @@ +/*global alert, console */ + +var Debug = Ember.Controller.extend(Ember.Evented, { + uploadButtonText: 'Import', + actions: { + importData: function (file) { + var self = this; + this.set('uploadButtonText', 'Importing'); + this.get('model').importFrom(file) + .then(function (response) { + console.log(response); + alert('@TODO: success'); + }) + .catch(function (response) { + console.log(response); + alert('@TODO: error'); + }) + .finally(function () { + self.set('uploadButtonText', 'Import'); + self.trigger('reset'); + }); + }, + sendTestEmail: function () { + this.get('model').sendTestEmail() + .then(function (response) { + console.log(response); + alert('@TODO: success'); + }) + .catch(function (response) { + console.log(response); + alert('@TODO: error'); + }); + } + } +}); + +export default Debug; diff --git a/core/client/models/base.js b/core/client/models/base.js index 8973d80383..9a70b16b27 100644 --- a/core/client/models/base.js +++ b/core/client/models/base.js @@ -5,6 +5,7 @@ function ghostPaths() { return { subdir: subdir, + adminRoot: subdir + '/ghost', apiRoot: subdir + '/ghost/api/v0.1' }; } @@ -29,5 +30,6 @@ var BaseModel = Ember.Object.extend({ BaseModel.apiRoot = ghostPaths().apiRoot; BaseModel.subdir = ghostPaths().subdir; +BaseModel.adminRoot = ghostPaths().adminRoot; export default BaseModel; \ No newline at end of file diff --git a/core/client/models/post.js b/core/client/models/post.js index 4c31a37dc6..6cbb444494 100644 --- a/core/client/models/post.js +++ b/core/client/models/post.js @@ -53,4 +53,4 @@ var PostModel = BaseModel.extend({ } }); -export default PostModel; +export default PostModel; \ No newline at end of file diff --git a/core/client/models/settings.js b/core/client/models/settings.js index ee5e0f79d7..8303eae2a7 100644 --- a/core/client/models/settings.js +++ b/core/client/models/settings.js @@ -2,7 +2,7 @@ var validator = window.validator; import BaseModel from 'ghost/models/base'; -export default BaseModel.extend({ +var SettingsModel = BaseModel.extend({ url: BaseModel.apiRoot + '/settings/?type=blog,theme,app', title: null, @@ -46,5 +46,31 @@ export default BaseModel.extend({ } return validationErrors; + }, + exportPath: BaseModel.adminRoot + '/export/', + importFrom: function (file) { + var formData = new FormData(); + formData.append('importfile', file); + return ic.ajax.request(BaseModel.apiRoot + '/db/', { + headers: { + 'X-CSRF-Token': $('meta[name="csrf-param"]').attr('content') + }, + type: 'POST', + data: formData, + dataType: 'json', + cache: false, + contentType: false, + processData: false + }); + }, + sendTestEmail: function () { + return ic.ajax.request(BaseModel.apiRoot + '/mail/test/', { + type: 'POST', + headers: { + 'X-CSRF-Token': $('meta[name="csrf-param"]').attr('content') + } + }); } -}); \ No newline at end of file +}); + +export default SettingsModel; diff --git a/core/client/router.js b/core/client/router.js index 11acbbd860..11e8f4f94d 100755 --- a/core/client/router.js +++ b/core/client/router.js @@ -24,6 +24,7 @@ Router.map(function () { this.route('debug'); this.route('apps'); }); + this.route('debug'); }); export default Router; diff --git a/core/client/routes/debug.js b/core/client/routes/debug.js new file mode 100644 index 0000000000..3fcacfec8b --- /dev/null +++ b/core/client/routes/debug.js @@ -0,0 +1,7 @@ +var DebugRoute = Ember.Route.extend({ + beforeModel: function () { + this.transitionTo('settings.debug'); + } +}); + +export default DebugRoute; diff --git a/core/client/routes/settings/debug.js b/core/client/routes/settings/debug.js new file mode 100644 index 0000000000..e6f18b64b4 --- /dev/null +++ b/core/client/routes/settings/debug.js @@ -0,0 +1,11 @@ +import SettingsModel from 'ghost/models/settings'; + +var settingsModel = SettingsModel.create(); + +var DebugRoute = Ember.Route.extend({ + model: function () { + return settingsModel; + } +}); + +export default DebugRoute; diff --git a/core/client/templates/components/file-upload.hbs b/core/client/templates/components/file-upload.hbs new file mode 100644 index 0000000000..8388128d2c --- /dev/null +++ b/core/client/templates/components/file-upload.hbs @@ -0,0 +1,2 @@ + + diff --git a/core/client/templates/settings/debug.hbs b/core/client/templates/settings/debug.hbs index bde0ba063e..2440b13ade 100644 --- a/core/client/templates/settings/debug.hbs +++ b/core/client/templates/settings/debug.hbs @@ -3,25 +3,23 @@
-
- Export + Export

Export the blog settings and data.

-
+ {{#gh-form id="settings-import" enctype="multipart/form-data"}}
- - + {{file-upload onUpload="importData" uploadButtonText=uploadButtonText}}

Import from another Ghost installation. If you import a user, this will replace the current user & log you out.

-
+ {{/gh-form}}
@@ -31,4 +29,13 @@
-
\ No newline at end of file +
+
+
+ + +

Sends a test email to your address.

+
+
+
+