Skip to content

Instantly share code, notes, and snippets.

@Pictor13
Created July 26, 2023 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pictor13/6b3dccf8a9fed708b15a51d6c7bd49d8 to your computer and use it in GitHub Desktop.
Save Pictor13/6b3dccf8a9fed708b15a51d6c7bd49d8 to your computer and use it in GitHub Desktop.
Distinguish event triggered by User or by jQuery (+ cloneEvent & EventProxy examples)
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