/* * function to load a set of cards only after the image has loaded on first load, and to add more * cards if a load more button is present. When the limit has been reached the load button will * disapear */ var firstLoad = true; function loadCards(userInput) { var actualCount = 1; var currentCount = 1; // loads the first round cards $('.' + userInput.cardClass).each(function (i) { actualCount++; if (i + 1 <= userInput.cardsToDisplay) { //$(this).waitForImages(function() { $(this).removeClass('hidden'); if (userInput.cardParent) { $('.' + userInput.cardClass).closest('.' + userInput.cardParent).find('.loader').remove(); } else { $('.' + userInput.cardClass).parent().find('.loader').remove(); } currentCount++; //}); } if (testCount(actualCount - 1, userInput.cardsToDisplay) === 1) { $('.' + userInput.loadMoreButtonClass).removeClass('hidden'); } else if (testCount(actualCount - 1, userInput.cardsToDisplay) === 0) { $('.' + userInput.loadMoreButtonClass).addClass('hidden'); } }); // loads the next round of cards on button press $('.' + userInput.loadMoreButtonClass).click(function () { $('.' + userInput.cardClass + ':hidden').each(function (i) { if (i + 1 <= userInput.cardsToDisplay) { //$(this).waitForImages(function() { $(this).removeClass('hidden'); currentCount++; if (i === 0) { //$(this).find('a:first').focus(); } if (isLastItem(actualCount, currentCount)) { $('.' + userInput.loadMoreButtonClass).addClass('hidden'); } //}); } }); }); } /** * This is the function to load cards from the JSON, it removes the class */ function loadJSONCards(userInput) { var actualCount = 1; var currentCount = 1; if (userInput.totalCards) { actualCount += userInput.totalCards; } // loads the first round cards $('.' + userInput.cardClass).each(function (i) { if (i + 1 <= userInput.cardsToDisplay) { //$(this).waitForImages(function() { $(this).removeClass('hidden'); $('.' + userInput.cardClass).parent().find('.loader').remove(); //}); currentCount++; } if (testCount(actualCount - 1, userInput.cardsToDisplay) === 1) { $('.' + userInput.loadMoreButtonClass).removeClass('hidden'); } else if (testCount(actualCount - 1, userInput.cardsToDisplay) === 0) { $('.' + userInput.loadMoreButtonClass).addClass('hidden'); } }); // loads the next round of cards on button press $('.' + userInput.cardClass + ':hidden').each(function (i) { if (i + 1 <= userInput.cardsToDisplay) { //$(this).waitForImages(function() { $(this).removeClass('hidden'); if (i === 0) { $this = $(this); setTimeout(function () { $this.find('a:first').focus(); }, 300); } // $('.'+userInput.loadMoreButtonClass).removeClass('hidden') if (isLastItem(actualCount, currentCount) && (actualCount == currentCount)) { $('.' + userInput.loadMoreButtonClass).addClass('hidden'); } else { $('.' + userInput.loadMoreButtonClass).removeClass('hidden'); } //}); } }); } /* * Waits for one image to load, then removes the palceholder loading icon */ function loadAsset(targetContainer) { // $('.'+targetContainer).waitForImages(function() { // $(this).parent().find('.loader').addClass('hidden'); // $(this).removeClass('hidden'); // }); $('.' + targetContainer).each(function () { $(this).parent().find('.loader').addClass('hidden'); $(this).removeClass('hidden'); }); } function loadTop(targetContainer, cardTarget) { $('.' + cardTarget).each(function () { //$(this).waitForImages(function() { $(this).removeClass('hidden'); $('.' + targetContainer).parent().find('.loader').addClass('hidden'); //}); }); } // checks that there are more card items than the cardsToDisplay number function testCount(actualCount, initialCount) { if (actualCount === initialCount) { return 0; } else if (actualCount > initialCount) { return 1; } else { return 2; } } // remove button when the last card is loaded function isLastItem(actualCount, currentCount) { return (actualCount === currentCount); } function render(props) { return function (tok, i) { return (i % 2) ? props[tok] : tok; }; }