c1b59ce2f76851edae617791584a4648ab32b552
[archive/20170607/tools/tic.git] / public / src / js / model / JobPagingModel.js
1 define([
2     'jquery',
3     'lodash',
4     'js/util',
5     'js/logger'
6 ], function (
7     $,
8     _,
9     Util,
10     Logger
11 ) {
12     'use strict';
13
14     var logger = Logger('model/JobPagingModel.js');
15
16     /**
17      * paramObj = {
18      *      totalCount: '',
19      *      currentPageNum: ''
20      * };
21      */
22     var JobPagingModel = function (paramObj) {
23         logger.info('new JobPagingModel');
24
25         this.totalCount = 0;
26         this.totalPage = 0;
27
28         this.currentPoint = 1;
29         this.startPoint = 1;
30         this.endPoint = 1;
31
32         this.maxPoint = 5;
33         this.itemsOnPage = 10;
34
35         this.init(paramObj);
36     }
37
38     JobPagingModel.prototype.init = function (obj) {
39         logger.info('JobPagingModel.init : ' + JSON.stringify(obj));
40
41         this.setTotalCount(obj.totalCount);
42         this.setCurrentPoint(obj.currentPageNum);
43     };
44
45     JobPagingModel.prototype.getMaxPoint = function () {
46         return this.maxPoint;
47     };
48
49     JobPagingModel.prototype.setMaxPoint = function (value) {
50         this.maxPoint = value;
51     };
52
53     JobPagingModel.prototype.getEndPoint = function () {
54         return this.endPoint;
55     };
56
57     JobPagingModel.prototype.setEndPoint = function () {
58         var total, start, max, diff;
59         total = this.getTotalPage();
60         start = this.getStartPoint();
61         max = this.getMaxPoint();
62         diff = total - start;
63
64         if (diff <= 0) {
65             this.endPoint = start;
66         } else if (diff < max) {
67             this.endPoint = total;
68         } else {
69             this.endPoint = start + max - 1;
70         }
71
72     };
73
74     JobPagingModel.prototype.getStartPoint = function () {
75         return this.startPoint;
76     };
77
78     JobPagingModel.prototype.setStartPoint = function () {
79         var current, max;
80         current = this.getCurrentPoint();
81         max = this.getMaxPoint();
82         
83         if (current <= max) {
84             this.startPoint = 1;
85         } else {
86             this.startPoint = (Math.floor(current / max) * max) + 1;
87         }
88     };
89
90     JobPagingModel.prototype.getCurrentPoint = function () {
91         return this.currentPoint;
92     };
93
94     JobPagingModel.prototype.setCurrentPoint = function (value) {
95         this.currentPoint = value || 1;
96
97         this.setStartPoint();
98         this.setEndPoint();
99     };
100
101     JobPagingModel.prototype.getTotalPage = function (value) {
102         return this.totalPage;
103     };
104
105     JobPagingModel.prototype.setTotalPage = function (value) {
106         this.totalPage = Math.ceil(this.getTotalCount() / this.itemsOnPage);
107     };
108
109     JobPagingModel.prototype.getTotalCount = function () {
110         return this.totalCount;
111     };
112
113     JobPagingModel.prototype.setTotalCount = function (value) {
114         this.totalCount = value || 0;
115
116         this.setTotalPage();
117     };
118
119     return JobPagingModel;
120
121 });