Common.js 2.33 KB
import baseStyle from "antd/lib/style/index.css";
angular.module("esNgAntd").service("esNgAntd", ["$compile", function ($compile) {
  this.styleSheets = null;
  this.conflictType = null;

  this.createStyle = function (key, style) {
    if (!document.querySelector("#antd")) {
      let styleElement = document.createElement("style");
      styleElement.setAttribute("id", "antd");
      styleElement.setAttribute("type", "text/css");
      styleElement.innerHTML = baseStyle.toString();
      document.head.appendChild(styleElement);

      if (this.styleSheets) {
        this.disableStyle("anticon");
      }
    }

    if (!document.querySelector("#" + key)) {
      let styleElement = document.createElement("style");
      styleElement.setAttribute("id", key);
      styleElement.setAttribute("type", "text/css");
      styleElement.innerHTML = style.toString();
      document.head.appendChild(styleElement);

      if (this.styleSheets) {
        this.disableStyle(key);
      }
    }
  };

  this.disableStyle = function (name) {
    for (let i = 0; i < this.styleSheets.cssRules.length; i++) {
      let rule = this.styleSheets.cssRules[i];

      if (rule.selectorText && rule.selectorText.indexOf(name) !== -1 && rule.selectorText.indexOf("ant3") === -1) {
        rule.selectorText = rule.selectorText.split(",").map(function (item) {
          return ".ant3 " + item;
        }).join(",");
      }
    }
  };

  this.conflict = function (filename, type) {
    this.conflictType = type;

    for (let i = 0; i < document.styleSheets.length; i++) {
      const element = document.styleSheets[i];

      if (element.href && element.href.indexOf(filename) !== -1) {
        this.styleSheets = element;
      }
    }
  };

  this.clearAttribute = function (element, attrs) {
    for (const attr of attrs) {
      element.removeAttribute(attr);
    }
  };

  this.createLayer = function (content, scope) {
    let div = document.createElement("div");
    div.innerHTML = content;
    document.body.appendChild(div);
    $compile(div)(scope);
  };

  this.getOffset = function (ele) {
    if (!ele || ele.nodeType != 1) {
      return;
    }

    let rect = ele.getBoundingClientRect();
    let doc = ele.ownerDocument.documentElement;
    return {
      top: rect.top + window.pageYOffset - doc.clientTop,
      left: rect.left + window.pageXOffset - doc.clientLeft
    };
  };
}]);