Blame view

build/Select/Select.js 6.46 KB
3a3ecabe   Imshann   init
1
  import template from "./Select.html";
dd962f77   Imshann   优化
2
3
4
  import style from "antd/lib/select/style/index.css";
  angular
      .module("esNgAntd")
81f8a467   Imshann   调整组件前缀
5
      .directive("antdSelect", function ($compile, $timeout, esNgAntd) {
dd962f77   Imshann   优化
6
          return {
81f8a467   Imshann   调整组件前缀
7
              controllerAs: "antdSelect",
dd962f77   Imshann   优化
8
9
10
11
12
              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
                      if (typeof func === "function" && func() !== undefined) {
061629e7   Imshann   add
70
71
                          let containerElement = func();
                          containerElement.style.position = "relative";
dd962f77   Imshann   优化
72
73
74
75
76
77
78
79
80
81
82
83
84
85
                          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
86
  
dd962f77   Imshann   优化
87
88
89
90
91
92
93
94
95
                  $scope.myEvent = function () {
                      $timeout(() => {
                          $scope.state.open = false;
                          document.body.removeEventListener(
                              "click",
                              $scope.myEvent
                          );
                      }, 0);
                  };
3a3ecabe   Imshann   init
96
  
dd962f77   Imshann   优化
97
98
99
100
                  $scope.handleBlur = function () {
                      // 事件绑定
                      document.body.addEventListener("click", $scope.myEvent);
                  };
3a3ecabe   Imshann   init
101
  
dd962f77   Imshann   优化
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
                  $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
123
          </div></div>`)($scope)[0]
dd962f77   Imshann   优化
124
                      );
3a3ecabe   Imshann   init
125
  
dd962f77   Imshann   优化
126
127
                      if ($scope.state.popup === null) {
                          let func = $scope.getPopupContainer();
3a3ecabe   Imshann   init
128
  
dd962f77   Imshann   优化
129
130
131
132
133
134
135
136
137
138
139
                          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
140
141
                      }
  
dd962f77   Imshann   优化
142
143
144
145
                      $scope.state.open = !$scope.state.open;
                      $scope.handleBlur();
                  };
              },
81f8a467   Imshann   调整组件前缀
146
              require: ["?^antdForm", "?^antdFormItem"],
dd962f77   Imshann   优化
147
148
149
150
151
152
153
              link: function (
                  $scope,
                  $element,
                  $attrs,
                  $controllers,
                  $transclude
              ) {
81f8a467   Imshann   调整组件前缀
154
                  let [antdForm, antdFormItem] = $controllers;
dd962f77   Imshann   优化
155
                  esNgAntd.createStyle("ant-select", style);
a468667f   Imshann   优化
156
  
81f8a467   Imshann   调整组件前缀
157
158
159
                  if (antdForm) {
                      $scope.antdForm = antdForm.getContext();
                      $scope.antdForm.state.formItems.push($scope);
dd962f77   Imshann   优化
160
                  }
a468667f   Imshann   优化
161
  
81f8a467   Imshann   调整组件前缀
162
163
                  if (antdFormItem) {
                      $scope.antdFormItem = antdFormItem.getContext();
a468667f   Imshann   优化
164
165
166
167
168
                  }
  
                  $timeout(function () {
                      $scope.setValue($scope.value || $scope.defaultValue);
                  }, 100);
dd962f77   Imshann   优化
169
170
171
              },
          };
      });