Skip to content

Commit

Permalink
Search answer keys and when user types input
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitri committed Apr 20, 2017
1 parent 8c34910 commit e74cf7c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
4 changes: 3 additions & 1 deletion component/Bubbles.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ function Bubbles(container, self, options) {
widerBy = options.widerBy || 2; // add a little extra width to bubbles to make sure they don't break
sidePadding = options.sidePadding || 6; // padding on both sides of chat bubbles
inputCallbackFn = options.inputCallbackFn || (function(o){ console.log(o); }); // should we display an input field?

var standingAnswer = "ice"; // remember where to restart convo if interrupted

// set up the stage
container.classList.add("bubble-container");
Expand Down Expand Up @@ -56,6 +58,7 @@ function Bubbles(container, self, options) {
this.talk = function(convo, here) {
this.convo = convo;
this.reply(this.convo[here]);
here ? standingAnswer = here : false;
}
this.reply = function(turn) {
turn = typeof turn !== "undefined" ? turn : this.convo.ice;
Expand All @@ -78,7 +81,6 @@ function Bubbles(container, self, options) {
}

// navigate "answers"
var standingAnswer = "ice";
this.answer = function(key){
var func = function(key){ typeof window[key] === "function" ? window[key]() : false; }
this.convo[key] !== undefined ? (this.reply(this.convo[key]), standingAnswer = key) : func(key);
Expand Down
43 changes: 33 additions & 10 deletions component/start-here/hello.html
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,39 @@
<script>
var givemeBubbles = new Bubbles(document.getElementById("chat"), "givemeBubbles", {
inputCallbackFn: function(o){
//console.log(o.convo[o.standingAnswer].reply);
givemeBubbles.talk({
"ice": {
"says":
[
"WTF are you talking about?",
],
"reply" : o.convo[o.standingAnswer].reply
},
})

console.log(o);

// do this if no answer matched
var miss = function(){
givemeBubbles.talk({
"ice": {
"says":
[
"WTF are you talking about?",
],
"reply" : o.convo[o.standingAnswer].reply
},
})
}

// do this if answer found
var match = function(key){
console.log(key);
givemeBubbles.talk(convo, key); // restart current convo from point found in the answer
}

// sanitize text for search function
var strip = function(text){
return text.toLowerCase().replace(/\ [.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
}

// search function
o.convo[o.standingAnswer].reply.forEach(function(e, i) {
(strip(e.question).includes(strip(o.input)) && o.input.length > 0) ? found = e.answer : found = false;
});
found ? match(found) : miss();

}
});
givemeBubbles.talk(convo);
Expand Down

0 comments on commit e74cf7c

Please sign in to comment.