From fd0d6f9eb2252f4a241438459f019ddb20c7b9f0 Mon Sep 17 00:00:00 2001 From: Liza Golubenko Date: Tue, 25 Jul 2017 04:46:13 +0300 Subject: [PATCH 1/6] fix pagination --- common.blocks/pager/pager.bemhtml.js | 7 ++++-- common.blocks/pager/pager.bemtree.js | 33 +++++++++++++++++++++------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/common.blocks/pager/pager.bemhtml.js b/common.blocks/pager/pager.bemhtml.js index 506cf97..3332a38 100644 --- a/common.blocks/pager/pager.bemhtml.js +++ b/common.blocks/pager/pager.bemhtml.js @@ -1,4 +1,7 @@ -block('pager').elem('item').match(function() { return this.ctx.url !== true; }).replace()(function() { +block('pager') +.elem('item') +.match(function() { return this.ctx.url !== true; }) +.replace()(function() { const type2caption = { first: '<<', prev: '<', @@ -15,6 +18,6 @@ block('pager').elem('item').match(function() { return this.ctx.url !== true; }). 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] + text: type2caption[type] || this.ctx.number }; }); diff --git a/common.blocks/pager/pager.bemtree.js b/common.blocks/pager/pager.bemtree.js index 54d153e..a804704 100644 --- a/common.blocks/pager/pager.bemtree.js +++ b/common.blocks/pager/pager.bemtree.js @@ -1,13 +1,30 @@ -block('pager').content()(function() { - var pagination = Object.assign({ current: true }, this.data.pagination); +block('pager').content()(node => { + const { data } = node; + const pagination = Object.assign({ current: true }, data.pagination); + const pageCount = pagination.last ? pagination.last.match(/\&page=(\d+)/i)[1] : + parseInt(pagination.prev.match(/\&page=(\d+)/i)[1]) + 1; + const result = []; - return ['first', 'prev', 'current', 'next', 'last'].reduce((acc, type) => { - pagination[type] && acc.push({ + const createItem = (type, index) => { + return { elem: 'item', elemMods: { type: type }, - url: pagination[type] - }); + url: pagination[type] || `?page=${index}`, + number: index + }; + }; + + for (let i = 1; i <= pageCount; i++) { + result.push(createItem('number', i)); + } + + // собираем последовательность из стрелок начала, цифер и стрелок конца + return ['first', 'prev'].reduce((acc, type, index) => { + pagination[type] && acc.push(createItem(type, index)); + return acc; + }, []).concat(result, ['next', 'last'].reduce((acc, type, index) => { + pagination[type] && acc.push(createItem(type, index)); + return acc; + }, [])); - return acc; - }, []); }); From a7c9f08a52a170330b4fd6b5b14b1101f253bc14 Mon Sep 17 00:00:00 2001 From: Liza Golubenko Date: Mon, 31 Jul 2017 14:47:58 +0300 Subject: [PATCH 2/6] save previous url params when changing page event happends --- common.blocks/pager/pager.bemtree.js | 18 ++++++++++-------- .../sidebar/__sorting/sidebar__sorting.js | 2 +- server/controllers/gh.js | 17 ++++++++++++++++- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/common.blocks/pager/pager.bemtree.js b/common.blocks/pager/pager.bemtree.js index a804704..ea91def 100644 --- a/common.blocks/pager/pager.bemtree.js +++ b/common.blocks/pager/pager.bemtree.js @@ -1,30 +1,32 @@ block('pager').content()(node => { const { data } = node; const pagination = Object.assign({ current: true }, data.pagination); - const pageCount = pagination.last ? pagination.last.match(/\&page=(\d+)/i)[1] : - parseInt(pagination.prev.match(/\&page=(\d+)/i)[1]) + 1; const result = []; const createItem = (type, index) => { return { elem: 'item', + mix: { block, elem: 'item' }, elemMods: { type: type }, - url: pagination[type] || `?page=${index}`, + url: pagination[type] || `${data.exceptPagUrl}&page=${index}`, number: index }; }; - for (let i = 1; i <= pageCount; i++) { + for (let i = 1; i <= data.pageCount; i++) { result.push(createItem('number', i)); } // собираем последовательность из стрелок начала, цифер и стрелок конца - return ['first', 'prev'].reduce((acc, type, index) => { + return [ + ['first', 'prev'].reduce((acc, type, index) => { pagination[type] && acc.push(createItem(type, index)); return acc; - }, []).concat(result, ['next', 'last'].reduce((acc, type, index) => { + }, []), + result, + ['next', 'last'].reduce((acc, type, index) => { pagination[type] && acc.push(createItem(type, index)); return acc; - }, [])); - + }, []) + ]; }); diff --git a/common.blocks/sidebar/__sorting/sidebar__sorting.js b/common.blocks/sidebar/__sorting/sidebar__sorting.js index af03bc8..4bd2d8d 100644 --- a/common.blocks/sidebar/__sorting/sidebar__sorting.js +++ b/common.blocks/sidebar/__sorting/sidebar__sorting.js @@ -28,7 +28,7 @@ modules.define( return Object.assign( Location.getUri().queryParams, { sort, direction }, - { pagination: undefined } + { page: '1' } ); } diff --git a/server/controllers/gh.js b/server/controllers/gh.js index 568d4da..f49b828 100644 --- a/server/controllers/gh.js +++ b/server/controllers/gh.js @@ -56,11 +56,26 @@ function getIndex(req, res) { const issuesData = responses[0]; const labelsData = responses[1]; + const querystring = require('querystring'); + + const pageCount = issuesData.pagination.last ? + querystring.parse(issuesData.pagination.last).page : + parseInt(querystring.parse(issuesData.pagination.prev).page) + 1; + + var exceptPagUrl = querystring.parse(issuesData.pagination.last || issuesData.pagination.prev); + delete exceptPagUrl.page; + + exceptPagUrl = Object.keys(exceptPagUrl).reduce((resStr, key) => { + return (resStr += '&' + key + '=' + exceptPagUrl[key]); + }, '').substr(1); + render(req, res, { view: 'page-index', issues: issuesData.issues, pagination: issuesData.pagination, - labels: labelsData + labels: labelsData, + pageCount, + exceptPagUrl }); }).catch(err => onError(req, res, err)); } From fa3df3be96d07ad4033f24303210b1fb1e20a603 Mon Sep 17 00:00:00 2001 From: Liza Golubenko Date: Mon, 31 Jul 2017 19:22:49 +0300 Subject: [PATCH 3/6] fix page params --- common.blocks/sidebar/__sorting/sidebar__sorting.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/common.blocks/sidebar/__sorting/sidebar__sorting.js b/common.blocks/sidebar/__sorting/sidebar__sorting.js index 4bd2d8d..fe478c2 100644 --- a/common.blocks/sidebar/__sorting/sidebar__sorting.js +++ b/common.blocks/sidebar/__sorting/sidebar__sorting.js @@ -21,14 +21,17 @@ modules.define( direction = 'asc'; } - Location.change({ params: this._getParams(sortType, direction) }); + var params = this._getParams(sortType, direction); + delete params.page; + + Location.change({ params: params }); + window.location.reload(); }, _getParams: function(sort, direction) { return Object.assign( Location.getUri().queryParams, - { sort, direction }, - { page: '1' } + { sort, direction } ); } From 1916d22c1eaa121723f2b165b63d3635b8641e25 Mon Sep 17 00:00:00 2001 From: Liza Golubenko Date: Fri, 4 Aug 2017 12:29:56 +0300 Subject: [PATCH 4/6] add arrow fn and fix ajax --- common.blocks/pager/pager.bemhtml.js | 12 ++++++------ common.blocks/sidebar/__sorting/sidebar__sorting.js | 1 - 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/common.blocks/pager/pager.bemhtml.js b/common.blocks/pager/pager.bemhtml.js index 3332a38..c2974de 100644 --- a/common.blocks/pager/pager.bemhtml.js +++ b/common.blocks/pager/pager.bemhtml.js @@ -1,7 +1,7 @@ block('pager') .elem('item') -.match(function() { return this.ctx.url !== true; }) -.replace()(function() { +.match(node => { return node.ctx.url !== true; }) +.replace()(node => { const type2caption = { first: '<<', prev: '<', @@ -10,14 +10,14 @@ block('pager') 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] || this.ctx.number + mix: { block: node.block, elem: node.elem, elemMods }, + url: node.ctx.url, + text: type2caption[type] || node.ctx.number }; }); diff --git a/common.blocks/sidebar/__sorting/sidebar__sorting.js b/common.blocks/sidebar/__sorting/sidebar__sorting.js index fe478c2..dfc6cfd 100644 --- a/common.blocks/sidebar/__sorting/sidebar__sorting.js +++ b/common.blocks/sidebar/__sorting/sidebar__sorting.js @@ -25,7 +25,6 @@ modules.define( delete params.page; Location.change({ params: params }); - window.location.reload(); }, _getParams: function(sort, direction) { From 729dd84f685750919c786184a01ee822a0b0f31b Mon Sep 17 00:00:00 2001 From: Liza Golubenko Date: Tue, 8 Aug 2017 09:06:52 +0300 Subject: [PATCH 5/6] some fixes --- common.blocks/pager/pager.bemhtml.js | 2 +- common.blocks/pager/pager.bemtree.js | 4 +- .../sidebar/__sorting/sidebar__sorting.js | 5 +- server/controllers/gh.js | 91 +++++++++++-------- 4 files changed, 61 insertions(+), 41 deletions(-) diff --git a/common.blocks/pager/pager.bemhtml.js b/common.blocks/pager/pager.bemhtml.js index c2974de..64ed315 100644 --- a/common.blocks/pager/pager.bemhtml.js +++ b/common.blocks/pager/pager.bemhtml.js @@ -1,6 +1,6 @@ block('pager') .elem('item') -.match(node => { return node.ctx.url !== true; }) +.match(node => node.ctx.url !== true) .replace()(node => { const type2caption = { first: '<<', diff --git a/common.blocks/pager/pager.bemtree.js b/common.blocks/pager/pager.bemtree.js index ea91def..d73f98c 100644 --- a/common.blocks/pager/pager.bemtree.js +++ b/common.blocks/pager/pager.bemtree.js @@ -1,6 +1,6 @@ block('pager').content()(node => { const { data } = node; - const pagination = Object.assign({ current: true }, data.pagination); + const pagination = data.pagination; const result = []; const createItem = (type, index) => { @@ -17,7 +17,7 @@ block('pager').content()(node => { 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)); diff --git a/common.blocks/sidebar/__sorting/sidebar__sorting.js b/common.blocks/sidebar/__sorting/sidebar__sorting.js index dfc6cfd..ccd947b 100644 --- a/common.blocks/sidebar/__sorting/sidebar__sorting.js +++ b/common.blocks/sidebar/__sorting/sidebar__sorting.js @@ -30,7 +30,10 @@ modules.define( _getParams: function(sort, direction) { return Object.assign( Location.getUri().queryParams, - { sort, direction } + { + sort: sort, + direction: direction + } ); } diff --git a/server/controllers/gh.js b/server/controllers/gh.js index f49b828..4cbed08 100644 --- a/server/controllers/gh.js +++ b/server/controllers/gh.js @@ -56,26 +56,15 @@ function getIndex(req, res) { const issuesData = responses[0]; const labelsData = responses[1]; - const querystring = require('querystring'); - - const pageCount = issuesData.pagination.last ? - querystring.parse(issuesData.pagination.last).page : - parseInt(querystring.parse(issuesData.pagination.prev).page) + 1; - - var exceptPagUrl = querystring.parse(issuesData.pagination.last || issuesData.pagination.prev); - delete exceptPagUrl.page; - - exceptPagUrl = Object.keys(exceptPagUrl).reduce((resStr, key) => { - return (resStr += '&' + key + '=' + exceptPagUrl[key]); - }, '').substr(1); + const paginationData = getPaginationData(issuesData.pagination); render(req, res, { view: 'page-index', issues: issuesData.issues, pagination: issuesData.pagination, labels: labelsData, - pageCount, - exceptPagUrl + pageCount: paginationData.pageCount, + exceptPagUrl: paginationData.exceptPagUrl }); }).catch(err => onError(req, res, err)); } @@ -84,16 +73,44 @@ 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, + exceptPagUrl: paginationData.exceptPagUrl + }, { + block: 'issues' + } + ); + }) .catch(err => onError(req, res, err)); } +function getPaginationData(issuesPag) { + const querystring = require('querystring'); + + const pageCount = issuesPag.last ? + querystring.parse(issuesPag.last).page : + parseInt(querystring.parse(issuesPag.prev).page) + 1; + + let exceptPagUrl = querystring.parse(issuesPag.last || issuesPag.prev); + delete exceptPagUrl.page; + + exceptPagUrl = Object.keys(exceptPagUrl).reduce((resStr, key) => { + return (resStr += '&' + key + '=' + exceptPagUrl[key]); + }, '').substr(1); + + return { + exceptPagUrl: exceptPagUrl, + pageCount: pageCount + }; +} + function getComplexIssue(req, res) { logger.log('getComplexIssue', req.params.id); @@ -142,8 +159,8 @@ function getComments(req, res) { comments, issueId: req.params.id }, { - block: 'comments' - })) + block: 'comments' + })) .catch(err => onError(req, res, err)); } @@ -167,20 +184,20 @@ 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' - }, dataType === 'issue' ? { issue: data } : { comment: data })) : - res.json(data); - }).catch(err => onError(req, res, err)); + 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' + }, dataType === 'issue' ? { issue: data } : { comment: data })) : + res.json(data); + }).catch(err => onError(req, res, err)); } function addComment(req, res) { From e2076b1df44e50ba4a62518fb77d04686bc401ff Mon Sep 17 00:00:00 2001 From: Liza Golubenko Date: Sat, 30 Sep 2017 15:03:11 +0300 Subject: [PATCH 6/6] pagination: fix state params passed form server --- common.blocks/pager/pager.bemtree.js | 2 +- server/controllers/gh.js | 34 +++++++++++++--------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/common.blocks/pager/pager.bemtree.js b/common.blocks/pager/pager.bemtree.js index d73f98c..3760cb7 100644 --- a/common.blocks/pager/pager.bemtree.js +++ b/common.blocks/pager/pager.bemtree.js @@ -8,7 +8,7 @@ block('pager').content()(node => { elem: 'item', mix: { block, elem: 'item' }, elemMods: { type: type }, - url: pagination[type] || `${data.exceptPagUrl}&page=${index}`, + url: pagination[type] || `${data.exceptPaginationUrl}&page=${index}`, number: index }; }; diff --git a/server/controllers/gh.js b/server/controllers/gh.js index 4cbed08..82b57f2 100644 --- a/server/controllers/gh.js +++ b/server/controllers/gh.js @@ -64,7 +64,7 @@ function getIndex(req, res) { pagination: issuesData.pagination, labels: labelsData, pageCount: paginationData.pageCount, - exceptPagUrl: paginationData.exceptPagUrl + exceptPaginationUrl: paginationData.exceptPaginationUrl }); }).catch(err => onError(req, res, err)); } @@ -82,7 +82,7 @@ function getIssues(req, res) { issues: issuesData.issues, pagination: issuesData.pagination, pageCount: paginationData.pageCount, - exceptPagUrl: paginationData.exceptPagUrl + exceptPaginationUrl: paginationData.exceptPaginationUrl }, { block: 'issues' } @@ -92,21 +92,18 @@ function getIssues(req, res) { } function getPaginationData(issuesPag) { - const querystring = require('querystring'); - - const pageCount = issuesPag.last ? + const querystring = require('querystring'), + pageCount = issuesPag.last ? querystring.parse(issuesPag.last).page : parseInt(querystring.parse(issuesPag.prev).page) + 1; - let exceptPagUrl = querystring.parse(issuesPag.last || issuesPag.prev); - delete exceptPagUrl.page; + let exceptPaginationUrl = querystring.parse(issuesPag.last && issuesPag.last.substr(1) || issuesPag.prev.substr(1)); + delete exceptPaginationUrl.page; - exceptPagUrl = Object.keys(exceptPagUrl).reduce((resStr, key) => { - return (resStr += '&' + key + '=' + exceptPagUrl[key]); - }, '').substr(1); + exceptPaginationUrl = '?' + querystring.stringify(exceptPaginationUrl); return { - exceptPagUrl: exceptPagUrl, + exceptPaginationUrl: exceptPaginationUrl, pageCount: pageCount }; } @@ -189,13 +186,14 @@ function _getData(req, res, dataType, urlPart) { type === 'form' ? render(req, res, { view: 'page-post' - }, - Object.assign({ - block: 'send-form', - mix: { block: dataType, elem: 'send-form' }, - formType: dataType, - reqType: 'edit' - }, dataType === 'issue' ? { issue: data } : { comment: data })) : + }, { + block: 'send-form', + mix: { block: dataType, elem: 'send-form' }, + formType: dataType, + reqType: 'edit', + issue: data, + comment: data + }) : res.json(data); }).catch(err => onError(req, res, err)); }