Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {authorizeFunc} from './authorize.js';
  2. import {getToken} from './gettoken.js';
  3. import {getRandomPhotos} from './getrandomphotos.js';
  4. import {searchPhotos} from './search.js';
  5. import {searchTest} from './searchTest.js';
  6.  
  7. // Get images container div.
  8. let imgContainer = document.getElementById('imgContainer')
  9.  
  10. // Redirect user to authorization link.
  11. let authorizeBtn = document.getElementById('authorizeBtn');
  12. authorizeBtn.addEventListener('click', authorizeFunc);
  13.  
  14. // Get user token by sending POST to API
  15. let tokenBtn = document.getElementById('tokenBtn');
  16. tokenBtn.addEventListener('click', getToken);
  17.  
  18. // Create current page container.
  19. let getPage = 1;
  20.  
  21. // Get search input value.
  22. let searchField = document.getElementById('searchField');
  23.  
  24. // Get search button and add onClick listener.
  25. let searchButton = document.getElementById('searchButton');
  26. searchButton.addEventListener('click', () => {
  27.     // Reset pagination.
  28.     getPage = 1;
  29.     // Request api for pictures.
  30.     searchPhotos(searchField.value, getPage);
  31. });
  32.  
  33. // Create flag to recognize lazy loading.
  34. let obj = { isFetching: false };
  35.  
  36. // Lazy loading.
  37. window.onscroll = (event) => {
  38.  
  39.     let currentScrollPosition = window.innerHeight + window.scrollY;
  40.     let currentDocumentHeight = document.body.offsetHeight;
  41.     let currentScrollPercent = (currentScrollPosition / currentDocumentHeight) * 100;
  42.    
  43.     if (currentScrollPercent >= 80 && obj.isFetching == false) {
  44.         console.log('call next page');
  45.         obj.isFetching = true;
  46.         console.log(obj.isFetching);
  47.         // Set pagination to another page.
  48.         getPage = getPage + 1;
  49.         searchPhotos(searchField.value, getPage, obj.searchTest);
  50.     }
  51. };
  52.  
  53. // Display random photos on main page if user is logged in.
  54. window.onload = () => {
  55.     // Check if token is empty in cookies.
  56.     if (document.cookie.split("token=")[1] === null) {
  57.         console.log('im here');
  58.         // Hide login form
  59.         document.getElementById('login').style.display = 'none';
  60.  
  61.         // Get random photos from API.
  62.         getRandomPhotos()
  63.     }
  64. }
  65.  
  66. // Trash
  67. console.log(document.cookie);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement