Blame view

build/Pagination/Pagination.js 7.72 KB
3a3ecabe   Imshann   init
1
  import template from "./Pagination.html";
dd962f77   Imshann   优化
2
3
  import style from "antd/lib/pagination/style/index.css";
  angular.module("esNgAntd").directive("esPagination", function (esNgAntd) {
3a3ecabe   Imshann   init
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
      return {
          controllerAs: "esPagination",
          restrict: "E",
          transclude: true,
          replace: true,
          scope: {
              defaultCurrent: "@",
              current: "@",
              total: "@",
              defaultPageSize: "@",
              pageSize: "@",
              onChange: "&",
              onShowSizeChange: "&",
              showQuickJumper: "@",
              showSizeChanger: "@",
710b4ac0   Imshann   优化
19
              size: "@",
80abca23   Imshann   feat(pagination):...
20
              showTotal: "&",
3a3ecabe   Imshann   init
21
22
          },
          template: template,
710b4ac0   Imshann   优化
23
          controller: function ($scope, $element, $attrs) {
3a3ecabe   Imshann   init
24
25
26
27
28
29
30
31
32
33
              this.getContext = function () {
                  return $scope;
              };
  
              $scope.state = {
                  total: null,
                  current: null,
                  pageSize: null,
                  pageNum: null,
                  pageNumList: null,
4b23b387   Imshann   feat(pagination):...
34
                  defaultPageSize: $scope.defaultPageSize || 10,
3a3ecabe   Imshann   init
35
              };
80abca23   Imshann   feat(pagination):...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
              $scope.watch = {
                  total: function (newVal, oldVal) {
                      if (newVal && oldVal) {
                          $scope.state.total = Number(newVal);
                          $scope.state.pageNum = $scope.getPageNum();
                          $scope.state.pageNumList = $scope.getPageNumList();
                      }
                  },
              };
  
              for (const key in $scope.watch) {
                  $scope.$watch(key, $scope.watch[key], true);
              }
  
              $scope.handleShowTotal = function () {
                  return $scope.showTotal({
                      total: $scope.state.total,
                  });
              };
3a3ecabe   Imshann   init
55
  
061629e7   Imshann   add
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
              $scope.getItemLinkClassName = function (value) {
                  if (typeof value === "number") {
                      return (
                          "ant-pagination-item" +
                          ($scope.state.current === value
                              ? " ant-pagination-item-active"
                              : "")
                      );
                  } else {
                      return "ant-pagination-jump-next ant-pagination-jump-next-custom-icon";
                  }
              };
  
              $scope.getPageNumList = function () {
                  let pageNumList = [$scope.state.current];
                  let pageNum = $scope.getPageNum();
  
                  if (pageNum <= 7 || $scope.state.current - 1 < 4) {
                      for (let i = $scope.state.current - 1; i > 0; i--) {
                          pageNumList.unshift(i);
                      }
                  } else {
                      let len =
                          $scope.state.current - 1 > 2
                              ? 2
                              : $scope.state.current - 1;
  
                      for (let i = 1; i <= len; i++) {
                          pageNumList.unshift($scope.state.current - i);
                      }
  
                      if ($scope.state.current - 2 > 2) {
                          pageNumList.unshift("prev");
                          pageNumList.unshift(1);
                      }
                  }
  
                  if (pageNum <= 7 || pageNum - $scope.state.current < 4) {
                      for (let i = $scope.state.current + 1; i <= pageNum; i++) {
                          pageNumList.push(i);
                      }
                  } else {
                      let limit =
                          3 - $scope.state.current > 0
                              ? 2 + (3 - $scope.state.current)
                              : 2;
                      let len =
                          pageNum - $scope.state.current > limit
                              ? limit
                              : pageNum - $scope.state.current;
  
                      for (let i = 1; i <= len; i++) {
                          pageNumList.push($scope.state.current + i);
                      }
  
                      if (pageNum - $scope.state.current > 2) {
                          pageNumList.push("next");
                          pageNumList.push(pageNum);
                      }
                  }
  
                  return pageNumList;
              };
  
3a3ecabe   Imshann   init
120
121
122
123
124
125
126
127
128
              $scope.getPageNum = function () {
                  return (
                      Math.ceil(
                          $scope.state.total /
                              ($scope.pageSize || $scope.defaultPageSize || 10)
                      ) || 1
                  );
              };
  
061629e7   Imshann   add
129
130
131
132
              $scope.getPopupContainer = function () {
                  return $element[0].querySelector(".ant-pagination-options");
              };
  
3a3ecabe   Imshann   init
133
134
135
136
137
138
139
140
              $scope.handleNext = function () {
                  if ($scope.state.current === $scope.state.pageNum) {
                      return false;
                  }
  
                  $scope.handleClick(++$scope.state.current);
              };
  
3a3ecabe   Imshann   init
141
142
143
144
145
146
147
148
149
              $scope.handlePrev = function () {
                  if ($scope.state.current === 1) {
                      return false;
                  }
  
                  $scope.handleClick(--$scope.state.current);
              };
  
              $scope.handleClick = function (value) {
e7610b68   Imshann   feat(pagination):...
150
151
152
153
                  if (value === $scope.state.current) {
                      return;
                  }
  
061629e7   Imshann   add
154
155
156
157
158
159
160
161
                  if (value === "next") {
                      value = $scope.state.current + 5;
                  }
  
                  if (value === "prev") {
                      value = $scope.state.current - 5;
                  }
  
e7610b68   Imshann   feat(pagination):...
162
                  $scope.setCurrent($scope.getCurrent(value));
3a3ecabe   Imshann   init
163
164
165
166
167
168
169
170
171
172
173
174
175
              };
  
              $scope.handleChange = function () {
                  let current = $scope.state.current;
  
                  if ($scope.state.current > $scope.state.pageNum) {
                      current = $scope.state.pageNum;
                  } else if ($scope.state.current < 1) {
                      current = 1;
                  }
  
                  $scope.onChange({
                      page: current,
e7610b68   Imshann   feat(pagination):...
176
                      pageSize: parseInt($scope.state.pageSize),
3a3ecabe   Imshann   init
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
                  });
              };
  
              $scope.handleSelectChange = function (value) {
                  $scope.state.current = 1;
                  $scope.state.pageSize = parseInt(value);
                  $scope.handleChange();
              };
  
              $scope.getCurrent = function (number) {
                  if (number > $scope.state.pageNum) {
                      return $scope.state.pageNum;
                  }
  
                  if (number < 1) {
                      return 1;
                  }
  
                  return parseInt(number);
              };
  
1b6f912f   Imshann   优化
198
              $scope.setCurrent = function (value) {
3a3ecabe   Imshann   init
199
200
201
202
203
                  if (!value) {
                      return;
                  }
  
                  $scope.state.current = $scope.getCurrent(value);
061629e7   Imshann   add
204
                  $scope.state.pageNumList = $scope.getPageNumList();
3a3ecabe   Imshann   init
205
                  $scope.handleChange();
1b6f912f   Imshann   优化
206
207
208
209
              };
  
              $scope.handleBlur = function (event) {
                  $scope.setCurrent(event.target.value);
3a3ecabe   Imshann   init
210
211
                  event.target.value = null;
              };
1b6f912f   Imshann   优化
212
213
214
215
216
217
218
  
              $scope.onKeyPress = function (event) {
                  if (event.keyCode === 13 || event.keyCode === 32) {
                      $scope.setCurrent(event.target.value);
                      event.target.value = null;
                  }
              };
3a3ecabe   Imshann   init
219
220
          },
          link: function ($scope, $element, $attrs, $controllers, $transclude) {
dd962f77   Imshann   优化
221
              esNgAntd.createStyle("ant-pagination", style);
3a3ecabe   Imshann   init
222
223
224
225
226
227
228
              $scope.state.total = Number($scope.total || 0);
              $scope.state.current = Number(
                  $scope.current || $scope.defaultCurrent || 1
              );
              $scope.state.pageSize =
                  $scope.pageSize || $scope.defaultPageSize || 10;
              $scope.state.pageNum = $scope.getPageNum();
061629e7   Imshann   add
229
              $scope.state.pageNumList = $scope.getPageNumList();
3a3ecabe   Imshann   init
230
231
232
          },
      };
  });