spine_paginator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'spine_paginator/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "spine_paginator"
8
+ gem.version = SpinePaginator::VERSION
9
+ gem.authors = ["vkill"]
10
+ gem.email = ["vkill.net@gmail.com"]
11
+ gem.description = %q{Paginator for Spine}
12
+ gem.summary = %q{Paginator for Spine}
13
+ gem.homepage = "https://github.com/vkill/spine_paginator"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,199 @@
1
+ Spine = @Spine or require('spine')
2
+ Model = Spine.Model
3
+
4
+ class Paginator
5
+ @DEFAULT_PER_PAGE = 25
6
+ @MAX_PER_PAGE = null
7
+ @WINDOW = 4
8
+ @OUTER_WINDOW = 0
9
+ @LEFT = 0
10
+ @RIGHT = 0
11
+
12
+ constructor: (records, @_page, options={}) ->
13
+ @_page = parseInt(@_page)
14
+ @_page = 1 if isNaN(@_page) or @_page <= 0
15
+
16
+ @_originalPage = @_page
17
+
18
+ @perPage = options.perPage || @constructor.DEFAULT_PER_PAGE
19
+ @perPage = parseInt(@perPage)
20
+ @perPage = @constructor.DEFAULT_PER_PAGE if isNaN(@perPage) or @perPage <= 0
21
+
22
+ @maxPerPage = options.maxPerPage || @constructor.MAX_PER_PAGE
23
+
24
+ @window = options.window || options.inner_window || @constructor.WINDOW
25
+ outer_window = options.outer_window || @constructor.OUTER_WINDOW
26
+ @left = options.left || @constructor.LEFT
27
+ @left = outer_window if @left == 0
28
+ @right = options.right || @constructor.RIGHT
29
+ @right = outer_window if @right == 0
30
+
31
+ @originalRecords = @cloneArray(records)
32
+ @totalCount = @originalRecords.length
33
+
34
+ @skipbuildButtonsAndLocals = options.skipbuildButtonsAndLocals
35
+
36
+ @records = []
37
+ @buttons = []
38
+ @locals = {}
39
+
40
+ @per()
41
+
42
+
43
+ per: (num)->
44
+ n = parseInt(num)
45
+ if !isNaN(n) and n > 0
46
+ @perPage = n
47
+ # when perPage changed, reset @_page
48
+ @_page = @_originalPage
49
+
50
+ fromN = @offsetValue()
51
+ toN = fromN + @limitValue()
52
+
53
+ @records = @originalRecords.slice(fromN, toN)
54
+
55
+ @buildButtonsAndLocals() unless @skipbuildButtonsAndLocals?
56
+ @
57
+
58
+ totalPages: ->
59
+ Math.ceil(@totalCount / @limitValue())
60
+
61
+ currentPage: ->
62
+ return @firstPage() unless @limitValue()?
63
+ (@offsetValue() / @limitValue()) + 1
64
+
65
+ firstPage: -> 1
66
+
67
+ isFirstPage: -> @currentPage() == @firstPage()
68
+
69
+ lastPage: -> @totalPages()
70
+
71
+ isLastPage: -> @currentPage() >= @lastPage()
72
+
73
+
74
+ pages: ->
75
+ currentPage = @currentPage()
76
+ firstPage = @firstPage()
77
+ lastPage = @lastPage()
78
+
79
+ _pages = []
80
+ last = null
81
+ for page in [firstPage..lastPage]
82
+ result = @buildPage(page, last, currentPage, firstPage, lastPage)
83
+ if result.isLeftOuter or result.isRightOuter or result.isInsideWindow
84
+ last = null
85
+ else
86
+ last = 'gap'
87
+ _pages.push result
88
+ _pages
89
+
90
+ curPage: ->
91
+ currentPage = @currentPage()
92
+ firstPage = @firstPage()
93
+ lastPage = @lastPage()
94
+ @buildPage(currentPage, null, currentPage, firstPage, lastPage)
95
+
96
+
97
+ # private
98
+
99
+ limitValue: ->
100
+ @perPage = @totalCount if @perPage > @totalCount
101
+ @perPage = @maxPerPage if @maxPerPage? and @perPage > @maxPerPage
102
+ @perPage
103
+
104
+ offsetValue: ->
105
+ totalPages = @totalPages()
106
+ @_page = totalPages if @_page > totalPages
107
+ (@_page - 1) * @limitValue()
108
+
109
+ buildPage: (page, last, currentPage, firstPage, lastPage) ->
110
+ number: page
111
+ isCurrent: page == currentPage
112
+ isFirst: page == firstPage
113
+ isLast: page == lastPage
114
+ isPrev: page == (currentPage - 1)
115
+ isNext: page == (currentPage + 1)
116
+ isLeftOuter: page <= @left
117
+ isRightOuter: (lastPage - page) < @right
118
+ isInsideWindow: Math.abs(currentPage - page) <= @window
119
+ isWasTruncated: last == 'gap'
120
+
121
+ buildButtonsAndLocals: ->
122
+ _buttons = []
123
+ _locals = {}
124
+
125
+ curPage = @curPage()
126
+ pages = @pages()
127
+
128
+ unless curPage.isFirst
129
+ _buttons.push('first')
130
+ _locals.hasFirst = true
131
+ else
132
+ _locals.hasFirst = false
133
+
134
+ unless curPage.isFirst
135
+ _buttons.push('prev')
136
+ _locals.hasPrev = true
137
+ else
138
+ _locals.hasPrev = false
139
+
140
+ _locals.pages = []
141
+ for page in pages
142
+ if page.isLeftOuter or page.isRightOuter or page.isInsideWindow
143
+ if page.isCurrent
144
+ _buttons.push('current')
145
+ _locals.pages.push({number: page.number, current: true})
146
+ else
147
+ _buttons.push(page.number)
148
+ _locals.pages.push({number: page.number, current: false})
149
+
150
+ else if !page.isWasTruncated
151
+ _buttons.push('gap')
152
+ _locals.pages.push({number: page.number, gap: true})
153
+
154
+ unless curPage.isLast
155
+ _buttons.push('next')
156
+ _locals.hasNext = true
157
+ else
158
+ _locals.hasNext = false
159
+
160
+ unless curPage.isLast
161
+ _buttons.push('last')
162
+ _locals.hasLast = true
163
+ else
164
+ _locals.hasLast = false
165
+
166
+ _locals.first = @firstPage()
167
+ _locals.current = @currentPage()
168
+ _locals.last = @lastPage()
169
+
170
+ _locals.numStart = if @records.length == 0 then 0 else @offsetValue() + 1
171
+ _locals.numEnd = @offsetValue() + @records.length
172
+ _locals.numTotal = @totalCount
173
+
174
+ @buttons = _buttons
175
+ @locals = _locals
176
+
177
+ cloneArray: (array) ->
178
+ (value.clone() for value in array)
179
+
180
+ Extend =
181
+ _perPaginateRecords: -> @records
182
+ page: (n, options={})-> new Paginator(@_perPaginateRecords(), n, options)
183
+
184
+ Model.Paginator =
185
+ extended: ->
186
+ @extend Extend
187
+
188
+ Spine.Paginator = Paginator
189
+
190
+ # Usage
191
+ #
192
+ # class App.MyModel extends Spine.Model
193
+ # @extend Spine.Model.Paginator
194
+ #
195
+ # App.MyModel.fetch()
196
+ # pagination = App.MyModel.page(6).per(10)
197
+ # pagination.locals
198
+ #
199
+
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spine_paginator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - vkill
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-09 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Paginator for Spine
15
+ email:
16
+ - vkill.net@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - Gruntfile.coffee
24
+ - LICENSE-MIT
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - component.json
29
+ - dist/.gitkeep
30
+ - dist/spine.paginator.js
31
+ - dist/spine.paginator.min.js
32
+ - lib/spine_paginator.rb
33
+ - lib/spine_paginator/railtie.rb
34
+ - lib/spine_paginator/version.rb
35
+ - package.json
36
+ - spec/lib/.gitkeep
37
+ - spec/lib/spine.js
38
+ - spec/spine.paginator/.gitkeep
39
+ - spine_paginator.gemspec
40
+ - src/spine.paginator.coffee
41
+ homepage: https://github.com/vkill/spine_paginator
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.24
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Paginator for Spine
65
+ test_files:
66
+ - spec/lib/.gitkeep
67
+ - spec/lib/spine.js
68
+ - spec/spine.paginator/.gitkeep