Skip to content

Commit

Permalink
Sync
Browse files Browse the repository at this point in the history
git-svn-id: svn://cherokee-project.com/CTK/trunk@4078 5dc97367-97f1-0310-9951-d761b3857238
  • Loading branch information
alobbs committed Dec 31, 2009
1 parent 364e280 commit b84d1d1
Showing 1 changed file with 60 additions and 6 deletions.
66 changes: 60 additions & 6 deletions CTK/Dialog.py
Expand Up @@ -35,9 +35,29 @@
"""

JS = """
$("#%(id)s").dialog();
$("#%(id)s").dialog(%(dialog_props)s);
"""

def py2js_dic (d):
js_pairs = []

for key in d:
val = d[key]
if type(val) == bool:
js_pairs.append ('%s: %s' %(key, ('false', 'true')[val]))
elif type(val) == int:
js_pairs.append ('%s: %d' %(key, val))
elif type(val) == str:
if '/* code */' in val:
js_pairs.append ("%s: %s" %(key, val))
else:
js_pairs.append ("%s: '%s'" %(key, val))
else:
assert false, "Unknown data type"

return "{%s}" % (", ".join(js_pairs))


class Dialog (Container):
def __init__ (self, props=None):
Container.__init__ (self)
Expand All @@ -47,15 +67,32 @@ def __init__ (self, props=None):
else:
self.props = {}

self.title = self.props.pop('title', '')
self.id = 'dialog%d'%(self.uniq_id)
self.id = 'dialog%d'%(self.uniq_id)
self.title = self.props.pop('title', '')
self.buttons = []

# Defaults
if not 'modal' in self.props:
self.props['modal'] = True
if not 'resizable' in self.props:
self.props['resizable'] = False

def AddButton (self, caption, action):
self.buttons.append ((caption, action))

def Render (self):
# Build buttons
if self.buttons:
self.props['buttons'] = self._render_buttons_property()

# Render
render_child = Container.Render (self)
dialog_props = py2js_dic (self.props)

props = {'id': self.id,
'title': self.title,
'content': render_child.html}
props = {'id': self.id,
'title': self.title,
'content': render_child.html,
'dialog_props': dialog_props}

html = HTML %(props)
js = JS %(props)
Expand All @@ -66,3 +103,20 @@ def Render (self):

return render

def _render_buttons_property (self):
buttons_js = []

# Render the button entries
for tmp in self.buttons:
button_caption, action = tmp
tmp = '"%(button_caption)s": function() {'
if action == "close":
tmp += '$(this).dialog("close");'
else:
tmp += 'window.location.replace("%(action)s")'
tmp += '}'
buttons_js.append (tmp%(locals()))

# Add the property
js = '/* code */ { %s }' %(','.join(buttons_js))
return js

0 comments on commit b84d1d1

Please sign in to comment.