forked from mitchgre/gregsList
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects.js
844 lines (687 loc) · 20.8 KB
/
objects.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/*
This file will contain a main object specialized for this project.
*/
/*
A class to contain mappings to functions
*/
function glo() // gregsList Object
{
this.user =
{
name : null,
password : null,
token : null,
sessionId : null
};
this.goals =
{
parent: this,
contents: [],
filler: fillGoals,
type: "goal",
get: "getGoals",
display: displayTable,
table: $("#tableOfGoals")[0],
removeFunction: remover,
editFunction: editGoal,
updater: "updateGoal",
destroyer: "removeGoal",
displayKeys: ["value"]
};
this.industries =
{
parent: this,
contents: [],
filler: fillIndustries,
type: "industry",
get: "getIndustries",
display: displayTable,
table: $("#tableOfIndustries")[0],
removeFunction: remover,
editFunction: editIndustry,
updater: "updateIndustry",
destroyer: "removeIndustry",
displayKeys: ["name"]
};
this.postings =
{
parent: this,
contents: [], // array of values to be filled from data store
filler: fillPostings, // javascript function to fill contents from data store
type: "posting",
get: "getPostings", // php function to call
display: displayTable, // javascript function to display
table: $("#tableOfPostings")[0], // a reference to the div container. set in setupPostings()
add: null, // php function to add posting
removeFunction: remover, // javascript function to remove
editFunction: editPosting,
updater: "updatePosting", // php function to update
destroyer: "removePosting", // php function to remove
displayKeys: ["sid","title","url","location","company","source"] // values to display in table
};
this.companies =
{
parent: this,
contents: [],
filler: fillCompanies,
type: "company",
get: "getCompanies",
display: displayTable,
table: $("#tableOfCompanies")[0],
add: null,
removeFunction: remover,
destroyer: "removeCompany", // php function to remove
displayKeys: ["name"] // values to display in table
};
this.locations =
{
parent: this,
contents: [],
filler: fillLocations,
type: "location",
get: "getLocations",
display: displayTable,
table: $("#tableOfLocations")[0],
add: null,
removeFunction: remover,
destroyer: "removeLocation", // php function to remove
displayKeys: ["name"] // values to display in table
};
this.contacts =
{
parent: this,
contents: [],
filler: fillContacts,
type: "contact",
get: "getContacts",
display: displayTable,
table: $("#tableOfContacts")[0],
add: null,
editFunction: editContact,
updater: "updateContact", // php function to update
removeFunction: remover,
destroyer: "removeContact", // php function to remove
displayKeys: ["fname","lname","email","phone",
"facebook","linkedin","github"] // values to display in table
};
this.schedules =
{
parent: this,
contents: [],
filler: fillSchedules,
removeFunction: remover,
type: "schedule",
get: "getSchedules",
display: displayTable,//displaySchedules,
destroyer: "removeSchedule", // php function to remove
table: $("#tableOfEvents")[0],
displayKeys: ["name","description","contact","start","end"],
add: null
};
this.blog =
{
parent: this,
contents: [],
filler: fillBlog,
removeFunction: remover,
type: "blog",
get: "getBlog",
display: displayTable,//displaySchedules,
destroyer: "removeBlog", // php function to remove
table: $("#tableOfBlogs")[0],
displayKeys: ["text"],
add: null
};
this.login();
}
/*
On page load, open a dialog box asking for user name and password.
On success, pass to customs function.
*/
glo.prototype.login =
function login()
{
var object = this;
// build form
var string = '<div id="login" title="Welcome to gregsList!">';
string += '<ul id="loginInstructions">';
string += '<li><td>Please enter a username and password.';
string += '<li>If you lost your password, you will need to create a new username.';
string += '</ul>';
string += '<table>';
string += '<tr>';
string += '<td>Username</td>';
string += '<td>';
string += '<input id="embedUsername" value="">';
string += '</td>';
string += '</tr>';
string += '<tr><td>Password</td>';
string += '<td><input id="embedPassword" value=""></td></tr>';
string += '</div>';
// create dialog
var dialog = $(string).dialog
(
{
modal:true,
closeOnEscape: false,
// open: function(event, ui) { $(".ui-dialog-titlebar-close", ui.dialog || ui).hide(); },
beforeclose: function (event, ui) { return false; },
dialogClass: "noclose",
box: $(this).dialog,
buttons:
{
/* // We doan need no steenken cancelations.
Cancel: function()
{
$(this).dialog("close")
// box("close")
},
*/
"Login":function()
{
// get values from form
var username = $("#embedUsername")[0].value;
var password = $("#embedPassword")[0].value;
console.log("attempting login");
console.log("username: " + username);
console.log("password: " + password);
// validate these against database.
// on successful validation, insert data to glo
// possible responses:
// 1. username and password are taken, and they match (success)
// 2. username and password are taken, but don't match (prompt user for new username)
// 3. username is not taken, and password is not empty (create new account)
// 4. username is not taken, but password is empty (prompt user for non-empty password)
var box = $(this);
$.ajax
(
{
url: "butler.php",
type: "post",
dataType: "text",
data:
{
user: username,
pass: password,
},
success: function(response)
{
// done all we can do here.
// close up shop, and send user to customs for processing.
object.customs(response,username,password,dialog,box);
} // end success
} // end ajax data
); // end .ajax call
} // end login button function
} // end buttons
} // end dialog data
); // end dialog function
} // end login function
/*
After getting a login response from the server, error check that form
was properly filled.
If not filled properly, send them back to the login.
*/
glo.prototype.customs =
function customs(response,username,password,dialog,box)
{
var object = this;
console.log(response);
// tie up login's loose ends.
emptyElement("login");
removeElement("login");
box.dialog("close");
// give 'em the boiler room treatment
// might want to act accordingly, but for now each of these is the same
// alert and send back to login.
if (response === 'invalid combination.')
{
alert(response);
object.login(); // try again
}
else if (response === "password cannot be empty.")
{
alert(response);
object.login(); // try again
}
else if (response === "username cannot be empty.")
{
alert(response);
object.login(); // try again
}
else if (response === "error creating new user.")
{
alert(response);
object.login(); // try again
}
else
{
// houston you are clear for lift off, here are your papers, happy hunting
object.user.name = username;
object.user.password = password;
object.refresh();
}
}
glo.prototype.refresh =
function refresh()
{
var object = this;
object.setupGoals();
object.setupIndustries();
object.setupCompanies();
object.setupLocations();
object.setupPostings();
object.setupContacts();
object.setupSchedules();
object.setupBlog();
}
/*
empty tables and calendars then refill them from db
*/
glo.prototype.setupSchedules =
function setupSchedules()
{
var object = this.schedules;
console.log("from setupSchedule: object=");
console.log(object);
// empty calendar events
$("#calendarPortlet").fullCalendar('removeEvents');
$("#calendar").fullCalendar('removeEvents');
// empty main table
var calEd = $("#calEdit")[0];
emptyElement(calEd);
// var calEdField = addInput(calEd,'text','','','EventToAdd');
var addButton = addInput(calEd,'button','','Add Event','addEventButton');
addButton.data = object;
addButton.type="button";
// wire button
addButton.onclick = insertSchedule;
// insert empty results table
var table = createAppendedChildToParent('table',calEd);
this.schedules.table = table;
table.id = 'tableOfEvents';
table.className = 'io';
// table.style.tableLayout = "fixed";
// table.style.width = "50%";
// get events from db and refill calendars in callback
getStuff(this.schedules);
}
/*
Empty the tables of goals and rebuild them.
*/
glo.prototype.setupGoals =
function setupGoals()
{
var goals = $("#goals")[0]; // container div
emptyElement(goals);
// add text fields
// goals.appendChild(document.createTextNode('Goals'));
var goalField = addInput(goals,'text','','','GoalToAdd');
// add button
var addButton = addInput(goals,'button','','Add Goal','addGoalButton');
// wire button
addButton.onclick = insertGoal.bind(this);
// insert empty results table
var table = createAppendedChildToParent('table',goals);
this.goals.table = table;
table.id = 'tableOfGoals';
table.className = 'io';
// fill results table
getStuff(this.goals);
}
/*
Empty the tables of industries and rebuild them.
*/
glo.prototype.setupIndustries =
function setupIndustries()
{
var industries = $("#industries")[0]; // container div
emptyElement(industries);
// add text fields
// industries.appendChild(document.createTextNode('Industries'));
var industryField = addInput(industries,'text','','','IndustryToAdd');
// add button
var addButton = addInput(industries,'button','','Add Industry','addIndustryButton');
// wire button
addButton.onclick = insertIndustry.bind(this);
// insert empty results table
var table = createAppendedChildToParent('table',industries);
this.industries.table = table;
table.id = 'tableOfIndustries';
table.className = 'io';
// fill results table
getStuff(this.industries);
}
/*
Empty the tables of companies and rebuild them.
*/
glo.prototype.setupCompanies =
function setupCompanies()
{
var companies = $("#companies")[0]; // container div
emptyElement(companies);
// add text fields
// companies.appendChild(document.createTextNode('Companies'));
var companyField = addInput(companies,'text','','','CompanyToAdd');
// add button
var addButton = addInput(companies,'button','','Add Company','addCompanyButton');
// wire button
addButton.onclick = insertCompany.bind(this);
// insert empty results table
var table = createAppendedChildToParent('table',companies);
this.companies.table = table;
table.id = 'tableOfCompanies';
table.className = 'io';
// fill results table
getStuff(this.companies);
}
/*
Empty the tables of locations and rebuild them.
*/
glo.prototype.setupLocations =
function setupLocations()
{
var locations = $("#locations")[0]; // container div
emptyElement(locations);
// add text fields
// locations.appendChild(document.createTextNode('Locations'));
var locationField = addInput(locations,'text','','','LocationToAdd');
// add button
var addButton = addInput(locations,'button','','Add Location','addLocationButton');
// wire button
addButton.onclick = insertLocation.bind(this);
// insert empty results table
var table = createAppendedChildToParent('table',locations);
this.locations.table = table;
table.id = 'tableOfLocations';
table.className = 'io';
// fill results table
getStuff(this.locations);
}
/*
Empty the tables of postings and rebuild them.
*/
glo.prototype.setupPostings =
function setupPostings()
{
var postings = $("#postings")[0]; // container div
//var postings = this.postings.table;
emptyElement(postings);
var addButton = addInput(postings,'button','','Add posting','addPostingButton');
/*
// add inputs
var input = createAppendedChildToParent('table',postings);
input.style.width = "100%";
input.style.tableLayout = "fixed";
var tr = createAppendedChildToParent('tr',input);
// add button
var td = createAppendedChildToParent('td',tr);
var addButton = addInput(td,'button','','Add posting','addPostingButton');
// add text fields
var td = createAppendedChildToParent('td',tr);
td.appendChild(document.createTextNode('Title'));
var titleField = addInput(td,'text','','','postingTitleToAdd');
var td = createAppendedChildToParent('td',tr);
td.appendChild(document.createTextNode('Link'));
var linkField = addInput(td,'text','','','postingLinkToAdd');
var td = createAppendedChildToParent('td',tr);
td.appendChild(document.createTextNode('Company'));
var companyField = addInput(td,'text','','','postingCompanyToAdd');
var td = createAppendedChildToParent('td',tr);
td.appendChild(document.createTextNode('Location'));
var companyField = addInput(td,'text','','','postingLocationToAdd');
var td = createAppendedChildToParent('td',tr);
td.appendChild(document.createTextNode('Source'));
var sourceField = addInput(td,'text','','','postingSourceToAdd');
*/
// wire button
addButton.onclick = insertPosting.bind(this);
// insert empty results table
var table = createAppendedChildToParent('table',postings);
this.postings.table = table;
table.id = 'tableOfPostings';
table.className = 'io';
// fill results table
getStuff(this.postings);
}
/*
Empty the tables of contacts and rebuild them.
*/
glo.prototype.setupContacts =
function setupContacts()
{
var contacts = $("#contacts")[0]; // container div
emptyElement(contacts);
var input = createAppendedChildToParent('table',contacts);
// input.style.width = "100%";
// input.style.tableLayout = "fixed";
var tr = createAppendedChildToParent('tr',input);
var td = createAppendedChildToParent('td',tr);
// add text fields
td.appendChild(document.createTextNode('First Name'));
var fnameField = addInput(td,'text','','','fnameToAdd');
// new cell
td = createAppendedChildToParent('td',tr);
td.appendChild(document.createTextNode('Last Name'));
var lnameField = addInput(td,'text','','','lnameToAdd');
// new cell
td = createAppendedChildToParent('td',tr);
td.appendChild(document.createTextNode('Email'));
var emailField = addInput(td,'text','','','emailToAdd');
// new row
tr = createAppendedChildToParent('tr',input);
td = createAppendedChildToParent('td',tr);
td.appendChild(document.createTextNode('Phone'));
var phoneField = addInput(td,'text','','','phoneToAdd');
// new cell
td = createAppendedChildToParent('td',tr);
td.appendChild(document.createTextNode('Facebook'));
var facebookField = addInput(td,'text','','','facebookToAdd');
// new row
tr = createAppendedChildToParent('tr',input);
// new cell
td = createAppendedChildToParent('td',tr);
td.appendChild(document.createTextNode('Linked In'));
var linkedinField = addInput(td,'text','','','linkedinToAdd');
// new cell
td = createAppendedChildToParent('td',tr);
td.appendChild(document.createTextNode('Github'));
var githubField = addInput(td,'text','','','githubToAdd');
// new cell
td = createAppendedChildToParent('td',tr);
// add button
var addButton = addInput(td,'button','','Add Contact','addContactButton');
// wire button
addButton.onclick = insertContact.bind(this);
// insert empty results table
var table = createAppendedChildToParent('table',contacts);
this.contacts.table = table;
table.id = 'tableOfContacts';
table.className = 'io';
// fill results table
getStuff(this.contacts);
}
/*
Empty the tables of blog and rebuild them.
*/
glo.prototype.setupBlog =
function setupBlog()
{
var blog = $("#blog")[0]; // container div
emptyElement(blog);
// add text fields
// blog.appendChild(document.createTextNode('Blog'));
var blogField = addInput(blog,'text','','','BlogToAdd');
// add button
var addButton = addInput(blog,'button','','Add Blog','addBlogButton');
// wire button
addButton.onclick = insertBlog.bind(this);
// insert empty results table
var table = createAppendedChildToParent('table',blog);
this.blog.table = table;
table.id = 'tableOfBlog';
table.className = 'io';
// fill results table
getStuff(this.blog);
}
/*
This function should remove an element from the database.
*/
function remover(e)
{
// e is an event
// object is bound to the button as "data" property
console.log(this.data);
var object = this.data;
// console.log("removing row");
// console.log(e);
// console.log(this);
console.log("this.sid:");
console.log(this.sid);
var link = this.sid;
console.log("object.destroyer:");
console.log(object.destroyer);
// create a new div for validation
$("<div title='Remove Item?'><p>Are you sure?</p></div>").dialog
(
{
modal:true,
buttons:
{
Cancel: function()
{
$(this).dialog("close")
},
"Remove Item":function()
{
$(this).dialog("close");
$.ajax
(
{
url: "butler.php",
type: "post",
dataType: "text",
data:
{
user: object.parent.user.name,
pass: object.parent.user.password,
func: object.destroyer,
url: link
// company: comp,
// source: src
},
success: function(resp)
{
console.log(JSON.parse(resp));
//
if (JSON.parse(resp) === true)
{
console.log("removal worked");
// displayTable(object,[]);
getStuff(object);
}
else
{
console.log("removal failed");
getStuff(object);
}
}
}
);
}
}
}
);
}
/*
A middle man to get things from the butler.
*/
function getStuff(object)
{
console.log("getting for ");
console.log(object);
// console.log(callback);
var getter = object.get;
// var callback = object.display;
var callback = object.filler;
$.ajax
(
{
async: true,
url: "butler.php",
type: "post",
//dataType: "text",
dataType: "json",
data:
{
// getter: true
user: object.parent.user.name,
pass: object.parent.user.password,
func: getter
},
success: function(resp)
{
console.log("got stuff!");
console.log(resp);
//console.log(object);
//callback(object, JSON.parse(resp));
callback(object, resp);
}
}
)
}
/*
A generalized callback function to display tables.
*/
function displayTable(object)
{
// console.log("displaying table");
var table = object.table;
emptyElement(table);
var tr = createAppendedChildToParent('tr',table);
console.log("from displayTable");
console.log(object);
// console.log("input");
// console.log(input);
// add headers (for table)
for (var i = 0; i < object.displayKeys.length; i++)
{
var th = createAppendedChildToParent('th',tr);
var content = document.createTextNode(object.displayKeys[i]);
th.appendChild(content);
}
// insert into DOM elements stored in object
for (var i = 0; i < object.contents.length; i++)
{
tr = createAppendedChildToParent('tr',table);
var contents = object.contents[i];
for (var key in contents)
{
if (isInArray(key, object.displayKeys))
{
var td = createAppendedChildToParent('td',tr);
// http://stackoverflow.com/questions/10888198/how-do-i-get-html-tags-inside-of-a-javascript-text-node
// the following does not work
// if element is a link
// --------------------------
/*
var content = document.createTextNode(contents[key]);
td.appendChild(content);
*/
// there's probably a way around using innerHTML
// but this is quick and dirty for now
td.innerHTML = contents[key];
// next cell
}
}
td = createAppendedChildToParent('td',tr);
var button = createAppendedChildToParent('input',td);
button.type = "button"; button.value = "remove";
button.data = object; button.sid = object.contents[i].sid;
button.onclick = object.removeFunction;
var button = createAppendedChildToParent('input',td);
button.type = "button"; button.value = "edit";
button.data = object; button.sid = object.contents[i].sid;
button.onclick = object.editFunction;
}
}