|
| 1 | +You are a developer assigned to making an app more accessible to a wider audience that may be using different JavaScript environments. You are performing a code review and you are to refactor the below Servicenow Server Script from ES12 compatible to be ES5/global compatible. |
| 2 | + |
| 3 | +```javascript |
| 4 | +// example code below |
| 5 | +(function() { |
| 6 | + |
| 7 | + // Pagination |
| 8 | + data.page_id = $sp.getParameter('id') |
| 9 | + const countGa = new GlideAggregate('x_snc_ehd_servic_0_prompt'); |
| 10 | + countGa.addAggregate('COUNT'); |
| 11 | + countGa.query(); |
| 12 | + if (countGa.next()) { |
| 13 | + data.count = parseInt(countGa.getAggregate('COUNT')); |
| 14 | + } |
| 15 | + data.page = parseInt($sp.getParameter('page')) || 1; |
| 16 | + data.display = parseInt($sp.getParameter('display')) > 0 ? parseInt($sp.getParameter('display')) : 10 || 10; |
| 17 | + data.pages = calculatePaginationPages(data.count, data.display, data.page); |
| 18 | + if (data.pages[data.pages.length - 1] < data.page) { |
| 19 | + data.page = Math.ceil(data.count / data.display); |
| 20 | + } |
| 21 | + data.rowStart = (data.page - 1) * data.display; |
| 22 | + const rowEnd = data.rowStart + data.display; |
| 23 | + |
| 24 | + // Data |
| 25 | + data.recordsFields = ['number', 'short_description'] // Place the column names you need in here. |
| 26 | + data.records = []; |
| 27 | + const recordsGr = new GlideRecord('incident'); // Place the table name here |
| 28 | + recordsGr.chooseWindow(data.rowStart, rowEnd); |
| 29 | + recordsGr.query(); |
| 30 | + while (recordsGr.next()){ |
| 31 | + const record = {}; |
| 32 | + for (let i of data.recordsFields){ |
| 33 | + record[i] = (recordsGr.getElement(i) + "").trim(); |
| 34 | + } |
| 35 | + data.records.push(record); |
| 36 | + } |
| 37 | + |
| 38 | + function calculatePaginationPages(totalRecords, recordsPerPage, currentPage) { |
| 39 | + if (recordsPerPage <= 0) { |
| 40 | + return []; |
| 41 | + } |
| 42 | + |
| 43 | + let totalPages = Math.ceil(totalRecords / recordsPerPage); |
| 44 | + let pages = []; |
| 45 | + |
| 46 | + let startPage, endPage; |
| 47 | + |
| 48 | + if (totalPages <= 7) { |
| 49 | + startPage = 1; |
| 50 | + endPage = totalPages; |
| 51 | + } else { |
| 52 | + if (currentPage < 4) { |
| 53 | + startPage = 1; |
| 54 | + endPage = 5; |
| 55 | + } else if (currentPage == 5){ |
| 56 | + startPage = 3; |
| 57 | + endPage = 7; |
| 58 | + } else if (currentPage + 2 >= totalPages) { |
| 59 | + startPage = totalPages - 4; |
| 60 | + endPage = totalPages; |
| 61 | + } else { |
| 62 | + startPage = currentPage - 2; |
| 63 | + endPage = currentPage + 2; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if (startPage > 1) { |
| 68 | + pages.push(1); |
| 69 | + if (startPage > 2) { |
| 70 | + pages.push('...'); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + for (let i = startPage; i <= endPage; i++) { |
| 75 | + pages.push(i); |
| 76 | + } |
| 77 | + |
| 78 | + if (endPage < totalPages) { |
| 79 | + if (endPage < totalPages - 1) { |
| 80 | + pages.push('...'); |
| 81 | + } |
| 82 | + pages.push(totalPages); |
| 83 | + } |
| 84 | + |
| 85 | + return pages; |
| 86 | + } |
| 87 | +})(); |
| 88 | +``` |
0 commit comments