const PROJECT_OBJECT = "div"; const PROJECT_TITLE = "h1"; const PROJECT_META = "p"; let currentRecord = undefined; let currentProject = undefined; let projectList = undefined; let recordTags = undefined; function loadProjectList() { return api.getProjects().then((projects) => { projectList = Object.values(projects); var container = u(u("#project-list").first()); container.children().remove(); Object.values(projects).forEach((project) => { var obj = createNode(PROJECT_OBJECT); var data = undefined; data = createNode(PROJECT_TITLE); append(obj, data); u(data).text(project["name"]); var duration = Number((parseFloat(project["duration"]) / 60 / 60).toFixed(2)); var unit = "hours"; data = createNode(PROJECT_META); append(obj, data); u(data).text(duration + " " + unit); data = createNode(PROJECT_META); append(obj, data); u(data).text(project["record_count"] + " records"); obj = u(obj); obj.data("project-id", project["project_id"]); obj.on("click", projectClicked); container.append(obj); }); }); } function loadRecordTags() { return api.getRecordTags().then((tags) => { projectList = Object.values(projects); var container = u(u("#project-list").first()); container.children().remove(); Object.values(projects).forEach((project) => { var obj = createNode(PROJECT_OBJECT); var data = undefined; data = createNode(PROJECT_TITLE); append(obj, data); u(data).text(project["name"]); var duration = Number((parseFloat(project["duration"]) / 60 / 60).toFixed(2)); var unit = "hours"; data = createNode(PROJECT_META); append(obj, data); u(data).text(duration + " " + unit); data = createNode(PROJECT_META); append(obj, data); u(data).text(project["record_count"] + " records"); obj = u(obj); obj.data("project-id", project["project_id"]); obj.on("click", projectClicked); container.append(obj); }); }); } // Created new DOM object // element: Type of DOM object (div, p, ...) function createNode(element) { return document.createElement(element); } // Appends child to parent // parent: DOM to append child to // el: DOM child to append to parent function append(parent, el) { return parent.appendChild(el); } function projectClicked(event) { var projectObject = event.target; if (projectObject.tagName !== "DIV") { projectObject = projectObject.parentElement; } var projectId = u(projectObject).data("project-id"); return api.startRecord(projectId).then((record) => { currentRecord = record; currentProject = getProjectById(record.project_id); setState(States.RECORDING); }); } function getProjectById(id) { for (let i = 0; i < projectList.length; i++) { const element = projectList[i]; if (element.project_id == id) { return element; } } return undefined; } function updateCurrentRecord() { if (currentRecord === undefined) { return; } return api.getRecord(currentRecord.record_id).then((record) => { currentRecord = record; currentProject = getProjectById(record.project_id); return record; }); } function stopRecord() { if (currentRecord === undefined) { return; } return api.endRecord(currentRecord.record_id).then(() => { setState(States.IDLE); currentRecord = undefined; }); } function initRunningRecords() { return api.getRunningRecords().then((records) => { if (records === undefined || records.length <= 0) { currentRecord = undefined; setState(States.IDLE); return; } // Assuming, there is only one currentRecord = records[0]; }); }