Ghost/ghost/admin/mirage/utils.js
Kevin Ansfield 6a3cfc2ca8 Use token input to allow selection of multiple authors in PSM
requires https://github.com/TryGhost/Ghost/pull/9426
- fixed default token component display in {{gh-token-input}}
    - if no `tokenComponent` is passed to `{{gh-token-input}}` then it should default to the ember-drag-drop `draggable-object` component but instead it didn't output anything
    - put `draggable-object` in quotes because `{{component}}` needs a component name rather than an object
    - rename `option` attribute to `content` to match the default `{{draggable-object}}` interface
- add embedded `authors` attr to the Post model
    - ensure authors is populated when starting new post
    - add validation for empty authors list
- swap author dropdown for a token input in PSM
- show all post authors in posts list
- update tests for `authors`
  - always provide through an authors array
  - fix mirage serialisation for paginated responses (embedded records were not being serialised)
- unify tags and author inputs design
  - remove highlight of primary tags
  - highlight internal tags
  - remove unnecessary/redundant title attributes on tags
  - use SVG icon for "remove option" button in token inputs
2018-03-27 18:50:52 +01:00

70 lines
1.6 KiB
JavaScript

/* eslint-disable max-statements-per-line */
import {Response} from 'ember-cli-mirage';
export function paginatedResponse(modelName) {
return function (schema, request) {
let page = +request.queryParams.page || 1;
let limit = +request.queryParams.limit || 15;
let collection = schema[modelName].all();
return paginateModelCollection(modelName, collection, page, limit);
};
}
export function paginateModelCollection(modelName, collection, page, limit) {
let pages, next, prev, models;
if (limit === 'all') {
pages = 1;
} else {
limit = +limit;
let start = (page - 1) * limit;
let end = start + limit;
pages = Math.ceil(collection.models.length / limit);
models = collection.models.slice(start, end);
if (start > 0) {
prev = page - 1;
}
if (end < collection.models.length) {
next = page + 1;
}
}
collection.meta = {
pagination: {
page,
limit,
pages,
total: collection.models.length,
next: next || null,
prev: prev || null
}
};
if (models) {
collection.models = models;
}
return collection;
}
export function maintenanceResponse() {
return new Response(503, {}, {
errors: [{
errorType: 'Maintenance'
}]
});
}
export function versionMismatchResponse() {
return new Response(400, {}, {
errors: [{
errorType: 'VersionMismatchError'
}]
});
}