Skip to content

Instantly share code, notes, and snippets.

@jhoughjr
Created October 4, 2018 17:21
Show Gist options
  • Save jhoughjr/ca67528d4a9348426844a71949674fde to your computer and use it in GitHub Desktop.
Save jhoughjr/ca67528d4a9348426844a71949674fde to your computer and use it in GitHub Desktop.
fuckmylife
Parse.serverURL = 'http://178.128.153.185:1337/parse'
Parse.initialize("tracktivityserver");
function HighLightTR(el, backColor,textColor){
if(typeof(preEl)!='undefined') {
preEl.bgColor=orgBColor;
try{ChangeTextColor(preEl,orgTColor);}catch(e){;}
}
orgBColor = el.bgColor;
orgTColor = el.style.color;
el.bgColor=backColor;
try{ChangeTextColor(el,textColor);}catch(e){;}
preEl = el;
}
function ChangeTextColor(a_obj,a_color){ ;
for (i=0;i<a_obj.cells.length;i++)
a_obj.cells(i).style.color=a_color;
}
var handleThemeChange = function() {
var themePicker = document.getElementById("theme-selector");
var htmlTag = document.getElementsByTagName("html")[0];
htmlTag.setAttribute("data-theme",themePicker.value);
}
var buildMyTeams = async function() {
console.log("building")
var Teams = Parse.Object.extend("team");
var query = new Parse.Query(Teams);
query.include("owner")
query.equalTo("owner", Parse.User.current());
query.find()
.then((teams) => {
var table = await buildTeamsTable(teams);
var teamsDiv = document.getElementById("myteams");
table.setAttribute("id","myteams");
teamsDiv.parentNode.replaceChild(table,teamsDiv);
})
}
var buildTeamsTable = async function(teams) {
var _table_ = document.createElement('table'),
_th_ = document.createElement('th'),
_tr_ = document.createElement('tr'),
_tbody_ = document.createElement("tbody");
var table = _table_.cloneNode(false);
var hdr = _tr_.cloneNode(false);
var tb = _tbody_.cloneNode(false);
var teamName = _th_.cloneNode(false);
var owner = _th_.cloneNode(false);
teamName.textContent = "Team"
owner.textContent = "Owner";
hdr.appendChild(teamName);
hdr.appendChild(owner);
table.appendChild(hdr);
for (var i=0, maxi=teams.length; i < maxi; ++i) {
var row = await rowForTeam(teams[i]);
tb.appendChild(row);
}
table.appendChild(tb);
return table;
}
var buildMembersTable = async function(arr) {
var _table_ = document.createElement('table'),
_th_ = document.createElement('th'),
_tr_ = document.createElement('tr'),
_tbody_ = document.createElement("tbody");
var table = _table_.cloneNode(false);
var hdr = _tr_.cloneNode(false);
var tb = _tbody_.cloneNode(false);
var teamName = _th_.cloneNode(false);
var owner = _th_.cloneNode(false);
teamName.textContent = "Team"
owner.textContent = "Owner";
hdr.appendChild(teamName);
hdr.appendChild(owner);
table.appendChild(hdr);
for (var i=0, maxi=arr.length; i < maxi; ++i) {
tb.appendChild( rowForMember(arr[i]) );
}
table.appendChild(tb);
return table;
}
async function rowForTeam(t) {
var _tr_ = document.createElement('tr'),
_td_ = document.createElement('td')
var tr = _tr_.cloneNode(false)
var owner = _td_.cloneNode(false);
var name = _td_.cloneNode(false);
name.textContent = t.get("name");
name.setAttribute("id","name")
var o = await t.relation("owner").query().find()
tr.appendChild(name);
tr.appendChild(owner);
tr.addEventListener("click", function () {
var ssid = this.cells.namedItem("name").textContent;
selectTeamName(ssid);
HighLightTR(this, '#00000000' ,'#ffffffa8');
}, false);
return tr;
}
var selectTeamName = function(name) {
var members = getMembersForTeam(name)
var table = buildTableOfMembers(members)
console.log(table)
}
var createTeam = function() {
var name = document.getElementById("teamname").value
var Team = Parse.Object.extend("team")
console.log("creating team " + name)
var t = new Team()
t.set("name", name)
var o = t.relation("owner")
o.add(Parse.User.current())
t.save().then((team) => {
// The object was saved successfully.
console.log("team created " + team)
}, (error) => {
// The save failed.
// error is a Parse.Error with an error code and message.
alert("Error: " + error.code + " " + error.message);
});
}
// MEMBERS
var getMembersForTeam = function(teamname) {
var Teams = Parse.Object.extend("team");
var query = new Parse.Query(Teams);
query.include("owner")
query.equalTo("owner", Parse.User.current());
query.find()
.then((teams) => {
return teams[0].relation("members").query().find()
})
}
var buildTableOfMembers = function(members) {
var _table_ = document.createElement('table'),
_th_ = document.createElement('th'),
_tr_ = document.createElement('tr'),
_tbody_ = document.createElement("tbody");
var table = _table_.cloneNode(false);
var hdr = _tr_.cloneNode(false);
var tb = _tbody_.cloneNode(false);
var teamName = _th_.cloneNode(false);
var owner = _th_.cloneNode(false);
teamName.textContent = "Team"
owner.textContent = "Owner";
hdr.appendChild(teamName);
hdr.appendChild(owner);
table.appendChild(hdr);
for (var i=0, maxi=members.length; i < maxi; ++i) {
tb.appendChild( rowForMember(members[i]) );
}
table.appendChild(tb);
return table;
}
var rowForMember = function(member) {
var _tr_ = document.createElement('tr'),
_td_ = document.createElement('td')
var tr = _tr_.cloneNode(false)
var owner = _td_.cloneNode(false);
var name = _td_.cloneNode(false);
name.textContent = member.get("username");
name.setAttribute("id","name")
var o = member.relation("owner")
tr.appendChild(name);
// tr.addEventListener("click", function () {
// var ssid = this.cells.namedItem("name").textContent;
// selectTeamName(ssid);
// HighLightTR(this, '#00000000' ,'#ffffffa8');
// }, false);
return tr;
}
window.onload = function() {
buildMyTeams()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment