ImagePreviewGroup.js 5.16 KB
import template from "./ImagePreviewGroup.html";
angular.module("esNgAntd").directive("antdImagePreviewGroup", ["esNgAntd", function (esNgAntd) {
  return {
    template: template,
    restrict: "E",
    replace: true,
    transclude: true,
    scope: {
      preview: "="
    },
    controller: function ($scope, $element, $attrs) {
      $scope.state = {
        current: 0,
        visible: false,
        src: null,
        childrens: []
      };

      $scope.addChildren = function (children) {
        [children].forEach(function (value, key) {
          if ([undefined, null, ""].includes(value)) {
            throw new Error(`${["children"][key]} parameter of addChildren method is required.`);
          }
        });
        $scope.state.childrens.push(children);
        return $scope.state.childrens.length - 1;
      };

      $scope.setCurrent = function (value) {
        [value].forEach(function (value, key) {
          if ([undefined, null, ""].includes(value)) {
            throw new Error(`${["value"][key]} parameter of setCurrent method is required.`);
          }
        });
        $scope.state.current = value;

        if (typeof $scope.preview === "object" && typeof $scope.preview.onCurrentChange === "function") {
          $scope.preview.onCurrentChange(value);
        }
      };

      $scope.setVisible = function (value) {
        [value].forEach(function (value, key) {
          if ([undefined, null, ""].includes(value)) {
            throw new Error(`${["value"][key]} parameter of setVisible method is required.`);
          }
        });
        $scope.state.visible = value;

        if (typeof $scope.preview === "object" && typeof $scope.preview.onVisibleChange === "function") {
          $scope.preview.onVisibleChange(value);
        }
      };

      $scope.handleOpen = function (index) {
        [index].forEach(function (value, key) {
          if ([undefined, null, ""].includes(value)) {
            throw new Error(`${["index"][key]} parameter of handleOpen method is required.`);
          }
        });
        $scope.setCurrent(index);
        $scope.state.src = $scope.state.childrens[index].state.src;
        $scope.setVisible(true);
      };

      $scope.handleClose = function () {
        $scope.setVisible(false);
      };

      $scope.handlePrev = function () {
        $scope.setCurrent($scope.state.current > 0 ? $scope.state.current - 1 : 0);
        $scope.state.src = $scope.state.childrens[$scope.state.current].state.src;
      };

      $scope.handleNext = function () {
        $scope.setCurrent($scope.state.current < $scope.state.childrens.length - 1 ? $scope.state.current + 1 : $scope.state.childrens.length - 1);
        $scope.state.src = $scope.state.childrens[$scope.state.current].state.src;
      };

      $scope.handlePreview = function () {
        let className;

        if ($attrs.class) {
          className = ` ${$attrs.class}`;
        }

        esNgAntd.createLayer(`
            <div class="ant-image-preview-root${className}">
                <div class="ant-image-preview-mask" ng-if="state.visible"></div>
                <div class="ant-image-preview-wrap" ng-show="state.visible">
                    <div class="ant-image-preview">
                        <div class="ant-image-preview-content">
                            <div class="ant-image-preview-body">
                                <ul class="ant-image-preview-operations">
                                    <li class="ant-image-preview-operations-operation">
                                        <span class="anticon anticon-close ant-image-preview-operations-icon" ng-click="handleClose()">
                                            <antd-icon type="CloseOutlined"></antd-icon>
                                        </span>
                                    </li> 
                                </ul>
                                <div class="ant-image-preview-img-wrapper">
                                    <img class="ant-image-preview-img" ng-src="{{state.src}}"/>
                                </div>
                                <div ng-class="{'ant-image-preview-switch-left': true, 'ant-image-preview-switch-left-disabled': state.current===0}" ng-click="handlePrev()">
                                    <antd-icon type="LeftOutlined"></antd-icon>
                                </div>
                                <div ng-class="{'ant-image-preview-switch-right': true,'ant-image-preview-switch-right-disabled': (state.childrens.length-1)===state.current}"  ng-click="handleNext()">
                                    <antd-icon type="RightOutlined"></antd-icon>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        `, $scope);
      };

      this.getContext = function () {
        return $scope;
      };
    },
    link: function ($scope, $element, $attrs) {
      [$element, $attrs].forEach(function (value, key) {
        if ([undefined, null, ""].includes(value)) {
          throw new Error(`${["$element", "$attrs"][key]} parameter of constructor method is required.`);
        }
      });
      $scope.handlePreview();
    }
  };
}]);