Blame view

build/Pagination/Pagination.js 7.02 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: "@",
3a3ecabe   Imshann   init
20
21
          },
          template: template,
710b4ac0   Imshann   优化
22
          controller: function ($scope, $element, $attrs) {
3a3ecabe   Imshann   init
23
24
25
26
27
28
29
30
31
32
33
34
              this.getContext = function () {
                  return $scope;
              };
  
              $scope.state = {
                  total: null,
                  current: null,
                  pageSize: null,
                  pageNum: null,
                  pageNumList: null,
              };
  
061629e7   Imshann   add
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
              $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
99
100
101
102
103
104
105
106
107
              $scope.getPageNum = function () {
                  return (
                      Math.ceil(
                          $scope.state.total /
                              ($scope.pageSize || $scope.defaultPageSize || 10)
                      ) || 1
                  );
              };
  
061629e7   Imshann   add
108
109
110
111
              $scope.getPopupContainer = function () {
                  return $element[0].querySelector(".ant-pagination-options");
              };
  
3a3ecabe   Imshann   init
112
113
114
115
116
117
118
119
              $scope.handleNext = function () {
                  if ($scope.state.current === $scope.state.pageNum) {
                      return false;
                  }
  
                  $scope.handleClick(++$scope.state.current);
              };
  
3a3ecabe   Imshann   init
120
121
122
123
124
125
126
127
128
              $scope.handlePrev = function () {
                  if ($scope.state.current === 1) {
                      return false;
                  }
  
                  $scope.handleClick(--$scope.state.current);
              };
  
              $scope.handleClick = function (value) {
061629e7   Imshann   add
129
130
131
132
133
134
135
136
137
                  if (value === "next") {
                      value = $scope.state.current + 5;
                  }
  
                  if (value === "prev") {
                      value = $scope.state.current - 5;
                  }
  
                  $scope.setCurrent($scope.getCurrent(value)); // 更新回调
3a3ecabe   Imshann   init
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  
                  $scope.onChange({
                      page: $scope.state.current,
                      pageSize: $scope.state.pageSize,
                  });
              };
  
              $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,
                      pageSize: $scope.state.pageSize,
                  });
              };
  
              $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   优化
178
              $scope.setCurrent = function (value) {
3a3ecabe   Imshann   init
179
180
181
182
183
                  if (!value) {
                      return;
                  }
  
                  $scope.state.current = $scope.getCurrent(value);
061629e7   Imshann   add
184
                  $scope.state.pageNumList = $scope.getPageNumList();
3a3ecabe   Imshann   init
185
                  $scope.handleChange();
1b6f912f   Imshann   优化
186
187
188
189
              };
  
              $scope.handleBlur = function (event) {
                  $scope.setCurrent(event.target.value);
3a3ecabe   Imshann   init
190
191
                  event.target.value = null;
              };
1b6f912f   Imshann   优化
192
193
194
195
196
197
198
  
              $scope.onKeyPress = function (event) {
                  if (event.keyCode === 13 || event.keyCode === 32) {
                      $scope.setCurrent(event.target.value);
                      event.target.value = null;
                  }
              };
3a3ecabe   Imshann   init
199
200
          },
          link: function ($scope, $element, $attrs, $controllers, $transclude) {
dd962f77   Imshann   优化
201
              esNgAntd.createStyle("ant-pagination", style);
3a3ecabe   Imshann   init
202
203
204
205
206
207
208
              $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
209
              $scope.state.pageNumList = $scope.getPageNumList();
3a3ecabe   Imshann   init
210
211
212
          },
      };
  });