Skip to content

Instantly share code, notes, and snippets.

@m-x-k
Last active April 8, 2018 01:22
Show Gist options
  • Save m-x-k/3ecf588452c691684fbbad8a27382979 to your computer and use it in GitHub Desktop.
Save m-x-k/3ecf588452c691684fbbad8a27382979 to your computer and use it in GitHub Desktop.
ChatBot with reveal design pattern
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="keywords" content="design patterns">
<title>Mastering Javascript</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="revealWithGlobalAccessDesignPattern.js"></script>
</head>
<body>
<h1>ChatBot</h1>
<h3>[Reveal Design Pattern with Global Access]</h3>
<div class="container">
<table class="advert table table-hover">
</table>
<span id="talk">
</span>
</div>
</body>
</html>
(function(win, doc, $) {
var chatModule = (function() {
var _leadSelf = '<strong>ME:</strong> ',
_leadComputer = '<strong>PC:</strong> ',
_leadComputerQuote = '<strong>QUOTE:</strong> ',
_aSaid = ["This is a Cyber Chat"],
_msgYes = "Yes, that's a great idea."
_msgNo = "No, that must be a mistake."
_aQuote = ["Equations are the devil's sentences.",
"Be yourself; everyone else is already taken.",
"Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.",
"I have not failed. I've just found 10,000 ways that won't work."
]
function _echo(msg, style) {
_aSaid.push("<tr class=\"" + style + "\"><td>" + msg + "</td></tr>");
var aSaidLength = _aSaid.length,
start = Math.max(aSaidLength - 6, 0),
out = "";
for (var i=start; i<aSaidLength; i++) {
out += _aSaid[i];
}
$('.advert').html(out);
$('#talk span').text(msg);
}
function talk(msg) {
_echo(_leadSelf + msg, "success");
}
function replayYesNo() {
var msg = Math.random()>.5 ? _msgYes : _msgNo;
_echo(_leadComputer + msg, "warning");
}
function sayQuote() {
var msg = _aQuote[Math.floor(Math.random()* _aQuote.length)];
_echo("<small>" + _leadComputerQuote + msg + "</small>", "info");
}
return {
talk: talk,
replayYesNo: replayYesNo,
sayQuote: sayQuote
};
})();
$(doc).ready(function() {
chatModule.talk("this is great");
chatModule.replayYesNo();
chatModule.sayQuote();
});
if (!win.chatModule) win.chatModule = chatModule;
})(window, document, jQuery);
console.log(window.chatModule);
// You can test with Developer Tools: >>chatModule.talk("Hello World")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment