Advertisement
Guest User

Untitled

a guest
Dec 29th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. var _ = require('lodash')
  2. var plastiq = require('plastiq')
  3. var h = plastiq.html;
  4. var bind = plastiq.bind;
  5.  
  6. var recognition = new webkitSpeechRecognition();
  7.  
  8. var model = {
  9. query: '',
  10. contacts: [
  11. {name: "Paul"},
  12. {name: "Levan"},
  13. {name: "Tim"},
  14. {name: "Artem"},
  15. {name: "Josh"},
  16. {name: "Adrain"},
  17. {name: "Andrew"},
  18. {name: "Dereck"},
  19. {name: "Orfi"},
  20. {name: "Hibri"}
  21. ],
  22. matchingContacts: function() {
  23. if(this.query == '') {
  24. return this.contacts
  25. }
  26. return _.where(this.contacts, function(c) {
  27. return c.name.toLowerCase().indexOf(model.query) > -1
  28. })
  29. }
  30. }
  31.  
  32. function voiceSearch() {
  33. return new Promise(function (result) {
  34. var recognition = new webkitSpeechRecognition();
  35. recognition.continuous = true;
  36. recognition.interimResults = true;
  37. recognition.lang = "en-GB";
  38. recognition.onresult = function(event) {
  39. var spokenWords = '';
  40. for (var i = event.resultIndex; i < event.results.length; ++i) {
  41. spokenWords += event.results[i][0].transcript;
  42. }
  43. model.query = spokenWords;
  44. result();
  45. }
  46. recognition.start();
  47. })
  48. }
  49.  
  50. function render(model) {
  51. return h('div',
  52. h('div',
  53. h('label', "search"), ' ',
  54. h('input', {type: 'text', value: model.query}),
  55. h('button', {onclick: voiceSearch}, "Search by voice")
  56. ),
  57. h('ul',
  58. model.matchingContacts().map(function(c) {
  59. return h('li', c.name)
  60. })
  61. )
  62. );
  63. }
  64.  
  65. plastiq.attach(document.body, render, model);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement