-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55c7900
commit ec0dd52
Showing
11 changed files
with
159 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 0 additions & 83 deletions
83
modules/ppcp-button/resources/js/modules/Helper/ApmButton.js
This file was deleted.
Oops, something went wrong.
100 changes: 100 additions & 0 deletions
100
modules/ppcp-button/resources/js/modules/Helper/ApmButtons.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
|
||
export const apmButtonsInit = (selector = '.ppcp-button-apm') => { | ||
if (window.ppcpApmButtons) { | ||
return; | ||
} | ||
window.ppcpApmButtons = new ApmButtons(selector); | ||
} | ||
|
||
export class ApmButtons { | ||
|
||
constructor(selector) { | ||
this.selector = selector; | ||
this.containers = []; | ||
|
||
// Reloads button containers. | ||
this.reloadContainers(); | ||
|
||
// Refresh button layout. | ||
jQuery(window).resize(() => { | ||
this.refresh(); | ||
}).resize(); | ||
|
||
// Observes for new buttons. | ||
(new MutationObserver(this.observeElementsCallback.bind(this))) | ||
.observe(document.body, { childList: true, subtree: true }); | ||
} | ||
|
||
observeElementsCallback(mutationsList, observer) { | ||
const observeSelector = this.selector + ', .widget_shopping_cart, .widget_shopping_cart_content'; | ||
|
||
let shouldReload = false; | ||
for (let mutation of mutationsList) { | ||
if (mutation.type === 'childList') { | ||
mutation.addedNodes.forEach(node => { | ||
if (node.matches && node.matches(observeSelector)) { | ||
shouldReload = true; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
if (shouldReload) { | ||
this.reloadContainers(); | ||
this.refresh(); | ||
} | ||
}; | ||
|
||
reloadContainers() { | ||
jQuery(this.selector).each((index, el) => { | ||
const parent = jQuery(el).parent(); | ||
if (!this.containers.some($el => $el.is(parent))) { | ||
this.containers.push(parent); | ||
} | ||
}); | ||
console.log('this.containers', this.containers); | ||
} | ||
|
||
refresh() { | ||
for (const container of this.containers) { | ||
const $container = jQuery(container); | ||
|
||
// Check width and add classes | ||
const width = $container.width(); | ||
|
||
$container.removeClass('ppcp-width-500 ppcp-width-300 ppcp-width-min'); | ||
|
||
if (width >= 500) { | ||
$container.addClass('ppcp-width-500'); | ||
} else if (width >= 300) { | ||
$container.addClass('ppcp-width-300'); | ||
} else { | ||
$container.addClass('ppcp-width-min'); | ||
} | ||
|
||
// Check first apm button | ||
const $firstApmButton = $container.find(this.selector + ':visible').first(); | ||
const $firstElement = $container.children(':visible').first(); | ||
|
||
let isFirstElement = false; | ||
if ($firstApmButton.is($firstElement)) { | ||
isFirstElement = true; | ||
} | ||
|
||
// Assign margins to buttons | ||
$container.find(this.selector).each((index, el) => { | ||
const $el = jQuery(el); | ||
const height = $el.height(); | ||
|
||
if (isFirstElement) { | ||
$el.css('margin-top', `0px`); | ||
return true; | ||
} | ||
|
||
$el.css('margin-top', `${Math.round(height * 0.3)}px`); | ||
}); | ||
|
||
} | ||
} | ||
|
||
} |
Oops, something went wrong.