app.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. var baseUrl = '';
  2. (function($) {
  3. $(document).ready(function() {
  4. var $main = $('#main');
  5. //
  6. // function home() {
  7. // $('#main')
  8. // }
  9. //
  10. // function showAlert(type, text) {
  11. // $('#alert')
  12. // .removeClass('success')
  13. // }
  14. var routeMap = {
  15. about: {
  16. data: function(cb) {
  17. $.get(baseUrl + '/data/about.html', cb);
  18. },
  19. controller: function(aboutHtml) {
  20. console.log('about', aboutHtml);
  21. $('#main').html(aboutHtml);
  22. }
  23. },
  24. welcome: {
  25. data: function(cb) {
  26. console.log('get data for welcome');
  27. $.get(baseUrl + '/data/products.json', function(data) {
  28. console.log('welcome.data.success', data);
  29. cb(data);
  30. }, 'json');
  31. },
  32. controller: function(products) {
  33. console.log('controller', arguments, products);
  34. products.forEach(product => {
  35. $main.append('<div class="product">' +
  36. '<img src="/images/products/' + product.image + '" alt="' + product.name + '" />' +
  37. '<div class="overlay">' + product.name + '</div>' +
  38. '</div>');
  39. })
  40. }
  41. },
  42. cart: {
  43. data: function(cb) {
  44. $.get(baseUrl + '/data/products.json', function(products) {
  45. console.log('products', products);
  46. $.get(baseUrl + '/data/cart.json', function(cart) {
  47. console.log('cart', cart);
  48. // console.log(cb.toString());
  49. cb({products, cart});
  50. });
  51. });
  52. },
  53. controller: function(data) {
  54. // console.log('controller', arguments, data);
  55. // $('#main').html(data.products.length);
  56. var products = data.products;
  57. var cart = data.cart;
  58. console.log(products);
  59. var $table = $('<table class="cart"><tr>' +
  60. '<th></th>' +
  61. '<th>Product&nbsp;ref</th>' +
  62. '<th>Product&nbsp;name</th>' +
  63. '<th>Qty.</th>' +
  64. '<th>Unit&nbsp;price</th>' +
  65. '<th>Price</th>' +
  66. '</tr></table>').appendTo($main);
  67. var total = 0.0;
  68. cart.forEach(function(item) {
  69. console.log('cart item', item);
  70. var product = _.find(products, {
  71. id: item.productId
  72. });
  73. total += item.quantity * product.price;
  74. $table.append('<tr>' +
  75. '<td class="thumb"><img src="/images/products/' + product.image + '" alt="' + product.name + '" /></td>' +
  76. '<td>' + product.productRef + '</td>' +
  77. '<td>' + product.name + '</td>' +
  78. '<td>' + item.quantity + '</td>' +
  79. '<td>' + product.price + '€</td>' +
  80. '<td>' + item.quantity * product.price + '€</td>' +
  81. '</tr>'
  82. )
  83. });
  84. $table.append('<tr><td colspan="5"></td>' +
  85. '<td><strong>' +total + '€</strong></td></tr>'
  86. );
  87. }
  88. }
  89. };
  90. function router(e) {
  91. var hash = window.location.hash;
  92. hash = hash ? hash.substr(1) : 'welcome';
  93. console.log('hash changed', hash);
  94. var existingRoutes = Object.keys(routeMap);
  95. if(existingRoutes.indexOf(hash) === -1) {
  96. console.log('error', 'route does not exist');
  97. }
  98. var routeHandler = routeMap[hash];
  99. if(
  100. typeof routeHandler.data !== 'function' ||
  101. typeof routeHandler.controller !== 'function'
  102. ) {
  103. console.log('Error, route handler for "' + hash + '" is invalid', routeHandler);
  104. }
  105. routeHandler.data(function(data) {
  106. // console.log('got data', hash, data);
  107. $main.empty();
  108. routeHandler.controller(data)
  109. });
  110. }
  111. window.addEventListener('hashchange', router);
  112. router();
  113. // console.log('ok loading products...');
  114. // $.get('/data/products.json?ts=' + Date.now(), function(data) {
  115. // alert('done');
  116. // // console.log('got products', data);
  117. // }, 'json');
  118. });
  119. })(jQuery);