Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: cdent/tsapp-timetrack
base: 4412f70988
...
head fork: cdent/tsapp-timetrack
compare: b1d257f5b5
  • 4 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 83 additions and 14 deletions.
  1. +80 −14 assets/timer.js
  2. +3 −0  timer.html
View
94 assets/timer.js
@@ -14,17 +14,37 @@ $(function() {
"use strict";
- var timer = {
- // where we keep the time info
- data: {}
- };
+ /*
+ * Some locally useful variables.
+ */
+ var spaceName = tiddlyweb.status.space.name,
+ privateBag = spaceName + '_private',
+ publicBag = spaceName + '_public',
+ /*
+ * Our namespace and data holder.
+ *
+ * data keeps start and stop information for each category:
+ * timer.data[cat].start => list of times
+ * timer.data[cat].stop => list of times
+ */
+ timer = {
+ data: {}
+ };
// DOM event handlers
$.extend(timer, {
+ /*
+ * Do nothing with the event.
+ */
noOp: function() {
return false;
},
+ /*
+ * Add a new timer tag/category via the input
+ * text box. When it changes, we update the
+ * select dropdown and switch the current category.
+ */
updateTimeTag: function(ev) {
var newTag = $(this).val(),
option = $('<option>')
@@ -36,6 +56,10 @@ $(function() {
timer.refreshDisplay(ev, newTag);
},
+ /*
+ * Select a category from the select, setting the
+ * current category and updating the display.
+ */
selectTimeTag: function(ev) {
var selected = $(this).val();
localStorage.setItem('timer.currentCat', selected);
@@ -44,6 +68,9 @@ $(function() {
});
+ /*
+ * A bad error handler, waiting for some goodness.
+ */
timer.errorHandler = function(xhr, status, error) {
console.log(xhr, status, error);
};
@@ -66,7 +93,8 @@ $(function() {
};
/*
- * Update the list display of total info.
+ * Update the list display of total info for each
+ * category.
*/
timer.updateTotals = function() {
var cats = Object.keys(timer.data);
@@ -80,13 +108,19 @@ $(function() {
};
/*
- * Show the results for the current category in the button.
+ * Show the results for the current category in the button,
+ * when we switch to it.
*/
timer.changeCurrent = function(cat) {
$('#action').text(timer.timeString(timer.data[cat].total))
.attr('class', (timer.data[cat].on ? 'on' : 'off'));
};
+ /*
+ * Manage the main display. If we have just switched to a new
+ * category we just changeCurrent, but if we have refrshed all
+ * the data, then recalculate totals and display.
+ */
timer.refreshDisplay = function(ev, data) {
$('#action').attr('disabled', false);
@@ -100,6 +134,10 @@ $(function() {
return timer.changeCurrent(data);
}
+ /*
+ * Set the options in the select dropdown to the
+ * available categories.
+ */
function setSelect(cats) {
var selector = $('select[name="timetag"]');
selector.empty();
@@ -112,7 +150,11 @@ $(function() {
}
- // gather data from tiddlers
+ /*
+ * Treat the incoming data as tiddlers, parse for
+ * categories and stop and start status, push into
+ * timer.data[cat].{stop,start}
+ */
$.each(data, function(index, tiddler) {
var tags = tiddler.tags;
// array to hash
@@ -130,6 +172,10 @@ $(function() {
}
});
});
+
+ /*
+ * Update the display with calculated info.
+ */
var cats = Object.keys(timer.data),
currentCat = localStorage.getItem('timer.currentCat') || 'test';
setSelect(cats);
@@ -144,10 +190,13 @@ $(function() {
timer.changeCurrent(currentCat);
};
- // get all the current time info
- // XXX: destaticify
+ /*
+ * Load the timer information from the bags. Announce when
+ * we have it.
+ */
timer.loadTags = function() {
- var searchURI = '/search.json?q=bag:timer_public%20tag:"timer:*"';
+ var searchURI = '/search.json?q=(bag:' + privateBag + '%20OR%20bag:'
+ + publicBag + ')%20tag:"timer:*"';
$('#action').text('loading...').attr('disabled', true);
$.ajax({
url: searchURI,
@@ -158,15 +207,23 @@ $(function() {
});
};
+ function tiddlerURI(cat, title) {
+ var bag = cat.match(/\.p$/) ? privateBag : publicBag;
+ return '/bags/' + encodeURIComponent(bag) + '/tiddlers/'
+ + encodeURIComponent(title);
+ }
+
/*
- * Create a new tiddler either starting of stop in the current tag.
+ * Create a new tiddler either starting or stopping in the current tag.
+ * This is in response to the big button being clicked. When the
+ * tiddler has been pushed, refresh the data and recalculate.
*/
timer.startStop = function(ev) {
var cat = $('select[name="timetag"]').val(),
timestamp = Math.round(new Date().getTime() / 1000),
annotation = $('textarea').val(),
tag,
- uri = '/bags/timer_public/tiddlers/' + timestamp,
+ uri = tiddlerURI(cat, timestamp),
tiddler;
if (timer.data[cat].on) {
@@ -223,7 +280,7 @@ $(function() {
function getCallback(data) {
var title = data.title,
tag = data.tags[0].replace(/timer:/, 'timer-' + timestamp + ':'),
- uri = '/bags/timer_public/tiddlers/' + title;
+ uri = tiddlerURI(category, title);
data.tags = [tag];
$.ajax({
@@ -244,7 +301,7 @@ $(function() {
}
$.each(tiddlerTitles, function(index, title) {
- var uri = '/bags/timer_public/tiddlers/' + title + '.json';
+ var uri = tiddlerURI(category, title) + '.json';
$.ajax({
url: uri,
success: getCallback,
@@ -271,6 +328,15 @@ $(function() {
* Define a private collection of functions for performing
* the time keeping calculations. Export the calculateTime
* function to the context (in this case the timer object).
+ *
+ * Essentially what these functions do is order the starts
+ * and stops and then remove any repeats of the same type
+ * such that the resulting list is start, stop, start, stop
+ * etc, against which is it is then possible to do some sums.
+ *
+ * This wouldn't be needed if there was greater constraint
+ * on the creation of the data, but the idea here is that
+ * multiple inputs might be creating timer tiddlers.
*/
(function(context) {
"use strict";
View
3  timer.html
@@ -17,6 +17,8 @@
<select name="timetag">
</select>
<input name="newtimetag" placeholder="new category">
+ <span class="info">Append '.p' to a category to make it
+ private.</span>
<input id="resettag" name="resettag" type="reset">
</form>
<button id="action">loading...</button>
@@ -27,6 +29,7 @@
<ul id="totals">
</ul>
<script src="/bags/common/tiddlers/jquery.js"></script>
+ <script src="/status.js"></script>
<script src="/bags/timer_public/tiddlers/timer.js"></script>
</body>
</html>

No commit comments for this range

Something went wrong with that request. Please try again.