Package library :: Package pyjamas :: Module Location
[hide private]
[frames] | no frames]

Source Code for Module library.pyjamas.Location

 1  from __pyjamas__ import JS 
 2   
3 -def makeUrlDict(urlstring):
4 dict = {} 5 pairs = urlstring.split("&") 6 for pair in pairs: 7 if len(pair) < 3: continue 8 kv = pair.split("=",1) 9 dict[kv[0]] = kv[1] 10 return dict
11
12 -def makeUrlStringFromDict(d):
13 pairs = [] 14 for k,v in d.iteritems(): 15 pairs.append(k+"="+v) 16 return "&".join(pairs)
17
18 -class Location:
19 """ 20 Retrieve this class by calling Window.getLocation(). 21 22 This provides a pyjs wrapper for the current location, 23 with some utility methods for convenience. 24 25 """
26 - def __init__(self, location):
27 self.location = location 28 self.searchDict = None
29
30 - def getHash(self):
31 return self.location.hash
32
33 - def getHashDict(self):
34 if not self.hashDict or self.hashDictHash != self.getHash(): 35 self.hashDictHash = self.getHash() 36 self.hashDict = makeUrlDict(self.getHash()[1:]) 37 return self.hashDict
38
39 - def getHost(self):
40 return self.location.host
41
42 - def getHostname(self):
43 return self.location.hostname
44
45 - def getHref(self):
46 return self.location.href
47
48 - def getPageHref(self):
49 """ 50 Return href with any search or hash stripped 51 """ 52 href = self.location.href 53 if href.find("?"): href = href.split("?")[0] 54 if href.find("#"): href = href.split("#")[0] 55 return href
56
57 - def getPathname(self):
58 return self.location.pathname
59
60 - def getPort(self):
61 return self.location.port
62
63 - def getProtocol(self):
64 return self.location.protocol
65
66 - def getSearch(self):
67 return self.location.search or ""
68
69 - def getSearchDict(self):
70 if self.searchDict is None: 71 search = self.getSearch()[1:] 72 self.searchDict = makeUrlDict(search) 73 return self.searchDict
74
75 - def getSearchVar(self, key, default=None):
76 return self.getSearchDict().get(key, default)
77
78 - def reload(self):
79 self.location.reload()
80
81 - def setHref(self, href):
82 self.location.href = href
83
84 - def setSearch(self, search):
85 self.location.search = search
86
87 - def setSearchDict(self, searchDict):
88 self.setSearch(makeUrlStringFromDict(searchDict))
89
90 - def setHash(self, hash):
91 self.location.hash = hash
92
93 - def setHashDict(self, hashDict):
94 self.setHash(makeUrlStringFromDict(hashDict))
95