Index: documentViewer.py
===================================================================
--- documentViewer.py	(revision 22)
+++ documentViewer.py	(revision 25)
@@ -18,4 +18,12 @@
 import urllib
 import zLOG
+
+def getInt(number, default=0):
+    """returns always an int (0 in case of problems)"""
+    try:
+        return int(number)
+    except:
+        return default
+    
 
 def getTextFromNode(nodename):
@@ -80,12 +88,15 @@
 
     
-    def __init__(self,id,imageViewerUrl,title="",digilibBaseUrl=None):
+    def __init__(self,id,imageViewerUrl,title="",digilibBaseUrl=None,thumbcols=2,thumbrows=10):
         """init document viewer"""
         self.id=id
         self.title=title
         self.imageViewerUrl=imageViewerUrl
-        self.digilibBaseUrl = digilibBaseUrl
-        if not self.digilibBaseUrl:
+        if not digilibBaseUrl:
             self.digilibBaseUrl = self.findDigilibUrl()
+        else:
+            self.digilibBaseUrl = digilibBaseUrl
+        self.thumbcols = thumbcols
+        self.thumbrows = thumbrows
         # add template folder so we can always use template.something
         self.manage_addFolder('template')
@@ -93,5 +104,5 @@
 
     security.declareProtected('View','index_html')
