2018-01-11 01:57:43 +03:00
|
|
|
/* eslint-disable ghost/ember/alias-model-in-controller */
|
2016-07-06 22:47:30 +03:00
|
|
|
import $ from 'jquery';
|
2017-08-22 10:53:26 +03:00
|
|
|
import Controller from '@ember/controller';
|
2016-05-24 15:06:59 +03:00
|
|
|
import PaginationMixin from 'ghost-admin/mixins/pagination';
|
2017-05-29 21:50:03 +03:00
|
|
|
import Table from 'ember-light-table';
|
2016-05-24 15:06:59 +03:00
|
|
|
import ghostPaths from 'ghost-admin/utils/ghost-paths';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {assign} from '@ember/polyfills';
|
|
|
|
import {computed} from '@ember/object';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2016-04-15 17:45:50 +03:00
|
|
|
|
2016-06-11 19:52:36 +03:00
|
|
|
export default Controller.extend(PaginationMixin, {
|
2018-01-11 20:43:23 +03:00
|
|
|
session: service(),
|
2016-04-15 17:45:50 +03:00
|
|
|
|
|
|
|
queryParams: ['order', 'direction'],
|
|
|
|
order: 'created_at',
|
|
|
|
direction: 'desc',
|
|
|
|
|
|
|
|
paginationModel: 'subscriber',
|
|
|
|
|
|
|
|
total: 0,
|
|
|
|
table: null,
|
2016-05-08 16:15:04 +03:00
|
|
|
subscriberToDelete: null,
|
2016-04-15 17:45:50 +03:00
|
|
|
|
|
|
|
// paginationSettings is replaced by the pagination mixin so we need a
|
|
|
|
// getter/setter CP here so that we don't lose the dynamic order param
|
|
|
|
paginationSettings: computed('order', 'direction', {
|
|
|
|
get() {
|
|
|
|
let order = this.get('order');
|
|
|
|
let direction = this.get('direction');
|
|
|
|
|
|
|
|
let currentSettings = this._paginationSettings || {
|
|
|
|
limit: 30
|
|
|
|
};
|
|
|
|
|
|
|
|
return assign({}, currentSettings, {
|
|
|
|
order: `${order} ${direction}`
|
|
|
|
});
|
|
|
|
},
|
|
|
|
set(key, value) {
|
|
|
|
this._paginationSettings = value;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
columns: computed('order', 'direction', function () {
|
|
|
|
let order = this.get('order');
|
|
|
|
let direction = this.get('direction');
|
|
|
|
|
|
|
|
return [{
|
2017-08-17 13:46:10 +03:00
|
|
|
label: 'Email Address',
|
2016-04-15 17:45:50 +03:00
|
|
|
valuePath: 'email',
|
|
|
|
sorted: order === 'email',
|
2016-10-07 15:58:53 +03:00
|
|
|
ascending: direction === 'asc',
|
|
|
|
classNames: ['gh-subscribers-table-email-cell'],
|
|
|
|
cellClassNames: ['gh-subscribers-table-email-cell']
|
2016-04-15 17:45:50 +03:00
|
|
|
}, {
|
|
|
|
label: 'Subscription Date',
|
2016-06-14 11:11:59 +03:00
|
|
|
valuePath: 'createdAtUTC',
|
2016-04-15 17:45:50 +03:00
|
|
|
format(value) {
|
|
|
|
return value.format('MMMM DD, YYYY');
|
|
|
|
},
|
|
|
|
sorted: order === 'created_at',
|
2016-10-07 15:58:53 +03:00
|
|
|
ascending: direction === 'asc',
|
|
|
|
classNames: ['gh-subscribers-table-date-cell'],
|
|
|
|
cellClassNames: ['gh-subscribers-table-date-cell']
|
2016-04-15 17:45:50 +03:00
|
|
|
}, {
|
|
|
|
label: 'Status',
|
|
|
|
valuePath: 'status',
|
|
|
|
sorted: order === 'status',
|
2016-10-07 15:58:53 +03:00
|
|
|
ascending: direction === 'asc',
|
|
|
|
classNames: ['gh-subscribers-table-status-cell'],
|
|
|
|
cellClassNames: ['gh-subscribers-table-status-cell']
|
2016-05-08 16:15:04 +03:00
|
|
|
}, {
|
|
|
|
label: '',
|
|
|
|
sortable: false,
|
|
|
|
cellComponent: 'gh-subscribers-table-delete-cell',
|
2016-10-07 15:58:53 +03:00
|
|
|
align: 'right',
|
|
|
|
classNames: ['gh-subscribers-table-delete-cell'],
|
|
|
|
cellClassNames: ['gh-subscribers-table-delete-cell']
|
2016-04-15 17:45:50 +03:00
|
|
|
}];
|
|
|
|
}),
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
loadFirstPage() {
|
|
|
|
let table = this.get('table');
|
|
|
|
|
|
|
|
return this._super(...arguments).then((results) => {
|
|
|
|
table.addRows(results);
|
|
|
|
return results;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
loadNextPage() {
|
|
|
|
let table = this.get('table');
|
|
|
|
|
|
|
|
return this._super(...arguments).then((results) => {
|
|
|
|
table.addRows(results);
|
|
|
|
return results;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
sortByColumn(column) {
|
|
|
|
let table = this.get('table');
|
|
|
|
|
|
|
|
if (column.sorted) {
|
|
|
|
this.setProperties({
|
2016-06-14 11:11:59 +03:00
|
|
|
order: column.get('valuePath').trim().replace(/UTC$/, '').underscore(),
|
2016-04-15 17:45:50 +03:00
|
|
|
direction: column.ascending ? 'asc' : 'desc'
|
|
|
|
});
|
|
|
|
table.setRows([]);
|
|
|
|
this.send('loadFirstPage');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addSubscriber(subscriber) {
|
|
|
|
this.get('table').insertRowAt(0, subscriber);
|
|
|
|
this.incrementProperty('total');
|
|
|
|
},
|
|
|
|
|
2016-05-08 16:15:04 +03:00
|
|
|
deleteSubscriber(subscriber) {
|
|
|
|
this.set('subscriberToDelete', subscriber);
|
|
|
|
},
|
|
|
|
|
|
|
|
confirmDeleteSubscriber() {
|
|
|
|
let subscriber = this.get('subscriberToDelete');
|
|
|
|
|
|
|
|
return subscriber.destroyRecord().then(() => {
|
|
|
|
this.set('subscriberToDelete', null);
|
|
|
|
this.get('table').removeRow(subscriber);
|
|
|
|
this.decrementProperty('total');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
cancelDeleteSubscriber() {
|
|
|
|
this.set('subscriberToDelete', null);
|
|
|
|
},
|
|
|
|
|
2016-04-15 17:45:50 +03:00
|
|
|
reset() {
|
|
|
|
this.get('table').setRows([]);
|
|
|
|
this.send('loadFirstPage');
|
|
|
|
},
|
|
|
|
|
|
|
|
exportData() {
|
|
|
|
let exportUrl = ghostPaths().url.api('subscribers/csv');
|
|
|
|
let accessToken = this.get('session.data.authenticated.access_token');
|
|
|
|
let downloadURL = `${exportUrl}?access_token=${accessToken}`;
|
|
|
|
let iframe = $('#iframeDownload');
|
|
|
|
|
|
|
|
if (iframe.length === 0) {
|
|
|
|
iframe = $('<iframe>', {id: 'iframeDownload'}).hide().appendTo('body');
|
|
|
|
}
|
|
|
|
|
|
|
|
iframe.attr('src', downloadURL);
|
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
initializeTable() {
|
|
|
|
this.set('table', new Table(this.get('columns'), this.get('subscribers')));
|
|
|
|
},
|
|
|
|
|
|
|
|
// capture the total from the server any time we fetch a new page
|
|
|
|
didReceivePaginationMeta(meta) {
|
|
|
|
if (meta && meta.pagination) {
|
|
|
|
this.set('total', meta.pagination.total);
|
|
|
|
}
|
2016-04-15 17:45:50 +03:00
|
|
|
}
|
|
|
|
});
|