diff options
author | Ilmari Lauhakangas <ilmari.lauhakangas@libreoffice.org> | 2018-01-30 13:27:55 +0200 |
---|---|---|
committer | Olivier Hallot <olivier.hallot@libreoffice.org> | 2018-01-30 18:57:58 +0100 |
commit | e37c19a9dd3f2d3e5a31f91975ee5a8aac2dc2af (patch) | |
tree | c52d3fc1398dce623939bfbf07e3e823385439d0 /help3xsl/paginathing.js | |
parent | 64187c4328623fca6c00d2f5c9f659211ee18883 (diff) |
Switched from list.js to fuse.js and paginathing.js
Fuse.js has Apache License 2.0 and paginathing.js has
Expat License (aka MIT).
Paginathing is a paginating utility and it was originally jQuery,
but due to performance concerns I converted it to pure JavaScript
and rather brutally adapted it to our needs.
Fuse.js gives us fuzzy search with multiple strings. The settings
can be refined, if needed. Settings can be easily tested with the
live demo: http://fusejs.io/
Change-Id: I1cf6a6f7d06adbcac95760db90187ee26be8e908
Reviewed-on: https://gerrit.libreoffice.org/48906
Reviewed-by: Olivier Hallot <olivier.hallot@libreoffice.org>
Tested-by: Olivier Hallot <olivier.hallot@libreoffice.org>
Diffstat (limited to 'help3xsl/paginathing.js')
-rw-r--r-- | help3xsl/paginathing.js | 242 |
1 files changed, 242 insertions, 0 deletions
diff --git a/help3xsl/paginathing.js b/help3xsl/paginathing.js new file mode 100644 index 0000000000..6eb1ca6ab3 --- /dev/null +++ b/help3xsl/paginathing.js @@ -0,0 +1,242 @@ +/** + * Paginathing + * Paginate Everything + * + * Original @author Alfred Crosby <https://github.com/alfredcrosby> + * Inspired from http://esimakin.github.io/twbs-pagination/ + * Modified to pure JavaScript and specialised to LibreOffice Help by + * Ilmari Lauhakangas + * + * MIT License (Expat) + * + * Copyright (c) 2018 Alfred Crosby + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +var options = { + perPage: 10, + limitPagination: 6, + prevNext: true, + firstLast: true, + prevText: '<<', + nextText: '>>', + firstText: '<', + lastText: '>', + containerClass: 'pagination-container', + ulClass: 'pagination', + liClass: 'page', + activeClass: 'active', + disabledClass: 'disabled' +}; + +var Paginator = function(element) { + el = element; + startPage = 1; + currentPage = 1; + pageDivision = 0; + totalItems = el.getElementsByClassName('fuseshown').length; + var limitPagination = options.limitPagination; + pageDivision = Math.ceil(totalItems / options.perPage); + // let's not display pagination leading nowhere + function pagLimit() { + if (options.limitPagination >= pageDivision) { + limitPagination = pageDivision; + } else { + limitPagination = options.limitPagination; + } + return limitPagination; + } + totalPages = Math.max(pageDivision, pagLimit()); + existingContainer = document.getElementsByClassName('pagination-container')[0]; + if (existingContainer) { + parent = existingContainer.parentNode; + parent.removeChild(existingContainer); + } + container = document.createElement('nav'); + container.setAttribute('class', options.containerClass); + ul = document.createElement('ul'); + ul.setAttribute('class', options.ulClass); + + function paginationFunc(type, page) { + var li = document.createElement('li'); + var a = document.createElement('a'); + a.setAttribute('href', '#'); + var cssClass = type === 'number' ? options.liClass : type; + var text = document.createTextNode(type === 'number' ? page : paginationText(type)); + + li.classList.add(cssClass); + li.setAttribute('data-pagination-type', type); + li.setAttribute('data-page', page); + a.appendChild(text); + li.appendChild(a); + + return li; + } + + function paginationText(type) { + return options[type + 'Text']; + } + + function buildPagination() { + var pagination = []; + var prev = currentPage - 1 < startPage ? startPage : currentPage - 1; + var next = currentPage + 1 > totalPages ? totalPages : currentPage + 1; + + var start = 0; + var end = 0; + var limit = limitPagination; + + if (limit) { + if (currentPage <= Math.ceil(limit / 2) + 1) { + start = 1; + end = limit; + } else if (currentPage + Math.floor(limit / 2) >= totalPages) { + start = totalPages + 1 - limit; + end = totalPages; + } else { + start = currentPage - Math.ceil(limit / 2); + end = currentPage + Math.floor(limit / 2); + } + } else { + start = startPage; + end = totalPages; + } + + + // "First" button + if (options.firstLast) { + pagination.push(paginationFunc('first', startPage)); + } + + // "Prev" button + if (options.prevNext) { + pagination.push(paginationFunc('prev', prev)); + } + + // Pagination + for (var i = start; i <= end; i++) { + pagination.push(paginationFunc('number', i)); + } + + // "Next" button + if (options.prevNext) { + pagination.push(paginationFunc('next', next)); + } + + // "Last" button + if (options.firstLast) { + pagination.push(paginationFunc('last', totalPages)); + } + return pagination; + } + + function render(page) { + // Remove children before re-render (prevent duplicate) + while (ul.hasChildNodes()) { + ul.removeChild(ul.lastChild); + } + + var paginationBuild = buildPagination(); + + paginationBuild.forEach(function(item) { + ul.appendChild(item); + }); + + // Manage active DOM + var startAt = page === 1 ? 0 : (page - 1) * options.perPage; + var endAt = page * options.perPage; + + var domLi = el.getElementsByClassName("fuseshown"); + + for (var i = 0, len = domLi.length; i < len; i++) { + var item = domLi[i]; + + if (i >= startAt && i <= endAt) { + item.classList.remove('hidden'); + } else { + item.classList.add('hidden'); + } + } + + // Manage active state + var ulKids = ul.getElementsByTagName("li"); + + for (var i = 0, len = ulKids.length; i < len; i++) { + var _li = ulKids[i]; + var type = _li.getAttribute('data-pagination-type'); + + switch (type) { + case 'number': + if (parseInt(_li.getAttribute('data-page')) === page) { + _li.classList.add(options.activeClass); + } + break; + case 'first': + page === startPage && _li.classList.toggle(options.disabledClass); + break; + case 'last': + page === totalPages && _li.classList.toggle(options.disabledClass); + break; + case 'prev': + (page - 1) < startPage && _li.classList.toggle(options.disabledClass); + break; + case 'next': + (page + 1) > totalPages && _li.classList.toggle(options.disabledClass); + break; + default: + break; + } + } + + el.after(container); + container.appendChild(ul); + } + + function handle() { + var pagLi = container.childNodes[0].childNodes; + + for (var i = 0, len = pagLi.length; i < len; i++) { + (function() { + var item = pagLi[i]; + + item.addEventListener('click', function(e) { + e.preventDefault(); + var page = parseInt(item.getAttribute('data-page')); + currentPage = page; + // let's prevent the pagination from flowing to two rows + if (currentPage >= 98) { + limitPagination = 4; + } else { + limitPagination = pagLimit(); + } + show(page); + }); + }()); + } + } + + function show(page) { + render(page); + handle(); + } + + show(startPage); + + return; +}; |