-
Notifications
You must be signed in to change notification settings - Fork 1
/
NewItem.js
executable file
·160 lines (121 loc) · 4.69 KB
/
NewItem.js
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
$(document).ready(function() {
var categoryToSubCategory = {};
$(".addItemContainer").hide();
/*
* Ready button click event
*/
$("#Ready").click(function () {
$("#Ready").slideUp(400);
$("#Custom").slideUp(400);
$("#addReadyItemContainer").slideDown(500);
});
/*
* Custom button click event
*/
$("#Custom").click(function () {
$("#Ready").slideUp(400);
$("#Custom").slideUp(400);
$("#addCustomItemContainer").slideDown(500);
});
/*
* Cancel button click event
*/
$(".cancelButton").click(function () {
$(".addItemContainer").slideUp(400);
$("#Ready").slideDown(500);
$("#Custom").slideDown(500);
});
var Item = Parse.Object.extend("Cost_Items");
var query = new Parse.Query(Item);
/*
* Get all the categories and sub-categories from Parse
*/
query.find({
success: function (results) {
for (var i = 0 ; i < results.length ; i++) {
var category = results[i].get("Category");
var subCategory = results[i].get("SubCategory");
if (categoryToSubCategory.hasOwnProperty(category)) {
var existingSubCategories = categoryToSubCategory[category];
if (subCategory && $.inArray(subCategory, existingSubCategories) < 0){
existingSubCategories.push(subCategory);
}
}
else{
if (subCategory) {
categoryToSubCategory[category] = [subCategory];
}
}
}
/*
* Save the new cost item
*/
$(".saveButton").click(function () {
var prefix = $(this).closest('div').attr('id') == 'readyButtons' ? 'ready' : 'custom';
var Amount = $('#' + prefix + 'Amount').val();
var insertCostItem = true;
if (!commonObj.isAmountInDailyBudget(Amount)) {
insertCostItem = confirm("You are about to go over your daily budget. Continue anyway?")
}
if (!commonObj.isAmountInInMonthlyBudget(Amount)) {
insertCostItem = confirm("You are about to go over your monthly budget. Continue anyway?")
}
if (insertCostItem) {
var Category = $('#' + prefix + 'Category').val();
var SubCategories = $('#' + prefix + 'SubCategories').val();
var user = Parse.User.current();
var newItem = new Item();
newItem.set("Category", Category);
newItem.set("SubCategory", SubCategories);
newItem.set("Amount", Amount);
newItem.set("user", user);
newItem.save({
success: function () {
document.location = "MainPage.html";
}
});
}
});
$(document).trigger('dataLoaded');
}
});
/*
* Add the categories to the combo-box after the data is loaded
*/
$(document).on("dataLoaded", function() {
var keys = Object.keys(categoryToSubCategory);
for (var i = 0 ; i < keys.length ; i++){
var opt = document.createElement('option');
opt.value = keys[i];
opt.textContent = keys[i];
$("#readyCategory").append(opt);
}
$("#readyCategory").on('change', function () {
list(categoryToSubCategory[$(this).val()])
})
});
/*
* Add the sub-categories to the combo-box
*/
function list(array_list) {
$("#readySubCategories").html("");
var pleaseSelectOption = document.createElement('option');
pleaseSelectOption.value = '';
pleaseSelectOption.textContent = '-- Please Select --';
$("#readySubCategories").append(pleaseSelectOption);
$(array_list).each(function (i) {
var opt = document.createElement('option');
opt.value = this.toString();
opt.textContent = this.toString();
$("#readySubCategories").append(opt);
});
}
var MSM = $("#mobileMenuButton");
MSM.click(function(event) {
$("#sideMenu").slideDown(400);
});
var CMSM = $("#closeMobileMenuButton");
CMSM.click(function(event) {
$("#sideMenu").slideUp(200);
});
});