summaryrefslogtreecommitdiffstats
path: root/web/js/labitrack.d/39-pagination.coffee
blob: 98311f593157b1c5f34b8c87bd7a6e4ce41b8d97 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
λ.pagination = (->

	class pgs
		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
			label: pgno
		}
		first: -> @pages.push {
			id: 'first'
			link: @link 1
			label: '|←'
			classes: 'prev'
		}
		prev: (pg) ->
			i =
				id: 'prev'
				link: @link pg
				label: '←'
			if @pg is 1
				i.link = undefined
				i.classes = 'disabled'
			@pages.push i
		next: (pg) ->
			i =
				id: 'next'
				link: @link pg
				label: '→'
			if @pg is @cnt
				i.link = undefined
				i.classes = 'disabled'
			@pages.push i
		last: (pg) -> @pages.push {
			id: 'last'
			link: @link pg
			label: '→|'
			classes: 'next'
		}

	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)

		left -= first + prev
		right -= next + last

		if page - left < 1
			right += left - page + 1
			left = page - 1

		if page + right > cnt
			oldright = right
			right = cnt - page
			left += oldright - right

		if page - left < 1
			left = page - 1

		if page - left isnt 1
			ldots = true
			left--

		if page + right isnt cnt
			rdots = true
			right--

		p.first() if first
		p.prev page-1 if prev
		p.dots() if ldots
		while left > 0
			p.page page - left--
		p.pages[(p.page page)-1].classes += ' active'
		while right-- > 0
			p.page page + ++left
		p.dots() if rdots
		p.next page+1 if next
		p.last cnt if last
		return p.pages
)()

###cnt = parseInt process.argv[2], 10

str_repeat = (str, fac) -> new Array(fac + 1).join(str)

for i in [1..cnt]
	paging = pagination i, cnt
	pgs = []
	k = 0
	for page, j in paging
		if page.id is 'dots'
			pgs.push '…'
		else if typeof page.id is 'string'
			pgs.push page.id[0]
		else
			str = page.id+''
			pgs.push str[str.length-1]
		if page.id is i
			k = j
	console.log pgs.join ' '
	console.log str_repeat('  ', k)+'^'
	###