diff --git a/src/scripts/background.js b/src/scripts/background.js index 15a73f47e..067a356f6 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -224,6 +224,7 @@ var TogglButton = { } else if (data.action === "UPDATE" && (TogglButton.$curEntry === null || entry.id === TogglButton.$curEntry.id)) { if (entry.duration >= 0) { TogglButton.$latestStoppedEntry = entry; + TogglButton.updateEntriesDb(); entry = null; } TogglButton.$curEntry = entry; @@ -232,6 +233,25 @@ var TogglButton = { TogglButton.setBrowserAction(entry); }, + updateEntriesDb: function () { + var added = false; + if (!TogglButton.$user.time_entries) { + TogglButton.$user.time_entries = {}; + } else { + TogglButton.$user.time_entries.forEach(function (entry, index) { + if (entry.id === TogglButton.$latestStoppedEntry.id) { + TogglButton.$user.time_entries[index] = TogglButton.$latestStoppedEntry; + added = true; + } + }); + } + + // entry not present in entries array. Let's add it + if (!added) { + TogglButton.$user.time_entries.push(TogglButton.$latestStoppedEntry); + } + }, + findProjectByName: function (name) { var key; for (key in TogglButton.$user.projectMap) { @@ -370,6 +390,7 @@ var TogglButton = { onLoad: function (xhr) { if (xhr.status === 200) { TogglButton.$latestStoppedEntry = JSON.parse(xhr.responseText).data; + TogglButton.updateEntriesDb(); TogglButton.$nannyTimer = TogglButton.$curEntry = null; TogglButton.stopCheckingUserState(); TogglButton.setBrowserAction(null); @@ -377,7 +398,7 @@ var TogglButton = { sendResponse({success: true, type: "Stop"}); chrome.tabs.query({active: true, currentWindow: true}, function (tabs) { if (!!tabs[0]) { - chrome.tabs.sendMessage(tabs[0].id, {type: "stop-entry"}); + chrome.tabs.sendMessage(tabs[0].id, {type: "stop-entry", user: TogglButton.$user}); } }); } diff --git a/src/scripts/common.js b/src/scripts/common.js index cded84047..358d37a7c 100644 --- a/src/scripts/common.js +++ b/src/scripts/common.js @@ -138,13 +138,16 @@ var togglbutton = { return {left: left, top: top}; }, - calculateTrackedTime: function (description) { - var duration = 0; - togglbutton.entries.forEach(function (entry) { - if (entry.description.toLowerCase() === description.toLowerCase()) { - duration += entry.duration; - } - }); + calculateTrackedTime: function () { + var duration = 0, + description = togglbutton.mainDescription.toLowerCase(); + if (!!togglbutton.entries) { + togglbutton.entries.forEach(function (entry) { + if (entry.description.toLowerCase() === description) { + duration += entry.duration; + } + }); + } return secondsToTime(duration, togglbutton.duration_format); }, @@ -338,7 +341,7 @@ var togglbutton = { if (!link.classList.contains("min")) { link.innerHTML = 'Start timer'; } - chrome.extension.sendMessage({type: 'stop'}, togglbutton.addEditForm); + chrome.extension.sendMessage({type: 'stop', respond: true}, togglbutton.addEditForm); closeTagsList(true); editForm.style.display = "none"; return false; @@ -433,6 +436,10 @@ var togglbutton = { createTimerLink: function (params) { var link = createLink('toggl-button'); togglbutton.currentDescription = invokeIfFunction(params.description); + if (!!params.calculateTotal) { + togglbutton.mainDescription = invokeIfFunction(params.description); + } + function activate() { if (document.querySelector(".toggl-button.active")) { document.querySelector(".toggl-button.active").classList.remove('active'); @@ -468,6 +475,7 @@ var togglbutton = { deactivate(); opts = { type: 'stop', + respond: true, service: togglbutton.serviceName }; } else { @@ -531,9 +539,20 @@ var togglbutton = { link.innerHTML = linkText; }, + updateTrackedTimerLink: function () { + var totalTime = $(".toggl-tracked"), + duration = togglbutton.calculateTrackedTime(); + + if (!!totalTime) { + totalTime.innerHTML = "

Time tracked

" + duration + "

"; + } + }, + newMessage: function (request, sender, sendResponse) { if (request.type === 'stop-entry') { togglbutton.updateTimerLink(); + togglbutton.entries = request.user.time_entries; + togglbutton.updateTrackedTimerLink(); } else if (request.type === 'sync') { if ($("#toggl-button-edit-form") !== null) { $("#toggl-button-edit-form").remove(); diff --git a/src/scripts/content/trello.js b/src/scripts/content/trello.js index 5c9982bc0..2901fce3e 100644 --- a/src/scripts/content/trello.js +++ b/src/scripts/content/trello.js @@ -15,7 +15,8 @@ togglbutton.render('.window-header:not(.toggl)', {observe: true}, function (elem link = togglbutton.createTimerLink({ className: 'trello', description: titleElem.innerText, - projectName: projectElem.innerText + projectName: projectElem.innerText, + calculateTotal: true }); container.appendChild(link);