From f5ecbedeeb9d1646bec19e6c4f207a5b0a319cb2 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 30 Jul 2012 21:01:28 +0200 Subject: Added search tab. --- app.py | 29 ++++++++++++++++++++++++++++- static/index.html | 6 ++++++ static/init.js | 21 +++++++++++++++++++++ static/style.css | 13 +++++++------ 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index 6ea7082..55e514f 100755 --- a/app.py +++ b/app.py @@ -1,6 +1,6 @@ #!/usr/bin/env python2 -import db, codec, os, json, mimetypes, datetime +import db, codec, os, json, mimetypes, datetime, re, cgi from config import config class JSONApplication(object): @@ -54,9 +54,36 @@ class JSONApplication(object): start_response('200 OK', [('Content-Type', 'application/json')]) return [json.dumps({'recoded': not cached})] + re_search = re.compile(r'(")?((?(1)[^"]|[^ ])+)(?(1)")') + + def search(self, environ, start_response, path): + args = cgi.FieldStorage(environ = environ) + query = args.getvalue('q') + r = self.re_search.findall(query) + d = {} + l = [] + for _, v in r: + if ':' in v: + k, v = v.split(':', 1) + d[k] = v + else: + l.append(v) + + results = [] + try: + session = db.Session() + r = db.Track.search(session, *l, **d) + results = [x.dict() for x in r] + finally: + session.close() + + start_response('200 OK', []) + return json.dumps(results) + handlers = { 'list': list, 'hint': hint, + 'search': search, } def __call__(self, environ, start_response, path): diff --git a/static/index.html b/static/index.html index be4476e..b2bb27e 100644 --- a/static/index.html +++ b/static/index.html @@ -29,6 +29,7 @@
+
+ + +
diff --git a/static/init.js b/static/init.js index 2cdd84b..2ef7c9b 100644 --- a/static/init.js +++ b/static/init.js @@ -62,6 +62,21 @@ function load_directory(dir_id, dir_item) { }); } +function search_results(data) { + var results = $('#search-results'); + results.empty(); + $.each(data, function(i, track) { + var li = $(templates.directory_item(track)); + console.log(li); + $(li, 'a').click(function() { + console.log('clicked'); + playlist.add(track); + return false; + }); + results.append(li); + }); +} + $(document).ready(function() { $('#tabs').tabs(); preload_images(); @@ -81,4 +96,10 @@ $(document).ready(function() { playlist.hintnext(); } }); + $('#search_box').keypress(function(event) { + if(event.keyCode == 13) { + var val = $(this).val(); + $.get('/json/search?q=' + escape(val), search_results, 'json'); + } + }); }); diff --git a/static/style.css b/static/style.css index 9466b71..3aab5aa 100644 --- a/static/style.css +++ b/static/style.css @@ -1,14 +1,14 @@ * { padding: 0; margin: 0; } .ui-tabs-nav li a { font-size: small; } #progress { margin: .5em 1em; width: 300px; } -#directory-list, #playlist { font-size: small; } -#directory-list a, #playlist a { color: inherit; text-decoration: inherit; } -#directory-list a:hover, #playlist a:hover { text-decoration: underline; } -#directory-list .dir, #directory-list .track, #playlist a.play { background-repeat: no-repeat; padding-left: 20px; } +#directory-list, #playlist, #search-results { font-size: small; } +#directory-list a, #playlist a, #search-results a { color: inherit; text-decoration: inherit; } +#directory-list a:hover, #playlist a:hover, #search-results a:hover { text-decoration: underline; } +#directory-list .dir, #directory-list .track, #playlist a.play, #search-results .track { background-repeat: no-repeat; padding-left: 20px; } #directory-list .dir { background-image: url('/static/icons/folder.png'); } -#directory-list .track { background-image: url('/static/icons/music.png'); } +#directory-list .track, #search-results .track { background-image: url('/static/icons/music.png'); } #directory-list .nocache, #playlist .nocache a.play { background-image: url('/static/icons/music_nocache.png'); } -#directory-list { list-style-type: none; } +#directory-list, #search-results { list-style-type: none; } #playlist .playing a.play { background-image: url('/static/icons/music_playing.png'); } #playlist .loading a.play { background-image: url('/static/icons/loading.gif'); } #playlist { border-collapse: collapse; background: white; width: 100%; } @@ -16,3 +16,4 @@ #playlist td { padding: .3em; background: white; border: 1px solid #ddd; border-width: 0 1px; } #playlist tr:last-child td { border-bottom-width: 1px; } #playlist th { background: #ddd; text-align: left; padding: .5em; } +#search-results { margin-top: 1em; } -- cgit v1.2.3