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