UNPKG

1.57 kBJavaScriptView Raw
1/**
2 * @copyright Copyright (c) 2019 Maxim Khorin <maksimovichu@gmail.com>
3 */
4'use strict';
5
6const Base = require('./DataProvider');
7
8module.exports = class ActiveDataProvider extends Base {
9
10 prepareTotalCount () {
11 return this.query.count();
12 }
13
14 prepareModels () {
15 if (this.pagination) {
16 this.pagination.totalCount = this.totalCount;
17 if (this.pagination.pageSize > 0) {
18 this.query.limit(this.pagination.getLimit()).offset(this.pagination.getOffset());
19 }
20 }
21 if (this.sort) {
22 this.query.addOrder(this.sort.getOrders());
23 }
24 return this.query.all();
25 }
26
27 setSort (data) {
28 super.setSort(data);
29 if (this.sort) {
30 const model = this.query.model;
31 const names = Object.keys(this.sort.attrs);
32 names.length
33 ? this.setSortByNames(names, model)
34 : this.setSortByAttrNames(model);
35 }
36 }
37
38 setSortByNames (names, model) {
39 for (const name of names) {
40 if (!this.sort.attrs[name].label) {
41 this.sort.attrs[name].label = model.getAttrLabel(name);
42 }
43 }
44 }
45
46 setSortByAttrNames (model) {
47 for (const name of model.ATTRS) {
48 this.sort.attrs[name] = {
49 asc: {[name]: this.sort.ASC},
50 desc: {[name]: this.sort.DESC},
51 label: model.getAttrLabel(name)
52 };
53 }
54 }
55};
\No newline at end of file