diff --git a/javaScriptTraining/homeWork6(YouTube)/assets/js/HtmlHelper.js b/javaScriptTraining/homeWork6(YouTube)/assets/js/HtmlHelper.js index a1b7a78..f5cbeee 100644 --- a/javaScriptTraining/homeWork6(YouTube)/assets/js/HtmlHelper.js +++ b/javaScriptTraining/homeWork6(YouTube)/assets/js/HtmlHelper.js @@ -3,7 +3,7 @@ var HtmlHelper = (function () { function HtmlHelper() {} var apiHandler = new ApiHandler(), - paginationHelper = new PaginationHelper(); + pagenationHelper = new PagenationHelper(); HtmlHelper.prototype.initializePage = function () { var _this = this, @@ -17,8 +17,10 @@ var HtmlHelper = (function () { event.preventDefault(); if (event.keyCode === 13) { apiHandler.searchForVideos($searchBox.value).then(function (videos) { - paginationHelper.setCurrentPage(1); + pagenationHelper.setCurrentPage(1); _this.renderGrid(videos); + _this.renderPagenation(videos); + }); } }); @@ -26,15 +28,15 @@ var HtmlHelper = (function () { document.body.appendChild($search); window.addEventListener('resize', function () { var videos = apiHandler.getVideos(); - if (videos.length) { - paginationHelper.setCurrentPage(1); + if (videos && videos.length > 0) { _this.renderGrid(videos); + _this.renderPagenation(videos) } }); } HtmlHelper.prototype.renderPagenation = function (items) { - this.renderPageNumbers(paginationHelper.getPageCount(items, apiHandler)); + this.renderPageNumbers(pagenationHelper.getPageCount(items, apiHandler)); } HtmlHelper.prototype.clearGrid = function () { @@ -58,13 +60,12 @@ var HtmlHelper = (function () { HtmlHelper.prototype.renderPageNumbers = function (numberOfpages) { var _this = this, - $paginationFragment = document.createElement('div'), - $anchor; + $paginationFragment = document.createElement('div'); $paginationFragment.setAttribute('id', 'pagination'); $paginationFragment.setAttribute('class', 'pagination-controls'); _this.clearPagination(); for (var i = 0; i < numberOfpages; i++) { - $anchor = document.createElement('a'); + var $anchor = document.createElement('a'); $anchor.appendChild(document.createTextNode(i + 1)); $anchor.setAttribute('id', 'page' + (i + 1)); $anchor.setAttribute('href', '#'); @@ -83,8 +84,8 @@ var HtmlHelper = (function () { } function paginationClick(event) { - if (event.target.tagName.toLowerCase() === 'a') { - paginationHelper.setCurrentPage(event.target.text); + if (event.target.tagName.toLocaleLowerCase() === 'a') { + pagenationHelper.setCurrentPage(event.target.text); HtmlHelper.prototype.renderGrid(apiHandler.getVideos()); HtmlHelper.prototype.setSelectedPageCss(); } @@ -92,40 +93,37 @@ var HtmlHelper = (function () { HtmlHelper.prototype.setSelectedPageCss = function () { var $paginationElement = document.querySelector('#pagination'), - currentPage = paginationHelper.getCurrentPage(), - $selectedPagination = $paginationElement.querySelector('#page' + currentPage), - $previousActivePage; + currentPage = pagenationHelper.getCurrentPage(), + $selectedPagination = $paginationElement.querySelector('#page' + currentPage); if (!$selectedPagination) { currentPage = 1; - paginationHelper.setCurrentPage(currentPage); + pagenationHelper.setCurrentPage(currentPage); $selectedPagination = $paginationElement.querySelector('#page' + currentPage); } - $previousActivePage = $paginationElement.querySelector('.active'); + var $previousActivePage = $paginationElement.querySelector('.active'); if ($previousActivePage) { $previousActivePage.classList.remove('active'); } - if ($selectedPagination) { - $selectedPagination.classList.add('active'); - } + $selectedPagination.classList.add('active'); } HtmlHelper.prototype.renderGrid = function (videos) { var _this = this, $videosSection = document.createElement('div'), itemCount = apiHandler.getVideosPerPage(), - startIndex = paginationHelper.getStartIndex(itemCount), + startIndex = itemCount + pagenationHelper.getStartIndex(itemCount), range = startIndex + itemCount, - numberOfpages, $node; + numberOfpages; $videosSection.setAttribute('id', 'youtube-container'); _this.clearGrid(); for (var i = startIndex; i < range; i++) { if (videos[i]) { - $node = this.getVideoItem(videos[i], i); + var $node = this.getVideoItem(videos[i], i); $videosSection.appendChild($node); } } document.body.appendChild($videosSection); - numberOfpages = paginationHelper.getPageCount(videos, apiHandler.getVideosPerPage()); + numberOfpages = pagenationHelper.getPageCount(videos, apiHandler); _this.renderPageNumbers(numberOfpages); } diff --git a/javaScriptTraining/homeWork6(YouTube)/assets/js/PagenationHelper.js b/javaScriptTraining/homeWork6(YouTube)/assets/js/PagenationHelper.js new file mode 100644 index 0000000..21cf7fb --- /dev/null +++ b/javaScriptTraining/homeWork6(YouTube)/assets/js/PagenationHelper.js @@ -0,0 +1,26 @@ +var PagenationHelper = (function () { + 'use strict'; + + function PagenationHelper() {}; + + PagenationHelper.prototype.getPageCount = function (items, apiHandler) { + var videosPerPage = apiHandler.getVideosPerPage(), + totalVideos = items.length, + pageCount = Math.floor(totalVideos / videosPerPage); + pageCount = pageCount + (totalVideos % videosPerPage === 0 ? 0 : 1); + return pageCount; + } + + PagenationHelper.prototype.getCurrentPage = function () { + return this.currentPage || 1; + } + + PagenationHelper.prototype.setCurrentPage = function (pageNumber) { + this.currentPage = pageNumber; + } + + PagenationHelper.prototype.getStartIndex = function (noOfVideos) { + return (this.getCurrentPage() * noOfVideos) - noOfVideos + } + return PagenationHelper; +})(); diff --git a/javaScriptTraining/homeWork6(YouTube)/assets/js/PaginationHelper.js b/javaScriptTraining/homeWork6(YouTube)/assets/js/PaginationHelper.js deleted file mode 100644 index 0048c42..0000000 --- a/javaScriptTraining/homeWork6(YouTube)/assets/js/PaginationHelper.js +++ /dev/null @@ -1,23 +0,0 @@ -var PaginationHelper = (function () { - 'use strict'; - - function PaginationHelper() {}; - - PaginationHelper.prototype.getPageCount = function (items, videosPerPage) { - var totalVideos = items.length; - return Math.floor(totalVideos / videosPerPage) + (totalVideos % videosPerPage === 0 ? 0 : 1); - } - - PaginationHelper.prototype.getCurrentPage = function () { - return this.currentPage || 1; - } - - PaginationHelper.prototype.setCurrentPage = function (pageNumber) { - this.currentPage = pageNumber; - } - - PaginationHelper.prototype.getStartIndex = function (noOfVideos) { - return (this.getCurrentPage() * noOfVideos) - noOfVideos - } - return PaginationHelper; -})(); diff --git a/javaScriptTraining/homeWork6(YouTube)/assets/specs/ApiHandler.spec.js b/javaScriptTraining/homeWork6(YouTube)/assets/specs/ApiHandler.spec.js index ecdb259..86e6cb9 100644 --- a/javaScriptTraining/homeWork6(YouTube)/assets/specs/ApiHandler.spec.js +++ b/javaScriptTraining/homeWork6(YouTube)/assets/specs/ApiHandler.spec.js @@ -1,25 +1,10 @@ -describe('ApiHandler Module', function () { +describe('ApiHandler', function () { var apiHandler; beforeAll(function () { apiHandler = new ApiHandler(); }); - it('Test setter and getter for api handler expected behavior', function () { + it('first', function () { apiHandler.setVideos([]); expect(apiHandler.getVideos().length).toBe(0); - }); - it('Test setter and getter for api handler un equality behavior', function () { - apiHandler.setVideos([]); - expect(apiHandler.getVideos().length).not.toBe(1); - }); - it('Test url for API call', function () { - var queryParams = { - key: "token", - part: "part", - type: "type", - maxResults: "max", - q: "text" - }, - url = apiHandler.getApiUrl(queryParams); - expect(url).toBe("https://www.googleapis.com/youtube/v3/search?key=token&part=part&type=type&maxResults=max&q=text"); - }); + }) }) diff --git a/javaScriptTraining/homeWork6(YouTube)/assets/specs/PaginationHelper.spec.js b/javaScriptTraining/homeWork6(YouTube)/assets/specs/PaginationHelper.spec.js deleted file mode 100644 index 6c5fb4d..0000000 --- a/javaScriptTraining/homeWork6(YouTube)/assets/specs/PaginationHelper.spec.js +++ /dev/null @@ -1,62 +0,0 @@ -describe('Pagination Helper Module getPageCount function', function () { - var paginationHelper; - beforeAll(function () { - paginationHelper = new PaginationHelper(); - }); - it("Test page count for 2 videos per page", function () { - var videos = [{ - "one": "one" - }, { - "two": "two" - }]; - expect(paginationHelper.getPageCount(videos, 2)).toBe(1); - }); - it("Test page count for 4 videos per page", function () { - var videos = [{ - "one": "one" - }, { - "two": "two" - }]; - expect(paginationHelper.getPageCount(videos, 4)).toBe(1); - }); - it("Test page count for 4 videos per page", function () { - var videos = [{ - "one": "one" - }, { - "two": "two" - }, { - "three": "three" - }, { - "four": 4 - }, { - "five": 5 - }]; - expect(paginationHelper.getPageCount(videos, 4)).toBe(2); - }); -}); -describe('Pagination Helper Module getCurrentPage function', function () { - var paginationHelper; - beforeAll(function () { - paginationHelper = new PaginationHelper(); - }); - it("Test current page when un initialized", function () { - expect(paginationHelper.getCurrentPage()).toBe(1); - }); - it("Test current page when surrent page is set to 2", function () { - paginationHelper.setCurrentPage(2); - expect(paginationHelper.getCurrentPage()).toBe(2); - }); -}); -describe('Pagination Helper Module getStartIndex function', function () { - var paginationHelper; - beforeAll(function () { - paginationHelper = new PaginationHelper(); - }); - it("Test start indexfor 2 videos per page", function () { - expect(paginationHelper.getStartIndex(2)).toBe(0); - }); - it("Test start indexfor 8 videos per page", function () { - paginationHelper.setCurrentPage(2); - expect(paginationHelper.getStartIndex(8)).toBe(8); - }); -}); diff --git a/javaScriptTraining/homeWork6(YouTube)/index.html b/javaScriptTraining/homeWork6(YouTube)/index.html index 4f56fa9..d0a88a9 100644 --- a/javaScriptTraining/homeWork6(YouTube)/index.html +++ b/javaScriptTraining/homeWork6(YouTube)/index.html @@ -23,7 +23,7 @@
- + diff --git a/javaScriptTraining/homeWork6(YouTube)/karma.conf.js b/javaScriptTraining/homeWork6(YouTube)/karma.conf.js index dd5674b..cc21cf5 100644 --- a/javaScriptTraining/homeWork6(YouTube)/karma.conf.js +++ b/javaScriptTraining/homeWork6(YouTube)/karma.conf.js @@ -17,7 +17,7 @@ module.exports = function (config) { files: [ "js/config.js", "js/ApiHandler.js", - "js/PaginationHelper.js", + "js/PagenationHelper.js", "js/HtmlHelper.js", "js/index.js", "specs/*.spec.js"],