-    def index_html(self,mode,url,start=0,pn=1):
+    def index_html(self,mode,url,start=None,pn=1):
         '''
         view it
@@ -113,23 +124,25 @@
         print "dlbaseurl:", self.digilibBaseUrl
 
-        docinfo = self.getDocinfo(mode=mode, url=url)
-        pageinfo = self.getPageinfo(start=start,current=pn)
+        docinfo = self.getDocinfo(mode=mode,url=url)
+        pageinfo = self.getPageinfo(start=start,current=pn,docinfo=docinfo)
         pt = getattr(self.template, 'viewer_main')
         return pt(docinfo=docinfo,pageinfo=pageinfo)
   
   
-    def imageLink(self,nr):
-        """link hinter den images"""
-        paramsTmp=cgi.parse_qs(self.REQUEST['QUERY_STRING'])
-        params={}
-        for x in paramsTmp.iteritems():
-                params[x[0]]=x[1][0]
-    
-        params['pn']=nr
-        newUrl=self.REQUEST['URL']+"?"+urllib.urlencode(params)
-        return newUrl
-        
+    def getLink(self,param=None,val=None):
+        """link to documentviewer with parameter param set to val"""
+        params=cgi.parse_qs(self.REQUEST['QUERY_STRING'])
+        if param is not None:
+            if val is None and params.has_key(param):
+                del params[param]
+            else:
+                params[param] = val
+        
+        url=self.REQUEST['URL']+"?"+urllib.urlencode(params, doseq=True)
+        return url
+
+    
     def getStyle(self, idx, selected, style=""):
-        """returns a string with the given style + 'sel' if path == selected."""
+        """returns a string with the given style and append 'sel' if path == selected."""
         #zLOG.LOG("documentViewer (getstyle)", zLOG.INFO, "idx: %s selected: %s style: %s"%(idx,selected,style))
         if idx == selected:
@@ -137,45 +150,4 @@
         else:
             return style    
-
-            
-    def thumbruler(self,cols,rows,start,maximum):
-        """ruler for thumbs"""
-        ret=""
-        paramsTmp=cgi.parse_qs(self.REQUEST['QUERY_STRING'])
-        params={}
-        for x in paramsTmp.iteritems():
-
-            if not x[0]=="start":
-                params[x[0]]=x[1][0]
-
-        newUrlSelect=self.REQUEST['URL']+"?"+urllib.urlencode(params)    
-        if start>0:
-            newStart=max(start-cols*rows,0)
-            params['start']=newStart
-            newUrl=self.REQUEST['URL']+"?"+urllib.urlencode(params)
-            ret+="""<a href="%s">prev</a>"""%newUrl
-
-
-        ret+="""<select onChange="location.href='%s&start='+this.options[this.selectedIndex].value">"""%newUrlSelect
-        nr,rest=divmod(maximum,cols*rows)
-        if rest > 0:
-            nr+=1
-        for i in range(nr):
-            nr=i*cols*rows
-           
-            if (start >= nr) and (start < nr+cols*rows):  
-                ret+="""<option value="%s" selected>%s</option>"""%(nr,nr)
-            else:
-                ret+="""<option value="%s">%s</option>"""%(nr,nr)
-        ret+="</select>"
-        
-        if start<maximum:
-            newStart=min(start+cols*rows,maximum)
-            params['start']=newStart
-            newUrl=self.REQUEST['URL']+"?"+urllib.urlencode(params)
-            ret+="""<a href="%s">next</a>"""%newUrl
-        
-        return ret
-    
         
         
@@ -187,5 +159,4 @@
             docinfo = {}
             
-        metaData=self.metadata.main.meta.bib
         if dom is None:
             server="http://nausikaa.mpiwg-berlin.mpg.de/digitallibrary/servlet/Texter?fn="
@@ -197,15 +168,16 @@
                 return docinfo
         
-        type=dom.xpath("//bib/@type")
-        if type and (len(type)>0):
-            type=type[0].value
+        metaData=self.metadata.main.meta.bib
+        bibtype=dom.xpath("//bib/@type")
+        if bibtype and (len(bibtype)>0):
+            bibtype=bibtype[0].value
         else:
-            type="generic"
-        type=type.replace("-"," ")# wrong typesiin index meta "-" instead of " "
-        hash=metaData.generateMappingForType(type)
-
-        docinfo['author']=getTextFromNode(dom.xpath("//bib/%s"%hash['author'][0])[0])
-        docinfo['title']=getTextFromNode(dom.xpath("//bib/%s"%hash['title'][0])[0])
-        docinfo['year']=getTextFromNode(dom.xpath("//bib/%s"%hash['year'][0])[0])
+            bibtype="generic"
+        bibtype=bibtype.replace("-"," ") # wrong typesiin index meta "-" instead of " " (not wrong! ROC)
+        bibmap=metaData.generateMappingForType(bibtype)
+        if len(bibmap) > 0:
+            docinfo['author']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['author'][0])[0])
+            docinfo['title']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['title'][0])[0])
+            docinfo['year']=getTextFromNode(dom.xpath("//bib/%s"%bibmap['year'][0])[0])
         
         return docinfo
@@ -243,5 +215,5 @@
            pt=getParamFromDigilib(image,'size')
            docinfo['imagePath'] = image
-           docinfo['numberOfPages'] = pt
+           docinfo['numPages'] = pt
            
        viewerUrls=dom.xpath("//texttool/digiliburlprefix")
@@ -269,5 +241,5 @@
         path=path.replace("/mpiwg/online","")
         pt=getParamFromDigilib(path,'size')
-        docinfo['numberOfPages'] = pt
+        docinfo['numPages'] = pt
         imageUrl=genericDigilib+"/servlet/Scaler?fn=%s"%path
         docinfo['imageURL'] = imageUrl
@@ -300,9 +272,26 @@
         
         
-    def getPageinfo(self, start, current):
+    def getPageinfo(self, current, start=None, rows=None, cols=None, docinfo=None):
         """returns pageinfo with the given parameters"""
         pageinfo = {}
+        current = getInt(current)
+        pageinfo['current'] = current
+        rows = int(rows or self.thumbrows)
+        pageinfo['rows'] = rows
+        cols = int(cols or self.thumbcols)
+        pageinfo['cols'] = cols
+        grpsize = cols * rows
+        pageinfo['groupsize'] = grpsize
+        start = getInt(start, default=(int(current / grpsize) * grpsize +1))
+        print "start3:", start
         pageinfo['start'] = start
-        pageinfo['current'] = current
+        pageinfo['end'] = start + grpsize
+        if docinfo is not None:
+            np = int(docinfo['numPages'])
+            pageinfo['end'] = min(pageinfo['end'], np)
+            pageinfo['numgroups'] = int(np / grpsize)
+            if np % grpsize > 0:
+                pageinfo['numgroups'] += 1
+                
         return pageinfo
                 
@@ -361,20 +350,4 @@
 #
 
-    def image(self,mode,url,pn):
-        """give image out"""
-        if mode=="texttool": #index.meta with texttool information
-            (viewerUrl,imagepath,textpath)=parseUrlTextTool(url)
-            if not viewerUrl:
-                viewerUrl=self.imageViewerUrl
-            url=viewerUrl+"pn=%s&fn=%s"%(pn,imagepath[0])
-            ret="""<iframe height="100%%" width="100%%" src="%s"/>"""%url
-            return url
-        elif mode=="imagepath":
-            url=url.replace("/mpiwg/online","")
-            url=self.imageViewerUrl+"pn=%s&fn=%s"%(pn,url)
-            ret="""<iframe height="100%%" width="100%%" src="%s"/>"""%url
-            return url
-            
-        
     def findDigilibUrl(self):
         """try to get the digilib URL from zogilib"""
@@ -386,9 +359,11 @@
             return None
     
-    def changeDocumentViewer(self,imageViewerUrl,title="",digilibBaseUrl=None,RESPONSE=None):
+    def changeDocumentViewer(self,imageViewerUrl,title="",digilibBaseUrl=None,thumbrows=2,thumbcols=10,RESPONSE=None):
         """init document viewer"""
         self.title=title
         self.imageViewerUrl=imageViewerUrl
         self.digilibBaseUrl = digilibBaseUrl
+        self.thumbrows = thumbrows
+        self.thumbcols = thumbcols
         
         if RESPONSE is not None:
