From 802a86a276784601e0a952611bfcfb823831608f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Asbj=C3=B8rn=20Sloth=20T=C3=B8nnesen?= <asbjorn@asbjorn.biz>
Date: Fri, 10 Aug 2012 21:14:52 +0000
Subject: implement search support
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Asbjørn Sloth Tønnesen <asbjorn@asbjorn.biz>
---
 web/js/labitrack.d/.gitignore               |  1 +
 web/js/labitrack.d/31-handlebars-helpers.js |  3 ++
 web/js/labitrack.d/39-pagination.coffee     | 50 ++++++++++--------
 web/js/labitrack.d/43-browse.coffee         |  2 +-
 web/js/labitrack.d/55-search.coffee         | 81 +++++++++++++++++++++++++++++
 5 files changed, 115 insertions(+), 22 deletions(-)
 create mode 100644 web/js/labitrack.d/55-search.coffee

(limited to 'web/js')

diff --git a/web/js/labitrack.d/.gitignore b/web/js/labitrack.d/.gitignore
index fc7aaf7..515beb9 100644
--- a/web/js/labitrack.d/.gitignore
+++ b/web/js/labitrack.d/.gitignore
@@ -1,2 +1,3 @@
 39-pagination.js
 43-browse.js
+55-search.js
diff --git a/web/js/labitrack.d/31-handlebars-helpers.js b/web/js/labitrack.d/31-handlebars-helpers.js
index 6fa462a..6bd222f 100644
--- a/web/js/labitrack.d/31-handlebars-helpers.js
+++ b/web/js/labitrack.d/31-handlebars-helpers.js
@@ -5,6 +5,9 @@
 	Handlebars.registerHelper('pagination', function(){
 		return new Handlebars.SafeString(λ.template('pagination', this));
 	});
+	Handlebars.registerHelper('objecttable', function(){
+		return new Handlebars.SafeString(λ.template('objecttable', this));
+	});
 	Handlebars.registerHelper('dump_ctx', function(){
 		console.log({'ctx': this});
 	});
