Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

desfrenes http://www.desfrenes.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Generating Taconite command documents

// description of your code here
Hello,

This is a port of a php class used to generate XML taconite command documents, useful for (very) easy and powerful ajaxy stuff, if you don't know what that is just check it there in french : http://www.desfrenes.com/playground/taconite/ or there in english : http://www.malsup.com/jquery/taconite/.

Basically what it does is generate an XML document that is later processed by a javascript plugin which executes a serie of DOM modifications.

About the code, I'm a Django beginner as well as a Python beginner so kind advices are welcome.

Cheers.
   1  
   2  # usage:
   3  #
   4  # t = Taconite()
   5  #
   6  # t.append("#toto","<label>test</label>")
   7  # t.remove("#tutu")
   8  # t.js('alert("hello world");')
   9  # t.toggleClass('blue','body')
  10  # t.css("body","background-color","white")
  11  # [...]
  12  # print t.toprettyxml()
  13  
  14  import xml.dom.minidom as dom
  15  
  16  class Taconite(dom.Document):
  17      def __init__(self):
  18          dom.Document.__init__(self)
  19          taconite = self.createElement("taconite")
  20          self.appendChild(taconite)
  21  
  22      def __str__(self):
  23          return self.toxml(encoding="utf-8")
  24      
  25      def camelizeCssProperty(self,property):
  26          words = property.split('-')
  27          camelized = words[0].lower()
  28          for word in words[1:] :
  29              camelized = camelized + word[0].upper() + word[1:]
  30          return camelized
  31      
  32      def js(self,script):
  33          command = self.createElement("eval")
  34          js = self.createTextNode(script)
  35          command.appendChild(js)
  36          self.childNodes[0].appendChild(command)
  37      
  38      def changeContentCommand(self,method,selector,content):
  39          html_dom = dom.parseString(content)
  40          command = self.createElement(method)
  41          command.setAttribute("select",selector)
  42          command.appendChild(html_dom.childNodes[0])
  43          self.childNodes[0].appendChild(command)
  44      
  45      def changeStateCommand(self,action,selector):
  46          command = self.createElement(action)
  47          command.setAttribute("select",selector)
  48          self.childNodes[0].appendChild(command)
  49      
  50      def CssCommand(self,action,css_class,selector):
  51          command1 = self.createElement(action)
  52          command1.setAttribute("select",selector)
  53          command1.setAttribute("arg1",css_class)
  54          command2 = self.createElement(action)
  55          command2.setAttribute("select",selector)
  56          command2.setAttribute("value",css_class)
  57          self.childNodes[0].appendChild(command1)
  58          self.childNodes[0].appendChild(command2)
  59      
  60      def addClass(self,css_class,selector):
  61          self.CssCommand("addClass",css_class,selector)
  62  
  63      def removeClass(self,css_class,selector):
  64          self.CssCommand("remove",css_class,selector)
  65  
  66      def toggleClass(self,css_class,selector):
  67          self.CssCommand("toggleClass",css_class,selector)
  68      
  69      def append(self,selector,content):
  70          self.changeContentCommand("append",selector,content)
  71      
  72      def prepend(self,selector,content):
  73          self.changeContentCommand("prepend",selector,content)
  74          
  75      
  76      def before(self,selector,content):
  77          self.changeContentCommand("before",selector,content)
  78          
  79      
  80      def after(self,selector,content):
  81          self.changeContentCommand("after",selector,content)
  82      
  83      def wrap(self,selector,content):
  84          self.changeContentCommand("wrap",selector,content)
  85      
  86      def replace(self,selector,content):
  87          self.changeContentCommand("replace",selector,content)
  88      
  89      def replaceContent(self,selector,content):
  90          self.changeContentCommand("replaceContent",selector,content)
  91      
  92      def remove(self,selector):
  93          self.changeStateCommand("remove",selector)
  94      
  95      def show(self,selector):
  96          self.changeStateCommand("show",selector)
  97      
  98      def hide(self,selector):
  99          self.changeStateCommand("hide",selector)
 100      
 101      def removeContent(self,selector):
 102          self.changeStateCommand("empty",selector)
 103      
 104      def css(self,selector,property,value):
 105          command = self.createElement("css")
 106          command.setAttribute("select",selector)
 107          command.setAttribute("name",self.camelizeCssProperty(property))
 108          command.setAttribute("value",value)
 109          self.childNodes[0].appendChild(command)
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS