summaryrefslogtreecommitdiffstats
path: root/web/js/labitrack.d/55-search.coffee
blob: b0814f2280ffe937b86c4aaf138045f4408fbab3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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

			if meta.count is 1
				url = '/view/' + @collection.at(0).id
				return Backbone.history.navigate(url, true)

		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'