mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-23 10:53:34 +03:00
Added utils
service with downloadFile()
method
no issue - pattern of downloading a file by creating an iframe and setting the `src` attribute was repeated throughout the codebase and was using a mix of native and jQuery patterns - added a `utils` service for housing one-off utility methods like this to avoid repetition and mixed patterns becoming more widespread (we want to get rid of jQuery usage)
This commit is contained in:
parent
611fbf5926
commit
fe48f7ed80
@ -33,6 +33,7 @@ export default class MembersController extends Controller {
|
|||||||
@service membersStats;
|
@service membersStats;
|
||||||
@service router;
|
@service router;
|
||||||
@service store;
|
@service store;
|
||||||
|
@service utils;
|
||||||
|
|
||||||
queryParams = [
|
queryParams = [
|
||||||
'label',
|
'label',
|
||||||
@ -265,15 +266,7 @@ export default class MembersController extends Controller {
|
|||||||
let downloadParams = new URLSearchParams(this.getApiQueryObject());
|
let downloadParams = new URLSearchParams(this.getApiQueryObject());
|
||||||
downloadParams.set('limit', 'all');
|
downloadParams.set('limit', 'all');
|
||||||
|
|
||||||
let iframe = document.getElementById('iframeDownload');
|
this.utils.downloadFile(`${exportUrl}?${downloadParams.toString()}`);
|
||||||
|
|
||||||
if (!iframe) {
|
|
||||||
iframe = document.createElement('iframe');
|
|
||||||
iframe.id = 'iframeDownload';
|
|
||||||
iframe.style.display = 'none';
|
|
||||||
document.body.append(iframe);
|
|
||||||
}
|
|
||||||
iframe.setAttribute('src', `${exportUrl}?${downloadParams.toString()}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
|
@ -42,6 +42,7 @@ export default Controller.extend({
|
|||||||
notifications: service(),
|
notifications: service(),
|
||||||
session: service(),
|
session: service(),
|
||||||
settings: service(),
|
settings: service(),
|
||||||
|
utils: service(),
|
||||||
|
|
||||||
importErrors: null,
|
importErrors: null,
|
||||||
importSuccessful: false,
|
importSuccessful: false,
|
||||||
@ -140,14 +141,7 @@ export default Controller.extend({
|
|||||||
},
|
},
|
||||||
|
|
||||||
downloadFile(endpoint) {
|
downloadFile(endpoint) {
|
||||||
let downloadURL = this.get('ghostPaths.url').api(endpoint);
|
this.utils.downloadFile(this.ghostPaths.url.api(endpoint));
|
||||||
let iframe = $('#iframeDownload');
|
|
||||||
|
|
||||||
if (iframe.length === 0) {
|
|
||||||
iframe = $('<iframe>', {id: 'iframeDownload'}).hide().appendTo('body');
|
|
||||||
}
|
|
||||||
|
|
||||||
iframe.attr('src', downloadURL);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
async saveOAuthSettings() {
|
async saveOAuthSettings() {
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
/* eslint-disable ghost/ember/alias-model-in-controller */
|
/* eslint-disable ghost/ember/alias-model-in-controller */
|
||||||
import $ from 'jquery';
|
|
||||||
import Controller from '@ember/controller';
|
import Controller from '@ember/controller';
|
||||||
import {computed} from '@ember/object';
|
import {computed} from '@ember/object';
|
||||||
import {isEmpty} from '@ember/utils';
|
import {isEmpty} from '@ember/utils';
|
||||||
@ -48,6 +47,7 @@ export default Controller.extend({
|
|||||||
notifications: service(),
|
notifications: service(),
|
||||||
session: service(),
|
session: service(),
|
||||||
settings: service(),
|
settings: service(),
|
||||||
|
utils: service(),
|
||||||
|
|
||||||
dirtyAttributes: false,
|
dirtyAttributes: false,
|
||||||
newNavItem: null,
|
newNavItem: null,
|
||||||
@ -127,14 +127,7 @@ export default Controller.extend({
|
|||||||
},
|
},
|
||||||
|
|
||||||
downloadTheme(theme) {
|
downloadTheme(theme) {
|
||||||
let downloadURL = `${this.get('ghostPaths.apiRoot')}/themes/${theme.name}/download/`;
|
this.utils.downloadFile(`${this.get('ghostPaths.apiRoot')}/themes/${theme.name}/download/`);
|
||||||
let iframe = $('#iframeDownload');
|
|
||||||
|
|
||||||
if (iframe.length === 0) {
|
|
||||||
iframe = $('<iframe>', {id: 'iframeDownload'}).hide().appendTo('body');
|
|
||||||
}
|
|
||||||
|
|
||||||
iframe.attr('src', downloadURL);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
deleteTheme(theme) {
|
deleteTheme(theme) {
|
||||||
|
@ -20,6 +20,7 @@ export default Controller.extend({
|
|||||||
notifications: service(),
|
notifications: service(),
|
||||||
session: service(),
|
session: service(),
|
||||||
slugGenerator: service(),
|
slugGenerator: service(),
|
||||||
|
utils: service(),
|
||||||
|
|
||||||
personalToken: null,
|
personalToken: null,
|
||||||
limitErrorMessage: null,
|
limitErrorMessage: null,
|
||||||
@ -381,18 +382,7 @@ export default Controller.extend({
|
|||||||
},
|
},
|
||||||
|
|
||||||
_exportDb(filename) {
|
_exportDb(filename) {
|
||||||
let exportUrl = this.get('ghostPaths.url').api('db');
|
this.utils.downloadFile(`${this.ghostPaths.url.api('db')}?filename=${filename}`);
|
||||||
let downloadURL = `${exportUrl}?filename=${filename}`;
|
|
||||||
let iframe = document.getElementById('iframeDownload');
|
|
||||||
|
|
||||||
if (!iframe) {
|
|
||||||
iframe = document.createElement('iframe');
|
|
||||||
iframe.id = 'iframeDownload';
|
|
||||||
iframe.style.display = 'none';
|
|
||||||
document.body.append(iframe);
|
|
||||||
}
|
|
||||||
|
|
||||||
iframe.setAttribute('src', downloadURL);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
deleteUser: task(function *() {
|
deleteUser: task(function *() {
|
||||||
|
16
ghost/admin/app/services/utils.js
Normal file
16
ghost/admin/app/services/utils.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import Service from '@ember/service';
|
||||||
|
|
||||||
|
export default class UtilsService extends Service {
|
||||||
|
downloadFile(url) {
|
||||||
|
let iframe = document.getElementById('iframeDownload');
|
||||||
|
|
||||||
|
if (!iframe) {
|
||||||
|
iframe = document.createElement('iframe');
|
||||||
|
iframe.id = 'iframeDownload';
|
||||||
|
iframe.style.display = 'none';
|
||||||
|
document.body.append(iframe);
|
||||||
|
}
|
||||||
|
|
||||||
|
iframe.setAttribute('src', url);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user