changeset 56:0249782e231e

pics2: add np_keys and selected thumbnail anchoring
author paulo
date Tue, 10 Sep 2013 01:04:24 -0700
parents 0fbe37b56e84
children b7966ae653f2 675002109a6a
files pics2/np_keys.js pics2/pics_app.py pics2/pics_test_server.py
diffstat 3 files changed, 74 insertions(+), 21 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/pics2/np_keys.js	Tue Sep 10 01:04:24 2013 -0700
     1.3 @@ -0,0 +1,23 @@
     1.4 +function getKeypress(e) {
     1.5 +	c = null
     1.6 +
     1.7 +	if (e.which == null)
     1.8 +		c = String.fromCharCode(e.keyCode);   // IE
     1.9 +	else if (e.which != 0 && e.charCode != 0)
    1.10 +		c = String.fromCharCode(e.which);     // All others
    1.11 +
    1.12 +	if (c != null) {
    1.13 +		if (c == 'n')
    1.14 +			goHref('next');
    1.15 +		else if (c == 'p')
    1.16 +			goHref('prev');
    1.17 +		else if (c == 'u')
    1.18 +			goHref('up');
    1.19 +	}
    1.20 +}
    1.21 +
    1.22 +function goHref(id) {
    1.23 +	window.location.href = document.getElementById(id).href;
    1.24 +}
    1.25 +
    1.26 +document.onkeypress = getKeypress
     2.1 --- a/pics2/pics_app.py	Thu Aug 22 23:53:56 2013 -0700
     2.2 +++ b/pics2/pics_app.py	Tue Sep 10 01:04:24 2013 -0700
     2.3 @@ -79,16 +79,12 @@
     2.4  		return (root, header, body)
     2.5  	
     2.6  	
     2.7 -	def _go_thumbnail_links_to_browse_imgs_html_body(self, body, t, b, selclass=None):
     2.8 +	def _go_thumbnail_links_to_browse_imgs_html_body(self, body, t, b, a_args={}, img_args={}):
     2.9  		thumb_img_url = self._get_pics_url(t)
    2.10  		browse_url = self._get_app_url(b)
    2.11  
    2.12 -		a = body.a(href=browse_url)
    2.13 -
    2.14 -		if selclass is not None:
    2.15 -			a.img(src=thumb_img_url, klass=selclass)
    2.16 -		else:
    2.17 -			a.img(src=thumb_img_url)
    2.18 +		a = body.a(href=browse_url, **a_args)
    2.19 +		a.img(src=thumb_img_url, **img_args)
    2.20  
    2.21  		body.text(' ')
    2.22  	
    2.23 @@ -167,7 +163,7 @@
    2.24  		html_p = html_body.p
    2.25  		for (t, b) in _get_images(d):
    2.26  			if from_img is not None and b == from_img:
    2.27 -				self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, b, "sel2")
    2.28 +				self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, b, img_args={"klass":"sel2", "id":"selected"})
    2.29  			else:
    2.30  				self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, b)
    2.31  	
    2.32 @@ -201,6 +197,8 @@
    2.33  			raise AssertionError
    2.34  	
    2.35  		(html_root, html_header, html_body) = self._get_standard_html_doc(u"%s \u2014 %s of %s" % (d, x, len(imgs)))
    2.36 +
    2.37 +		html_header.script('', type="text/javascript", src=self._get_pics_url("np_keys.js"))
    2.38  	
    2.39  		browse_img_url = self._get_pics_url(img)
    2.40  		html_body.p.img(src=browse_img_url)
    2.41 @@ -208,13 +206,21 @@
    2.42  		logging.debug("imgs_circ = %s" % imgs_circ)
    2.43  	
    2.44  		html_p = html_body.p
    2.45 -		for i in imgs_circ:
    2.46 -			if i is not None:
    2.47 -				(t, b) = i
    2.48 +		for (i, img_c) in enumerate(imgs_circ):
    2.49 +			if img_c is not None:
    2.50 +				(t, b) = img_c
    2.51 +				a_args = {}
    2.52 +				img_args = {}
    2.53  				if b == img:
    2.54 -					self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, d + "?from=" + img, "sel")
    2.55 -				else:
    2.56 -					self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, b)
    2.57 +					a_args = {"id": "up"}
    2.58 +					img_args = {"klass": "sel"}
    2.59 +					b = "%s?from=%s#selected" % (d, img)
    2.60 +				elif i == v + 1:
    2.61 +					a_args = {"id": "next"}
    2.62 +				elif i == v - 1:
    2.63 +					a_args = {"id": "prev"}
    2.64 +
    2.65 +				self._go_thumbnail_links_to_browse_imgs_html_body(html_p, t, b, a_args, img_args)
    2.66  	
    2.67  		return html_root
    2.68  
     3.1 --- a/pics2/pics_test_server.py	Thu Aug 22 23:53:56 2013 -0700
     3.2 +++ b/pics2/pics_test_server.py	Tue Sep 10 01:04:24 2013 -0700
     3.3 @@ -1,14 +1,38 @@
     3.4 -import wsgiref.simple_server 
     3.5 -import SocketServer
     3.6 +import os
     3.7 +import sys
     3.8 +import signal
     3.9 +
    3.10 +import cherrypy
    3.11 +from cherrypy import wsgiserver
    3.12  
    3.13  import pics_app
    3.14  
    3.15  
    3.16 -class ThreadingWSGIServer(SocketServer.ThreadingMixIn, wsgiref.simple_server.WSGIServer):
    3.17 -	pass
    3.18 +def sighandler(signum, frame):
    3.19 +	sys.stderr.write("Caught signal: %s \n" % signum)
    3.20 +	server.stop()
    3.21 +
    3.22 +
    3.23 +class FileServerRoot:
    3.24 +	def default(self, *args):
    3.25 +		if len(args) == 0:
    3.26 +			raise cherrypy.HTTPError(404)
    3.27 +
    3.28 +		filepath = os.path.abspath(os.path.join(*args))
    3.29 +		return cherrypy.lib.static.serve_file(filepath)
    3.30 +
    3.31 +	default.exposed = True
    3.32  
    3.33  
    3.34  if __name__ == "__main__":
    3.35 -	httpd = ThreadingWSGIServer(('', 8000), wsgiref.simple_server.WSGIRequestHandler)
    3.36 -	httpd.set_app(pics_app.app)
    3.37 -	httpd.serve_forever()
    3.38 +	fileServerApp = cherrypy.Application(FileServerRoot())
    3.39 +	dispatcher = wsgiserver.WSGIPathInfoDispatcher({
    3.40 +		"/pics.fcgi": pics_app.app,
    3.41 +		"": fileServerApp,
    3.42 +	})
    3.43 +	server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 8000), dispatcher)
    3.44 +
    3.45 +	signal.signal(signal.SIGINT, sighandler)
    3.46 +	signal.signal(signal.SIGTERM, sighandler)
    3.47 +
    3.48 +	server.start()