2017-09-11 10:41:17 +03:00
|
|
|
import ModalComponent from 'ghost-admin/components/modal-base';
|
2016-08-17 18:01:46 +03:00
|
|
|
import ghostPaths from 'ghost-admin/utils/ghost-paths';
|
2016-08-24 21:22:20 +03:00
|
|
|
import {
|
|
|
|
UnsupportedMediaTypeError,
|
|
|
|
isThemeValidationError
|
|
|
|
} from 'ghost-admin/services/ajax';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {computed} from '@ember/object';
|
|
|
|
import {get} from '@ember/object';
|
|
|
|
import {mapBy, or} from '@ember/object/computed';
|
|
|
|
import {run} from '@ember/runloop';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2016-08-17 18:01:46 +03:00
|
|
|
|
2017-11-24 21:53:19 +03:00
|
|
|
const DEFAULTS = {
|
|
|
|
accept: ['application/zip', 'application/x-zip-compressed'],
|
|
|
|
extensions: ['zip']
|
|
|
|
};
|
|
|
|
|
2016-08-17 18:01:46 +03:00
|
|
|
export default ModalComponent.extend({
|
2018-01-11 20:43:23 +03:00
|
|
|
eventBus: service(),
|
|
|
|
store: service(),
|
2016-08-17 18:01:46 +03:00
|
|
|
|
2017-11-24 21:53:19 +03:00
|
|
|
accept: null,
|
|
|
|
extensions: null,
|
2017-02-21 21:28:44 +03:00
|
|
|
themes: null,
|
2016-08-17 18:01:46 +03:00
|
|
|
closeDisabled: false,
|
|
|
|
file: null,
|
|
|
|
theme: false,
|
|
|
|
displayOverwriteWarning: false,
|
|
|
|
|
|
|
|
hideUploader: or('theme', 'displayOverwriteWarning'),
|
2018-01-11 20:43:23 +03:00
|
|
|
currentThemeNames: mapBy('model.themes', 'name'),
|
2016-08-17 18:01:46 +03:00
|
|
|
|
|
|
|
uploadUrl: computed(function () {
|
|
|
|
return `${ghostPaths().apiRoot}/themes/upload/`;
|
|
|
|
}),
|
|
|
|
|
|
|
|
themeName: computed('theme.{name,package.name}', function () {
|
2017-03-03 18:31:42 +03:00
|
|
|
let themePackage = this.get('theme.package');
|
|
|
|
let name = this.get('theme.name');
|
2016-08-17 18:01:46 +03:00
|
|
|
|
2017-03-03 18:31:42 +03:00
|
|
|
return themePackage ? `${themePackage.name} - ${themePackage.version}` : name;
|
2016-08-17 18:01:46 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
fileThemeName: computed('file', function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
let file = this.file;
|
2016-08-17 18:01:46 +03:00
|
|
|
return file.name.replace(/\.zip$/, '');
|
|
|
|
}),
|
|
|
|
|
|
|
|
canActivateTheme: computed('theme', function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
let theme = this.theme;
|
2017-03-03 18:31:42 +03:00
|
|
|
return theme && !theme.get('active');
|
2016-08-17 18:01:46 +03:00
|
|
|
}),
|
|
|
|
|
2017-11-24 21:53:19 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
this.accept = this.accept || DEFAULTS.accept;
|
|
|
|
this.extensions = this.extensions || DEFAULTS.extensions;
|
|
|
|
},
|
|
|
|
|
2016-08-17 18:01:46 +03:00
|
|
|
actions: {
|
|
|
|
validateTheme(file) {
|
2017-03-03 18:31:42 +03:00
|
|
|
let themeName = file.name.replace(/\.zip$/, '').replace(/[^\w@.]/gi, '-').toLowerCase();
|
2016-09-14 20:23:04 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
let currentThemeNames = this.currentThemeNames;
|
2016-08-17 18:01:46 +03:00
|
|
|
|
|
|
|
this.set('file', file);
|
|
|
|
|
2016-09-14 11:54:16 +03:00
|
|
|
let [, extension] = (/(?:\.([^.]+))?$/).exec(file.name);
|
2019-03-06 16:53:54 +03:00
|
|
|
let extensions = this.extensions;
|
2016-09-14 11:54:16 +03:00
|
|
|
|
|
|
|
if (!extension || extensions.indexOf(extension.toLowerCase()) === -1) {
|
2016-08-17 18:01:46 +03:00
|
|
|
return new UnsupportedMediaTypeError();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.name.match(/^casper\.zip$/i)) {
|
2017-11-04 01:59:39 +03:00
|
|
|
return {payload: {errors: [{message: 'Sorry, the default Casper theme cannot be overwritten.<br>Please rename your zip file and try again.'}]}};
|
2016-08-17 18:01:46 +03:00
|
|
|
}
|
|
|
|
|
2017-02-21 21:28:44 +03:00
|
|
|
if (!this._allowOverwrite && currentThemeNames.includes(themeName)) {
|
2016-08-17 18:01:46 +03:00
|
|
|
this.set('displayOverwriteWarning', true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
confirmOverwrite() {
|
|
|
|
this._allowOverwrite = true;
|
|
|
|
this.set('displayOverwriteWarning', false);
|
|
|
|
|
|
|
|
// we need to schedule afterRender so that the upload component is
|
|
|
|
// displayed again in order to subscribe/respond to the event bus
|
|
|
|
run.schedule('afterRender', this, function () {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.eventBus.publish('themeUploader:upload', this.file);
|
2016-08-17 18:01:46 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
uploadStarted() {
|
|
|
|
this.set('closeDisabled', true);
|
|
|
|
},
|
|
|
|
|
|
|
|
uploadFinished() {
|
|
|
|
this.set('closeDisabled', false);
|
|
|
|
},
|
|
|
|
|
|
|
|
uploadSuccess(response) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.store.pushPayload(response);
|
2017-03-03 18:31:42 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
let theme = this.store.peekRecord('theme', response.themes[0].name);
|
2016-09-14 20:34:07 +03:00
|
|
|
|
|
|
|
this.set('theme', theme);
|
|
|
|
|
|
|
|
if (get(theme, 'warnings.length') > 0) {
|
2017-03-03 18:31:42 +03:00
|
|
|
this.set('validationWarnings', get(theme, 'warnings'));
|
2016-09-14 20:34:07 +03:00
|
|
|
}
|
|
|
|
|
2017-06-06 09:09:52 +03:00
|
|
|
// Ghost differentiates between errors and fatal errors
|
|
|
|
// You can't activate a theme with fatal errors, but with errors.
|
|
|
|
if (get(theme, 'errors.length') > 0) {
|
|
|
|
this.set('validationErrors', get(theme, 'errors'));
|
|
|
|
}
|
|
|
|
|
2017-09-01 15:46:25 +03:00
|
|
|
this.set('hasWarningsOrErrors', this.get('validationErrors.length') || this.get('validationWarnings.length'));
|
2017-06-06 09:09:52 +03:00
|
|
|
|
2016-08-17 18:01:46 +03:00
|
|
|
// invoke the passed in confirm action
|
2018-03-20 17:57:59 +03:00
|
|
|
this.get('model.uploadSuccess')(theme);
|
2016-08-17 18:01:46 +03:00
|
|
|
},
|
|
|
|
|
2016-08-24 21:22:20 +03:00
|
|
|
uploadFailed(error) {
|
|
|
|
if (isThemeValidationError(error)) {
|
2019-04-16 00:39:05 +03:00
|
|
|
let errors = error.payload.errors[0].details.errors;
|
2017-06-06 09:09:52 +03:00
|
|
|
let fatalErrors = [];
|
|
|
|
let normalErrors = [];
|
|
|
|
|
|
|
|
// to have a proper grouping of fatal errors and none fatal, we need to check
|
|
|
|
// our errors for the fatal property
|
2017-09-11 10:41:17 +03:00
|
|
|
if (errors && errors.length > 0) {
|
2018-01-05 18:38:23 +03:00
|
|
|
for (let i = 0; i < errors.length; i += 1) {
|
2017-06-06 09:09:52 +03:00
|
|
|
if (errors[i].fatal) {
|
|
|
|
fatalErrors.push(errors[i]);
|
|
|
|
} else {
|
|
|
|
normalErrors.push(errors[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('fatalValidationErrors', fatalErrors);
|
|
|
|
this.set('validationErrors', normalErrors);
|
2016-08-24 21:22:20 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-08-17 18:01:46 +03:00
|
|
|
confirm() {
|
|
|
|
// noop - we don't want the enter key doing anything
|
|
|
|
},
|
|
|
|
|
|
|
|
activate() {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.get('model.activate')(this.theme);
|
2018-03-20 17:57:59 +03:00
|
|
|
this.closeModal();
|
2016-08-17 18:01:46 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
closeModal() {
|
2019-03-06 16:53:54 +03:00
|
|
|
if (!this.closeDisabled) {
|
2016-08-17 18:01:46 +03:00
|
|
|
this._super(...arguments);
|
|
|
|
}
|
2016-08-24 21:22:20 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
reset() {
|
2019-04-16 00:39:05 +03:00
|
|
|
this.set('theme', null);
|
2017-06-06 09:09:52 +03:00
|
|
|
this.set('validationWarnings', []);
|
|
|
|
this.set('validationErrors', []);
|
|
|
|
this.set('fatalValidationErrors', []);
|
|
|
|
this.set('hasWarningsOrErrors', false);
|
2016-08-17 18:01:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|