Back to index

plone3  3.1.7
demoview.py
Go to the documentation of this file.
00001 # Copyright (c) 2005-2007
00002 # Authors: KSS Project Contributors (see docs/CREDITS.txt)
00003 #
00004 # This program is free software; you can redistribute it and/or modify
00005 # it under the terms of the GNU General Public License version 2 as published
00006 # by the Free Software Foundation.
00007 #
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU General Public License
00014 # along with this program; if not, write to the Free Software
00015 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00016 # 02111-1307, USA.
00017 
00018 from kss.core import KSSView, force_unicode, KSSExplicitError, kssaction
00019 import datetime
00020 
00021 class KSSDemoView(KSSView):
00022 
00023     def clearDivContent(self):
00024         """ clear div content """
00025         self.getCommandSet('core').clearChildNodes('div#demo')
00026         return self.render()
00027 
00028     def copyFromDivContent(self):
00029         """ copy div content """
00030         self.getCommandSet('core').copyChildNodesFrom('div#copy', 'demo')
00031         return self.render()
00032 
00033     def copyToDivContent(self):
00034         """ copy div content """
00035         self.getCommandSet('core').copyChildNodesTo('div#copy', 'demo')
00036         return self.render()
00037 
00038     def moveToDivContent(self):
00039         """ copy div content """
00040         self.getCommandSet('core').copyChildNodesTo('div#copy', 'demo')
00041         self.getCommandSet('core').clearChildNodes('div#copy')
00042         return self.render()
00043 
00044     def getDivContent(self):
00045         """ returns div content """
00046         self.getCommandSet('core').replaceInnerHTML('div#demo', '<h1>it worked</h1>')
00047         self.getCommandSet('core').replaceInnerHTML('div#demo', '<h1 id="workedagain">it worked&nbsp;again</h1>')
00048         return self.render()
00049 
00050     def getCorrespondingSelect(self, value):
00051         """ returns select content """
00052         mapping = {}
00053         mapping['']=[]
00054         mapping['animals']=['dog', 'cat', 'cow']
00055         mapping['machines']=['computer', 'car', 'airplane']
00056         # XXX Note that originally we just used replaceInnerHTML to just put
00057         # the options inside the select, however this is principally broken 
00058         # on IE due to an IE bug. Microsoft has confirmed the bug but is not
00059         # giving information on whether it has or it will ever be fixed.
00060         # For further info, see http://support.microsoft.com/default.aspx?scid=kb;en-us;276228
00061         # The current solution, replace the outer node, works solidly.
00062         result = ['<select id="second">']
00063         result.extend(['<option>%s</option>' % item for item in mapping[value]])
00064         result.append('</select>')
00065         self.getCommandSet('core').replaceHTML('select#second', ' '.join(result))
00066         return self.render()
00067 
00068     def getAutoupdateMarkup(self):
00069         """ returns the current time """
00070         self.getCommandSet('core').replaceInnerHTML('div#update-wrapper', '<div id="update-area"></div>')
00071         return self.render()
00072 
00073     def getCurrentTime(self):
00074         """ returns the current time """
00075         self.getCommandSet('core').replaceInnerHTML('div#update-area', "<p>%s</p>" % str(datetime.datetime.now()))
00076         return self.render()
00077         
00078     def getInputField(self, value):
00079         'Inserts the value as entered into an input field'
00080         # We need to make unicode. But on Z2 we receive utf-8, on Z3 unicode
00081         value = force_unicode(value, 'utf')
00082         self.getCommandSet('core').replaceInnerHTML('div#text', 
00083                             '<div><input type="text" name="value" value="'+value+'" /></div>' \
00084                             '<input type="button" value="save" id="save" />'
00085                            )
00086         return self.render()
00087         
00088     def saveText(self, value):
00089         'Inserts the value to display it on the page'
00090         # We need to make unicode. But on Z2 we receive utf-8, on Z3 unicode
00091         value = force_unicode(value, 'utf')
00092         self.getCommandSet('core').replaceInnerHTML('div#text', value+'<input type="hidden" name="value" value="'+value+'" />')
00093         return self.render()
00094     
00095     def expandSubTree(self, value, xvalue):
00096         'Expands given subtree'
00097         self.getCommandSet('core').replaceInnerHTML('#text', 'works, expand %s (xhtml attr: %s)' % (value, xvalue))
00098         return self.render()
00099         
00100     def collapseSubTree(self, value, xvalue):
00101         'Collapses given subtree'
00102         self.getCommandSet('core').replaceInnerHTML('#text', 'works, collapse %s (xhtml attr: %s)' % (value, xvalue))
00103         return self.render()
00104 
00105     def cancelSubmitSave(self, text_save):
00106         # We need to make unicode. But on Z2 we receive utf-8, on Z3 unicode
00107         text_save = force_unicode(text_save, 'utf')
00108         self.getCommandSet('core').replaceInnerHTML('div#async', 'Async saved %s' % text_save)
00109         return self.render()
00110 
00111     def removeNodeXpath(self):
00112         # XXX the xpath selector is now moved out of the core, see suppl, product "azaxslt"
00113         sel = self.getSelector('xpath', "//P[@id='xpath']/following-sibling::*[position()=1]")
00114         self.getCommandSet('core').deleteNode(sel)
00115         return self.render()
00116        
00117     def clickedButton(self, id):
00118         'Show status of the button clicked'
00119         self.getCommandSet('core').replaceInnerHTML('#update-status', "<p>Button <b>%s</b> clicked. <i>%s</i></p>" % (id, datetime.datetime.now()))
00120         return self.render()
00121 
00122     def updateSlaveSelector(self, masterid, value):
00123         """ returns select content """
00124         mapping = {}
00125         mapping['']=[]
00126         mapping['animals']=['dog', 'cat', 'cow']
00127         mapping['machines']=['computer', 'car', 'airplane']
00128         # calculate the slave id
00129         master, _dummy = masterid.split('-')
00130         slaveid = '%s-slave' % master
00131         # make the payload
00132         result = ['<select id="%s">' % slaveid]
00133         result.extend(['<option>%s</option>' % item for item in mapping[value]])
00134         result.append('</select>')
00135         # XXX See above remark why we need to replace the outer select.
00136         self.getCommandSet('core').replaceHTML('select#%s' % slaveid, ' '.join(result))
00137         return self.render()
00138 
00139     def formSubmitSave(self):
00140         result = ['<p>Async saved:</p><table><th>Name:</th><th>Value:</th>']
00141         for key, value in self.request.form.items():
00142             result.append('<tr><td>%s</td><td>%s</td></tr>' % (key, value))
00143         result.append('</table>')
00144         # We need to make unicode. But on Z2 we receive utf-8, on Z3 unicode
00145         retval = force_unicode(''.join(result), 'utf')
00146         self.getCommandSet('core').replaceInnerHTML('div#async', retval)
00147         return self.render()
00148 
00149     def reset(self):
00150         self.getCommandSet('effects').effect('.effects', 'appear')
00151         return self.render()
00152 
00153     @kssaction
00154     def errTest(self, id, act):
00155         if act == 'error':
00156             raise Exception, 'We have an error here.'
00157         elif act == 'explicit':
00158             raise KSSExplicitError, 'Explicit error raised.'
00159         elif act == 'empty':
00160             # Just do nothing, we want to return a response with no commands.
00161             # This is valid behaviour, should raise no error, however 
00162             # gives a warning in the kukit log.
00163             pass
00164         ## XXX This is commented out by default, but you can try this on your own
00165         ## server, together with the timeout buttons in the page template timeout buttons in the page template.
00166         ## 
00167         #elif act == 'timeout':
00168         #    # Wait longer then timeout, this is currently 4 s
00169         #    time.sleep(6.0);
00170         #    # the next reply will never arrive.
00171         #    self.replaceInnerHTML('#update-status', u'Timeout response, button %s clicked. %s' % (id, datetime.datetime.now()))
00172         else:
00173             # act = noerror: standard response.
00174             self.getCommandSet('core').replaceInnerHTML('#update-status', u'Normal response, button %s clicked. %s' % (id, datetime.datetime.now()))
00175         return self.render()
00176 
00177     def htmlReplace(self):
00178         """html replace"""
00179         self.getCommandSet('core').replaceHTML('div#frame', '<div id="frame"><h1 id="core">KSS for a life.</h1></div>')
00180         return self.render()
00181 
00182     def htmlInsertBefore(self, text=''):
00183         """html insert"""
00184         self.getCommandSet('core').insertHTMLBefore('#frame', '<div class="type1">KSS for a life. %s</div>' % text)
00185         return self.render()
00186 
00187     def htmlInsertAfter(self, text=''):
00188         """html insert"""
00189         self.getCommandSet('core').insertHTMLAfter('#frame', '<div class="type1">KSS for a life. %s</div>' % text)
00190         return self.render()
00191 
00192     def htmlInsertAsFirstChild(self, text=''):
00193         """html insert"""
00194         self.getCommandSet('core').insertHTMLAsFirstChild('div#frame',
00195                                                           "first: %s " % text)
00196         return self.render()
00197 
00198     def htmlInsertAsLastChild(self, text=''):
00199         """html insert"""
00200         self.getCommandSet('core').insertHTMLAsLastChild('div#frame',
00201                                                          "last: %s" % text)
00202         return self.render()
00203 
00204 
00205     # protocol
00206     def protocolSmallDataset(self):
00207         self.getCommandSet('core').replaceInnerHTML(
00208             '#dataset-output', '<em>Worked</em>')
00209         return self.render()
00210 
00211     def protocolLargeDataset(self):
00212         text = 'really ' * 690
00213         self.getCommandSet('core').replaceInnerHTML(
00214             '#dataset-output', '<em>%s</em>' % (text + 'large text'))
00215         return self.render()
00216 
00217     def protocolAMP(self):
00218         self.getCommandSet('core').replaceInnerHTML(
00219             '#character-output', 'text & stuff')
00220         return self.render()
00221 
00222     def protocolLT(self):
00223         self.getCommandSet('core').replaceInnerHTML(
00224             '#character-output', 'text < stuff')
00225         return self.render()
00226 
00227     def protocolGT(self):
00228         self.getCommandSet('core').replaceInnerHTML(
00229             '#character-output', 'text > stuff')
00230         return self.render()
00231 
00232     def protocolENDCDATA(self):
00233         self.getCommandSet('core').replaceInnerHTML(
00234             '#character-output', 'before ]]> after')
00235         return self.render()
00236 
00237 
00238     def protocolSmallAttribute(self):
00239         self.getCommandSet('core').setAttribute(
00240             '#attribute-output', 'class', 'some smallattr')
00241         return self.render()
00242 
00243     def protocolLargeAttribute(self):
00244         classes = ' '.join(['h' + hex(i)[2:] * i for i in range(70)])
00245         self.getCommandSet('core').setAttribute(
00246             '#attribute-output', 'class', classes + ' largeattr')
00247         return self.render()
00248 
00249     def protocolAttributeAMP(self):
00250         self.getCommandSet('core').setAttribute(
00251             '#character-attr-output', 'title', 'text & stuff')
00252         return self.render()
00253 
00254     def protocolAttributeLT(self):
00255         self.getCommandSet('core').setAttribute(
00256             '#character-attr-output', 'title', 'text < stuff')
00257         return self.render()
00258 
00259     def protocolAttributeGT(self):
00260         self.getCommandSet('core').setAttribute(
00261             '#character-attr-output', 'title', 'text > stuff')
00262         return self.render()
00263 
00264     def protocolAttributeENDCDATA(self):
00265         self.getCommandSet('core').setAttribute(
00266             '#character-attr-output', 'title', 'before ]]> after')
00267         return self.render()
00268