|
| 1 | +/** |
| 2 | +* Copyright 2025 Google LLC |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +const ModelProperty = { |
| 18 | + TABLE_HTML: 'table_html', |
| 19 | + ROW_COUNT: 'row_count', |
| 20 | + PAGE_SIZE: 'page_size', |
| 21 | + PAGE: 'page', |
| 22 | +}; |
| 23 | + |
| 24 | +const Event = { |
| 25 | + CHANGE_TABLE_HTML: `change:${ModelProperty.TABLE_HTML}`, |
| 26 | + CLICK: 'click', |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * Renders a paginated table and its controls into a given element. |
| 31 | + * @param {{ |
| 32 | + * model: !Backbone.Model, |
| 33 | + * el: !HTMLElement |
| 34 | + * }} options |
| 35 | + */ |
| 36 | +function render({model, el}) { |
| 37 | + const container = document.createElement('div'); |
| 38 | + container.innerHTML = model.get(ModelProperty.TABLE_HTML); |
| 39 | + |
| 40 | + const buttonContainer = document.createElement('div'); |
| 41 | + const prevPage = document.createElement('button'); |
| 42 | + const label = document.createElement('span'); |
| 43 | + const nextPage = document.createElement('button'); |
| 44 | + |
| 45 | + prevPage.type = 'button'; |
| 46 | + nextPage.type = 'button'; |
| 47 | + prevPage.textContent = 'Prev'; |
| 48 | + nextPage.textContent = 'Next'; |
| 49 | + |
| 50 | + /** Updates the button states and page label based on the model. */ |
| 51 | + function updateButtonStates() { |
| 52 | + const totalPages = Math.ceil( |
| 53 | + model.get(ModelProperty.ROW_COUNT) / model.get(ModelProperty.PAGE_SIZE)); |
| 54 | + const currentPage = model.get(ModelProperty.PAGE); |
| 55 | + |
| 56 | + label.textContent = `Page ${currentPage + 1} of ${totalPages}`; |
| 57 | + prevPage.disabled = currentPage === 0; |
| 58 | + nextPage.disabled = currentPage >= totalPages - 1; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Updates the page in the model. |
| 63 | + * @param {number} direction -1 for previous, 1 for next. |
| 64 | + */ |
| 65 | + function handlePageChange(direction) { |
| 66 | + const currentPage = model.get(ModelProperty.PAGE); |
| 67 | + const newPage = Math.max(0, currentPage + direction); |
| 68 | + if (newPage !== currentPage) { |
| 69 | + model.set(ModelProperty.PAGE, newPage); |
| 70 | + model.save_changes(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + prevPage.addEventListener(Event.CLICK, () => handlePageChange(-1)); |
| 75 | + nextPage.addEventListener(Event.CLICK, () => handlePageChange(1)); |
| 76 | + |
| 77 | + model.on(Event.CHANGE_TABLE_HTML, () => { |
| 78 | + // Note: Using innerHTML can be a security risk if the content is |
| 79 | + // user-generated. Ensure 'table_html' is properly sanitized. |
| 80 | + container.innerHTML = model.get(ModelProperty.TABLE_HTML); |
| 81 | + updateButtonStates(); |
| 82 | + }); |
| 83 | + |
| 84 | + // Initial setup |
| 85 | + updateButtonStates(); |
| 86 | + |
| 87 | + buttonContainer.appendChild(prevPage); |
| 88 | + buttonContainer.appendChild(label); |
| 89 | + buttonContainer.appendChild(nextPage); |
| 90 | + el.appendChild(container); |
| 91 | + el.appendChild(buttonContainer); |
| 92 | +} |
| 93 | + |
| 94 | +export default {render}; |
0 commit comments