Created
July 26, 2023 00:55
-
-
Save Pictor13/6b3dccf8a9fed708b15a51d6c7bd49d8 to your computer and use it in GitHub Desktop.
Distinguish event triggered by User or by jQuery (+ cloneEvent & EventProxy examples)
This file contains hidden or 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
const getCopy = obj => new Proxy( | |
obj, | |
{ | |
get: function( target, prop, receiver ) { | |
let value = target[ prop ]; | |
/*copy array*/ if ( Array.isArray( value ) ) return value.slice( 0 ); | |
/*copy object*/ if ( typeof value === "object" && value.constructor.name === "Object" ) return Object.assign( {}, value ); | |
/*copy primitive*/ return value; | |
} | |
} | |
); | |
const cloneEvent = (event) => { | |
function ClonedEvent(props){ | |
for(var x in props){ | |
this[x] = props[x]; | |
} | |
} | |
ClonedEvent.prototype = new Event(event.type, event); | |
ClonedEvent.prototype.constructor = ClonedEvent; | |
return new ClonedEvent(event); | |
} | |
////////////////////////////////////////////////////////// | |
function setModalUrl(event) { | |
if (!event.isTrigger) return true; | |
// expect an anchor <a>, in order to work with no-JS | |
const $link = $(event.target); | |
event.preventDefault(); | |
event.stopPropagation(); | |
const redirectModalError = event => { | |
console.warn( | |
'Unable to set the redirect URL in the modal. User will be redirected to a different domain without explicit notice. ' + | |
'Please report this message to the administrators.' | |
); | |
$link.trigger('click'); // using `extraParameters` arg doesn't work here 🤷 | |
return false; | |
} | |
const $modalSelector = $link.data('containerid'); | |
const $modal = $($modalSelector); | |
const $redirectTrigger = $modal.find('[data-js-redirect-url]') | |
if ($redirectTrigger.length === 0) { | |
return redirectModalError(event) | |
} | |
$redirectTrigger.each(el => { | |
const $urlTarget = $(el); | |
if ($urlTarget.is('form')) { | |
$urlTarget.attr('action', $link.attr('href')); | |
} else if ($urlTarget.is('a')) { | |
$urlTarget.attr('href', $link.attr('href')); | |
} else { | |
console.log('no type'); | |
return redirectModalError(event) | |
} | |
}); | |
}; | |
$('.js-flyin-overlay-toggler').on('click', setModalUrl); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment