app.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. var BASE_URL = 'http://192.168.1.127/api/';
  2. function http(url, params) {
  3. return new Promise(function(resolve, reject) {
  4. httpRequest = new XMLHttpRequest();
  5. httpRequest.onreadystatechange = function() {
  6. if (httpRequest.readyState == XMLHttpRequest.DONE ) {
  7. if (httpRequest.status == 200) {
  8. resolve(JSON.parse(httpRequest.responseText));
  9. } else {
  10. reject("pls fix later");
  11. }
  12. }
  13. };
  14. httpRequest.open('GET', url, true);
  15. httpRequest.setRequestHeader('lang', 'ES')
  16. var x = params ? params : null;
  17. httpRequest.send(null);
  18. });
  19. }
  20. http('http://192.168.1.127/api/products/listcategories').then(function(data) {
  21. }, function(err){
  22. console.log("err", err);
  23. });
  24. function logError (err) {
  25. console.log('Error: ', err);
  26. }
  27. var sections = [
  28. 'empresa',
  29. 'productos'
  30. ];
  31. function initProducts(category) {
  32. http(BASE_URL + 'products/category/' + category).then(function (products) {
  33. products.forEach(function (product) {
  34. productsListed_.push(product);
  35. })
  36. }, logError)
  37. }
  38. var selectedSection_ = {
  39. empresa: true,
  40. productos: false
  41. };
  42. var productsListed_ = [];
  43. rivets.configure({
  44. executeFunctions: true
  45. })
  46. function getCategories() {
  47. if(productCategories_.length === 0) {
  48. http(BASE_URL + 'products/listcategories').then(function (data) {
  49. data.forEach(function (category) {
  50. productCategories_.push(category)
  51. })
  52. }, logError)
  53. }
  54. }
  55. var productCategories_ = [];
  56. var app = document.getElementById('app');
  57. rivets.binders['src-strict'] = function(el, value) {
  58. var img = new Image()
  59. img.onload = function() {
  60. $(el).attr('src', value)
  61. }
  62. img.src = value
  63. }
  64. var view = rivets.bind(app, {
  65. sections: sections,
  66. selectedSection: selectedSection_,
  67. productCategories: productCategories_,
  68. productsListed: productsListed_,
  69. selectSection: function (event, scope) {
  70. var newValue = event.target.text;
  71. window.history.pushState({}, newValue, '#' + newValue)
  72. chooseSection(newValue);
  73. event.target.className = 'selected';
  74. scope.getData[newValue]();
  75. },
  76. selectCategory: function (event, scope) {
  77. var category = productCategories_.find(R.propEq('name', event.target.text));
  78. initProducts(category._id)
  79. },
  80. getData: {
  81. productos: getCategories,
  82. empresa: function(){}
  83. }
  84. });
  85. function chooseSection(section) {
  86. var navbarList = window.document.querySelectorAll('ul.navbar-list > li > a');
  87. navbarList.forEach(function (el) {
  88. var isSelected = el.text === section;
  89. el.className = isSelected ? 'selected' : '';
  90. selectedSection_[el.text] = isSelected;
  91. })
  92. }
  93. var elements = [];
  94. window.onscroll = function () {
  95. elements = elements.reduce(function (acc, e) {
  96. if (isInViewport(e)) {
  97. e.className = 'discover-li'
  98. return acc;
  99. } else {
  100. acc.push(e);
  101. return acc;
  102. }
  103. }, [])
  104. if (elements.length === 0) {
  105. window.onscroll = function () {};
  106. }
  107. };
  108. function isInViewport(element) {
  109. var rect = element.getBoundingClientRect();
  110. var html = document.documentElement;
  111. return (rect.top >= 0 &&
  112. rect.left >= 0 &&
  113. rect.bottom <= (window.innerHeight || html.clientHeight) &&
  114. rect.right <= (window.innerWidth || html.clientWidth))
  115. }
  116. window.onload = function () {
  117. chooseSection(window.location.hash.substr(1));
  118. }