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

Source Code for Module library.pyjamas.gmaps.Utils

  1  # Copyright (C) 2009 Daniel Carvalho <idnael@gmail.com> 
  2  # 
  3  # Licensed under the Apache License, Version 2.0 (the "License"); 
  4  # you may not use this file except in compliance with the License. 
  5  # You may obtain a copy of the License at 
  6  # 
  7  #     http://www.apache.org/licenses/LICENSE-2.0 
  8  # 
  9  # Unless required by applicable law or agreed to in writing, software 
 10  # distributed under the License is distributed on an "AS IS" BASIS, 
 11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 12  # See the License for the specific language governing permissions and 
 13  # limitations under the License. 
 14   
 15  from __pyjamas__ import JS 
 16   
 17   
 18  # Converts javascript structures from googlemaps javascript library to 
 19  # python structures, and vice-versa. 
 20  # 
 21  # Example: 
 22  # jsobj=JS("""[{nome:"danire", year:1814}, {nome:"joano", year:"1901"}]""") 
 23  # 
 24  # #print jsobj[0].nome  # this is an error! 
 25  # 
 26  # fields = dictToJs({"lista": 'l', "lista[]": 'd'}) 
 27  # pyobj=translateGmapsObject(jsobj,"lista",fields) 
 28  # for line in pyobj: 
 29  #   print line.nome, line.year 
 30  # 
 31  # jsobj2=translateGmapsObject(pyobj,"lista",fields,True) 
 32  # #jsobj2 is exactly the same as jsobj! 
 33   
 34   
35 -def translateGmapsObject(obj, fieldName, fields, pyToJs):
36 JS(""" 37 //console['log']("translateGmapsObject " + fieldNameXXX+"("+pyToJs+")") 38 39 if (! (@{{fieldName}} in @{{fields}})) 40 { 41 //console['log']("nothing") 42 return @{{obj}}; 43 } 44 else{ 45 @{{action}} = @{{fields}}[@{{fieldName}}] 46 //console['log']("action=" + action) 47 48 if (@{{action}} == 'd') 49 { 50 //console['log']("is dict") 51 // this newobj can be used in js and also in python, 52 // like this "newobj['field']" 53 var newobj = {} 54 for (var i in @{{obj}}) 55 // vai ficar disponivel como uma propriedade, no python! 56 newobj[i] = $m['translateGmapsObject'](@{{obj}}[i], i, @{{fields}}, @{{pyToJs}}); 57 return newobj 58 59 } 60 else if (@{{action}} == 'l') 61 { 62 if (@{{pyToJs}}) { 63 var newobj = $m['listToJs'](@{{obj}}) 64 //console['log']("is list py->js") 65 for (var i in newobj){ 66 newobj[i]=$m['translateGmapsObject']( 67 newobj[i], @{{fieldName}} + "[]", @{{fields}},@{{pyToJs}} ) ; 68 } 69 return newobj 70 }else{ 71 //console['log']("is list js->py") 72 var newobj = @{{list}}([]) 73 for (var i in @{{obj}}) 74 newobj['append']($m['translateGmapsObject']( 75 @{{obj}}[i], @{{fieldName}} + "[]", @{{fields}},@{{pyToJs}} )); 76 return newobj 77 } 78 } 79 else 80 { 81 //console['log']("is special") 82 return @{{action}}(@{{obj}}) 83 } 84 } 85 """)
86 87 88 # converts a python dict to js 89 # It can be used in python functions that have variable number of args 90 # 91 # like 92 # def MapOptions(**params): 93 # return dictToJs(params) 94 # 95 # if MapOptions is called without arguments, the for loop will 96 # raise an exception. 97 # I could use the test "if params" BUT it always gives True... 98 # So I have to catch the exception. 99 100
101 -def dictToJs(dict):
102 obj = JS("{}") 103 try: 104 for key in dict: 105 value = dict[key] 106 JS("@{{obj}}[@{{key}}] = @{{value}}") 107 except: 108 pass 109 110 return obj
111 112 113 # Converts a python list to a javascript list
114 -def listToJs(list):
115 obj = JS("[]") 116 for i in list: 117 obj.push(i) 118 return obj
119 120 121 # LISTENERS 122 123 # This functions add python listener methods to any 124 # gmaps javascript object 125 126
127 -def createListenerMethods(obj):
128 obj.addListener = __addListener 129 obj.removeListener = __removeListener 130 obj.clearListeners = __clearListeners 131 obj.clearInstanceListeners = __clearInstanceListeners 132 133 #obj.dumpListeners = __dumpListeners # para debug 134 135 obj.__listeners = {} #__ !
136 137
138 -def __dumpListeners():
139 self = JS("this") 140 print "DUMP" 141 for eventName in self.__listeners: 142 print " " + eventName 143 for list in self.__listeners[eventName]: 144 print " " + str(list)
145 146
147 -def __addListener(eventName, callback):
148 self = JS("this") 149 150 thelist = JS(""" 151 $wnd['google']['maps']['event']['addListener'](this, @{{eventName}}, function(event) { 152 @{{callback}}(event); 153 }) 154 """) 155 156 # I have to keep information about the registered listeners for 157 # this instance! 158 159 if eventName in self.__listeners: 160 self.__listeners[eventName].append(thelist) 161 else: 162 self.__listeners[eventName] = [thelist] 163 164 return thelist
165 166
167 -def __removeListener(list):
168 self = JS("this") 169 170 for eventName in self.__listeners: 171 if list in self.__listeners[eventName]: 172 JS("""$wnd['google']['maps']['event']['removeListener'](@{{list}});""") 173 self.__listeners[eventName].remove(list) 174 return
175 # if we get here, there is nothing to remove, 176 # the listener specified doesn't exist or does not belong to this object 177 178
179 -def __clearListeners(eventName):
180 self = JS("this") 181 182 JS("""$wnd['google']['maps']['event']['clearListeners'](this, @{{eventName}})""") 183 if eventName in self.__listeners: 184 del self.__listeners[eventName]
185 186
187 -def __clearInstanceListeners():
188 self = JS("this") 189 190 JS("""$wnd['google']['maps']['event']['clearInstanceListeners'](this)""") 191 self.__listeners = {}
192