-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigate.min.js
1 lines (1 loc) · 1.96 KB
/
navigate.min.js
1
function Navigate(e,t,n){var r={linkEvent:"click",selector:"a",ignoreHrefPrefix:["http://","https://","mailto:"],ignoreTarget:["_blank"],preventDefault:true};if(typeof n!=="object"){n={}}this.config=this.extend({},r,n);this.callback=t;this.filter=e;this.canChange=true}Navigate.prototype.enable=function(){this.canChange=true};Navigate.prototype.disable=function(){this.canChange=false};Navigate.prototype.parseLinks=function(e){var t=this;var n=function(e){t.parseLinkCallback(e,this);if(t.config.preventDefault){if(typeof e==="object"&&typeof e.preventDefault==="function"){e.preventDefault()}else{e.returnValue=false}}};if(this.filter(e)&&this.exceptions(e)){this.addEvent(e,this.config.linkEvent,n)}else{var r=e.querySelectorAll(this.config.selector);var i=r.length;for(var s=0;s<i;s++){if(this.filter(r[s])&&this.exceptions(r[s])){this.addEvent(r[s],this.config.linkEvent,n)}}}};Navigate.prototype.parseLinkCallback=function(e,t){var n=t.getAttribute("href");var r,i,s;if(this.config.removeHrefPrefix){r=this.config.removeHrefPrefix.length;for(s=0;s<r;s++){i=this.config.removeHrefPrefix[s];if(n.substr(0,i.length)===i){n=n.substr(i.length);break}}}if(this.canChange){this.callback(n,e)}};Navigate.prototype.exceptions=function(e){var t=e.getAttribute("href");var n=e.getAttribute("target");var r,i,s;if(this.config.ignoreHrefPrefix){r=this.config.ignoreHrefPrefix.length;for(s=0;s<r;s++){i=this.config.ignoreHrefPrefix[s];if(t.substr(0,i.length)===i){return false}}}if(typeof n==="string"&&this.config.ignoreTarget){r=this.config.ignoreTarget.length;for(s=0;s<r;s++){i=this.config.ignoreTarget[s];if(n.substr(0,i.length)===i){return false}}}return true};Navigate.prototype.extend=function(e){e=e||{};for(var t=1;t<arguments.length;t++){if(!arguments[t]){continue}for(var n in arguments[t]){if(arguments[t].hasOwnProperty(n)){e[n]=arguments[t][n]}}}return e};Navigate.prototype.addEvent=function(e,t,n){if(e.addEventListener){e.addEventListener(t,n)}else{e.attachEvent("on"+t,function(t){n.call(e,t)})}}