companion-install.js (4666B)
1 (function () { 2 3 function currentFrameAbsolutePosition(currentWindow) { 4 var currentParentWindow; 5 var positions = []; 6 var rect; 7 8 while (currentWindow !== window.top) { 9 currentParentWindow = currentWindow.parent; 10 for (var idx = 0; idx < currentParentWindow.frames.length; idx++) 11 if (currentParentWindow.frames[idx] === currentWindow) { 12 var iframes = currentParentWindow.document.getElementsByTagName('iframe'); 13 for (var iframeIdx = 0; iframeIdx < iframes.length; iframeIdx++) { 14 var frameElement = iframes[iframeIdx]; 15 if (frameElement.contentWindow === currentWindow) { 16 rect = frameElement.getBoundingClientRect(); 17 positions.push({ 18 x: rect.x, 19 y: rect.y 20 }); 21 } 22 } 23 currentWindow = currentParentWindow; 24 break; 25 } 26 } 27 return positions.reduce(function (accumulator, currentValue) { 28 return { 29 x: accumulator.x + currentValue.x, 30 y: accumulator.y + currentValue.y 31 }; 32 }, { 33 x: 0, 34 y: 0 35 }); 36 } 37 38 var popover = null; 39 var $ = jQuery; 40 var popoverTriggerSelector = []; 41 42 var updatePopoverPosition = function () { 43 var linkedTo = popover.data('linkedTo'); 44 var rect = linkedTo.getBoundingClientRect(); 45 var framePosition = currentFrameAbsolutePosition(linkedTo.ownerDocument.defaultView); 46 47 var style = { 48 top: (rect.top + framePosition.y + linkedTo.offsetHeight / 2 - popover.height() / 2) + "px", 49 left: (rect.left + framePosition.x + linkedTo.offsetWidth + 10) + "px", 50 }; 51 52 if (popover.hasClass('arrow-down')) { 53 style = { 54 top: (rect.top + framePosition.y - 150) + "px", 55 left: (rect.left + framePosition.x + linkedTo.offsetWidth / 2 - 160) + "px", 56 }; 57 } 58 59 popover.css(style); 60 }; 61 62 63 var hidePopover = function () { 64 popover.fadeOut(); 65 }; 66 67 var showPopover = function (linkedTo, position) { 68 if (!popover) { 69 popover = jQuery("#extend-themes-companion-popover"); 70 jQuery('body').append(popover); 71 popover.find('.extend-themes-companion-popover-close').click(function () { 72 popover.fadeOut(); 73 }); 74 } 75 76 position = position || 'left'; 77 78 popover.data('linkedTo', linkedTo); 79 popover.removeClass('arrow-left arrow-down').addClass('arrow-' + position); 80 updatePopoverPosition(); 81 popover.fadeIn(); 82 var currentWindow = linkedTo.ownerDocument.defaultView; 83 var currentDocument = linkedTo.ownerDocument; 84 85 $(currentWindow).on('resize.extend-themes-companion-popover', updatePopoverPosition); 86 $(currentDocument).find('body .wp-full-overlay-sidebar-content').on('scroll.extend-themes-companion-popover', updatePopoverPosition); 87 $(currentWindow).on('scroll.extend-themes-companion-popover', updatePopoverPosition); 88 89 $(currentDocument).on('click.extend-themes-companion-popover', '*', function (event) { 90 var target = event.currentTarget; 91 event.stopPropagation(); 92 93 if ($(target).is(popoverTriggerSelector.join(',')) || $(target).is(popover.data('linkedTo'))) { 94 event.preventDefault(); 95 96 return; 97 } 98 99 if (!popover.is(target)) { 100 if (!$.contains(popover[0], target)) { 101 popover.fadeOut(); 102 $(currentWindow).off('resize.extend-themes-companion-popover'); 103 $(currentWindow).off('scroll.extend-themes-companion-popover'); 104 $('body .wp-full-overlay-sidebar-content').off('scroll.extend-themes-companion-popover'); 105 $(currentDocument).off('click.extend-themes-companion-popover'); 106 } 107 } 108 109 }); 110 111 }; 112 113 function initPopover() { 114 $('body').on('click', popoverTriggerSelector.join(','), function (event) { 115 showPopover(event.currentTarget); 116 }); 117 } 118 119 120 popoverTriggerSelector = ['.cp-add-section']; 121 initPopover(); 122 123 124 window.ExtendThemesCompanionPopover = { 125 showPopover: showPopover, 126 updatePopoverPosition: updatePopoverPosition, 127 hidePopover: hidePopover 128 }; 129 130 })();