-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
204 lines (137 loc) · 6.03 KB
/
Copy pathmain.js
File metadata and controls
204 lines (137 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
var PlantsController = (function() {
// plant function constructor
var Plant = function(id, name, amountWater, frequencyWater) {
this.id= id;
this.name= name;
this.amountWater= amountWater;
this.frequencyWater= frequencyWater;
};
Plant.prototype.wateringTime = function() {
// create resetable timer to run waterMe() on plant that needs to be watered.
};
function waterMe(frequencyWater, ID) {
// perform dom manipulation to reset watering indicator
console.log(`%{ID} has been watered!`)
};
var data = {
plants: [],
};
return {
addPlant: function (name, amtWat, freqWat) {
var newItem, ID;
if (data.plants.length > 0) {
ID = data.plants[data.plants.length -1].id + 1;
} else {
ID = 0;
}
newItem = new Plant(ID, name, amtWat, freqWat);
data.plants.push(newItem);
localStorage.setItem(Math.random() ,PlantsController.data);
return newItem
},
deletePlant: function (id) {
data.plants.splice(id,1);
},
testing: function() {
console.log(data);
},
};
})();
var UIcontroller = (function() {
var DOMstrings = {
inputName: '.pName',
inputAmount: '.aWater',
inputFrequency: '.often',
inputButton: '.add__btn',
plantContainer: '.plant__container',
}
return {
displayMonth: function() {
var now, year, months, month;
now = new Date();
months = ['January', 'Febuary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
month = now.getMonth();
year = now.getFullYear();
day = now.getDate();
date = day + ' ' + months[month] + ' ' + year;
monthyear = months[month] + ' ' + year;
// document.querySelector(/* DOM STRING FOR DATE*/).textContent = monthyear;
return date;
},
getInput: function() {
return {
name: document.querySelector(DOMstrings.inputName).value,
amountWater: document.querySelector(DOMstrings.inputAmount).value,
frequencyWater: document.querySelector(DOMstrings.inputFrequency).value,
};
},
addListItem: function(obj) {
var html, newHtml, element;
// Create HTML string with placeholder text
element = DOMstrings.plantContainer;
html = '<div class="plant__clearfix" id= "plant-%id%"> <button class= "water" onClick="waterMe(%frequencyWater% , %id%)"> water </button> <button class= "delete%id%" onClick= "Controller.removePlant(%id%)"> X </button> <div class="plantName"> %plantName% </div> <div class="amountWater"> %amountWater% Cups </div> <img src="https://picsum.photos/100/50"> </div>'
// Replace placeholder text with data from object
newHtml = html.replaceAll('%id%', obj.id);
newHtml = newHtml.replace('%plantName%', obj.name);
newHtml = newHtml.replace('%amountWater%', obj.amountWater);
newHtml = newHtml.replace('%frequencyWater%', obj.frequencyWater);
// Insert the HTML into the DOM
document.querySelector(element).insertAdjacentHTML('beforeend', newHtml)
},
deleteListItem: function(ID) {
var el = document.getElementById('plant-' + ID);
el.parentNode.removeChild(el);
},
clearFields: function() {
fields = document.querySelectorAll(DOMstrings.inputName + ',' + DOMstrings.inputAmount + ',' + DOMstrings.inputFrequency);
fieldsArr = Array.prototype.slice.call(fields);
fieldsArr.forEach(function(current, index, array) {
current.value = "";
});
fieldsArr[0].focus();
},
getDOMstrings: function() {
return DOMstrings;
},
};
}) ()
// GLOBAL APP CONTOLLER
var Controller = (function(PlantsController, UICtrl) {
var setupEventListeners = function() {
clickAdd = function() {
addPlant();
};
document.addEventListener('keypress', function(event) {
if (event.keyCode === 13 || event.which === 13) { //keyCode 13 is enter
addPlant();
}
});
};
var addPlant = function() {
var input, newItem
// 1. Get the input data from UI field
input = UIcontroller.getInput();
// Check that users input is valid for our data structure
if (input.name !== "" && !isNaN(input.frequencyWater) && input.frequencyWater > 0 && !isNaN(input.amountWater) && input.amountWater > 0) {
// 2. add the item to the plants controller
newItem = PlantsController.addPlant(input.name, input.amountWater, input.frequencyWater)
// 3. add the item to the UI
UICtrl.addListItem(newItem);
// 4. clear the fields.
UICtrl.clearFields();
}
};
return {
init: function () {
setupEventListeners();
UICtrl.displayMonth();
// Retrieve the object from storage
var retrievedObject = localStorage.getItem(data);
},
removePlant: function(id) {
PlantsController.deletePlant(id);
UIcontroller.deleteListItem(id);
},
};
}) (PlantsController, UIcontroller);
Controller.init();