urlパラメータマネージャ

10777 ワード

目的
url上のパラメータを動的に管理し、urlにパラメータを追加したり減らしたり、パラメータに属性を追加したり減らしたりすることができます.
例を挙げる
www.a.b.c.htm?attr=1_北京:上海、price_60-100:10-50&sort=collectionCount&sortMode=DESC
API
番号付け
方法
使用方法
パラメータ
インスタンスの使用
1
urlを入れる
manager.putUrl();
 
 
2
urlの取得
manager.getUrl();
 
 
3
urlのdomainの取得
manager.getDomain();
 
 
4
urlのparamを取得する
manager.getParamOld();
 
 
5
urlのparamを取得する(増加または減少した後)
manager.getParam();
 
 
6
パラメータをmapに分割
manager.getParamMapOld();
 
 
7
パラメータをmapに分割(増加または減少後)
manager.getParamMap();
 
 
8
urlのkeyの値を取得
manager.getOld(k);
k:パラメータ
manager.getOld("sort");
9
urlのkeyの値を取得します(増加または減少した後)
manager.get(k);
k:パラメータ
manager.get("sort");
10
urlのパラメータの値を取得する
manager.getAttr(attr,k);
attr:パラメータk:パラメータ内のキー値
manager.getAttr("attr","sort");
11
urlにパラメータを加える
manager.add(k,v);
k:パラメータv:値
manager.add("attr","price_60-100:200-300");
12
urlのkeyにパラメータを置く
manager.addAttr(attr,k,v);
attr:パラメータk:キーv:値
manager.addAttr("attr","price","60-100:200-300");
13
urlのパラメータを削除
manager.del(k);
k:パラメータ
manager.del("price");
14
urlのパラメータの属性を削除
manager.delAttr(attr,k);
attr:パラメータk:キー
manager.delAttr("attr","price");
Jqueryコードインスタンス
/**
 *      
 *     :www.a.b.c.htm?attr=1_  :  ,price_60-100:10-50&sort=collectionCount&sortMode=DESC
 * @author   
 * @version 2017-06-06
 */
