Skip to content

Commit

Permalink
Update calculated time after entry stop, #414
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrekV committed Jan 12, 2016
1 parent 9fc2c12 commit bf49b3f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
23 changes: 22 additions & 1 deletion src/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -370,14 +390,15 @@ 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);
if (!!timeEntry.respond) {
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});
}
});
}
Expand Down
35 changes: 27 additions & 8 deletions src/scripts/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -468,6 +475,7 @@ var togglbutton = {
deactivate();
opts = {
type: 'stop',
respond: true,
service: togglbutton.serviceName
};
} else {
Expand Down Expand Up @@ -531,9 +539,20 @@ var togglbutton = {
link.innerHTML = linkText;
},

updateTrackedTimerLink: function () {
var totalTime = $(".toggl-tracked"),
duration = togglbutton.calculateTrackedTime();

if (!!totalTime) {
totalTime.innerHTML = "<h3>Time tracked</h3><p title='Time tracked with Toggl: " + duration + "'>" + duration + "</p>";
}
},

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();
Expand Down
3 changes: 2 additions & 1 deletion src/scripts/content/trello.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit bf49b3f

Please sign in to comment.