r1273 - in wit/js: components services
Author: jruchaud Date: 2015-04-27 15:36:35 +0000 (Mon, 27 Apr 2015) New Revision: 1273 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/1273 Log: Implement a simple history Modified: wit/js/components/ActionsBar.js wit/js/components/Tags.js wit/js/components/Time.js wit/js/services/TimerService.js wit/js/services/database.js Modified: wit/js/components/ActionsBar.js =================================================================== --- wit/js/components/ActionsBar.js 2015-04-27 15:16:30 UTC (rev 1272) +++ wit/js/components/ActionsBar.js 2015-04-27 15:36:35 UTC (rev 1273) @@ -10,10 +10,23 @@ timer.stop(true); }, + prevHistory: function() { + timer.stop(); + timer.prevHistory(); + }, + + nextHistory: function() { + timer.stop(); + timer.nextHistory(); + }, + render: function() { return ( <div className="text-center"> <div className="btn-group" role="group"> + <button type="button" className="btn btn-primary btn-lg" onClick={this.prevHistory}> + <span className="glyphicon glyphicon-step-backward" aria-hidden="true"></span> + </button> <button type="button" className="btn btn-primary btn-lg"> <span className="glyphicon glyphicon-backward" aria-hidden="true"></span> </button> @@ -26,6 +39,9 @@ <button type="button" className="btn btn-primary btn-lg"> <span className="glyphicon glyphicon-forward" aria-hidden="true"></span> </button> + <button type="button" className="btn btn-primary btn-lg" onClick={this.nextHistory}> + <span className="glyphicon glyphicon-step-forward" aria-hidden="true"></span> + </button> </div> </div> ); Modified: wit/js/components/Tags.js =================================================================== --- wit/js/components/Tags.js 2015-04-27 15:16:30 UTC (rev 1272) +++ wit/js/components/Tags.js 2015-04-27 15:36:35 UTC (rev 1273) @@ -6,16 +6,24 @@ return timer; }, + componentDidMount: function() { + this.forceUpdate_bound = this.forceUpdate.bind(this); + timer.subscribeTagsChanged(this.forceUpdate_bound); + }, + + componentWillUnmount: function() { + timer.unsubscribeTagsChanged(this.forceUpdate_bound); + }, + onKey: function(e) { var target = e.target; var value = target.value; if (e.keyCode == 13 && value != "") { // On enter target.value = ""; - + + timer.stop(); timer.addTag(value); - timer.stop(); - this.forceUpdate(); } }, @@ -23,9 +31,8 @@ var target = e.target; var index = target.dataset.index; + timer.stop(); timer.removeTag(index); - timer.stop(); - this.forceUpdate(); }, render: function() { Modified: wit/js/components/Time.js =================================================================== --- wit/js/components/Time.js 2015-04-27 15:16:30 UTC (rev 1272) +++ wit/js/components/Time.js 2015-04-27 15:36:35 UTC (rev 1273) @@ -7,9 +7,14 @@ }, componentDidMount: function() { - timer.subscribeTimeChanged(this.forceUpdate.bind(this)); + this.forceUpdate_bound = this.forceUpdate.bind(this); + timer.subscribeTimeChanged(this.forceUpdate_bound); }, + componentWillUnmount: function() { + timer.unsubscribeTimeChanged(this.forceUpdate_bound); + }, + render: function() { return ( <h1 className="digit"> Modified: wit/js/services/TimerService.js =================================================================== --- wit/js/services/TimerService.js 2015-04-27 15:16:30 UTC (rev 1272) +++ wit/js/services/TimerService.js 2015-04-27 15:36:35 UTC (rev 1273) @@ -11,6 +11,8 @@ var time = null; var idleTime = null; +var historyIndex = -1; + var updateUserIdleTime = function() { x11.createClient(function(err, display) { var X = display.client; @@ -31,6 +33,8 @@ exports.start = function() { time = idleTime || new Date(); idleTime = null; + historyIndex = -1; + timeChange(); }; @@ -63,10 +67,12 @@ exports.addTag = function(tag) { tags.push(tag); + eventEmitter.emit('onTagsChanged'); }; exports.removeTag = function(index) { tags.splice(index, 1); + eventEmitter.emit('onTagsChanged'); }; var timeChangeTimer = null; @@ -82,6 +88,10 @@ } }; +exports.subscribeTagsChanged = function(callback) { + eventEmitter.on('onTagsChanged', callback); +}; + exports.subscribeTimeChanged = function(callback) { eventEmitter.on('onTimeChanged', callback); }; @@ -90,6 +100,18 @@ eventEmitter.on('onIdle', callback); }; +exports.unsubscribeTagsChanged = function(callback) { + eventEmitter.removeListener('onTagsChanged', callback); +}; + +exports.unsubscribeTimeChanged = function(callback) { + eventEmitter.removeListener('onTimeChanged', callback); +}; + +exports.unsubscribeIdle = function(callback) { + eventEmitter.removeListener('onIdle', callback); +}; + exports.keepIdle = function() { idleTime = null; }; @@ -110,3 +132,34 @@ clearTimeout(timeChangeTimer); eventEmitter.emit('onTimeChanged'); }; + +var setTagsFromHistory = function() { + + db.getHistory(historyIndex) + .then(function(logs) { + + if (logs && logs.length) { + var log = logs[0]; + tags = log.tags.slice(0); + } else { + tags = []; + } + eventEmitter.emit('onTagsChanged'); + }); +}; + +exports.prevHistory = function() { + historyIndex++; + + setTagsFromHistory(); +}; + +exports.nextHistory = function() { + if (historyIndex == -1) { + return; + } + + historyIndex--; + + setTagsFromHistory(); +}; Modified: wit/js/services/database.js =================================================================== --- wit/js/services/database.js 2015-04-27 15:16:30 UTC (rev 1272) +++ wit/js/services/database.js 2015-04-27 15:36:35 UTC (rev 1273) @@ -3,7 +3,7 @@ var Datastore = require('nedb'); var db = new Datastore({filename : path, autoload: true}); -exports.insertLog = function (tags, startDate, endDate) { +exports.insertLog = function(tags, startDate, endDate) { return new Promise(function(resolve, reject) { db.insert({ tags: tags, @@ -19,7 +19,7 @@ }); }; -exports.searchLogs = function (tags, startDate, endDate) { +exports.searchLogs = function(tags, startDate, endDate) { var query = {}; if (tags) { query.tags = { $in: tags }; } @@ -27,7 +27,7 @@ if (endDate) { query.endDate = { $lte: endDate.getTime() } } return new Promise(function(resolve, reject) { - db.find(query, function(err, docs) { + db.find(query).sort({ startDate: -1 }).exec(function(err, docs) { if (!err) { resolve(docs); } else { @@ -37,4 +37,15 @@ }); }; - +exports.getHistory = function(index) { + + return new Promise(function(resolve, reject) { + db.find({}).sort({ startDate: -1 }).skip(index).limit(1).exec(function (err, docs) { + if (!err) { + resolve(docs); + } else { + reject(err); + } + }); + }); +};
participants (1)
-
jruchaud@users.nuiton.org