| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- /**
- *
- * (c) Copyright Ascensio System SIA 2023
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
- ;(function() {
- var
- dropdownToggleHash = {};
- jQuery.extend({
- dropdownToggle: function(options) {
- // default options
- options = jQuery.extend({
- //switcherSelector: "#id" or ".class", - button
- //dropdownID: "id", - drop panel
- //anchorSelector: "#id" or ".class", - near field
- //noActiveSwitcherSelector: "#id" or ".class", - dont hide
- addTop: 0,
- addLeft: 0,
- position: "absolute",
- fixWinSize: true,
- enableAutoHide: true,
- showFunction: null,
- afterShowFunction: null,
- hideFunction: null,
- alwaysUp: false,
- simpleToggle: false,
- rightPos: false
- }, options);
-
- var _toggle = function(switcherObj, dropdownID, addTop, addLeft, fixWinSize, position, anchorSelector, showFunction, alwaysUp, simpleToggle, afterShowFunction) {
- var dropdownItem = jq("#" + dropdownID);
-
- if (typeof (simpleToggle) === "undefined" || simpleToggle === false) {
- fixWinSize = fixWinSize === true;
- addTop = addTop || 0;
- addLeft = addLeft || 0;
- position = position || "absolute";
-
- var targetPos = jq(anchorSelector || switcherObj).offset();
-
- if (!targetPos) return;
-
- var elemPosLeft = targetPos.left;
- var elemPosTop = targetPos.top + jq(anchorSelector || switcherObj).outerHeight();
- if (options.rightPos) {
- elemPosLeft = Math.max(0,targetPos.left - dropdownItem.outerWidth() + jq(anchorSelector || switcherObj).outerWidth());
- }
-
- var w = jq(window);
- var topPadding = w.scrollTop();
- var leftPadding = w.scrollLeft();
-
- if (position === "fixed") {
- addTop -= topPadding;
- addLeft -= leftPadding;
- }
-
- var scrWidth = w.width();
- var scrHeight = w.height();
-
- if (fixWinSize && (!options.rightPos)
- && (targetPos.left + addLeft + dropdownItem.outerWidth()) > (leftPadding + scrWidth)) {
- elemPosLeft = Math.max(0, leftPadding + scrWidth - dropdownItem.outerWidth()) - addLeft;
- }
-
- if (fixWinSize
- && (elemPosTop + dropdownItem.outerHeight()) > (topPadding + scrHeight)
- && (targetPos.top - dropdownItem.outerHeight()) > topPadding
- || alwaysUp) {
- elemPosTop = targetPos.top - dropdownItem.outerHeight();
- }
-
- dropdownItem.css(
- {
- "position": position,
- "top": elemPosTop + addTop,
- "left": elemPosLeft + addLeft
- });
- }
- if (typeof showFunction === "function") {
- showFunction(switcherObj, dropdownItem);
- }
-
- dropdownItem.toggle();
-
- if (typeof afterShowFunction === "function") {
- afterShowFunction(switcherObj, dropdownItem);
- }
- };
-
- var _registerAutoHide = function(event, switcherSelector, dropdownSelector, hideFunction) {
- if (jq(dropdownSelector).is(":visible")) {
- var $targetElement = jq((event.target) ? event.target : event.srcElement);
- if (!$targetElement.parents().andSelf().is(switcherSelector + ", " + dropdownSelector)) {
- if (typeof hideFunction === "function")
- hideFunction($targetElement);
- jq(dropdownSelector).hide();
- }
- }
- };
-
- if (options.switcherSelector && options.dropdownID) {
- var toggleFunc = function(e) {
- _toggle(jq(this), options.dropdownID, options.addTop, options.addLeft, options.fixWinSize, options.position, options.anchorSelector, options.showFunction, options.alwaysUp, options.simpleToggle, options.afterShowFunction);
- };
- if (!dropdownToggleHash.hasOwnProperty(options.switcherSelector + options.dropdownID)) {
- jq(document).on("click", options.switcherSelector, toggleFunc);
- dropdownToggleHash[options.switcherSelector + options.dropdownID] = true;
- }
- }
-
- if (options.enableAutoHide && options.dropdownID) {
- var hideFunc = function(e) {
- var allSwitcherSelectors = options.noActiveSwitcherSelector ?
- options.switcherSelector + ", " + options.noActiveSwitcherSelector : options.switcherSelector;
- _registerAutoHide(e, allSwitcherSelectors, "#" + options.dropdownID, options.hideFunction);
-
- };
- jq(document).unbind("click", hideFunc);
- jq(document).bind("click", hideFunc);
- }
-
- return {
- toggle: _toggle,
- registerAutoHide: _registerAutoHide
- };
- }
- });
- })();
|