diff --git a/.github/workflows/test.yml-template b/.github/workflows/test.yml-template new file mode 100644 index 00000000..44ac4e96 --- /dev/null +++ b/.github/workflows/test.yml-template @@ -0,0 +1,29 @@ +name: Test + +on: + pull_request: + branches: [ master ] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [20.x] + + steps: + - uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - run: npm install + - run: npm start & sleep 5 && npm test + - name: Upload tests report(cypress mochaawesome merged HTML report) + if: ${{ always() }} + uses: actions/upload-artifact@v2 + with: + name: report + path: reports diff --git a/README.md b/README.md index 064d543f..2834af1c 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ 1. Replace `` with your Github username in the link - - [DEMO LINK](https://.github.io/js_growth_table_DOM/) + - [DEMO LINK](https://dianakomar.github.io/js_growth_table_DOM/) 2. Follow [this instructions](https://mate-academy.github.io/layout_task-guideline/) - Run `npm run test` command to test your code; - Run `npm run test:only -- -n` to run fast test ignoring linter; diff --git a/package-lock.json b/package-lock.json index bf93e753..f2c093b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,7 @@ "@mate-academy/eslint-config": "latest", "@mate-academy/jest-mochawesome-reporter": "^1.0.0", "@mate-academy/linthtml-config": "latest", - "@mate-academy/scripts": "^1.8.5", + "@mate-academy/scripts": "^2.1.3", "@mate-academy/stylelint-config": "latest", "@parcel/transformer-sass": "^2.12.0", "cypress": "^13.13.0", @@ -1467,10 +1467,11 @@ "dev": true }, "node_modules/@mate-academy/scripts": { - "version": "1.8.6", - "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.8.6.tgz", - "integrity": "sha512-b4om/whj4G9emyi84ORE3FRZzCRwRIesr8tJHXa8EvJdOaAPDpzcJ8A0sFfMsWH9NUOVmOwkBtOXDu5eZZ00Ig==", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-2.1.3.tgz", + "integrity": "sha512-a07wHTj/1QUK2Aac5zHad+sGw4rIvcNl5lJmJpAD7OxeSbnCdyI6RXUHwXhjF5MaVo9YHrJ0xVahyERS2IIyBQ==", "dev": true, + "license": "MIT", "dependencies": { "@octokit/rest": "^17.11.2", "@types/get-port": "^4.2.0", diff --git a/package.json b/package.json index cb1ab6ac..cf906474 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "@mate-academy/eslint-config": "latest", "@mate-academy/jest-mochawesome-reporter": "^1.0.0", "@mate-academy/linthtml-config": "latest", - "@mate-academy/scripts": "^1.8.5", + "@mate-academy/scripts": "^2.1.3", "@mate-academy/stylelint-config": "latest", "@parcel/transformer-sass": "^2.12.0", "cypress": "^13.13.0", diff --git a/src/scripts/main.js b/src/scripts/main.js index a765fdb1..e4869c41 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -1,3 +1,77 @@ 'use strict'; -// write code here +const btnAppendRow = document.querySelector('.append-row'); +const btnRemoveRow = document.querySelector('.remove-row'); +const btnAppendColumn = document.querySelector('.append-column'); +const btnRemoveColumn = document.querySelector('.remove-column'); + +const table = document.querySelector('table'); +let rowsCount = document.querySelectorAll('tr').length; +const firstRow = document.querySelector('table tr'); +let colsCount = firstRow ? firstRow.querySelectorAll('td, th').length : 0; + +const maxCount = 10; +const minCount = 2; + +function updateButtons() { + btnAppendRow.disabled = rowsCount >= maxCount; + btnRemoveRow.disabled = rowsCount <= minCount; + btnAppendColumn.disabled = colsCount >= maxCount; + btnRemoveColumn.disabled = colsCount <= minCount; +} + +btnAppendRow.addEventListener('click', (e) => { + const newRow = document.createElement('tr'); + + if (rowsCount < maxCount) { + for (let i = 0; i < colsCount; i++) { + newRow.appendChild(document.createElement('td')); + } + + const tbody = table.querySelector('tbody') || table; + + tbody.appendChild(newRow); + rowsCount++; + } + updateButtons(); +}); + +btnAppendColumn.addEventListener('click', (e) => { + const rows = document.querySelectorAll('tr'); + + if (colsCount < maxCount) { + for (let i = 0; i < rows.length; i++) { + rows[i].appendChild(document.createElement('td')); + } + colsCount++; + } + updateButtons(); +}); + +btnRemoveRow.addEventListener('click', (e) => { + if (rowsCount > minCount) { + const rows = document.querySelectorAll('tr'); + + rows[rows.length - 1].remove(); + rowsCount--; + } + updateButtons(); +}); + +btnRemoveColumn.addEventListener('click', (e) => { + const rows = document.querySelectorAll('tr'); + + if (colsCount > minCount) { + for (const row of rows) { + const cells = row.querySelectorAll('td, th'); + + if (cells.length > 0) { + cells[cells.length - 1].remove(); + } + } + colsCount--; + } + updateButtons(); +}); + +updateButtons();