1 | define(["dojo/_base/lang"], function(lang){ |
---|
2 | |
---|
3 | // module: |
---|
4 | // dojox/app/utils/hash |
---|
5 | |
---|
6 | var hashUtil = { |
---|
7 | // summary: |
---|
8 | // This module contains the hash |
---|
9 | |
---|
10 | getParams: function(/*String*/ hash){ |
---|
11 | // summary: |
---|
12 | // get the params from the hash |
---|
13 | // |
---|
14 | // hash: String |
---|
15 | // the url hash |
---|
16 | // |
---|
17 | // returns: |
---|
18 | // the params object |
---|
19 | // |
---|
20 | var params; |
---|
21 | if(hash && hash.length){ |
---|
22 | // fixed handle view specific params |
---|
23 | |
---|
24 | while(hash.indexOf("(") > 0){ |
---|
25 | var index = hash.indexOf("("); |
---|
26 | var endindex = hash.indexOf(")"); |
---|
27 | var viewPart = hash.substring(index,endindex+1); |
---|
28 | if(!params){ params = {}; } |
---|
29 | params = hashUtil.getParamObj(params, viewPart); |
---|
30 | // next need to remove the viewPart from the hash, and look for the next one |
---|
31 | var viewName = viewPart.substring(1,viewPart.indexOf("&")); |
---|
32 | hash = hash.replace(viewPart, viewName); |
---|
33 | } |
---|
34 | // after all of the viewParts need to get the other params |
---|
35 | |
---|
36 | for(var parts = hash.split("&"), x = 0; x < parts.length; x++){ |
---|
37 | var tp = parts[x].split("="), name = tp[0], value = encodeURIComponent(tp[1] || ""); |
---|
38 | if(name && value){ |
---|
39 | if(!params){ params = {}; } |
---|
40 | params[name] = value; |
---|
41 | } |
---|
42 | } |
---|
43 | } |
---|
44 | return params; // Object |
---|
45 | }, |
---|
46 | |
---|
47 | getParamObj: function(/*Object*/ params, /*String*/ viewPart){ |
---|
48 | // summary: |
---|
49 | // called to handle a view specific params object |
---|
50 | // params: Object |
---|
51 | // the view specific params object |
---|
52 | // viewPart: String |
---|
53 | // the part of the view with the params for the view |
---|
54 | // |
---|
55 | // returns: |
---|
56 | // the params object for the view |
---|
57 | // |
---|
58 | var viewparams; |
---|
59 | var viewName = viewPart.substring(1,viewPart.indexOf("&")); |
---|
60 | var hash = viewPart.substring(viewPart.indexOf("&"), viewPart.length-1); |
---|
61 | for(var parts = hash.split("&"), x = 0; x < parts.length; x++){ |
---|
62 | var tp = parts[x].split("="), name = tp[0], value = encodeURIComponent(tp[1] || ""); |
---|
63 | if(name && value){ |
---|
64 | if(!viewparams){ viewparams = {}; } |
---|
65 | viewparams[name] = value; |
---|
66 | } |
---|
67 | } |
---|
68 | params[viewName] = viewparams; |
---|
69 | return params; // Object |
---|
70 | }, |
---|
71 | |
---|
72 | buildWithParams: function(/*String*/ hash, /*Object*/ params){ |
---|
73 | // summary: |
---|
74 | // build up the url hash adding the params |
---|
75 | // hash: String |
---|
76 | // the url hash |
---|
77 | // params: Object |
---|
78 | // the params object |
---|
79 | // |
---|
80 | // returns: |
---|
81 | // the params object |
---|
82 | // |
---|
83 | if(hash.charAt(0) !== "#"){ |
---|
84 | hash = "#"+hash; |
---|
85 | } |
---|
86 | for(var item in params){ |
---|
87 | var value = params[item]; |
---|
88 | // add a check to see if the params includes a view name if so setup the hash like (viewName&item=value); |
---|
89 | if(lang.isObject(value)){ |
---|
90 | hash = hashUtil.addViewParams(hash, item, value); |
---|
91 | }else{ |
---|
92 | if(item && value != null){ |
---|
93 | hash = hash+"&"+item+"="+params[item]; |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | return hash; // String |
---|
98 | }, |
---|
99 | |
---|
100 | addViewParams: function(/*String*/ hash, /*String*/ view, /*Object*/ params){ |
---|
101 | // summary: |
---|
102 | // add the view specific params to the hash for example (view1¶m1=value1) |
---|
103 | // hash: String |
---|
104 | // the url hash |
---|
105 | // view: String |
---|
106 | // the view name |
---|
107 | // params: Object |
---|
108 | // the params for this view |
---|
109 | // |
---|
110 | // returns: |
---|
111 | // the hash string |
---|
112 | // |
---|
113 | if(hash.charAt(0) !== "#"){ |
---|
114 | hash = "#"+hash; |
---|
115 | } |
---|
116 | var index = hash.indexOf(view); |
---|
117 | if(index > 0){ // found the view? |
---|
118 | if((hash.charAt(index-1) == "#" || hash.charAt(index-1) == "+") && // assume it is the view? or could check the char after for + or & or - |
---|
119 | (hash.charAt(index+view.length) == "&" || hash.charAt(index+view.length) == "+" || hash.charAt(index+view.length) == "-")){ |
---|
120 | // found the view at this index. |
---|
121 | var oldView = hash.substring(index-1,index+view.length+1); |
---|
122 | var paramString = hashUtil.getParamString(params); |
---|
123 | var newView = hash.charAt(index-1) + "(" + view + paramString + ")" + hash.charAt(index+view.length); |
---|
124 | hash = hash.replace(oldView, newView); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | return hash; // String |
---|
129 | }, |
---|
130 | |
---|
131 | getParamString: function(/*Object*/ params){ |
---|
132 | // summary: |
---|
133 | // return the param string |
---|
134 | // params: Object |
---|
135 | // the params object |
---|
136 | // |
---|
137 | // returns: |
---|
138 | // the params string |
---|
139 | // |
---|
140 | var paramStr = ""; |
---|
141 | for(var item in params){ |
---|
142 | var value = params[item]; |
---|
143 | if(item && value != null){ |
---|
144 | paramStr = paramStr+"&"+item+"="+params[item]; |
---|
145 | } |
---|
146 | } |
---|
147 | return paramStr; // String |
---|
148 | }, |
---|
149 | |
---|
150 | getTarget: function(/*String*/ hash, /*String?*/ defaultView){ |
---|
151 | // summary: |
---|
152 | // return the target string |
---|
153 | // hash: String |
---|
154 | // the hash string |
---|
155 | // defaultView: String |
---|
156 | // the optional defaultView string |
---|
157 | // |
---|
158 | // returns: |
---|
159 | // the target string |
---|
160 | // |
---|
161 | if(!defaultView){ defaultView = ""} |
---|
162 | while(hash.indexOf("(") > 0){ |
---|
163 | var index = hash.indexOf("("); |
---|
164 | var endindex = hash.indexOf(")"); |
---|
165 | var viewPart = hash.substring(index,endindex+1); |
---|
166 | var viewName = viewPart.substring(1,viewPart.indexOf("&")); |
---|
167 | hash = hash.replace(viewPart, viewName); |
---|
168 | } |
---|
169 | |
---|
170 | return (((hash && hash.charAt(0) == "#") ? hash.substr(1) : hash) || defaultView).split('&')[0]; // String |
---|
171 | } |
---|
172 | }; |
---|
173 | |
---|
174 | return hashUtil; |
---|
175 | |
---|
176 | }); |
---|