Blame view

build/Popover/Popover.js 8.47 KB
3a3ecabe   Imshann   init
1
  import template from "./Popover.html";
6fcf6aec   Imshann   feat(popover): 优化组件
2
3
4
  import style from "antd/lib/popover/style/index.css";
  angular
      .module("esNgAntd")
81f8a467   Imshann   调整组件前缀
5
      .directive("antdPopover", function ($compile, $timeout, esNgAntd) {
6fcf6aec   Imshann   feat(popover): 优化组件
6
          return {
81f8a467   Imshann   调整组件前缀
7
              controllerAs: "antdPopover",
6fcf6aec   Imshann   feat(popover): 优化组件
8
9
10
11
12
13
14
15
16
              restrict: "E",
              transclude: true,
              replace: true,
              scope: {
                  title: "@",
                  content: "@",
                  context: "=",
                  placement: "@",
                  getPopupContainer: "&",
ff56db90   Imshann   优化
17
                  trigger: "@",
6fcf6aec   Imshann   feat(popover): 优化组件
18
19
20
21
22
23
24
25
26
27
28
              },
              template: template,
              controller: function ($scope, $element, $attrs) {
                  this.getContext = function () {
                      return $scope;
                  };
  
                  $scope.state = {
                      visible: false,
                      content: $scope.content ? $scope.content : "",
                      clickTarget: null,
ff56db90   Imshann   优化
29
30
                      trigger: $scope.trigger || "hover",
                      closing: false,
6fcf6aec   Imshann   feat(popover): 优化组件
31
32
33
                  };
  
                  $scope.myEvent = function () {
3a3ecabe   Imshann   init
34
35
36
37
38
39
40
41
                      setTimeout(() => {
                          $scope.state.visible = false;
                          $scope.$apply();
                          document.body.removeEventListener(
                              "click",
                              $scope.myEvent
                          );
                      }, 0);
6fcf6aec   Imshann   feat(popover): 优化组件
42
43
44
                  };
  
                  $scope.handleClick = function () {
ff56db90   Imshann   优化
45
46
47
48
                      if ($scope.state.trigger !== "click") {
                          return;
                      }
  
6fcf6aec   Imshann   feat(popover): 优化组件
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
                      if ($scope.state.visible === false) {
                          // 气泡层(显示)
                          $scope.state.visible = true; // 处理位置
  
                          $scope.handlePosition(); // 事件绑定
  
                          document.body.addEventListener("click", $scope.myEvent);
                      } else {
                          setTimeout(() => {
                              $scope.state.visible = false;
                              $scope.$apply();
                              document.body.removeEventListener(
                                  "click",
                                  $scope.myEvent
                              );
                          }, 0);
                      }
                  };
3a3ecabe   Imshann   init
67
  
ff56db90   Imshann   优化
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
                  $scope.showPopover = function () {
                      // 气泡层(显示)
                      $scope.state.visible = true; // 调整位置
  
                      $scope.handlePosition();
                  };
  
                  $scope.hidePopover = function () {
                      $scope.state.closing = true;
                      $timeout(function () {
                          if ($scope.state.closing === false) {
                              return;
                          }
  
                          $scope.state.visible = false;
                      }, 100);
                  };
  
                  $scope.onMouseEnter = function () {
                      if ($scope.state.trigger !== "hover") {
                          return;
                      }
  
                      if ($scope.state.closing === true) {
                          $scope.state.closing = false;
                      }
  
                      $scope.showPopover();
                  };
  
                  $scope.onMouseLeave = function () {
                      if ($scope.state.trigger !== "hover") {
                          return;
                      }
  
                      $scope.hidePopover();
                  };
  
6fcf6aec   Imshann   feat(popover): 优化组件
106
107
108
109
                  $scope.handlePosition = function () {
                      setTimeout(function () {
                          let popover =
                              $scope.state.popover.querySelector(".ant-popover");
3a3ecabe   Imshann   init
110
  
6fcf6aec   Imshann   feat(popover): 优化组件
111
112
113
114
115
                          if ($scope.placement === "top") {
                              popover.style["top"] = $scope.getTop();
                          } else if ($scope.placement === "bottom") {
                              popover.style["top"] = $scope.getTop();
                          }
3a3ecabe   Imshann   init
116
  
6fcf6aec   Imshann   feat(popover): 优化组件
117
118
119
                          popover.style.left = $scope.getLeft() + "px";
                      }, 0);
                  };
3a3ecabe   Imshann   init
120
  
6fcf6aec   Imshann   feat(popover): 优化组件
121
122
123
124
125
                  $scope.getLeft = function () {
                      let parent = $scope.getPopupContainer();
                      let popover =
                          $scope.state.popover.querySelector(".ant-popover");
                      let target = $scope.state.target;
3a3ecabe   Imshann   init
126
  
6fcf6aec   Imshann   feat(popover): 优化组件
127
128
129
130
                      if (parent) {
                          return -(
                              popover.clientWidth / 2 -
                              target.clientWidth / 2
3a3ecabe   Imshann   init
131
                          );
6fcf6aec   Imshann   feat(popover): 优化组件
132
133
                      } else {
                          let offset = esNgAntd.getOffset(target);
3a3ecabe   Imshann   init
134
                          return (
6fcf6aec   Imshann   feat(popover): 优化组件
135
136
                              offset.left -
                              (popover.clientWidth / 2 - target.clientWidth / 2)
3a3ecabe   Imshann   init
137
138
                          );
                      }
6fcf6aec   Imshann   feat(popover): 优化组件
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
                  };
  
                  $scope.getTop = function () {
                      let parent = $scope.getPopupContainer();
                      let popover =
                          $scope.state.popover.querySelector(".ant-popover");
                      let target = $scope.state.target;
  
                      if (parent) {
                          if ($scope.placement === "top") {
                              return -popover.clientHeight + "px";
                          } else if ($scope.placement === "bottom") {
                          }
                      } else {
                          let offset = esNgAntd.getOffset(target);
  
                          if ($scope.placement === "top") {
                              return offset.top - popover.clientHeight - 4 + "px";
                          } else if ($scope.placement === "bottom") {
                              return offset.top + target.clientHeight + 4 + "px";
                          }
                      }
                  };
              },
81f8a467   Imshann   调整组件前缀
163
              require: ["?^antdTable"],
6fcf6aec   Imshann   feat(popover): 优化组件
164
165
166
167
168
169
170
              link: function (
                  $scope,
                  $element,
                  $attrs,
                  $controllers,
                  $transclude
              ) {
81f8a467   Imshann   调整组件前缀
171
                  let [antdTable] = $controllers;
6fcf6aec   Imshann   feat(popover): 优化组件
172
173
174
175
176
177
                  esNgAntd.createStyle("ant-popover", style);
                  $scope.state.target = $element[0];
                  let title = $scope.title
                      ? `<div class="ant-popover-title"><span>${$scope.title}</span></div>`
                      : "";
                  let content = `<div>
ff56db90   Imshann   优化
178
              <div style="outline:none" ng-class="'ant-popover ant-popover-placement-${$scope.placement}'+(!state.visible?' ant-popover-hidden': '')+(state.visible?' ant-zoom-big-enter ant-zoom-big-enter-active': '')" ng-mouseleave="onMouseLeave()" ng-mouseenter="onMouseEnter()">
3a3ecabe   Imshann   init
179
                  <div class="ant-popover-content">
6fcf6aec   Imshann   feat(popover): 优化组件
180
181
182
                      <div class="ant-popover-arrow">
                          <span class="ant-popover-arrow-content"></span>
                      </div>
3a3ecabe   Imshann   init
183
184
185
                      <div class="ant-popover-inner">
                          <div>
                              ${title}
6fcf6aec   Imshann   feat(popover): 优化组件
186
                              <div class="ant-popover-inner-content"></div>
3a3ecabe   Imshann   init
187
188
189
190
191
                          </div>
                      </div>
                  </div>
              </div>
          </div>`;
6fcf6aec   Imshann   feat(popover): 优化组件
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
                  let div = document.createElement("div");
  
                  if (typeof $scope.getPopupContainer() === "function") {
                      $scope.state.popupContainer = $scope.getPopupContainer()();
                  }
  
                  if (!$scope.state.popupContainer) {
                      div.style.position = "absolute";
                      div.style.top = "0px";
                      div.style.left = "0px";
                      div.style.width = "100%";
                      div.innerHTML = content;
                      document.body.appendChild(div);
                  } else {
                      $scope.state.popupContainer.appendChild(div);
                  }
  
                  div.addEventListener("click", function (event) {
                      event.stopPropagation();
                  });
                  $scope.state.target.addEventListener("click", (e) => {
                      e.stopPropagation();
                  });
                  $compile(div)($scope);
                  div.querySelector(".ant-popover-inner-content").innerHTML =
                      $scope.state.content;
e7610b68   Imshann   feat(pagination):...
218
  
81f8a467   Imshann   调整组件前缀
219
                  if (!antdTable) {
e7610b68   Imshann   feat(pagination):...
220
221
222
223
224
                      $compile(div.querySelector(".ant-popover-inner-content"))(
                          $scope.$parent
                      );
                  }
  
6fcf6aec   Imshann   feat(popover): 优化组件
225
226
227
228
                  $scope.state.popover = div;
              },
          };
      });