Advertisement
zoranrelic

Untitled

Apr 20th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1.  
  2. //Budget controler
  3. var budgetController = (function() {
  4.  
  5. //kreiram puno objects sa function construction object
  6.  
  7. var Expense = function(id, description, value) {
  8. this.id = id;
  9. this.description = description;
  10. this.value = value;
  11. };
  12.  
  13. var Income = function(id, description, value) {
  14. this.id = id;
  15. this.description = description;
  16. this.value = value;
  17. };
  18.  
  19. //da ima npr 10 incoma stavili bi ih u array
  20. var allExpenses =[];
  21. var allIncomes = [];
  22. var totalExpenses = 0;
  23.  
  24. //ili još bolje zbog data strukture
  25. var data = {
  26. allItems: {
  27. exp: [],
  28. inc: [],
  29. },
  30. totals: {
  31. exp: 0,
  32. inc: 0
  33. }
  34. };
  35.  
  36. return {
  37. addItem: function(type, des, val) {
  38. var newItem, ID;
  39. //[1,2,3,4,5], next ID = 6
  40. //[1,3,6,8,9], next ID = 10
  41. // ID = last ID + 1
  42.  
  43. //CREATE NEW ID
  44. if (data.allItems[type].length > 0) {
  45. ID = data.allItems[type][data.allItems[type].length - 1].id + 1;
  46. } else {
  47. ID = 0;
  48. }
  49.  
  50. //create new item based on 'inc' or 'exp' type
  51. if(type === 'exp') {
  52. newItem = new Expense ( ID, des, val);
  53. }else if (type === 'inc'){
  54. newItem = new Income (ID, des, val );
  55. }
  56. //push it into our data structure
  57. data.allItems[type].push(newItem);
  58.  
  59. //return new element
  60. return newItem;
  61.  
  62. },
  63. testing: function(){
  64. console.log(data);
  65. }
  66. };
  67. })();
  68.  
  69.  
  70.  
  71. // UI controler
  72. var UIController = (function(){
  73.  
  74. //iz razloga što imamo puno classes koje ak promjenim html sve treba mjenjati onda ću tu složiti objekt sa svima
  75. var DOMstrings = {
  76. inputType: '.add__type',
  77. inputDescription: '.add__description',
  78. inputValue: '.add__value',
  79. inputBtn: '.add__btn',
  80. incomeContainer: '.income__list',
  81. expensesContainer: '.expenses__list'
  82. };
  83.  
  84. return {
  85. getInput: function() {
  86. return {
  87. type: document.querySelector(DOMstrings.inputType).value, // biti će ili inc ili exp
  88. description: document.querySelector(DOMstrings.inputDescription).value,
  89. value: document.querySelector(DOMstrings.inputValue).value
  90. };
  91. },
  92. getDOMstrings: function() {
  93. return DOMstrings; // expose DOMstrings into the public
  94. },
  95.  
  96.  
  97. addListItem: function(obj, type) {
  98. //create HTML string with placeholder text
  99. var html, newHtml, element;
  100.  
  101. if(type === 'inc') {
  102. element = DOMstrings.incomeContainer;
  103.  
  104. html = '<div class="item clearfix" id="inc-%id%"> <div class="item__description">%description%</div><div class="right clearfix"><div class="item__value">%value%</div><div class="item__delete"><button class="item__delete--btn"><i class="ion-ios-close-outline"></i></button></div></div></div>';
  105.  
  106. }else if (type === 'exp') {
  107. element = DOMstrings.expensesContainer;
  108.  
  109. html = '<div class="item clearfix" id="exp-%id%"><div class="item__description">%description%</div><div class="right clearfix"><div class="item__value">%value%</div><div class="item__percentage">21%</div><div class="item__delete"><button class="item__delete--btn"><i class="ion-ios-close-outline"></i></button></div></div></div>';
  110. }
  111.  
  112. //replace the placeholder text with some actual data
  113.  
  114.  
  115. newHtml = html.replace('%id%', obj.id);
  116. newHtml = newHtml.replace('%description%', obj.description);
  117. newHtml = newHtml.replace('%value%', formatNumber(obj.value, type));
  118. // Insert theHTML into the DOM ima u dokumentaciji developer mozzila
  119.  
  120. document.querySelector(element).insertAdjacentHTML('beforeend', newHtml);
  121. },
  122.  
  123.  
  124.  
  125. };
  126. })();
  127.  
  128. // Global app controler
  129.  
  130. var controller =(function(budgetCtrl, UICtrl){
  131.  
  132. var setUpEventListeners = function() {
  133.  
  134. var DOM = UICtrl.getDOMstrings();
  135.  
  136. // upute su bile unutar query selectora ali kad smo dodali keypress napravili smo novi function po DRY principu
  137. document.querySelector(DOM.inputBtn).addEventListener("click", ctrlAddItem);
  138.  
  139. // za enter umjesto buttona
  140. document.addEventListener("keypress" , function(event){
  141. if(event.keyCode === 13 || event.which === 13){
  142. ctrlAddItem();
  143. }
  144. });
  145.  
  146.  
  147. };
  148. var ctrlAddItem = function() {
  149. var input, newItem;
  150.  
  151. // 1. Get field input data
  152. input = UICtrl.getInput();
  153.  
  154.  
  155. // 2. Add the item to budget controler
  156. newItem = budgetCtrl.addItem(input.type, input.description, input.value);
  157.  
  158. // 3. Add the new item to UI
  159. UICtrl.addListItem(newItem, input.type);
  160.  
  161. // 4. Calculate the budget
  162. // 5. Display the budget on the UI
  163.  
  164. };
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. return {
  172. init: function() {
  173. console.log("Aplication has started");
  174. setUpEventListeners();
  175. }
  176. };
  177.  
  178.  
  179. })(budgetController, UIController);
  180.  
  181. controller.init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement