2018-10-18 02:18:29 +03:00
|
|
|
import Controller from '@ember/controller';
|
2020-05-11 13:37:35 +03:00
|
|
|
import config from 'ghost-admin/config/environment';
|
2019-04-04 14:25:16 +03:00
|
|
|
import copyTextToClipboard from 'ghost-admin/utils/copy-text-to-clipboard';
|
2018-10-23 12:29:49 +03:00
|
|
|
import {
|
|
|
|
IMAGE_EXTENSIONS,
|
|
|
|
IMAGE_MIME_TYPES
|
|
|
|
} from 'ghost-admin/components/gh-image-uploader';
|
2018-10-18 02:18:29 +03:00
|
|
|
import {alias} from '@ember/object/computed';
|
|
|
|
import {computed} from '@ember/object';
|
2021-05-12 14:33:36 +03:00
|
|
|
import {htmlSafe} from '@ember/template';
|
2018-11-06 13:59:47 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2018-10-19 02:09:05 +03:00
|
|
|
import {task, timeout} from 'ember-concurrency';
|
2018-10-18 02:18:29 +03:00
|
|
|
|
|
|
|
export default Controller.extend({
|
2018-11-06 13:59:47 +03:00
|
|
|
config: service(),
|
2019-02-23 12:48:18 +03:00
|
|
|
ghostPaths: service(),
|
2018-11-06 13:59:47 +03:00
|
|
|
|
2018-10-23 12:29:49 +03:00
|
|
|
imageExtensions: IMAGE_EXTENSIONS,
|
|
|
|
imageMimeTypes: IMAGE_MIME_TYPES,
|
2020-05-05 21:14:45 +03:00
|
|
|
showRegenerateKeyModal: false,
|
|
|
|
selectedApiKey: null,
|
|
|
|
isApiKeyRegenerated: false,
|
2018-10-23 12:29:49 +03:00
|
|
|
|
2020-05-11 13:37:35 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
if (this.isTesting === undefined) {
|
|
|
|
this.isTesting = config.environment === 'test';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-10-18 02:18:29 +03:00
|
|
|
integration: alias('model'),
|
|
|
|
|
2019-02-23 12:48:18 +03:00
|
|
|
apiUrl: computed(function () {
|
|
|
|
let origin = window.location.origin;
|
|
|
|
let subdir = this.ghostPaths.subdir;
|
2019-03-19 14:47:29 +03:00
|
|
|
let url = this.ghostPaths.url.join(origin, subdir);
|
2019-02-23 12:48:18 +03:00
|
|
|
|
2019-03-19 14:47:29 +03:00
|
|
|
return url.replace(/\/$/, '');
|
2019-02-23 12:48:18 +03:00
|
|
|
}),
|
|
|
|
|
2020-05-05 21:14:45 +03:00
|
|
|
regeneratedKeyType: computed('isApiKeyRegenerated', 'selectedApiKey', function () {
|
|
|
|
if (this.isApiKeyRegenerated) {
|
|
|
|
return this.get('selectedApiKey.type');
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}),
|
|
|
|
|
2018-10-18 02:18:29 +03:00
|
|
|
allWebhooks: computed(function () {
|
|
|
|
return this.store.peekAll('webhook');
|
|
|
|
}),
|
|
|
|
|
2018-11-06 13:59:47 +03:00
|
|
|
filteredWebhooks: computed('integration.id', 'allWebhooks.@each.{isNew,isDeleted}', function () {
|
2018-10-18 02:18:29 +03:00
|
|
|
return this.allWebhooks.filter((webhook) => {
|
|
|
|
let matchesIntegration = webhook.belongsTo('integration').id() === this.integration.id;
|
|
|
|
|
|
|
|
return matchesIntegration
|
|
|
|
&& !webhook.isNew
|
|
|
|
&& !webhook.isDeleted;
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
|
2018-10-23 12:29:49 +03:00
|
|
|
iconImageStyle: computed('integration.iconImage', function () {
|
|
|
|
let url = this.integration.iconImage;
|
|
|
|
if (url) {
|
|
|
|
let styles = [
|
|
|
|
`background-image: url(${url})`,
|
2018-10-24 16:04:17 +03:00
|
|
|
'background-size: 50%',
|
2018-10-23 12:29:49 +03:00
|
|
|
'background-position: 50%',
|
|
|
|
'background-repeat: no-repeat'
|
|
|
|
];
|
|
|
|
return htmlSafe(styles.join('; '));
|
|
|
|
}
|
2019-06-24 18:33:21 +03:00
|
|
|
|
|
|
|
return htmlSafe('');
|
2018-10-23 12:29:49 +03:00
|
|
|
}),
|
|
|
|
|
2018-10-18 02:18:29 +03:00
|
|
|
actions: {
|
2018-10-23 12:29:49 +03:00
|
|
|
triggerIconFileDialog() {
|
|
|
|
let input = document.querySelector('input[type="file"][name="iconImage"]');
|
|
|
|
input.click();
|
|
|
|
},
|
|
|
|
|
|
|
|
setIconImage([image]) {
|
|
|
|
this.integration.set('iconImage', image.url);
|
|
|
|
},
|
|
|
|
|
2018-10-18 02:18:29 +03:00
|
|
|
save() {
|
|
|
|
return this.save.perform();
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleUnsavedChangesModal(transition) {
|
|
|
|
let leaveTransition = this.leaveScreenTransition;
|
|
|
|
|
|
|
|
if (!transition && this.showUnsavedChangesModal) {
|
|
|
|
this.set('leaveScreenTransition', null);
|
|
|
|
this.set('showUnsavedChangesModal', false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!leaveTransition || transition.targetName === leaveTransition.targetName) {
|
|
|
|
this.set('leaveScreenTransition', transition);
|
|
|
|
|
|
|
|
// if a save is running, wait for it to finish then transition
|
|
|
|
if (this.save.isRunning) {
|
|
|
|
return this.save.last.then(() => {
|
|
|
|
transition.retry();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// we genuinely have unsaved data, show the modal
|
|
|
|
this.set('showUnsavedChangesModal', true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
leaveScreen() {
|
|
|
|
let transition = this.leaveScreenTransition;
|
|
|
|
|
|
|
|
if (!transition) {
|
|
|
|
this.notifications.showAlert('Sorry, there was an error in the application. Please let the Ghost team know what happened.', {type: 'error'});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// roll back changes on model props
|
|
|
|
this.integration.rollbackAttributes();
|
|
|
|
|
|
|
|
return transition.retry();
|
2018-10-19 19:34:53 +03:00
|
|
|
},
|
|
|
|
|
2018-10-19 19:37:27 +03:00
|
|
|
deleteIntegration() {
|
|
|
|
this.integration.destroyRecord();
|
|
|
|
},
|
|
|
|
|
|
|
|
confirmIntegrationDeletion() {
|
|
|
|
this.set('showDeleteIntegrationModal', true);
|
|
|
|
},
|
|
|
|
|
|
|
|
cancelIntegrationDeletion() {
|
|
|
|
this.set('showDeleteIntegrationModal', false);
|
|
|
|
},
|
2018-10-23 12:29:49 +03:00
|
|
|
|
2020-05-05 21:14:45 +03:00
|
|
|
confirmRegenerateKeyModal(apiKey) {
|
|
|
|
this.set('showRegenerateKeyModal', true);
|
|
|
|
this.set('isApiKeyRegenerated', false);
|
|
|
|
this.set('selectedApiKey', apiKey);
|
|
|
|
},
|
|
|
|
|
|
|
|
cancelRegenerateKeyModal() {
|
|
|
|
this.set('showRegenerateKeyModal', false);
|
|
|
|
},
|
|
|
|
|
|
|
|
regenerateKey() {
|
|
|
|
this.set('isApiKeyRegenerated', true);
|
|
|
|
},
|
|
|
|
|
2018-10-19 19:34:53 +03:00
|
|
|
confirmWebhookDeletion(webhook) {
|
|
|
|
this.set('webhookToDelete', webhook);
|
|
|
|
},
|
|
|
|
|
|
|
|
cancelWebhookDeletion() {
|
|
|
|
this.set('webhookToDelete', null);
|
|
|
|
},
|
|
|
|
|
|
|
|
deleteWebhook() {
|
|
|
|
return this.webhookToDelete.destroyRecord();
|
2018-10-18 02:18:29 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-04-06 13:47:28 +03:00
|
|
|
save: task(function* () {
|
2020-05-11 13:37:35 +03:00
|
|
|
return yield this.integration.save();
|
2020-04-06 13:47:28 +03:00
|
|
|
}),
|
|
|
|
|
2018-10-19 02:09:05 +03:00
|
|
|
copyContentKey: task(function* () {
|
2019-04-04 14:25:16 +03:00
|
|
|
copyTextToClipboard(this.integration.contentKey.secret);
|
2020-05-11 13:37:35 +03:00
|
|
|
yield timeout(this.isTesting ? 50 : 3000);
|
2018-10-19 02:09:05 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
copyAdminKey: task(function* () {
|
2019-04-04 14:25:16 +03:00
|
|
|
copyTextToClipboard(this.integration.adminKey.secret);
|
2020-05-11 13:37:35 +03:00
|
|
|
yield timeout(this.isTesting ? 50 : 3000);
|
2019-02-23 12:48:18 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
copyApiUrl: task(function* () {
|
2019-04-04 14:25:16 +03:00
|
|
|
copyTextToClipboard(this.apiUrl);
|
2020-05-11 13:37:35 +03:00
|
|
|
yield timeout(this.isTesting ? 50 : 3000);
|
2019-04-04 14:25:16 +03:00
|
|
|
})
|
2018-10-18 02:18:29 +03:00
|
|
|
});
|