app.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. function http(url, params) {
  2. return new Promise(function(resolve, reject) {
  3. httpRequest = new XMLHttpRequest();
  4. httpRequest.onreadystatechange = function() {
  5. if (httpRequest.readyState == XMLHttpRequest.DONE ) {
  6. if (httpRequest.status == 200) {
  7. resolve(httpRequest.responseText);
  8. } else {
  9. reject("pls fix later");
  10. }
  11. }
  12. };
  13. httpRequest.open('GET', url, true);
  14. httpRequest.setRequestHeader('lang', 'ES')
  15. var x = params ? params : null;
  16. httpRequest.send(null);
  17. });
  18. }
  19. http('http://192.168.1.127/api/products/category/58caba2ce53f0644597b74e8').then(function(data) {
  20. //console.log("2017 no frameworks", data);
  21. }, function(err){
  22. console.log("err",err);
  23. });
  24. var sections = [
  25. 'empresa',
  26. 'productos'
  27. ];
  28. var selectedSection_ = {
  29. empresa: true,
  30. productos: false
  31. };
  32. rivets.configure({
  33. executeFunctions: true
  34. })
  35. var app = document.getElementById('app');
  36. var view = rivets.bind(app, {
  37. sections: sections,
  38. selectedSection: selectedSection_,
  39. selectSection: function (event, scope) {
  40. var newValue = event.target.text;
  41. window.history.pushState({}, newValue, '#' + newValue)
  42. chooseSection(newValue);
  43. event.target.className = 'selected';
  44. }
  45. });
  46. function chooseSection(section) {
  47. var navbarList = window.document.querySelectorAll('ul.navbar-list > li > a');
  48. navbarList.forEach(function (el) {
  49. var isSelected = el.text === section;
  50. el.className = isSelected ? 'selected' : '';
  51. selectedSection_[el.text] = isSelected;
  52. })
  53. }
  54. var elements = [];
  55. window.onscroll = function () {
  56. elements = elements.reduce(function (acc, e) {
  57. if (isInViewport(e)) {
  58. e.className = 'discover-li'
  59. return acc;
  60. } else {
  61. acc.push(e);
  62. return acc;
  63. }
  64. }, [])
  65. if (elements.length === 0) {
  66. window.onscroll = function () {};
  67. }
  68. };
  69. function isInViewport(element) {
  70. var rect = element.getBoundingClientRect();
  71. var html = document.documentElement;
  72. return (rect.top >= 0 &&
  73. rect.left >= 0 &&
  74. rect.bottom <= (window.innerHeight || html.clientHeight) &&
  75. rect.right <= (window.innerWidth || html.clientWidth))
  76. }
  77. window.onload = function () {
  78. chooseSection(window.location.hash.substr(1));
  79. }