Blame view

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