Back to index

plone3  3.1.7
typewriter.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 from time import time
00020 from persistent.list import PersistentList
00021 
00022 class KSSTypewriterDemo(KSSView):
00023 
00024     def displayedKeys(self):
00025         keys = [
00026             [113, 119, 101, 114, 116, 122, 117, 105, 111, 112],
00027             [97, 115, 100, 102, 103, 104, 106, 107, 108],
00028             [121, 120, 99, 118, 98, 110, 109],
00029             [32]
00030         ]
00031         return keys
00032     
00033     def setCurrentLesson(self, title):
00034         text = self.getLessons()[title].strip()
00035         self.request.SESSION['typewriter'] = text
00036         self.getCommandSet('core').replaceInnerHTML('div#pattern', text)
00037         self.getCommandSet('core').setAttribute('input#field', 'value', '')
00038         self.getCommandSet('core').removeClass('input.lesson', 'active')
00039         self.getCommandSet('core').addClass('input#lesson_%s' % self.getLessonTitles().index(title), 'active')
00040         return self.render()
00041 
00042 
00043     def loadInitialPattern(self):
00044         lessonText = self.getLessons()[self.getLessonTitles()[0]]
00045         self.request.SESSION['typewriter'] = lessonText
00046         return lessonText
00047 
00048 
00049 
00050     def getLessons(self):
00051         lessons = {
00052             'Lesson 1'      : 'This is our first lesson at the SnowSprint',
00053             'Lesson 2'      : 'KSS is a generic javascript AJAX framework',
00054         }
00055         return lessons
00056 
00057     def getLessonTitles(self):
00058         titles = self.getLessons().keys()
00059         titles.sort()
00060         return titles
00061 
00062     
00063     def keydown(self, keycode):
00064         keycode = int(keycode)
00065         if keycode == 16:
00066             self.getCommandSet('core').addClass('div#key_16', 'active')
00067             for key in range(97, 97+26):
00068                 self.getCommandSet('core').replaceInnerHTML('div#key_%s' % key, chr(key).upper())
00069         elif keycode in range(97, 97+26) + range(65, 65+26) + [32]:
00070             smallKeycode = keycode
00071             if keycode in range(65, 65+26):
00072                 smallKeycode += 32
00073             self.getCommandSet('core').addClass('div#key_%s' % smallKeycode, 'active')
00074             currentText = self.request.SESSION['typewriter']
00075             if len(currentText)>0 and currentText[0]==chr(keycode):
00076                 currentText = currentText[1:]
00077                 self.request.SESSION['typewriter'] = currentText
00078                 if len(currentText)==0:
00079                     currentText = 'please select another lesson'
00080                     self.getCommandSet('core').removeClass('input.lesson', 'active')
00081                 self.getCommandSet('core').replaceInnerHTML('div#pattern', currentText.replace(' ', ' '))
00082         return self.render()
00083 
00084     def keyup(self, keycode):
00085         keycode = int(keycode)
00086         if keycode == 16:
00087             for key in range(97, 97+26):
00088                 self.getCommandSet('core').replaceInnerHTML('div#key_%s' % key, chr(key))
00089             self.getCommandSet('core').removeClass('div#key_16', 'active')
00090         elif keycode in range(97, 97+26) + range(65, 65+26) + [32]:
00091             smallKeycode = keycode
00092             if keycode in range(65, 65+26):
00093                 smallKeycode += 32
00094             self.getCommandSet('core').removeClass('div#key_%s' % smallKeycode, 'active')
00095         return self.render()
00096 
00097     def mousedown(self, content):
00098         if content.strip()=='Shift':
00099             if self.request.SESSION.get('typewriter_shift', False):
00100                 self.request.SESSION['typewriter_shift'] = False
00101                 return self.keyup(16)
00102             else:
00103                 self.request.SESSION['typewriter_shift'] = True
00104                 return self.keydown(16)
00105         try:
00106             keycode = ord(content.strip())
00107         except:
00108             keycode = 32
00109         return self.keydown(keycode)
00110 
00111     def mouseup(self, content, fieldValue):
00112         if content.strip()=='Shift':
00113             return self.render()
00114         try:
00115             keycode = ord(content.strip())
00116         except:
00117             keycode = 32
00118             content = ' '
00119         if self.request.SESSION.get('typewriter_shift', False):
00120             self.keyup(16)
00121         if keycode in range(97, 97+26) + range(65, 65+26) + [32]:
00122             self.getCommandSet('core').setAttribute('input#field', 'value', fieldValue + content)
00123             self.getCommandSet('core').focus('input#field')
00124         return self.keyup(keycode)