Blame view

build/Input/Input.js 3.68 KB
3a3ecabe   Imshann   init
1
  import style from "antd/lib/input/style/index.css";
81f8a467   Imshann   调整组件前缀
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  angular
      .module("esNgAntd")
      .directive("antdInput", function ($compile, esNgAntd) {
          return {
              controllerAs: "antdInput",
              restrict: "E",
              transclude: true,
              replace: true,
              scope: {
                  value: "@",
                  placeholder: "@",
                  addonBefore: "@",
                  addonAfter: "@",
                  disabled: "@",
                  onChange: "&",
                  maxLength: "@",
              },
              controller: function ($scope, $element, $attrs) {
                  this.getContext = function () {
                      return $scope;
                  };
3a3ecabe   Imshann   init
23
  
81f8a467   Imshann   调整组件前缀
24
25
26
                  $scope.state = {
                      inputEventTarget: null,
                  };
3a3ecabe   Imshann   init
27
  
81f8a467   Imshann   调整组件前缀
28
29
30
                  $scope.handleClick = function (event) {
                      $scope.state.inputEventTarget = event;
                  };
3a3ecabe   Imshann   init
31
  
81f8a467   Imshann   调整组件前缀
32
33
34
35
36
                  $scope.handleChange = function () {
                      $scope.onChange({
                          event: $scope.state.inputEventTarget,
                      });
                  };
3a3ecabe   Imshann   init
37
  
81f8a467   Imshann   调整组件前缀
38
39
40
41
                  $scope.getTemplate = function () {
                      let maxLengthAttribute = "";
                      let styleAttribute = "";
                      let idAttribute = "";
3a3ecabe   Imshann   init
42
  
81f8a467   Imshann   调整组件前缀
43
44
45
                      if ($scope.maxLength) {
                          maxLengthAttribute = `maxlength="${$scope.maxLength}"`;
                      }
3a3ecabe   Imshann   init
46
  
81f8a467   Imshann   调整组件前缀
47
48
49
                      if ($scope.style) {
                          styleAttribute = `style="${$scope.style}"`;
                      }
3a3ecabe   Imshann   init
50
  
81f8a467   Imshann   调整组件前缀
51
52
53
                      if ($scope.antdFormItem && $scope.antdFormItem.name) {
                          idAttribute = `id="${$scope.antdFormItem.name}"`;
                      }
3a3ecabe   Imshann   init
54
  
81f8a467   Imshann   调整组件前缀
55
56
57
                      let templates = [
                          `<input type="text" class="ant-input" placeholder={{placeholder}} ng-change="handleChange()" ng-model="value" ng-focus="handleClick($event)" ng-disabled="disabled==='true'" ${styleAttribute} ${maxLengthAttribute} ${idAttribute}/>`,
                          `<span class="ant-input-group-wrapper" ng-if="addonBefore||addonAfter">
3a3ecabe   Imshann   init
58
59
60
61
62
63
          <span class="ant-input-wrapper ant-input-group" style="">
              <span class="ant-input-group-addon" ng-if="addonBefore">{{addonBefore}}</span>
              <input type="text" class="ant-input" ng-change="handleChange()" ng-model="value" ng-focus="handleClick($event)" ng-disabled="disabled==='true'" style="${$scope.style}" ${styleAttribute} ${maxLengthAttribute} ${idAttribute}/>
              <span class="ant-input-group-addon" ng-if="addonAfter">{{addonAfter}}</span>
          </span>
      </span>`,
81f8a467   Imshann   调整组件前缀
64
                      ];
3a3ecabe   Imshann   init
65
  
81f8a467   Imshann   调整组件前缀
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
                      if ($scope.addonBefore || $scope.addonAfter) {
                          return templates[1];
                      } else {
                          return templates[0];
                      }
                  };
              },
              require: ["?^antdFormItem", "?^antdForm"],
              link: function (
                  $scope,
                  $element,
                  $attrs,
                  $controllers,
                  $transclude
              ) {
                  let [antdFormItem, antdForm] = $controllers;
                  esNgAntd.createStyle("ant-input", style); // 上下文
1b6f912f   Imshann   优化
83
  
81f8a467   Imshann   调整组件前缀
84
85
86
87
                  if (antdForm) {
                      $scope.antdForm = antdForm.getContext();
                      $scope.antdForm.state.formItems.push($scope);
                  }
1b6f912f   Imshann   优化
88
  
81f8a467   Imshann   调整组件前缀
89
90
91
                  if (antdFormItem) {
                      $scope.antdFormItem = antdFormItem.getContext();
                  }
1b6f912f   Imshann   优化
92
  
81f8a467   Imshann   调整组件前缀
93
94
95
96
97
                  $scope.style = $attrs.style;
                  $element.replaceWith($compile($scope.getTemplate())($scope));
              },
          };
      });