Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
brainfood
/
solr-frontend
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
ba0249c5
authored
2016-02-05 12:06:29 -0600
by
Adam Heath
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
CLose to full test coverage.
1 parent
9542d73d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
230 additions
and
20 deletions
src/scripts/solr/model/Pagination.js
test/specs/Pagination.js
src/scripts/solr/model/Pagination.js
View file @
ba0249c
...
...
@@ -2,26 +2,33 @@ define(function(require) {
'use strict'
;
var
Backbone
=
require
(
'backbone'
);
function
preventDefault
(
e
)
{
if
(
e
)
{
e
.
preventDefault
();
}
}
function
stepFalse
(
e
)
{
e
.
preventDefault
(
);
preventDefault
(
e
);
return
false
;
}
var
Pagination
=
Backbone
.
Model
.
extend
({
defaults
:
{
currentPage
:
1
,
hasNext
:
false
,
hasNextJumpPage
:
false
,
hasPrevious
:
false
,
hasPreviousJumpPage
:
false
,
nextJumpPage
:
undefined
,
nextPage
:
undefined
,
pages
:
[],
pageJump
:
5
,
pageSize
:
10
,
previousJumpPage
:
undefined
,
previousPage
:
undefined
,
totalCount
:
0
,
totalPages
:
0
,
defaults
:
function
defaults
()
{
return
{
currentPage
:
1
,
hasNext
:
false
,
hasNextJumpPage
:
false
,
hasPrevious
:
false
,
hasPreviousJumpPage
:
false
,
nextJumpPage
:
undefined
,
nextPage
:
undefined
,
pages
:
[],
pageJump
:
5
,
pageSize
:
10
,
previousJumpPage
:
undefined
,
previousPage
:
undefined
,
totalCount
:
0
,
totalPages
:
0
,
};
},
initialize
:
function
(
data
,
options
)
{
var
buildPages
=
function
buildPages
()
{
...
...
@@ -55,8 +62,8 @@ define(function(require) {
startAt
=
1
;
}
if
(
endAt
-
startAt
<
9
)
{
/* global console:false */
console
.
log
(
'foo'
);
/
//
* global console:false */
//
console.log('foo');
}
for
(
var
i
=
startAt
;
i
<
endAt
;
i
++
)
{
...
...
@@ -90,6 +97,21 @@ define(function(require) {
totalPages
:
totalPages
,
});
};
this
.
on
(
'change:pageSize change:currentPage change:totalCount'
,
function
()
{
var
currentPage
=
parseInt
(
this
.
get
(
'currentPage'
));
var
pageSize
=
parseInt
(
this
.
get
(
'pageSize'
));
var
totalCount
=
parseInt
(
this
.
get
(
'totalCount'
));
if
(
!
currentPage
||
!
pageSize
||
!
totalCount
)
{
return
;
}
var
pages
=
[];
var
totalPages
=
Math
.
floor
((
totalCount
+
pageSize
-
1
)
/
pageSize
);
if
(
currentPage
<
0
)
{
this
.
set
(
'currentPage'
,
1
);
}
else
if
(
currentPage
>
totalPages
)
{
this
.
set
(
'currentPage'
,
totalPages
);
}
},
this
);
this
.
on
(
'change:pageSize change:currentPage change:totalCount'
,
buildPages
,
this
);
var
installActions
=
_
.
bind
(
function
installActions
(
eventName
,
nextName
,
hasNextName
,
previousName
,
hasPreviousName
,
pageCount
)
{
this
.
on
(
eventName
,
function
()
{
...
...
@@ -98,7 +120,7 @@ define(function(require) {
var
next
,
previous
;
if
(
hasNext
)
{
next
=
_
.
bind
(
function
(
e
)
{
e
.
preventDefault
(
);
preventDefault
(
e
);
this
.
set
(
'currentPage'
,
this
.
get
(
'currentPage'
)
+
pageCount
);
return
false
;
},
this
);
...
...
@@ -107,7 +129,7 @@ define(function(require) {
}
if
(
hasPrevious
)
{
previous
=
_
.
bind
(
function
(
e
)
{
e
.
preventDefault
(
);
preventDefault
(
e
);
this
.
set
(
'currentPage'
,
this
.
get
(
'currentPage'
)
-
pageCount
);
return
false
;
},
this
);
...
...
test/specs/Pagination.js
0 → 100644
View file @
ba0249c
define
(
function
(
require
)
{
'use strict'
;
var
_
=
require
(
'underscore'
);
var
Backbone
=
require
(
'backbone'
);
var
Pagination
=
require
(
'solr/model/Pagination'
);
describe
(
'Pagination'
,
function
()
{
it
(
'loads'
,
function
()
{
expect
(
Pagination
).
toBeDefined
();
});
describe
(
':pages'
,
function
()
{
it
(
'default value is not shared globally'
,
function
()
{
var
p1
=
new
Pagination
(),
p2
=
new
Pagination
();
expect
(
p1
.
get
(
'pages'
)).
not
.
toBe
(
p2
.
get
(
'pages'
));
});
});
describe
(
'singleton'
,
function
()
{
var
p
;
function
checkHas
(
hasNext
,
hasNextJump
,
hasNextJumpPage
,
hasPrevious
,
hasPreviousJump
,
hasPreviousJumpPage
)
{
var
wanted
=
{
hasNext
:
hasNext
,
hasNextJump
:
hasNextJump
,
hasNextJumpPage
:
hasNextJumpPage
,
hasPrevious
:
hasPrevious
,
hasPreviousJump
:
hasPreviousJump
,
hasPreviousJumpPage
:
hasPreviousJumpPage
,
};
it
(
'has'
,
function
()
{
var
got
=
{
hasNext
:
p
.
get
(
'hasNext'
),
hasNextJump
:
p
.
get
(
'hasNextJump'
),
hasNextJumpPage
:
p
.
get
(
'hasNextJumpPage'
),
hasPrevious
:
p
.
get
(
'hasPrevious'
),
hasPreviousJump
:
p
.
get
(
'hasPreviousJump'
),
hasPreviousJumpPage
:
p
.
get
(
'hasPreviousJumpPage'
),
};
expect
(
got
).
toEqual
(
wanted
);
});
}
function
checkPages
(
wantedLength
,
offset
,
currentPage
)
{
var
pages
;
beforeEach
(
function
()
{
pages
=
p
.
get
(
'pages'
);
});
//console.log(JSON.stringify(pages));
var
wanted
=
{
length
:
wantedLength
,
pages
:
[]
};
for
(
var
i
=
0
;
i
<
wantedLength
;
i
++
)
{
wanted
.
pages
[
i
]
=
{
current
:
i
+
1
===
currentPage
,
number
:
i
+
1
+
offset
};
}
it
(
'pages'
,
function
()
{
pages
=
p
.
get
(
'pages'
);
var
got
=
{
length
:
pages
.
length
,
pages
:
[]
};
for
(
var
i
=
0
;
i
<
pages
.
length
;
i
++
)
{
var
page
=
pages
[
i
];
got
.
pages
[
i
]
=
{
current
:
page
.
current
,
number
:
page
.
number
};
}
expect
(
got
).
toEqual
(
wanted
);
});
}
beforeEach
(
function
()
{
p
=
new
Pagination
();
});
describe
(
'default settings'
,
function
()
{
checkHas
(
false
,
undefined
,
false
,
false
,
undefined
,
false
);
checkPages
(
0
,
0
,
1
);
});
describe
(
'35 items'
,
function
()
{
beforeEach
(
function
()
{
p
.
set
({
totalCount
:
35
});
});
describe
(
'initial settings'
,
function
()
{
checkHas
(
true
,
false
,
false
,
false
,
false
,
false
);
checkPages
(
4
,
0
,
1
);
});
describe
(
'clamping'
,
function
()
{
describe
(
'set currentPage=5'
,
function
()
{
beforeEach
(
function
()
{
p
.
set
({
currentPage
:
5
});
});
checkHas
(
false
,
false
,
false
,
true
,
false
,
false
);
checkPages
(
4
,
0
,
4
);
});
describe
(
'set currentPage=-1'
,
function
()
{
beforeEach
(
function
()
{
p
.
set
({
currentPage
:
-
1
});
});
checkHas
(
true
,
false
,
false
,
false
,
false
,
false
);
checkPages
(
4
,
0
,
1
);
});
});
describe
(
'next[1]'
,
function
()
{
beforeEach
(
function
()
{
p
.
next
();
});
checkPages
(
4
,
0
,
2
);
describe
(
'previous'
,
function
()
{
beforeEach
(
function
()
{
p
.
previous
();
});
checkHas
(
true
,
false
,
false
,
false
,
false
,
false
);
checkPages
(
4
,
0
,
1
);
});
describe
(
'next[2]'
,
function
()
{
beforeEach
(
function
()
{
p
.
next
();
p
.
next
();
});
checkHas
(
false
,
false
,
false
,
true
,
false
,
false
);
checkPages
(
4
,
0
,
4
);
describe
(
'next[1] - clamp'
,
function
()
{
beforeEach
(
function
()
{
p
.
next
();
});
checkHas
(
false
,
false
,
false
,
true
,
false
,
false
);
checkPages
(
4
,
0
,
4
);
});
describe
(
'page[0].jump'
,
function
()
{
beforeEach
(
function
()
{
p
.
get
(
'pages'
)[
0
].
jump
();
});
checkHas
(
true
,
false
,
false
,
false
,
false
,
false
);
checkPages
(
4
,
0
,
1
);
});
describe
(
'page[3].jump'
,
function
()
{
beforeEach
(
function
()
{
p
.
get
(
'pages'
)[
3
].
jump
();
});
checkHas
(
false
,
false
,
false
,
true
,
false
,
false
);
checkPages
(
4
,
0
,
4
);
});
describe
(
'decrease to 25 items'
,
function
()
{
beforeEach
(
function
()
{
p
.
set
({
totalCount
:
25
});
});
checkHas
(
false
,
false
,
false
,
true
,
false
,
false
);
checkPages
(
3
,
0
,
3
);
describe
(
'increase to 150 items'
,
function
()
{
beforeEach
(
function
()
{
p
.
set
({
totalCount
:
150
});
});
checkHas
(
true
,
true
,
false
,
true
,
false
,
false
);
checkPages
(
9
,
0
,
3
);
});
});
});
});
describe
(
'previous[1]'
,
function
()
{
beforeEach
(
function
()
{
p
.
previous
();
});
checkHas
(
true
,
false
,
false
,
false
,
false
,
false
);
checkPages
(
4
,
0
,
1
);
});
});
describe
(
'150 items'
,
function
()
{
beforeEach
(
function
()
{
p
.
set
({
totalCount
:
150
});
});
checkHas
(
true
,
true
,
false
,
false
,
false
,
false
);
checkPages
(
9
,
0
,
1
);
describe
(
'page[7].jump'
,
function
()
{
beforeEach
(
function
()
{
p
.
get
(
'pages'
)[
7
].
jump
();
});
checkHas
(
true
,
true
,
false
,
true
,
true
,
false
);
checkPages
(
9
,
3
,
5
);
describe
(
'previousJump'
,
function
()
{
beforeEach
(
function
()
{
p
.
previousJump
();
});
checkHas
(
true
,
true
,
false
,
true
,
false
,
false
);
checkPages
(
9
,
0
,
3
);
});
});
});
describe
(
'no page jump'
,
function
()
{
beforeEach
(
function
()
{
p
.
set
({
pageJump
:
false
});
p
.
set
({
totalCount
:
150
});
});
checkHas
(
true
,
false
,
false
,
false
,
false
,
false
);
checkPages
(
9
,
0
,
1
);
});
});
});
});
Please
register
or
sign in
to post a comment