diff --git a/web/js/labitrack.d/39-pagination.coffee b/web/js/labitrack.d/39-pagination.coffee
index ca50b9b..98311f5 100644
--- a/web/js/labitrack.d/39-pagination.coffee
+++ b/web/js/labitrack.d/39-pagination.coffee
@@ -1,43 +1,51 @@
 λ.pagination = (->
-	link = (pg) -> '/browse' + (if pg > 1 then '/page/' + pg else '')
 
 	class pgs
-		constructor: -> @pages = []
+		constructor: (@prefix, @pg, @cnt) -> @pages = []
+		link: (pg) -> @prefix + (if pg > 1 then '/page/' + pg else '')
 		dots: -> @pages.push { id: 'dots', label: '…', classes: 'disabled' }
 		page: (pgno) -> @pages.push {
 			id: pgno
-			link: link pgno
+			link: @link pgno
 			label: pgno
 		}
 		first: -> @pages.push {
 			id: 'first'
-			link: link 1
+			link: @link 1
 			label: '|&larr;'
 			classes: 'prev'
 		}
-		prev: (pg) -> @pages.push {
-			id: 'prev'
-			link: link pg
-			label: '&larr;'
-		}
-		next: (pg) -> @pages.push {
-			id: 'next'
-			link: link pg
-			label: '&rarr;'
-		}
+		prev: (pg) ->
+			i =
+				id: 'prev'
+				link: @link pg
+				label: '&larr;'
+			if @pg is 1
+				i.link = undefined
+				i.classes = 'disabled'
+			@pages.push i
+		next: (pg) ->
+			i =
+				id: 'next'
+				link: @link pg
+				label: '&rarr;'
+			if @pg is @cnt
+				i.link = undefined
+				i.classes = 'disabled'
+			@pages.push i
 		last: (pg) -> @pages.push {
 			id: 'last'
-			link: link pg
+			link: @link pg
 			label: '&rarr;|'
 			classes: 'next'
 		}
 
-	return (page, cnt) ->
-		p = new pgs
-		first = page != 1
-		prev = page > 2
-		next = page + 1 < cnt
-		last = page < cnt
+	return (prefix, page, cnt) ->
+		p = new pgs prefix, page, cnt
+		first = page != 1 and cnt >= 10
+		prev = page > 2 or cnt < 10
+		next = page + 1 < cnt or cnt < 10
+		last = page < cnt and cnt >= 10
 		slots = 11
 
 		left = right = Math.floor(slots / 2)
diff --git a/web/js/labitrack.d/43-browse.coffee b/web/js/labitrack.d/43-browse.coffee
index b0d3358..f630d90 100644
--- a/web/js/labitrack.d/43-browse.coffee
+++ b/web/js/labitrack.d/43-browse.coffee
@@ -35,7 +35,7 @@ browse = Backbone.View.extend {
 		pages = []
 		if stats != undefined
 			pgcnt = Math.ceil(stats.count / 10)
-			pages = λ.pagination page, pgcnt
+			pages = λ.pagination '/browse', page, pgcnt
 
 		data = {
 			rows: @collection.toJSON(),
diff --git a/web/js/labitrack.d/55-search.coffee b/web/js/labitrack.d/55-search.coffee
new file mode 100644
index 0000000..38569af
--- /dev/null
+++ b/web/js/labitrack.d/55-search.coffee
@@ -0,0 +1,81 @@
+hdl_add = ->
+	console.log 'add'
+
+hdl_remove = ->
+	console.log 'remove'
+
+hdl_reset = ->
+	console.log 'reset'
+	@render()
+
+$ ->
+	form = $('.navbar-search')
+	q = form.find('input')[0]
+	form.bind 'submit', (e) ->
+		e.preventDefault()
+		url = '/search/' + encodeURIComponent q.value
+		Backbone.history.navigate(url, true)
+		return false
+
+collection = Backbone.Collection.extend {
+	model: λ.o,
+	url: -> '/search.json?offset='+(10*((@nextpage++)-1))+'&q='+encodeURIComponent(@q)
+	comparator: (object) -> object.id
+	fetchpage: (@q, page) ->
+		if page
+			@nextpage = page
+		@fetch()
+	parse: (data) ->
+		@meta = {count: data.count, query: data.query}
+		console.log data
+		return data.objects
+}
+
+search = Backbone.View.extend {
+	initialize: () ->
+		messages = @collection
+		messages.bind "reset", hdl_reset, @
+		messages.bind "add", hdl_add, @
+		messages.bind "remove", hdl_remove, @
+	render: (q, page) ->
+		if page then @page = page
+		if q then @q = q
+		page = @page
+		q = @q
+		meta = @collection.meta
+		pages = []
+		if meta != undefined
+			pgcnt = Math.ceil(meta.count / 10)
+			pages = λ.pagination '/search/'+q,  page, pgcnt
+
+		data = {
+			rows: @collection.toJSON(),
+			pages
+			meta
+		}
+		console.log data, meta
+		$(@el).html λ.template 'searchtable', data
+}
+
+collection = new collection()
+
+view = λ.routableview.extend {
+	initialize: () ->
+		λ.routableview.prototype.initialize.call(@)
+		@search = new search({collection: collection})
+	render: (q, page) ->
+		console.log 'render', q, page
+		q = decodeURIComponent q
+		page || (page = 1)
+		page = parseInt page, 10
+		console.log 'page', page
+		λ.setcontent 'search', {page}
+		@search.el = $(@el).find('#objecttable_ph')[0]
+		console.log @search.render
+		@search.render q, page
+		@search.collection.fetchpage q, page
+}
+
+view.route 'search'
+view.route 'search/:q'
+view.route 'search/:q/page/:page'
-- 
cgit v1.2.1