Blame view

build/Tabs/Tabs.js 1.94 KB
3a3ecabe   Imshann   init
1
2
  import template from "./Tabs.html";
  import style from "antd/lib/tabs/style/index.css";
6674355b   Imshann   优化选项阿凯
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  angular.module("esNgAntd").directive("antdTabs", ["esNgAntd", function (esNgAntd) {
    return {
      template: template,
      restrict: "E",
      replace: true,
      transclude: true,
      scope: {
        defaultActiveKey: "@",
        onChange: "&"
      },
      controller: function ($scope, $element) {
        this.getContext = function () {
          return $scope;
        };
3a3ecabe   Imshann   init
17
  
6674355b   Imshann   优化选项阿凯
18
19
20
21
22
23
24
        $scope.state = {
          childrens: [],
          labels: [],
          tabWidth: 0,
          offsetLeft: 0,
          activeKey: $scope.defaultActiveKey
        };
3a3ecabe   Imshann   init
25
  
6674355b   Imshann   优化选项阿凯
26
27
28
29
30
        $scope.handleClick = function (labelKey, tabKey, event) {
          let target = event.target;
          $scope.state.tabWidth = target.clientWidth;
          let currentMarginLeft = parseFloat(getComputedStyle(target, null).marginLeft);
          let beforeTarget = Array.prototype.slice.call(target.parentNode.querySelectorAll("div")).splice(0, tabKey);
3a3ecabe   Imshann   init
31
  
6674355b   Imshann   优化选项阿凯
32
33
34
35
36
37
38
39
40
          if (beforeTarget.length > 0) {
            $scope.state.offsetLeft = beforeTarget.map(function (item) {
              return item.clientWidth + parseFloat(getComputedStyle(item, null).marginLeft);
            }).reduce(function (prev, curr) {
              return prev + curr;
            }) + currentMarginLeft;
          } else {
            $scope.state.offsetLeft = 0;
          }
3a3ecabe   Imshann   init
41
  
6674355b   Imshann   优化选项阿凯
42
43
44
45
46
47
48
49
50
51
52
          $scope.state.activeKey = labelKey;
          $scope.state.childrens.forEach(function (element) {
            element.state.activeKey = labelKey;
          });
          $scope.onChange({
            key: labelKey
          });
        };
      },
      link: function ($scope, $element) {
        esNgAntd.createStyle("ant-tabs", style);
3a3ecabe   Imshann   init
53
  
6674355b   Imshann   优化选项阿凯
54
55
56
57
58
59
60
61
62
63
64
        if ($scope.defaultActiveKey) {
          setTimeout(() => {
            if ($element[0].querySelector(".ant-tabs-tab-active")) {
              $scope.state.tabWidth = $element[0].querySelector(".ant-tabs-tab-active").clientWidth;
              $scope.$apply();
            }
          }, 0);
        }
      }
    };
  }]);