cms/app-client/mirage/serializers/theme.js
author ymh <ymh.work@gmail.com>
Fri, 16 Dec 2016 17:43:07 +0100
changeset 474 245b4df137d3
parent 319 78990a8a069b
child 532 1190ea937f2d
permissions -rw-r--r--
Correct themes visualisation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
173
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import { JSONAPISerializer } from 'ember-cli-mirage';
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
import _ from 'lodash/lodash';
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
export default JSONAPISerializer.extend({
236
ac6928e86d14 Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents: 173
diff changeset
     6
474
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
     7
  serialize: function (response, request) {
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
     8
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
     9
    // Remove models with no name
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    10
    response.models = response.models.filter(element => element.label);
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    11
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    12
    let qParams = request.queryParams['theme'];
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    13
    if (qParams) {
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    14
      response.models = response.models
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    15
        .slice(0, response.models.length / Math.pow(2, qParams.length))
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    16
        .map(t => {
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    17
          t.count = Math.max(Math.floor(t.count / 2), 1);
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    18
          return t;
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    19
        });
173
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    }
236
ac6928e86d14 Adapt theme query limit and offset and sorting order in fixtures
Chloe Laisne <chloe.laisne@gmail.com>
parents: 173
diff changeset
    21
474
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    22
    let total = response.models.length;
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    23
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    24
    // Alphabetical order
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    25
    if (request.queryParams.sort === 'alphabetical') {
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    26
      response.models.sort(function (a, b) {
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    27
        var aLabel = a.label.toUpperCase();
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    28
        var bLabel = b.label.toUpperCase();
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    29
        if (aLabel < bLabel) { return -1; }
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    30
        if (aLabel > bLabel) { return 1; }
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    31
        return 0;
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    32
      });
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    33
      // Descending order
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    34
    } else if (request.queryParams.sort === 'descending') {
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    35
      response.models.sort(function (a, b) {
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    36
        return b.count - a.count;
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    37
      });
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    38
    }
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    39
    var array = [];
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    40
    if (typeof request.queryParams.index !== 'undefined' && typeof request.queryParams.limit !== 'undefined') {
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    41
      var begin = parseInt(request.queryParams.index) * parseInt(request.queryParams.limit) || 0;
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    42
      var end = begin + parseInt(request.queryParams.limit);
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    43
      array = response.models.slice(begin, end);
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    44
    } else {
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    45
      array = response.models;
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    46
    }
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    47
    return {
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    48
      themes: _(array).map((theme) => { return [theme.id, { count: theme.count, label: theme.label }]; }).object().value(),
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    49
      meta: {
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    50
        total: total
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    51
      }
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    52
    };
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    53
  }
245b4df137d3 Correct themes visualisation
ymh <ymh.work@gmail.com>
parents: 319
diff changeset
    54
173
cf7b221238fd Work on test fixtures:
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
});