Popover.js
4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import template from "./Popover.html";
class Popover {
props = {
title: String,
content: String,
context: Object,
placement: String,
getPopupContainer: Function,
};
state = {
visible: false,
content: this.props.content ? this.props.content : "",
clickTarget: null,
};
template = template;
useModules = ["$compile"];
constructor() {
this.state.target = $element[0];
let title = this.props.title
? `<div class="ant-popover-title"><span>${this.props.title}</span></div>`
: "";
let content = `<div>
<div style="outline:none" ng-class="'ant-popover ant-popover-placement-${
this.props.placement
}'+(!state.visible?' ant-popover-hidden': '')">
<div class="ant-popover-content">
<div class="ant-popover-arrow"></div>
<div class="ant-popover-inner">
<div>
${title}
<div class="ant-popover-inner-content">
${this.state.content.replace(
/\$scope/g,
"context"
)}
</div>
</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 {
div.innerHTML = content;
this.state.popupContainer.appendChild(div);
}
div.addEventListener("click", function (event) {
event.stopPropagation();
});
this.state.target.addEventListener("click", (e) => {
e.stopPropagation();
});
$compile(div)($scope);
this.state.popover = div;
}
myEvent() {
setTimeout(() => {
this.state.visible = false;
this.$apply();
document.body.removeEventListener("click", this.myEvent);
}, 0);
}
handleClick() {
if (this.state.visible === false) {
// 气泡层(显示)
this.state.visible = true;
// j
this.handlePosition();
// 事件绑定
document.body.addEventListener("click", this.myEvent);
} else {
setTimeout(() => {
this.state.visible = false;
this.$apply();
document.body.removeEventListener("click", this.myEvent);
}, 0);
}
}
showPopover() {
let popover = this.state.popover;
popover.classList.remove("ant-popover-hidden");
}
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 {
return (
$(target).offset().left -
(popover.clientWidth / 2 - target.clientWidth / 2)
);
}
}
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 {
if (this.props.placement === "top") {
return $(target).offset().top - popover.clientHeight - 4 + "px";
} else if (this.props.placement === "bottom") {
return $(target).offset().top + target.clientHeight + 4 + "px";
}
}
}
}