(function(){
	/**
	 * manager    ,     ,        
	 */
	if(!window.manager) {window.manager={};};
	
	/**
	 *  urlCache      url
	 *     :manager.putUrl();
	 */
	manager.putUrl = function(url){
		window.manager.urlCache = url;
	};
	
	/**
	 *   urlCache     url
	 *     :manager.getUrl();
	 */
	manager.getUrl = function(){
		if(window.manager.urlCache){
			return window.manager.urlCache;
		}else{
			var url = window.location.href;
			window.manager.urlCache = url;
			return url;
		}
	};
	
	/**
	 *   url  domain( urlCache     url)
	 *     :manager.getDomain();
	 */
	manager.getDomain = function(){
		var url = manager.getUrl();
		var url_arr = url.split("?");
		var domain = url_arr[0];
		return domain;
	};
	
	/**
	 *   url  param( urlCache     url)
	 *     :manager.getParam();
	 */
	manager.getParam = function(){
		var url = manager.getUrl();
		var url_arr = url.split("?");
		var param = "";
		if(url_arr.length > 1){
			param = url_arr[1];
		}
		return decodeURIComponent(param);
	};
	
	/**
	 *   url  param
	 *     :manager.getParamOld();
	 */
	manager.getParamOld = function(){
		var url = window.location.href;
		var url_arr = url.split("?");
		var param = "";
		if(url_arr.length > 1){
			param = url_arr[1];
		}
		return decodeURIComponent(param);
	};
	
	/**
	 *        map( urlCache     url)
	 *     :manager.getParamMap();
	 */
	manager.getParamMap = function(){
		var param = manager.getParam();
		var map = {};
		if(param == ""){
			return map;
		}
		var params = param.split("&");
		for (var i = 0; i < params.length; i++) {
			var paramss = params[i].split("=");
			var key ="";
			var value ="";
			if(paramss.length > 1){
				key = paramss[0];
				value = paramss[1];
			}
			map[key] = value;
		}
		return map;
	};
	
	/**
	 *        map
	 *     :manager.getParamMapOld();
	 */
	manager.getParamMapOld = function(){
		var param = manager.getParamOld();
		var map = {};
		if(param == ""){
			return map;
		}
		var params = param.split("&");
		for (var i = 0; i < params.length; i++) {
			var paramss = params[i].split("=");
			var key ="";
			var value ="";
			if(paramss.length > 1){
				key = paramss[0];
				value = paramss[1];
			}
			map[key] = value;
		}
		return map;
	};
	
	/**
	 *   url       ( urlCache     url)
	 *     :manager.get("sort");
	 */
	manager.get = function(k){
		var value = "";
		var map = manager.getParamMap();
		if(k == null || k == ""){
			return value;
		}
		if(map[k]){
			return map[k];
		}else{
			return value;
		}
	};
	
	/**
	 *   url       
	 *     :manager.getOld("sort");
	 */
	manager.getOld = function(k){
		var value = "";
		var map = manager.getParamMapOld();
		if(k == null || k == ""){
			return value;
		}
		if(map[k]){
			return map[k];
		}else{
			return value;
		}
	};
	
	/**
	 *   url          ( urlCache     url)
	 *     :manager.getAttr("attr","sort");
	 */
	manager.getAttr = function(attr,k){
		var value = "";
		var param = manager.get(attr);
		if(attr == null || attr == "" || k == null || k == "" ){
			return value;
		}
		if(param == null || param == ""){
			return value;
		}
		var params = param.split(",");
		for (var i = 0; i < params.length; i++) {
			var paramss = params[i].split("_");
			if(paramss.length > 1){
				if(k == paramss[0]){
					value = paramss[1];
					break;
				}
			}
		}
		return value;
	};
	
	/**
	 *  url    
	 *     :manager.add("attr","price_60-100:200-300");
	 */
	manager.add = function(k,v){
		manager.baseAdd(k,v);
//		manager.baseDel("pageNo");
//		manager.baseDel("pageSize");
	};
	
	/**
	 *  url   key    
	 *     :manager.addAttr("attr","price","60-100:200-300");
	 */
	manager.addAttr = function(attr,k,v){
		manager.baseAddAttr(attr,k,v);
	};
	
	/**
	 *  url    
	 *     :manager.del("price");
	 */
	manager.del = function(k){
		manager.baseDel(k);
	};
	
	/**
	 *  url        
	 *     :manager.delAttr("attr","price");
	 */
	manager.delAttr = function(attr,k){
		manager.baseDelAttr(attr,k);
	};
	
	/**
	 *  url      
	 *     :manager.baseAdd("price","60-100:200-300");
	 */
	manager.baseAdd = function(k,v){
		var domain = manager.getDomain();
		var param = manager.getParam();
		var map = manager.getParamMap();
		var pa = "";
		if(k == null || k ==""){
			return;
		}
		map[k] = v;//     ,        
		for(key in map){  
			var value = map[key];
			pa += "&" + key + "=" + value;
		}
		var url = domain + "?" + pa.substr(1);
		manager.putUrl(url);
	};
	
	/**
	 *  url            
	 *     :manager.baseAdd("attr","price","60-100:200-300");
	 */
	manager.baseAddAttr = function(attr,k,v){
		var domain = manager.getDomain();
		var param = manager.getParam();
		var map = manager.getParamMap();
		var pa = "";
		var value = "";
		var status = true;
		if(attr == null || attr=="" || k == null || k =="" || v == null || v == ""){
			return;
		}
		if(Object.keys(map).length == 0){
			pa += "&" + attr + "=" + k + "_" + v;
		}else{
			for(key in map){ 
				value = map[key];
				if(attr == key){
					var value = map[key];
					map[key]=value + "," + k + "_" + v;
					status = false;
					break;
				}
			}
			if(status){
				map[attr] = k + "_" + v ;
			}
		}
		for(key in map){  
			var value = map[key];
			pa += "&" + key + "=" + value;
		}
		var url = domain + "?" + pa.substr(1);
		manager.putUrl(url);
	};
	
	/**
	 *  url      
	 *     :manager.baseAdd("price");
	 */
	manager.baseDel = function(k){
		var domain = manager.getDomain();
		var param = manager.getParam();
		var map1 = manager.getParamMap();//   
		var map2 = {};//   
		var delList = new Array();
		var pa = "";
		var url = "";
		if(k == null || k =="" || param == null || param == "" || Object.keys(map1).length == 0){
			return;
		}
		delList.push(k);
		for(key in map1){  
			var value = map1[key];
			for(var i = 0 ; i < delList.length ; i++){
				var delk = delList[i];
				if(delk != key){
					map2[key] = value;
				}
			}
		}
		for(key in map2){  
			var value = map2[key];
			pa += "&" + key + "=" + value;
		}
		if(pa==null || pa==""){
			url = domain;
		}else{
			url = domain + "?" + pa.substr(1);
		}
		manager.putUrl(url);
	};
	
	/**
	 *  url           
	 *     :manager.baseAdd("attr","price");
	 */
	manager.baseDelAttr = function(attr,k){
		var domain = manager.getDomain();
		var param = manager.getParam();
		var map1 = manager.getParamMap();//  
		var map2 = {};//       map
		var map3 = {};
		var pa = "";
		var pa1 = "";
		var val = manager.get(attr);
		if(attr == null || attr == "" || k == null || k == "" || param == null || param == "" || Object.keys(map1).length == 0){
			return domain;
		}
		if(val == null  || val == ""){
			return domain;
		}
		var vals = val.split(",");
		for (var i = 0; i < vals.length; i++) {
			var valss = vals[i].split("_");
			if(valss.length > 1){
				var key = valss[0];
				var value = valss[1];
				map2[key] = value;
			}
		}
		for(key in map2){
			if(k != key){
				map3[key] = map2[key];
			}
		}
		if(Object.keys(map3).length == 0){
			manager.del(attr);
		}else{
			for(key in map3){
				pa1 += "," + key + "_" + map3[key];
			}
			manager.add(attr,pa1.substr(1));
		}
		
	};
	
	
	//===============    start=============
//	var a1 = manager.getDomain();
//	
//	var a2 = manager.getParam();
//	
//	var a3 = manager.getParamOld();
//	
//	var a4 = manager.getParamMap();
//	
//	var a5 = manager.getParamMapOld();
//	
//	var a6 = manager.get("sort");
//	
//	var a7 = manager.getOld("sort");
//	
//	manager.addAttr("attr","2","  :  ");
//	var a7_1 = manager.getUrl();
//	
//	manager.add("attr","price_60-100:200-300");
//	var a8 = manager.getUrl();
//	
//	var a9 = manager.getParam();
//	
//	var a10 = manager.getParamOld();
//	
//	var a11 = manager.getParamMap();
//	
//	var a12 = manager.getParamMapOld();
//	
//	var a13 = manager.get("attr");
//	
//	var a14 = manager.getOld("attr");
//	
//	manager.add("pageNo","1");
//	var a16 = manager.getUrl();
//	
//	var a17 = manager.getAttr("attr","price");
//	
//	manager.addAttr("attr","1","  :  ");
//	var a18 = manager.getUrl();
//	
//	manager.addAttr("attr","     ","Y-1670");
//	var a18_1 = manager.getUrl();
//	
//	manager.delAttr("attr","price");
//	var a19 = manager.getUrl();
	//==============    end=================
	
})();