UNPKG

nscomponentsreact

Version:

React Wrapper Components for NSComponents

265 lines (250 loc) 6.67 kB
var nsModuleExport = function(root,name,prototype) { if(typeof exports === 'object' && typeof module === 'object') { module.exports[name] = prototype; } else if (typeof define === "function" && define.amd) { define(name,[], function () {return prototype;}); } else if(typeof exports === 'object') { exports[name] = prototype; } else { root[name] = prototype; } };var __nsGlobal = (function () { if (typeof globalThis !== "undefined") return globalThis; if (typeof self !== "undefined") return self; if (typeof window !== "undefined") return window; if (typeof global !== "undefined") return global; return Function("return this")(); })();var nsIsWeb = function(root) { if(typeof exports === 'object' && typeof module === 'object') { return false; } else if (typeof define === "function" && define.amd) { return false; } else if(typeof exports === 'object') { return false; } else { return true; } };if(!nsIsWeb()) { var nsutilRef = require('./nsUtil.min.js'); var NSUtil = nsutilRef.NSUtil; var nscontainerbaseRef = require('./nsContainerBase.min.js'); var nsExtendPrototype = nscontainerbaseRef.nsExtendPrototype; var NSContainerBase = nscontainerbaseRef.NSContainerBase; } "use strict"; var NSRouter = (function() { function NSRouter(arrRoute,setting) { this.util = new NSUtil(); this.__arrRoute = arrRoute ? arrRoute : []; this.__setting = setting; this.__mode = "hash"; this.__root = "/"; this.__initialize(); } NSRouter.prototype.__initialize = function() { var isManual = false; var defaultRoute = "/"; if(this.__setting) { this.__mode = (this.__setting["mode"] && this.__setting["mode"] == "history" && !!(history.pushState)) ? "history" : "hash"; this.__root = this.__setting["root"] ? "/" + this.__removeSlashes(this.__setting["root"]) + "/" : "/"; defaultRoute = this.__setting["defaultRoute"] ? this.__setting["defaultRoute"] : defaultRoute; isManual = Boolean.parse(this.__setting["isManual"]); } if(!isManual) { window.addEventListener("hashchange", this.__hashChangeListener.bind(this)); var currentRoute = this.getCurrentRoute(); if(!currentRoute) { currentRoute = defaultRoute; } if(currentRoute) { currentRoute = this.__addSlashes(currentRoute); this.routeTo(currentRoute); } } }; NSRouter.prototype.addRoute = function(item) { if(item && this.__arrRoute) { this.__arrRoute.push(item); } }; NSRouter.prototype.removeRoute = function(param) { if(param && this.__arrRoute && this.__arrRoute.length > 0) { var item = {}; var length = this.__arrRoute.length; for(var count = 0;count < length;count++) { item = this.__arrRoute[count]; if(item["handler"] === param || item["route"] === param) { this.__arrRoute.splice(count, 1); break; } } } }; NSRouter.prototype.reset = function() { this.__arrRoute = []; this.__mode = "hash"; this.__root = "/"; }; NSRouter.prototype.getCurrentRoute = function() { var currentRoute = ""; if(this.__mode === "history") { if(!location) { currentRoute = ""; } else { currentRoute = this.__removeSlashes(decodeURI(location.pathname + location.search)); currentRoute = currentRoute.replace(/\?(.*)$/, ''); currentRoute = this.__root != '/' ? currentRoute.replace(this.__root, '') : currentRoute; } } else { if(!window) { return ""; } var match = window.location.href.match(/#(.*)$/); currentRoute = match ? match[1] : ''; } return this.__removeSlashes(currentRoute); }; NSRouter.prototype.callRoute = function(route) { var currentRoute = route || this.getCurrentRoute(); var item = {}; var length = this.__arrRoute.length; var callHandler = false; for(var count = 0;count < length;count++) { callHandler = false; item = this.__arrRoute[count]; if(item["route"] instanceof RegExp) { var match = currentRoute.match(item["route"]); if(match) { match.shift(); callHandler = true; } } else if(item["route"] === currentRoute) { callHandler = true; } if(callHandler) { if(item["handler"]) { item["handler"](item); } break; } } }; NSRouter.prototype.routeTo = function(route) { route = route ? route : ""; var paramRoute = this.__addSlashes(route); //route = paramRoute; var cancelled = this.__dispatchEvent(NSRouter.NavigationStart,{route: paramRoute},{route: paramRoute},null,true); if(cancelled) { this.__dispatchEvent(NSRouter.NavigationCancelled,{route: paramRoute},{route: paramRoute}); } else { if(this.__mode === "history") { history.pushState(null, null, this.__root + this.__removeSlashes(route)); } else { //window.location.href.match(/#(.*)$/); if(this.getCurrentRoute()=== this.__removeSlashes(route)) { this.callRoute(route); } else { window.location.href = window.location.href.replace(/#(.*)$/, '') + '#' + route; } } } this.__dispatchEvent(NSRouter.NavigationEnd,{route: paramRoute},{route: paramRoute}); }; NSRouter.prototype.__hashChangeListener = function(event) { // Current route url (getting rid of '#' in hash as well): var url = location.hash || "/"; if(url) { if(url.charAt(0) === "#") { url = url.substring(1); } if(url.charAt(0) != "/") { url = "/" + url; } } this.callRoute(url); //console.log(url); }; NSRouter.prototype.__isPushStateAvailable = function() { return !!(typeof window !== 'undefined' && window.history && window.history.pushState); }; NSRouter.prototype.__removeSlashes = function(path) { return path.toString().replace(/\/$/, "").replace(/^\/+/, ""); }; NSRouter.prototype.__addSlashes = function(path) { return (!path || path.charAt(0) === "/") ? path : "/" + path; }; NSRouter.prototype.__dispatchEvent = function(eventType,data,param,bubbles,cancelable) { return this.util.dispatchEvent(window,eventType,data,param,bubbles,cancelable); }; NSRouter.NavigationStart = "navigationStart"; NSRouter.NavigationEnd = "navigationEnd"; NSRouter.NavigationCancelled = "navigationCancelled"; NSRouter.NavigationError = "navigationError"; return NSRouter; })(); nsModuleExport(__nsGlobal,"NSRouter",NSRouter);