Author: jruchaud Date: 2015-04-28 12:55:14 +0000 (Tue, 28 Apr 2015) New Revision: 1277 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/1277 Log: Add completion in input tag Modified: wit/index.html wit/js/components/InputTag.js wit/js/components/Tags.js wit/js/services/TimerService.js wit/js/services/database.js Modified: wit/index.html =================================================================== --- wit/index.html 2015-04-28 09:20:20 UTC (rev 1276) +++ wit/index.html 2015-04-28 12:55:14 UTC (rev 1277) @@ -6,6 +6,7 @@ <script type="text/javascript" src="node_modules/react/dist/react-with-addons.js"></script> <script type="text/javascript" src="node_modules/react/dist/JSXTransformer.js"></script> + <script type="text/jsx" src="js/components/InputTag.js"></script> <script type="text/jsx" src="js/components/Tags.js"></script> <script type="text/jsx" src="js/components/Time.js"></script> <script type="text/jsx" src="js/components/ActionsBar.js"></script> Modified: wit/js/components/InputTag.js =================================================================== --- wit/js/components/InputTag.js 2015-04-28 09:20:20 UTC (rev 1276) +++ wit/js/components/InputTag.js 2015-04-28 12:55:14 UTC (rev 1277) @@ -0,0 +1,63 @@ +/** @jsx React.DOM */ + +var InputTag = React.createClass({ + + getInitialState: function() { + return {options: [], value: ""}; + }, + + getTags: function() { + var self = this; + timer.getLastTags().then(function(tags) { + self.setState({options: tags, value: ""}); + }); + }, + + onKey: function(e) { + var target = e.target; + var value = target.value; + + if (e.keyCode == 13 && value != "") { // On enter + this.setState({options: this.state.options, value: ""}); + + timer.stop(); + timer.addTag(value); + } + }, + + onChange: function(e) { + this.setState({options: this.state.options, value: e.target.value}); + }, + + render: function() { + var options = []; + + if (this.state.value) { + + for (var i = 0, l = this.state.options.length; i < l; i++) { + var item = this.state.options[i]; + + if (options.length < 3 && item.indexOf(this.state.value) == 0) { + var element = <option>{item}</option>; + options.push(element); + } + } + } + + return ( + <div> + <input id="search"className="form-control" + onKeyDown={this.onKey} onFocus={this.getTags} + onChange={this.onChange} placeholder="enter tag" + tabIndex="-1" autoComplete="off" autoCorrect="off" value={this.state.value} + autoCapitalize="off" spellCheck="false" role="textbox" + type="text" list="searchresults" /> + + <datalist id="searchresults"> + {options} + </datalist> + </div> + ); + } + +}); Modified: wit/js/components/Tags.js =================================================================== --- wit/js/components/Tags.js 2015-04-28 09:20:20 UTC (rev 1276) +++ wit/js/components/Tags.js 2015-04-28 12:55:14 UTC (rev 1277) @@ -15,18 +15,6 @@ 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); - } - }, - onRemove: function(e) { var target = e.target; var index = target.dataset.index; @@ -59,7 +47,7 @@ {rows} <li className="list-group-item"> - <input className="form-control" onKeyDown={this.onKey} placeholder="enter tag" tabIndex="-1" autoComplete="off" autoCorrect="off" autoCapitalize="off" spellCheck="false" role="textbox" type="text"></input> + <InputTag /> </li> </ul> ); Modified: wit/js/services/TimerService.js =================================================================== --- wit/js/services/TimerService.js 2015-04-28 09:20:20 UTC (rev 1276) +++ wit/js/services/TimerService.js 2015-04-28 12:55:14 UTC (rev 1277) @@ -165,3 +165,7 @@ setTagsFromHistory(); }; + +exports.getLastTags = function() { + return db.getLastTags(); +}; Modified: wit/js/services/database.js =================================================================== --- wit/js/services/database.js 2015-04-28 09:20:20 UTC (rev 1276) +++ wit/js/services/database.js 2015-04-28 12:55:14 UTC (rev 1277) @@ -57,6 +57,26 @@ }); }; +exports.getLastTags = function() { + + return new Promise(function(resolve, reject) { + db.find({}).sort({startDate: -1 }).limit(100).exec(function (err, docs) { + if (!err) { + var map = {}; + docs.forEach(function(log) { + log.tags.forEach(function(tag) { + map[tag] = true; + }); + }); + + resolve(Object.keys(map)); + } else { + reject(err); + } + }); + }); +}; + /******************************** * Window sessions db management ********************************/ @@ -103,4 +123,4 @@ } }); }); -}; \ No newline at end of file +};>>>>>>> .r1276