index.umd.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define("dom-zindex", ["exports"], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory(exports);
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory(mod.exports);
  11. global.domZindex = mod.exports;
  12. }
  13. })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports) {
  14. "use strict";
  15. Object.defineProperty(_exports, "__esModule", {
  16. value: true
  17. });
  18. _exports.getCurrent = _exports["default"] = void 0;
  19. _exports.getNext = getNext;
  20. _exports.getSubCurrent = getSubCurrent;
  21. _exports.getSubNext = getSubNext;
  22. _exports.setSubCurrent = _exports.setCurrent = void 0;
  23. var winDom = null;
  24. var bodyEl = null;
  25. var storeEl = null;
  26. var storeId = 'z-index-manage';
  27. var styleEl = null;
  28. var styleId = 'z-index-style';
  29. var storeMainKey = 'm';
  30. var storeSubKey = 's';
  31. var storeData = {
  32. m: 1000,
  33. s: 1000
  34. };
  35. function getDocument() {
  36. if (!winDom) {
  37. if (typeof document !== 'undefined') {
  38. winDom = document;
  39. }
  40. }
  41. return winDom;
  42. }
  43. function getBody() {
  44. if (winDom && !bodyEl) {
  45. bodyEl = winDom.body || winDom.getElementsByTagName('body')[0];
  46. }
  47. return bodyEl;
  48. }
  49. function getDomMaxZIndex() {
  50. var max = 0;
  51. var dom = getDocument();
  52. if (dom) {
  53. var body = getBody();
  54. if (body) {
  55. var allElem = body.getElementsByTagName('*');
  56. for (var i = 0; i < allElem.length; i++) {
  57. var elem = allElem[i];
  58. if (elem && elem.style && elem.nodeType === 1) {
  59. var zIndex = elem.style.zIndex;
  60. if (zIndex && /^\d+$/.test(zIndex)) {
  61. max = Math.max(max, Number(zIndex));
  62. }
  63. }
  64. }
  65. }
  66. }
  67. return max;
  68. }
  69. function getStyle() {
  70. if (!styleEl) {
  71. var dom = getDocument();
  72. if (dom) {
  73. styleEl = dom.getElementById(styleId);
  74. if (!styleEl) {
  75. styleEl = dom.createElement('style');
  76. styleEl.id = styleId;
  77. dom.getElementsByTagName('head')[0].appendChild(styleEl);
  78. }
  79. }
  80. }
  81. return styleEl;
  82. }
  83. function updateVar() {
  84. var styEl = getStyle();
  85. if (styEl) {
  86. var prefixes = '--dom-';
  87. var propKey = '-z-index';
  88. styEl.innerHTML = ':root{' + prefixes + 'main' + propKey + ':' + getCurrent() + ';' + prefixes + 'sub' + propKey + ':' + getSubCurrent() + '}';
  89. }
  90. }
  91. function getStoreDom() {
  92. if (!storeEl) {
  93. var dom = getDocument();
  94. if (dom) {
  95. storeEl = dom.getElementById(storeId);
  96. if (!storeEl) {
  97. var body = getBody();
  98. if (body) {
  99. storeEl = dom.createElement('div');
  100. storeEl.id = storeId;
  101. storeEl.style.display = 'none';
  102. body.appendChild(storeEl);
  103. setCurrent(storeData.m);
  104. setSubCurrent(storeData.s);
  105. }
  106. }
  107. }
  108. }
  109. return storeEl;
  110. }
  111. function createSetHandle(key) {
  112. return function (value) {
  113. if (value) {
  114. value = Number(value);
  115. storeData[key] = value;
  116. var el = getStoreDom();
  117. if (el) {
  118. if (el.dataset) {
  119. el.dataset[key] = value + '';
  120. } else {
  121. el.setAttribute('data-' + key, value + '');
  122. }
  123. }
  124. }
  125. updateVar();
  126. return storeData[key];
  127. };
  128. }
  129. var setCurrent = _exports.setCurrent = createSetHandle(storeMainKey);
  130. function createGetHandle(key, nextMethod) {
  131. return function getCurrent(currZindex) {
  132. var zIndex;
  133. var el = getStoreDom();
  134. if (el) {
  135. var domVal = el.dataset ? el.dataset[key] : el.getAttribute('data-' + key);
  136. if (domVal) {
  137. zIndex = Number(domVal);
  138. }
  139. }
  140. if (!zIndex) {
  141. zIndex = storeData[key];
  142. }
  143. if (currZindex) {
  144. if (Number(currZindex) < zIndex) {
  145. return nextMethod();
  146. }
  147. return currZindex;
  148. }
  149. return zIndex;
  150. };
  151. }
  152. var getCurrent = _exports.getCurrent = createGetHandle(storeMainKey, getNext);
  153. function getNext() {
  154. return setCurrent(getCurrent() + 1);
  155. }
  156. var setSubCurrent = _exports.setSubCurrent = createSetHandle(storeSubKey);
  157. var _getSubCurrent = createGetHandle(storeSubKey, getSubNext);
  158. function getSubCurrent() {
  159. return getCurrent() + _getSubCurrent();
  160. }
  161. function getSubNext() {
  162. setSubCurrent(_getSubCurrent() + 1);
  163. return getSubCurrent();
  164. }
  165. /**
  166. * Web common z-index style management
  167. */
  168. var DomZIndex = {
  169. setCurrent: setCurrent,
  170. getCurrent: getCurrent,
  171. getNext: getNext,
  172. setSubCurrent: setSubCurrent,
  173. getSubCurrent: getSubCurrent,
  174. getSubNext: getSubNext,
  175. getMax: getDomMaxZIndex
  176. };
  177. updateVar();
  178. var _default = _exports["default"] = DomZIndex;
  179. });