Blame view

build/Pagination/Pagination.js 7.92 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
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
              $scope.getPageNum = function () {
                  return (
                      Math.ceil(
                          $scope.state.total /
fe4cf8e2   Imshann   修复BUG
124
125
126
                              ($scope.state.pageSize ||
                                  $scope.defaultPageSize ||
                                  10)
3a3ecabe   Imshann   init
127
128
129
130
                      ) || 1
                  );
              };
  
061629e7   Imshann   add
131
132
133
134
              $scope.getPopupContainer = function () {
                  return $element[0].querySelector(".ant-pagination-options");
              };
  
3a3ecabe   Imshann   init
135
136
137
138
139
140
141
142
              $scope.handleNext = function () {
                  if ($scope.state.current === $scope.state.pageNum) {
                      return false;
                  }
  
                  $scope.handleClick(++$scope.state.current);
              };
  
3a3ecabe   Imshann   init
143
144
145
146
147
148
149
150
151
              $scope.handlePrev = function () {
                  if ($scope.state.current === 1) {
                      return false;
                  }
  
                  $scope.handleClick(--$scope.state.current);
              };
  
              $scope.handleClick = function (value) {
e7610b68   Imshann   feat(pagination):...
152
153
154
155
                  if (value === $scope.state.current) {
                      return;
                  }
  
061629e7   Imshann   add
156
157
158
159
160
161
162
163
                  if (value === "next") {
                      value = $scope.state.current + 5;
                  }
  
                  if (value === "prev") {
                      value = $scope.state.current - 5;
                  }
  
e7610b68   Imshann   feat(pagination):...
164
                  $scope.setCurrent($scope.getCurrent(value));
3a3ecabe   Imshann   init
165
166
167
168
              };
  
              $scope.handleChange = function () {
                  let current = $scope.state.current;
fe4cf8e2   Imshann   修复BUG
169
170
                  $scope.state.pageNum = $scope.getPageNum();
                  $scope.state.pageNumList = $scope.getPageNumList();
3a3ecabe   Imshann   init
171
172
173
174
175
176
177
178
179
  
                  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):...
180
                      pageSize: parseInt($scope.state.pageSize),
3a3ecabe   Imshann   init
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
                  });
              };
  
              $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   优化
202
              $scope.setCurrent = function (value) {
3a3ecabe   Imshann   init
203
204
205
206
207
                  if (!value) {
                      return;
                  }
  
                  $scope.state.current = $scope.getCurrent(value);
061629e7   Imshann   add
208
                  $scope.state.pageNumList = $scope.getPageNumList();
3a3ecabe   Imshann   init
209
                  $scope.handleChange();
1b6f912f   Imshann   优化
210
211
212
213
              };
  
              $scope.handleBlur = function (event) {
                  $scope.setCurrent(event.target.value);
3a3ecabe   Imshann   init
214
215
                  event.target.value = null;
              };
1b6f912f   Imshann   优化
216
217
218
219
220
221
222
  
              $scope.onKeyPress = function (event) {
                  if (event.keyCode === 13 || event.keyCode === 32) {
                      $scope.setCurrent(event.target.value);
                      event.target.value = null;
                  }
              };
3a3ecabe   Imshann   init
223
224
          },
          link: function ($scope, $element, $attrs, $controllers, $transclude) {
dd962f77   Imshann   优化
225
              esNgAntd.createStyle("ant-pagination", style);
3a3ecabe   Imshann   init
226
227
228
229
230
231
232
              $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
233
              $scope.state.pageNumList = $scope.getPageNumList();
3a3ecabe   Imshann   init
234
235
236
          },
      };
  });