OnlyOffice在线文档

jquery.dropdownToggle.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. *
  3. * (c) Copyright Ascensio System SIA 2023
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. ;(function() {
  19. var
  20. dropdownToggleHash = {};
  21. jQuery.extend({
  22. dropdownToggle: function(options) {
  23. // default options
  24. options = jQuery.extend({
  25. //switcherSelector: "#id" or ".class", - button
  26. //dropdownID: "id", - drop panel
  27. //anchorSelector: "#id" or ".class", - near field
  28. //noActiveSwitcherSelector: "#id" or ".class", - dont hide
  29. addTop: 0,
  30. addLeft: 0,
  31. position: "absolute",
  32. fixWinSize: true,
  33. enableAutoHide: true,
  34. showFunction: null,
  35. afterShowFunction: null,
  36. hideFunction: null,
  37. alwaysUp: false,
  38. simpleToggle: false,
  39. rightPos: false
  40. }, options);
  41. var _toggle = function(switcherObj, dropdownID, addTop, addLeft, fixWinSize, position, anchorSelector, showFunction, alwaysUp, simpleToggle, afterShowFunction) {
  42. var dropdownItem = jq("#" + dropdownID);
  43. if (typeof (simpleToggle) === "undefined" || simpleToggle === false) {
  44. fixWinSize = fixWinSize === true;
  45. addTop = addTop || 0;
  46. addLeft = addLeft || 0;
  47. position = position || "absolute";
  48. var targetPos = jq(anchorSelector || switcherObj).offset();
  49. if (!targetPos) return;
  50. var elemPosLeft = targetPos.left;
  51. var elemPosTop = targetPos.top + jq(anchorSelector || switcherObj).outerHeight();
  52. if (options.rightPos) {
  53. elemPosLeft = Math.max(0,targetPos.left - dropdownItem.outerWidth() + jq(anchorSelector || switcherObj).outerWidth());
  54. }
  55. var w = jq(window);
  56. var topPadding = w.scrollTop();
  57. var leftPadding = w.scrollLeft();
  58. if (position === "fixed") {
  59. addTop -= topPadding;
  60. addLeft -= leftPadding;
  61. }
  62. var scrWidth = w.width();
  63. var scrHeight = w.height();
  64. if (fixWinSize && (!options.rightPos)
  65. && (targetPos.left + addLeft + dropdownItem.outerWidth()) > (leftPadding + scrWidth)) {
  66. elemPosLeft = Math.max(0, leftPadding + scrWidth - dropdownItem.outerWidth()) - addLeft;
  67. }
  68. if (fixWinSize
  69. && (elemPosTop + dropdownItem.outerHeight()) > (topPadding + scrHeight)
  70. && (targetPos.top - dropdownItem.outerHeight()) > topPadding
  71. || alwaysUp) {
  72. elemPosTop = targetPos.top - dropdownItem.outerHeight();
  73. }
  74. dropdownItem.css(
  75. {
  76. "position": position,
  77. "top": elemPosTop + addTop,
  78. "left": elemPosLeft + addLeft
  79. });
  80. }
  81. if (typeof showFunction === "function") {
  82. showFunction(switcherObj, dropdownItem);
  83. }
  84. dropdownItem.toggle();
  85. if (typeof afterShowFunction === "function") {
  86. afterShowFunction(switcherObj, dropdownItem);
  87. }
  88. };
  89. var _registerAutoHide = function(event, switcherSelector, dropdownSelector, hideFunction) {
  90. if (jq(dropdownSelector).is(":visible")) {
  91. var $targetElement = jq((event.target) ? event.target : event.srcElement);
  92. if (!$targetElement.parents().andSelf().is(switcherSelector + ", " + dropdownSelector)) {
  93. if (typeof hideFunction === "function")
  94. hideFunction($targetElement);
  95. jq(dropdownSelector).hide();
  96. }
  97. }
  98. };
  99. if (options.switcherSelector && options.dropdownID) {
  100. var toggleFunc = function(e) {
  101. _toggle(jq(this), options.dropdownID, options.addTop, options.addLeft, options.fixWinSize, options.position, options.anchorSelector, options.showFunction, options.alwaysUp, options.simpleToggle, options.afterShowFunction);
  102. };
  103. if (!dropdownToggleHash.hasOwnProperty(options.switcherSelector + options.dropdownID)) {
  104. jq(document).on("click", options.switcherSelector, toggleFunc);
  105. dropdownToggleHash[options.switcherSelector + options.dropdownID] = true;
  106. }
  107. }
  108. if (options.enableAutoHide && options.dropdownID) {
  109. var hideFunc = function(e) {
  110. var allSwitcherSelectors = options.noActiveSwitcherSelector ?
  111. options.switcherSelector + ", " + options.noActiveSwitcherSelector : options.switcherSelector;
  112. _registerAutoHide(e, allSwitcherSelectors, "#" + options.dropdownID, options.hideFunction);
  113. };
  114. jq(document).unbind("click", hideFunc);
  115. jq(document).bind("click", hideFunc);
  116. }
  117. return {
  118. toggle: _toggle,
  119. registerAutoHide: _registerAutoHide
  120. };
  121. }
  122. });
  123. })();