3a3ecabe
Imshann
init
|
1
|
import template from "./Select.html";
|
dd962f77
Imshann
优化
|
2
3
4
5
6
7
8
9
10
11
12
|
import style from "antd/lib/select/style/index.css";
angular
.module("esNgAntd")
.directive("esSelect", function ($compile, $timeout, esNgAntd) {
return {
controllerAs: "esSelect",
restrict: "E",
transclude: true,
replace: true,
scope: {
value: "@",
|
a468667f
Imshann
优化
|
13
|
defaultValue: "@",
|
dd962f77
Imshann
优化
|
14
15
16
17
18
19
20
21
22
23
|
placeholder: "@",
onChange: "&",
placeholder: "@",
getPopupContainer: "&",
},
template: template,
controller: function ($scope, $element) {
this.getContext = function () {
return $scope;
};
|
3a3ecabe
Imshann
init
|
24
|
|
dd962f77
Imshann
优化
|
25
26
27
28
|
$scope.state = {
open: false,
childrens: [],
label: null,
|
a468667f
Imshann
优化
|
29
|
value: $scope.value || $scope.defaultValue,
|
dd962f77
Imshann
优化
|
30
31
|
popup: null,
};
|
3a3ecabe
Imshann
init
|
32
|
|
a468667f
Imshann
优化
|
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
$scope.setValue = function (value) {
let option = $scope.state.childrens.find(function (option) {
return option.value === value;
});
if (option) {
option.label = option.element.text();
$scope.state.label = option.label;
$scope.state.value = option.value;
} else {
$scope.state.label = null;
$scope.state.value = null;
}
};
|
dd962f77
Imshann
优化
|
48
49
50
|
$scope.addOption = function (option) {
$scope.state.childrens.push(option);
};
|
3a3ecabe
Imshann
init
|
51
|
|
dd962f77
Imshann
优化
|
52
53
54
55
56
57
58
59
|
$scope.handleClick = function (option) {
$scope.state.open = !$scope.state.open;
$scope.state.label = option.label;
$scope.state.value = option.value;
$scope.onChange({
value: $scope.state.value,
});
};
|
3a3ecabe
Imshann
init
|
60
|
|
dd962f77
Imshann
优化
|
61
62
63
64
|
$scope.getOffset = function (ele) {
if (!ele || ele.nodeType != 1) {
return;
}
|
3a3ecabe
Imshann
init
|
65
|
|
dd962f77
Imshann
优化
|
66
|
let func = $scope.getPopupContainer();
|
3a3ecabe
Imshann
init
|
67
|
|
dd962f77
Imshann
优化
|
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
if (typeof func === "function" && func() !== undefined) {
return {
top: $element[0].offsetTop,
left: $element[0].offsetLeft,
};
} else {
let rect = ele.getBoundingClientRect();
let doc = ele.ownerDocument.documentElement;
return {
top: rect.top + window.pageYOffset - doc.clientTop,
left:
rect.left + window.pageXOffset - doc.clientLeft,
};
}
};
|
3a3ecabe
Imshann
init
|
83
|
|
dd962f77
Imshann
优化
|
84
85
86
87
88
89
90
91
92
|
$scope.myEvent = function () {
$timeout(() => {
$scope.state.open = false;
document.body.removeEventListener(
"click",
$scope.myEvent
);
}, 0);
};
|
3a3ecabe
Imshann
init
|
93
|
|
dd962f77
Imshann
优化
|
94
95
96
97
|
$scope.handleBlur = function () {
// 事件绑定
document.body.addEventListener("click", $scope.myEvent);
};
|
3a3ecabe
Imshann
init
|
98
|
|
dd962f77
Imshann
优化
|
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
$scope.handleOpen = function (event) {
event.stopPropagation();
const { height, width } =
$element[0].getBoundingClientRect();
const { top, left } = $scope.getOffset($element[0]); // 处理标签
$scope.state.childrens.forEach(function (item) {
item.label = item.element.text();
});
let div = document.createElement("div");
div.style.position = "absolute";
div.style.left = 0;
div.style.top = 0;
div.style.width = "100%";
div.appendChild(
$compile(`<div><div ng-class="'ant-select-dropdown ant-select-dropdown-placement-bottomLeft'+(!state.open?' ant-select-dropdown-hidden':'')" style="width: ${width}px; left: ${left}px; top: ${
top + height + 2
}px;">
<div class="ant-select-item ant-select-item-option" ng-click="handleClick(option)" ng-repeat="option in state.childrens">
<div class="ant-select-item-option-content">{{option.label}}</div>
</div>
|
3a3ecabe
Imshann
init
|
120
|
</div></div>`)($scope)[0]
|
dd962f77
Imshann
优化
|
121
|
);
|
3a3ecabe
Imshann
init
|
122
|
|
dd962f77
Imshann
优化
|
123
124
|
if ($scope.state.popup === null) {
let func = $scope.getPopupContainer();
|
3a3ecabe
Imshann
init
|
125
|
|
dd962f77
Imshann
优化
|
126
127
128
129
130
131
132
133
134
135
136
|
if (
typeof func === "function" &&
func() !== undefined
) {
$element[0].style.position = "relative";
$scope.getPopupContainer()().appendChild(div);
} else {
document.body.appendChild(div);
}
$scope.state.popup = div;
|
3a3ecabe
Imshann
init
|
137
138
|
}
|
dd962f77
Imshann
优化
|
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
$scope.state.open = !$scope.state.open;
$scope.handleBlur();
};
},
require: ["?^esForm", "?^esFormItem"],
link: function (
$scope,
$element,
$attrs,
$controllers,
$transclude
) {
let [esForm, esFormItem] = $controllers;
esNgAntd.createStyle("ant-select", style);
|
a468667f
Imshann
优化
|
153
154
155
156
|
if (esForm) {
$scope.esForm = esForm.getContext();
$scope.esForm.state.formItems.push($scope);
|
dd962f77
Imshann
优化
|
157
|
}
|
a468667f
Imshann
优化
|
158
159
160
161
162
163
164
165
|
if (esFormItem) {
$scope.esFormItem = esFormItem.getContext();
}
$timeout(function () {
$scope.setValue($scope.value || $scope.defaultValue);
}, 100);
|
dd962f77
Imshann
优化
|
166
167
168
|
},
};
});
|