utils.py
branchstable
changeset 3838 9cc134372bf8
parent 3789 fb22b55f80f8
child 3851 3a18a0a24411
equal deleted inserted replaced
3837:0cb409871d42 3838:9cc134372bf8
   212 
   212 
   213 
   213 
   214     def add_raw(self, rawheader):
   214     def add_raw(self, rawheader):
   215         self.write(rawheader)
   215         self.write(rawheader)
   216 
   216 
   217     def define_var(self, var, value):
   217     def define_var(self, var, value, override=True):
   218         self.jsvars.append( (var, value) )
   218         """adds a javascript var declaration / assginment in the header
       
   219 
       
   220         :param var: the variable name
       
   221         :param value: the variable value (as a raw python value,
       
   222                       it will be jsonized later)
       
   223         :param override: if False, don't set the variable value if the variable
       
   224                          is already defined. Default is True.
       
   225         """
       
   226         self.jsvars.append( (var, value, override) )
   219 
   227 
   220     def add_post_inline_script(self, content):
   228     def add_post_inline_script(self, content):
   221         self.post_inlined_scripts.append(content)
   229         self.post_inlined_scripts.append(content)
   222 
   230 
   223     def add_onload(self, jscode, jsoncall=False):
   231     def add_onload(self, jscode, jsoncall=False):
   267         """
   275         """
   268         w = self.write
   276         w = self.write
   269         # 1/ variable declaration if any
   277         # 1/ variable declaration if any
   270         if self.jsvars:
   278         if self.jsvars:
   271             w(u'<script type="text/javascript"><!--//--><![CDATA[//><!--\n')
   279             w(u'<script type="text/javascript"><!--//--><![CDATA[//><!--\n')
   272             for var, value in self.jsvars:
   280             for var, value, override in self.jsvars:
   273                 w(u'%s = %s;\n' % (var, dumps(value)))
   281                 vardecl = u'%s = %s;' % (var, dumps(value))
       
   282                 if not override:
       
   283                     vardecl = (u'if (typeof %s == "undefined") {%s}' %
       
   284                                (var, vardecl))
       
   285                 w(vardecl + u'\n')
   274             w(u'//--><!]]></script>\n')
   286             w(u'//--><!]]></script>\n')
   275         # 2/ css files
   287         # 2/ css files
   276         for cssfile, media in self.cssfiles:
   288         for cssfile, media in self.cssfiles:
   277             w(u'<link rel="stylesheet" type="text/css" media="%s" href="%s"/>\n' %
   289             w(u'<link rel="stylesheet" type="text/css" media="%s" href="%s"/>\n' %
   278               (media, xml_escape(cssfile)))
   290               (media, xml_escape(cssfile)))