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
|
placeholder: "@",
onChange: "&",
placeholder: "@",
getPopupContainer: "&",
|
710b4ac0
Imshann
优化
|
18
|
size: "@",
|
dd962f77
Imshann
优化
|
19
20
|
},
template: template,
|
710b4ac0
Imshann
优化
|
21
|
controller: function ($scope, $element, $attrs) {
|
dd962f77
Imshann
优化
|
22
23
24
|
this.getContext = function () {
return $scope;
};
|
3a3ecabe
Imshann
init
|
25
|
|
dd962f77
Imshann
优化
|
26
27
28
29
|
$scope.state = {
open: false,
childrens: [],
label: null,
|
a468667f
Imshann
优化
|
30
|
value: $scope.value || $scope.defaultValue,
|
dd962f77
Imshann
优化
|
31
32
|
popup: null,
};
|
3a3ecabe
Imshann
init
|
33
|
|
a468667f
Imshann
优化
|
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
$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
优化
|
49
50
51
|
$scope.addOption = function (option) {
$scope.state.childrens.push(option);
};
|
3a3ecabe
Imshann
init
|
52
|
|
dd962f77
Imshann
优化
|
53
54
55
56
57
58
59
60
|
$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
|
61
|
|
dd962f77
Imshann
优化
|
62
63
64
65
|
$scope.getOffset = function (ele) {
if (!ele || ele.nodeType != 1) {
return;
}
|
3a3ecabe
Imshann
init
|
66
|
|
dd962f77
Imshann
优化
|
67
|
let func = $scope.getPopupContainer();
|
3a3ecabe
Imshann
init
|
68
|
|
dd962f77
Imshann
优化
|
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
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
|
84
|
|
dd962f77
Imshann
优化
|
85
86
87
88
89
90
91
92
93
|
$scope.myEvent = function () {
$timeout(() => {
$scope.state.open = false;
document.body.removeEventListener(
"click",
$scope.myEvent
);
}, 0);
};
|
3a3ecabe
Imshann
init
|
94
|
|
dd962f77
Imshann
优化
|
95
96
97
98
|
$scope.handleBlur = function () {
// 事件绑定
document.body.addEventListener("click", $scope.myEvent);
};
|
3a3ecabe
Imshann
init
|
99
|
|
dd962f77
Imshann
优化
|
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
$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
|
121
|
</div></div>`)($scope)[0]
|
dd962f77
Imshann
优化
|
122
|
);
|
3a3ecabe
Imshann
init
|
123
|
|
dd962f77
Imshann
优化
|
124
125
|
if ($scope.state.popup === null) {
let func = $scope.getPopupContainer();
|
3a3ecabe
Imshann
init
|
126
|
|
dd962f77
Imshann
优化
|
127
128
129
130
131
132
133
134
135
136
137
|
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
|
138
139
|
}
|
dd962f77
Imshann
优化
|
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
$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
优化
|
154
155
156
157
|
if (esForm) {
$scope.esForm = esForm.getContext();
$scope.esForm.state.formItems.push($scope);
|
dd962f77
Imshann
优化
|
158
|
}
|
a468667f
Imshann
优化
|
159
160
161
162
163
164
165
166
|
if (esFormItem) {
$scope.esFormItem = esFormItem.getContext();
}
$timeout(function () {
$scope.setValue($scope.value || $scope.defaultValue);
}, 100);
|
dd962f77
Imshann
优化
|
167
168
169
|
},
};
});
|