Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pagination #59

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions common.blocks/pager/pager.bemhtml.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
block('pager').elem('item').match(function() { return this.ctx.url !== true; }).replace()(function() {
block('pager')
.elem('item')
.match(node => node.ctx.url !== true)
.replace()(node => {
const type2caption = {
first: '<<',
prev: '<',
Expand All @@ -7,14 +10,14 @@ block('pager').elem('item').match(function() { return this.ctx.url !== true; }).
last: '>>'
};

const elemMods = this.elemMods;
const elemMods = node.elemMods;
const type = elemMods.type;

return {
block: 'button',
mods: { theme: 'islands', size: 'm', type: 'link', disabled: type === 'current' },
mix: { block: this.block, elem: this.elem, elemMods },
url: this.ctx.url,
text: type2caption[type]
mix: { block: node.block, elem: node.elem, elemMods },
url: node.ctx.url,
text: type2caption[type] || node.ctx.number
};
});
35 changes: 27 additions & 8 deletions common.blocks/pager/pager.bemtree.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
block('pager').content()(function() {
var pagination = Object.assign({ current: true }, this.data.pagination);
block('pager').content()(node => {
const { data } = node;
const pagination = data.pagination;
const result = [];

return ['first', 'prev', 'current', 'next', 'last'].reduce((acc, type) => {
pagination[type] && acc.push({
const createItem = (type, index) => {
return {
elem: 'item',
mix: { block, elem: 'item' },
elemMods: { type: type },
url: pagination[type]
});
url: pagination[type] || `${data.exceptPaginationUrl}&page=${index}`,
number: index
};
};

return acc;
}, []);
for (let i = 1; i <= data.pageCount; i++) {
result.push(createItem('number', i));
}

// gather arrows and numbers correct way
return [
['first', 'prev'].reduce((acc, type, index) => {
pagination[type] && acc.push(createItem(type, index));
return acc;
}, []),
result,
['next', 'last'].reduce((acc, type, index) => {
pagination[type] && acc.push(createItem(type, index));
return acc;
}, [])
];
});
11 changes: 8 additions & 3 deletions common.blocks/sidebar/__sorting/sidebar__sorting.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ modules.define(
direction = 'asc';
}

Location.change({ params: this._getParams(sortType, direction) });
var params = this._getParams(sortType, direction);
delete params.page;

Location.change({ params: params });
},

_getParams: function(sort, direction) {
return Object.assign(
Location.getUri().queryParams,
{ sort, direction },
{ pagination: undefined }
{
sort: sort,
direction: direction
}
);
}

Expand Down
80 changes: 53 additions & 27 deletions server/controllers/gh.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ function getIndex(req, res) {
const issuesData = responses[0];
const labelsData = responses[1];

const paginationData = getPaginationData(issuesData.pagination);

render(req, res, {
view: 'page-index',
issues: issuesData.issues,
pagination: issuesData.pagination,
labels: labelsData
labels: labelsData,
pageCount: paginationData.pageCount,
exceptPaginationUrl: paginationData.exceptPaginationUrl
});
}).catch(err => onError(req, res, err));
}
Expand All @@ -69,16 +73,41 @@ function getIssues(req, res) {
logger.log('getIssues');

makeIssueRequest(issuesRequestUrl, { query: req.query, token: getToken(req.user) })
.then(issuesData => render(req, res, {
view: 'page-index',
issues: issuesData.issues,
pagination: issuesData.pagination
}, {
block: 'issues'
}))
.then(issuesData => {
const paginationData = getPaginationData(issuesData.pagination);

render(req, res,
{
view: 'page-index',
issues: issuesData.issues,
pagination: issuesData.pagination,
pageCount: paginationData.pageCount,
exceptPaginationUrl: paginationData.exceptPaginationUrl
}, {
block: 'issues'
}
);
})
.catch(err => onError(req, res, err));
}

function getPaginationData(issuesPag) {
const querystring = require('querystring'),
pageCount = issuesPag.last ?
querystring.parse(issuesPag.last).page :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation here and everywhere

parseInt(querystring.parse(issuesPag.prev).page) + 1;

let exceptPaginationUrl = querystring.parse(issuesPag.last && issuesPag.last.substr(1) || issuesPag.prev.substr(1));
delete exceptPaginationUrl.page;

exceptPaginationUrl = '?' + querystring.stringify(exceptPaginationUrl);

return {
exceptPaginationUrl: exceptPaginationUrl,
pageCount: pageCount
};
}

function getComplexIssue(req, res) {
logger.log('getComplexIssue', req.params.id);

Expand Down Expand Up @@ -127,8 +156,8 @@ function getComments(req, res) {
comments,
issueId: req.params.id
}, {
block: 'comments'
}))
block: 'comments'
}))
.catch(err => onError(req, res, err));
}

Expand All @@ -152,24 +181,21 @@ function _getData(req, res, dataType, urlPart) {
makeCommentRequest(requestPath, { token })
)
.then(response => {
const data = dataType === 'issue' ? response.issues[0] : response;

type === 'form' ?
render(req, res, {
view: 'page-post'
},
Object.assign({
block: 'send-form',
mix: { block: dataType, elem: 'send-form' },
formType: dataType,
reqType: 'edit',
js: {
const data = dataType === 'issue' ? response.issues[0] : response;

type === 'form' ?
render(req, res, {
view: 'page-post'
}, {
block: 'send-form',
mix: { block: dataType, elem: 'send-form' },
formType: dataType,
reqType: 'edit'
}
}, dataType === 'issue' ? { issue: data } : { comment: data })) :
res.json(data);
}).catch(err => onError(req, res, err));
reqType: 'edit',
issue: data,
comment: data
}) :
res.json(data);
}).catch(err => onError(req, res, err));
}

function addComment(req, res) {
Expand Down