Blame view

src/Popover/Popover.js 5.83 KB
3a3ecabe   Imshann   init
1
  import template from "./Popover.html";
6fcf6aec   Imshann   feat(popover): 优化组件
2
  import style from "antd/lib/popover/style/index.css";
3a3ecabe   Imshann   init
3
4
  
  class Popover {
6fcf6aec   Imshann   feat(popover): 优化组件
5
6
      template = template;
  
ff56db90   Imshann   优化
7
      useModules = ["$compile", "$timeout", "esNgAntd"];
6fcf6aec   Imshann   feat(popover): 优化组件
8
  
3a3ecabe   Imshann   init
9
10
11
12
13
14
      props = {
          title: String,
          content: String,
          context: Object,
          placement: String,
          getPopupContainer: Function,
ff56db90   Imshann   优化
15
          trigger: String,
3a3ecabe   Imshann   init
16
17
18
19
20
21
      };
  
      state = {
          visible: false,
          content: this.props.content ? this.props.content : "",
          clickTarget: null,
ff56db90   Imshann   优化
22
23
          trigger: this.props.trigger || "hover",
          closing: false,
3a3ecabe   Imshann   init
24
25
      };
  
81f8a467   Imshann   调整组件前缀
26
      constructor(antdTable) {
6fcf6aec   Imshann   feat(popover): 优化组件
27
          esNgAntd.createStyle("ant-popover", style);
3a3ecabe   Imshann   init
28
29
30
31
32
          this.state.target = $element[0];
          let title = this.props.title
              ? `<div class="ant-popover-title"><span>${this.props.title}</span></div>`
              : "";
          let content = `<div>
ff56db90   Imshann   优化
33
              <div style="outline:none" ng-class="'ant-popover ant-popover-placement-${this.props.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
34
                  <div class="ant-popover-content">
6fcf6aec   Imshann   feat(popover): 优化组件
35
36
37
                      <div class="ant-popover-arrow">
                          <span class="ant-popover-arrow-content"></span>
                      </div>
3a3ecabe   Imshann   init
38
39
40
                      <div class="ant-popover-inner">
                          <div>
                              ${title}
6fcf6aec   Imshann   feat(popover): 优化组件
41
                              <div class="ant-popover-inner-content"></div>
3a3ecabe   Imshann   init
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
                          </div>
                      </div>
                  </div>
              </div>
          </div>`;
          let div = document.createElement("div");
          if (typeof this.props.getPopupContainer() === "function") {
              this.state.popupContainer = this.props.getPopupContainer()();
          }
          if (!this.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 {
3a3ecabe   Imshann   init
59
60
61
62
63
64
65
66
67
              this.state.popupContainer.appendChild(div);
          }
          div.addEventListener("click", function (event) {
              event.stopPropagation();
          });
          this.state.target.addEventListener("click", (e) => {
              e.stopPropagation();
          });
          $compile(div)($scope);
ff56db90   Imshann   优化
68
69
          div.querySelector(".ant-popover-inner-content").innerHTML =
              this.state.content;
81f8a467   Imshann   调整组件前缀
70
          if (!antdTable) {
e7610b68   Imshann   feat(pagination):...
71
72
73
74
              $compile(div.querySelector(".ant-popover-inner-content"))(
                  $scope.$parent
              );
          }
3a3ecabe   Imshann   init
75
76
77
78
79
80
81
82
83
84
85
86
          this.state.popover = div;
      }
  
      myEvent() {
          setTimeout(() => {
              this.state.visible = false;
              this.$apply();
              document.body.removeEventListener("click", this.myEvent);
          }, 0);
      }
  
      handleClick() {
ff56db90   Imshann   优化
87
88
89
          if (this.state.trigger !== "click") {
              return;
          }
3a3ecabe   Imshann   init
90
91
92
          if (this.state.visible === false) {
              // 气泡层(显示)
              this.state.visible = true;
6fcf6aec   Imshann   feat(popover): 优化组件
93
              // 处理位置
3a3ecabe   Imshann   init
94
95
96
97
98
99
100
101
102
103
104
105
              this.handlePosition();
              // 事件绑定
              document.body.addEventListener("click", this.myEvent);
          } else {
              setTimeout(() => {
                  this.state.visible = false;
                  this.$apply();
                  document.body.removeEventListener("click", this.myEvent);
              }, 0);
          }
      }
  
ff56db90   Imshann   优化
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
      showPopover() {
          // 气泡层(显示)
          this.state.visible = true;
          // 调整位置
          this.handlePosition();
      }
  
      hidePopover() {
          this.state.closing = true;
          $timeout(function () {
              if (this.state.closing === false) {
                  return;
              }
              this.state.visible = false;
          }, 100);
      }
  
      onMouseEnter() {
          if (this.state.trigger !== "hover") {
              return;
          }
          if (this.state.closing === true) {
              this.state.closing = false;
          }
          this.showPopover();
      }
  
      onMouseLeave() {
          if (this.state.trigger !== "hover") {
              return;
          }
          this.hidePopover();
      }
  
3a3ecabe   Imshann   init
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
      handlePosition() {
          setTimeout(function () {
              let popover = this.state.popover.querySelector(".ant-popover");
              if (this.props.placement === "top") {
                  popover.style["top"] = this.getTop();
              } else if (this.props.placement === "bottom") {
                  popover.style["top"] = this.getTop();
              }
              popover.style.left = this.getLeft() + "px";
          }, 0);
      }
  
      getLeft() {
          let parent = this.props.getPopupContainer();
          let popover = this.state.popover.querySelector(".ant-popover");
          let target = this.state.target;
          if (parent) {
              return -(popover.clientWidth / 2 - target.clientWidth / 2);
          } else {
6fcf6aec   Imshann   feat(popover): 优化组件
159
              let offset = esNgAntd.getOffset(target);
3a3ecabe   Imshann   init
160
              return (
6fcf6aec   Imshann   feat(popover): 优化组件
161
                  offset.left - (popover.clientWidth / 2 - target.clientWidth / 2)
3a3ecabe   Imshann   init
162
163
164
165
166
167
168
169
170
171
172
173
174
175
              );
          }
      }
  
      getTop() {
          let parent = this.props.getPopupContainer();
          let popover = this.state.popover.querySelector(".ant-popover");
          let target = this.state.target;
          if (parent) {
              if (this.props.placement === "top") {
                  return -popover.clientHeight + "px";
              } else if (this.props.placement === "bottom") {
              }
          } else {
6fcf6aec   Imshann   feat(popover): 优化组件
176
              let offset = esNgAntd.getOffset(target);
3a3ecabe   Imshann   init
177
              if (this.props.placement === "top") {
6fcf6aec   Imshann   feat(popover): 优化组件
178
                  return offset.top - popover.clientHeight - 4 + "px";
3a3ecabe   Imshann   init
179
              } else if (this.props.placement === "bottom") {
6fcf6aec   Imshann   feat(popover): 优化组件
180
                  return offset.top + target.clientHeight + 4 + "px";
3a3ecabe   Imshann   init
181
182
183
184
              }
          }
      }
  }