UNPKG

gd-sprest-js

Version:

SharePoint 2013/Online js components.

103 lines (102 loc) 4.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); // Method to set the click events exports.setClickEvents = function (el, items, onClick) { // Ensure the element exists if (el == null) { return; } // Method to get the item var getItem = function (el) { // Get the index var idx = parseInt(el.getAttribute("data-idx")); // Return the item return idx >= 0 && idx < items.length ? items[idx] : null; }; // Method to fix the position of the sub-menu var fixMenuPos = function (ev) { // Get the sub-menu var menu = ev.currentTarget.querySelector(".ms-ContextualMenu"); if (menu) { var offset = 0; // Get the position var pos = menu.getBoundingClientRect(); var parentPos = ev.currentTarget.getBoundingClientRect(); // See if it's visible horizontally offset = document.body.clientWidth - (pos.left + pos.width); if (offset < 0) { // See if the left position is set if (menu.style.left) { // Update the offset offset += parseFloat(menu.style.left.replace("px", "")); } // Adjust the position menu.style.left = offset + "px"; // Update the menu position pos = menu.getBoundingClientRect(); // See if the menu is overlapping the parent if (parentPos.left < pos.right) { // Update the offset offset -= (pos.right - parentPos.left); // Adjust the position menu.style.left = offset + "px"; } } // See if it's visible vertically offset = document.body.clientHeight - (pos.top + pos.height); if (offset < 0) { // See if the top position is set if (menu.style.top) { var position = parseFloat(menu.style.top.replace("px", "")); // Adjust the position menu.style.top = (position + offset) + "px"; } else { // Adjust the position; menu.style.top = offset + "px"; } // Update the menu position pos = menu.getBoundingClientRect(); } } }; // Parse the items for (var i = 0; i < items.length && i < el.children.length; i++) { var item = items[i]; var link = el.children[i].querySelector(".ms-ContextualMenu-link"); // Set the index link.setAttribute("data-idx", i.toString()); // See if this is a menu if (item.menu && item.menu.length > 0) { // Set the click events exports.setClickEvents(el.children[i].querySelector(".ms-ContextualMenu"), item.menu, onClick); // Add the events to fix the menu position el.children[i].addEventListener("focus", fixMenuPos); el.children[i].addEventListener("mouseover", fixMenuPos); } // Else, see there is a click event else if (item.onClick) { // Set the click event link.addEventListener("click", function (ev) { // Get the item var item = getItem(ev.currentTarget); if (item) { // Call the click event item.onClick(ev, item); } }); } // See if the click event exists if (onClick) { // Set the click event link.addEventListener("click", function (ev) { // Get the item var item = getItem(ev.currentTarget); if (item) { // Call the click event onClick(ev, item); } }); } } };