-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
313 lines (271 loc) · 7.95 KB
/
main.js
File metadata and controls
313 lines (271 loc) · 7.95 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
Devices = new Meteor.Collection("devices");
var displayChange = function() {
if (Session.get('mode') == undefined) {
$('.home').addClass('visible');
$('.detail-modal').removeClass('visible');
$('.input-modal').removeClass('visible');
} else if (Session.get('mode') == 'detail') {
$('.home').removeClass('visible');
$('.detail-modal').addClass('visible');
$('.input-modal').removeClass('visible');
} else if (Session.get('mode') == 'input') {
$('.home').removeClass('visible');
$('.detail-modal').removeClass('visible');
$('.input-modal').addClass('visible');
}
}
if (Meteor.isClient) {
Session.setDefault('device_selected', undefined);
Session.setDefault('mode', undefined);
Template.login.events({
'submit #login-form': function(e, t) {
e.preventDefault();
var email = t.find('#login-email').value,
password = t.find('#login-password').value;
email = $.trim(email);
password = $.trim(password);
// needs validation...
// If validation passes, supply the appropriate fields to the
// Meteor.loginWithPassword() function.
Meteor.loginWithPassword(email, password, function(err) {
if (err) {
// The user might not have been found, or their password
// could be incorrect. The login attempt has failed.
console.log('login error', err);
} else {
// The user has been logged in.
console.log('login successful');
Session.set('logged_in', true);
}
});
return false;
},
'click .close': function() {
$('.account-form').hide();
}
});
Template.register.events({
'submit #register-form': function(e, t) {
e.preventDefault();
var email = t.find('#account-email').value,
password = t.find('#account-password').value,
username = t.find('#account-name').value,
key = t.find('#account-key').value;
// Trim and validate the input
Meteor.call('checkKey', key, function(err, result) {
if (result) {
Accounts.createUser({ username: username, email: email, password: password }, function(err) {
if (err) {
// Inform the user that account creation failed
console.log('fail', err);
} else {
// Success. Account has been created and the user
// has logged in successfully.
Session.set('logged_in', true);
console.log('user created');
displayChange();
}
});
} else {
console.log('keycheck failed', result);
}
})
return false;
},
'click .close': function() {
$('.account-form').hide();
}
});
// the nav stuff
Template.nav.events({
'click .add_device': function() {
Session.set('mode', 'input');
displayChange();
},
'click .logo': function() {
Session.set('mode', undefined);
displayChange();
},
'click .show_login': function() {
$('.account-form').hide();
$('.login-form').show();
},
'click .show_register': function() {
$('.account-form').hide();
$('.register-form').show();
},
'click .user_logout': function() {
Meteor.logout(function(err) {
if (err) {
console.log('logout error', err);
} else {
console.log('logged out successfully');
}
});
}
});
// The 'homepage' list of devices
Template.device_list.events({
'click .device-thumb-mod': function() {
Session.set('device_selected', this._id);
Session.set('mode', 'detail');
displayChange();
}
});
Template.device_list.helpers({
'devices': function() {
return Devices.find({}).fetch();
},
//using this to put a class on the home div
'mode': function() {
if (Session.get('mode') == 'detail') {
return 'detail';
} else if (Session.get('mode') == 'input') {
return 'input';
} else {
return 'no-mode';
}
}
});
// View a device Detail modal
Template.device_detail.helpers({
'device': function() {
var devices = Devices.find({
_id: Session.get('device_selected')
}).fetch();
if (devices.length <= 0) {
return [];
}
//reverse the history array so the latest thing is on top
if (devices[0].hasOwnProperty('history')) {
for (var i = 0; i < devices.length; i++) {
devices[0].history.reverse();
}
}
return devices;
},
'user': function() {
var user = Meteor.user();
return user && user.username;
}
});
Template.device_detail.events({
'click .close': function() {
Session.set('device_selected', undefined);
Session.set('mode', undefined);
displayChange();
},
'click #checkout_submit': function() {
var user = Meteor.user(),
data = {},
buttonMsg;
var historyData = {
name: user.username
};
if(this.checked_out === false) {
// the user is trying to check OUT the device
// we want to...
// - put their name in the history
// - set checked_out to true
// - set button text var
// - set borrower to current user
data.checked_out = true;
data.borrower = user.username;
historyData.message = "checked out the device.";
buttonMsg = "Bring it back!";
} else {
// the user is trying to check IN the device
// we want to...
// - put their name in the history
// - set checked_out to false
// - set button text var
// - set borrower to null
data.checked_out = false;
data.borrower = null;
historyData.message = "checked in the device.";
buttonMsg = "Check it out!";
}
Meteor.call('pushToHistory', this._id, historyData);
$('#checkout_submit').attr('value', buttonMsg);
Meteor.call('updateDevice', this._id, data);
// if(user === null) {
// console.log('You must be logged-in to perform this action');
// return;
// }
// if (this.checked_out === false) {
// // check it out
// data.checked_out = true;
// buttonMsg = 'Bring it back!';
// Meteor.call('pushToHistory', this._id, {
// name: user.username,
// message: 'checked out the device.'
// });
// data.borrower = user.username;
// } else {
// // check it in
// data.checked_out = false;
// buttonMsg = 'Check it out!';
// Meteor.call('pushToHistory', this._id, {
// name: data.borrower,
// message: 'brought it back.'
// });
// }
},
'click #delete_device': function() {
var response = confirm('are you sure you want to delete this?');
if (response) {
Meteor.call('removeDevice', this._id);
Session.set('mode', undefined);
displayChange();
}
}
});
}
if (Meteor.isServer) {
Meteor.methods({
addDevice: function(data) {
Devices.insert({
img: data.device_img,
name: data.device_name,
class: data.device_class,
os: data.os,
resolution: data.res,
release: data.release,
notes: data.notes,
status: 'in',
borrower: undefined,
history: [{
'message': 'The device was created.'
}]
});
},
updateDevice: function(id, data) {
console.log('updating device', id, data);
Devices.update({
_id: id
}, {
$set: data
});
},
removeDevice: function(id) {
Devices.remove({
_id: id
});
},
pushToHistory: function(id, data) {
Devices.update({
_id: id
}, {
$push: {
'history': data
}
})
},
checkKey: function(key) {
return key === 'moist';
}
})
Meteor.startup(function() {
// Devices.remove({});
});
}