Blame view

build/Pagination/Pagination.js 8.04 KB
3a3ecabe   Imshann   init
1
  import template from "./Pagination.html";
dd962f77   Imshann   优化
2
  import style from "antd/lib/pagination/style/index.css";
81f8a467   Imshann   调整组件前缀
3
  angular.module("esNgAntd").directive("antdPagination", function (esNgAntd) {
3a3ecabe   Imshann   init
4
      return {
81f8a467   Imshann   调整组件前缀
5
          controllerAs: "antdPagination",
3a3ecabe   Imshann   init
6
7
8
9
10
11
12
13
14
15
16
17
18
          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
              $scope.watch = {
5b248282   Imshann   优化
37
38
                  total: function (newVal) {
                      if (newVal) {
80abca23   Imshann   feat(pagination):...
39
                          $scope.state.total = Number(newVal);
5b248282   Imshann   优化
40
                          $scope.reset();
80abca23   Imshann   feat(pagination):...
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
                          $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
56
  
061629e7   Imshann   add
57
58
59
60
61
62
63
64
65
66
67
68
69
              $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";
                  }
              };
  
5b248282   Imshann   优化
70
71
72
73
              $scope.reset = function () {
                  $scope.state.current = 1;
              };
  
061629e7   Imshann   add
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
120
121
122
123
124
              $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
125
126
127
128
              $scope.getPageNum = function () {
                  return (
                      Math.ceil(
                          $scope.state.total /
fe4cf8e2   Imshann   修复BUG
129
130
131
                              ($scope.state.pageSize ||
                                  $scope.defaultPageSize ||
                                  10)
3a3ecabe   Imshann   init
132
133
134
135
                      ) || 1
                  );
              };
  
061629e7   Imshann   add
136
137
138
139
              $scope.getPopupContainer = function () {
                  return $element[0].querySelector(".ant-pagination-options");
              };
  
3a3ecabe   Imshann   init
140
141
142
143
144
              $scope.handleNext = function () {
                  if ($scope.state.current === $scope.state.pageNum) {
                      return false;
                  }
  
5b248282   Imshann   优化
145
                  $scope.handleClick($scope.state.current + 1);
3a3ecabe   Imshann   init
146
147
              };
  
3a3ecabe   Imshann   init
148
149
150
151
152
              $scope.handlePrev = function () {
                  if ($scope.state.current === 1) {
                      return false;
                  }
  
5b248282   Imshann   优化
153
                  $scope.handleClick($scope.state.current - 1);
3a3ecabe   Imshann   init
154
155
156
              };
  
              $scope.handleClick = function (value) {
e7610b68   Imshann   feat(pagination):...
157
158
159
160
                  if (value === $scope.state.current) {
                      return;
                  }
  
061629e7   Imshann   add
161
162
163
164
165
166
167
168
                  if (value === "next") {
                      value = $scope.state.current + 5;
                  }
  
                  if (value === "prev") {
                      value = $scope.state.current - 5;
                  }
  
e7610b68   Imshann   feat(pagination):...
169
                  $scope.setCurrent($scope.getCurrent(value));
3a3ecabe   Imshann   init
170
171
172
173
              };
  
              $scope.handleChange = function () {
                  let current = $scope.state.current;
fe4cf8e2   Imshann   修复BUG
174
175
                  $scope.state.pageNum = $scope.getPageNum();
                  $scope.state.pageNumList = $scope.getPageNumList();
3a3ecabe   Imshann   init
176
177
178
179
180
181
182
183
184
  
                  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):...
185
                      pageSize: parseInt($scope.state.pageSize),
3a3ecabe   Imshann   init
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
                  });
              };
  
              $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   优化
207
              $scope.setCurrent = function (value) {
3a3ecabe   Imshann   init
208
209
210
211
212
                  if (!value) {
                      return;
                  }
  
                  $scope.state.current = $scope.getCurrent(value);
061629e7   Imshann   add
213
                  $scope.state.pageNumList = $scope.getPageNumList();
3a3ecabe   Imshann   init
214
                  $scope.handleChange();
1b6f912f   Imshann   优化
215
216
217
218
              };
  
              $scope.handleBlur = function (event) {
                  $scope.setCurrent(event.target.value);
3a3ecabe   Imshann   init
219
220
                  event.target.value = null;
              };
1b6f912f   Imshann   优化
221
222
223
224
225
226
227
  
              $scope.onKeyPress = function (event) {
                  if (event.keyCode === 13 || event.keyCode === 32) {
                      $scope.setCurrent(event.target.value);
                      event.target.value = null;
                  }
              };
3a3ecabe   Imshann   init
228
229
          },
          link: function ($scope, $element, $attrs, $controllers, $transclude) {
dd962f77   Imshann   优化
230
              esNgAntd.createStyle("ant-pagination", style);
3a3ecabe   Imshann   init
231
232
233
234
235
236
237
              $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
238
              $scope.state.pageNumList = $scope.getPageNumList();
3a3ecabe   Imshann   init
239
240
241
          },
      };
  });