terminal.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. // ------------- JQUERY TYPED.JS -------------------------------------------- //
  2. // ============ https://github.com/mattboldt/typed.js/ ====================== //
  3. // -------------------------------------------------------------------------- //
  4. !function($){
  5. "use strict";
  6. var Typed = function(el, options){
  7. this.el = $(el);
  8. this.options = $.extend({}, $.fn.typed.defaults, options);
  9. this.baseText = this.el.text() || this.el.attr('placeholder') || '';
  10. this.typeSpeed = this.options.typeSpeed;
  11. this.startDelay = this.options.startDelay;
  12. this.backSpeed = this.options.backSpeed;
  13. this.backDelay = this.options.backDelay;
  14. this.strings = this.options.strings;
  15. this.strPos = 0;
  16. this.arrayPos = 0;
  17. this.stopNum = 0;
  18. this.loop = this.options.loop;
  19. this.loopCount = this.options.loopCount;
  20. this.curLoop = 0;
  21. this.stop = false;
  22. this.showCursor = this.isInput ? false : this.options.showCursor;
  23. this.cursorChar = this.options.cursorChar;
  24. this.isInput = this.el.is('input');
  25. this.attr = this.options.attr || (this.isInput ? 'placeholder' : null);
  26. this.build();
  27. };
  28. Typed.prototype = {
  29. constructor: Typed
  30. , init: function(){
  31. // begin the loop w/ first current string (global self.string)
  32. // current string will be passed as an argument each time after this
  33. var self = this;
  34. self.timeout = setTimeout(function() {
  35. // Start typing
  36. self.typewrite(self.strings[self.arrayPos], self.strPos);
  37. }, self.startDelay);
  38. }
  39. , build: function(){
  40. // Insert cursor
  41. if (this.showCursor === true){
  42. this.cursor = $("<span class=\"typed-cursor\">" + this.cursorChar + "</span>");
  43. this.el.after(this.cursor);
  44. }
  45. this.init();
  46. }
  47. , typewrite: function(curString, curStrPos){
  48. if(this.stop === true)
  49. return;
  50. var humanize = Math.round(Math.random() * (100 - 30)) + this.typeSpeed;
  51. var self = this;
  52. // ------------- optional ------------- //
  53. // backpaces a certain string faster
  54. // ------------------------------------ //
  55. // if (self.arrayPos == 1){
  56. // self.backDelay = 50;
  57. // }
  58. // else{ self.backDelay = 500; }
  59. // contain typing function in a timeout humanize'd delay
  60. self.timeout = setTimeout(function() {
  61. // check for an escape character before a pause value
  62. // format: \^\d+ .. eg: ^1000 .. should be able to print the ^ too using ^^
  63. // single ^ are removed from string
  64. var charPause = 0;
  65. var substr = curString.substr(curStrPos);
  66. if (substr.charAt(0) === '^') {
  67. var skip = 1; // skip atleast 1
  68. if(/^\^\d+/.test(substr)) {
  69. substr = /\d+/.exec(substr)[0];
  70. skip += substr.length;
  71. charPause = parseInt(substr);
  72. }
  73. // strip out the escape character and pause value so they're not printed
  74. curString = curString.substring(0,curStrPos)+curString.substring(curStrPos+skip);
  75. }
  76. // timeout for any pause after a character
  77. self.timeout = setTimeout(function() {
  78. if(curStrPos === curString.length) {
  79. // fires callback function
  80. self.options.onStringTyped(self.arrayPos);
  81. // is this the final string
  82. if(self.arrayPos === self.strings.length-1) {
  83. // animation that occurs on the last typed string
  84. self.options.callback();
  85. self.curLoop++;
  86. // quit if we wont loop back
  87. if(self.loop === false || self.curLoop === self.loopCount)
  88. return;
  89. }
  90. self.timeout = setTimeout(function(){
  91. self.backspace(curString, curStrPos);
  92. }, self.backDelay);
  93. } else {
  94. /* call before functions if applicable */
  95. if(curStrPos === 0)
  96. self.options.preStringTyped(self.arrayPos);
  97. // start typing each new char into existing string
  98. // curString: arg, self.baseText: original text inside element
  99. var nextString = self.baseText + curString.substr(0, curStrPos+1);
  100. if (self.attr) {
  101. self.el.attr(self.attr, nextString);
  102. } else {
  103. self.el.text(nextString);
  104. }
  105. // add characters one by one
  106. curStrPos++;
  107. // loop the function
  108. self.typewrite(curString, curStrPos);
  109. }
  110. // end of character pause
  111. }, charPause);
  112. // humanized value for typing
  113. }, humanize);
  114. }
  115. , backspace: function(curString, curStrPos){
  116. // exit when stopped
  117. if (this.stop === true) {
  118. return;
  119. }
  120. // varying values for setTimeout during typing
  121. // can't be global since number changes each time loop is executed
  122. var humanize = Math.round(Math.random() * (100 - 30)) + this.backSpeed;
  123. var self = this;
  124. self.timeout = setTimeout(function() {
  125. // ----- this part is optional ----- //
  126. // check string array position
  127. // on the first string, only delete one word
  128. // the stopNum actually represents the amount of chars to
  129. // ------------- CUSTOM OPTIONS --------------------------------------------- //
  130. // ========================================================================== //
  131. // -------------------------------------------------------------------------- //
  132. if (self.arrayPos == 1) {
  133. self.stopNum = 17;
  134. self.backDelay = 500;
  135. }
  136. else if (self.arrayPos == 2) {
  137. self.stopNum = 54;
  138. }
  139. else{self.stopNum = 0;}
  140. // ----- continue important stuff ----- //
  141. // replace text with base text + typed characters
  142. var nextString = self.baseText + curString.substr(0, curStrPos);
  143. if (self.attr) {
  144. self.el.attr(self.attr, nextString);
  145. } else {
  146. self.el.text(nextString);
  147. }
  148. // if the number (id of character in current string) is
  149. // less than the stop number, keep going
  150. if (curStrPos > self.stopNum){
  151. // subtract characters one by one
  152. curStrPos--;
  153. // loop the function
  154. self.backspace(curString, curStrPos);
  155. }
  156. // if the stop number has been reached, increase
  157. // array position to next string
  158. else if (curStrPos <= self.stopNum) {
  159. self.arrayPos++;
  160. if(self.arrayPos === self.strings.length) {
  161. self.arrayPos = 0;
  162. self.init();
  163. } else
  164. self.typewrite(self.strings[self.arrayPos], curStrPos);
  165. }
  166. // humanized value for typing
  167. }, humanize);
  168. }
  169. // Start & Stop currently not working
  170. // , stop: function() {
  171. // var self = this;
  172. // self.stop = true;
  173. // clearInterval(self.timeout);
  174. // }
  175. // , start: function() {
  176. // var self = this;
  177. // if(self.stop === false)
  178. // return;
  179. // this.stop = false;
  180. // this.init();
  181. // }
  182. // Reset and rebuild the element
  183. , reset: function(){
  184. var self = this;
  185. clearInterval(self.timeout);
  186. var id = this.el.attr('id');
  187. this.el.after('<span id="' + id + '"/>')
  188. this.el.remove();
  189. this.cursor.remove();
  190. // Send the callback
  191. self.options.resetCallback();
  192. }
  193. };
  194. $.fn.typed = function (option) {
  195. return this.each(function () {
  196. var $this = $(this)
  197. , data = $this.data('typed')
  198. , options = typeof option == 'object' && option;
  199. if (!data) $this.data('typed', (data = new Typed(this, options)));
  200. if (typeof option == 'string') data[option]();
  201. });
  202. };
  203. $.fn.typed.defaults = {
  204. strings: ["These are the default values...", "You know what you should do?", "Use your own!", "Have a great day!"],
  205. // typing speed
  206. typeSpeed: 0,
  207. // time before typing starts
  208. startDelay: 0,
  209. // backspacing speed
  210. backSpeed: 0,
  211. // time before backspacing
  212. backDelay: 500,
  213. // loop
  214. loop: false,
  215. // false = infinite
  216. loopCount: false,
  217. // show cursor
  218. showCursor: true,
  219. // character for cursor
  220. cursorChar: "|",
  221. // attribute to type (null == text)
  222. attr: null,
  223. // call when done callback function
  224. callback: function() {},
  225. // starting callback function before each string
  226. preStringTyped: function() {},
  227. //callback for every typed string
  228. onStringTyped: function() {},
  229. // callback for reset
  230. resetCallback: function() {}
  231. };
  232. }(window.jQuery);
  233. // ------------- jQuery Cookie Plugin v1.4.1 -------------------------------- //
  234. // ============ https://github.com/carhartl/jquery-cookie =================== //
  235. // -------------------------------------------------------------------------- //
  236. (function (factory) {
  237. if (typeof define === 'function' && define.amd) {
  238. // AMD
  239. define(['jquery'], factory);
  240. } else if (typeof exports === 'object') {
  241. // CommonJS
  242. factory(require('jquery'));
  243. } else {
  244. // Browser globals
  245. factory(jQuery);
  246. }
  247. }(function ($) {
  248. var pluses = /\+/g;
  249. function encode(s) {
  250. return config.raw ? s : encodeURIComponent(s);
  251. }
  252. function decode(s) {
  253. return config.raw ? s : decodeURIComponent(s);
  254. }
  255. function stringifyCookieValue(value) {
  256. return encode(config.json ? JSON.stringify(value) : String(value));
  257. }
  258. function parseCookieValue(s) {
  259. if (s.indexOf('"') === 0) {
  260. // This is a quoted cookie as according to RFC2068, unescape...
  261. s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
  262. }
  263. try {
  264. // Replace server-side written pluses with spaces.
  265. // If we can't decode the cookie, ignore it, it's unusable.
  266. // If we can't parse the cookie, ignore it, it's unusable.
  267. s = decodeURIComponent(s.replace(pluses, ' '));
  268. return config.json ? JSON.parse(s) : s;
  269. } catch(e) {}
  270. }
  271. function read(s, converter) {
  272. var value = config.raw ? s : parseCookieValue(s);
  273. return $.isFunction(converter) ? converter(value) : value;
  274. }
  275. var config = $.cookie = function (key, value, options) {
  276. // Write
  277. if (arguments.length > 1 && !$.isFunction(value)) {
  278. options = $.extend({}, config.defaults, options);
  279. if (typeof options.expires === 'number') {
  280. var days = options.expires, t = options.expires = new Date();
  281. t.setTime(+t + days * 864e+5);
  282. }
  283. return (document.cookie = [
  284. encode(key), '=', stringifyCookieValue(value),
  285. options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  286. options.path ? '; path=' + options.path : '',
  287. options.domain ? '; domain=' + options.domain : '',
  288. options.secure ? '; secure' : ''
  289. ].join(''));
  290. }
  291. // Read
  292. var result = key ? undefined : {};
  293. // To prevent the for loop in the first place assign an empty array
  294. // in case there are no cookies at all. Also prevents odd result when
  295. // calling $.cookie().
  296. var cookies = document.cookie ? document.cookie.split('; ') : [];
  297. for (var i = 0, l = cookies.length; i < l; i++) {
  298. var parts = cookies[i].split('=');
  299. var name = decode(parts.shift());
  300. var cookie = parts.join('=');
  301. if (key && key === name) {
  302. // If second argument (value) is a function it's a converter...
  303. result = read(cookie, value);
  304. break;
  305. }
  306. // Prevent storing a cookie that we couldn't decode.
  307. if (!key && (cookie = read(cookie)) !== undefined) {
  308. result[name] = cookie;
  309. }
  310. }
  311. return result;
  312. };
  313. config.defaults = {};
  314. $.removeCookie = function (key, options) {
  315. if ($.cookie(key) === undefined) {
  316. return false;
  317. }
  318. // Must not alter options, thus extending a fresh object...
  319. $.cookie(key, '', $.extend({}, options, { expires: -1 }));
  320. return !$.cookie(key);
  321. };
  322. }));
  323. jQuery(document).ready(function($) {
  324. /*============================================
  325. Welcome Cookies
  326. ==============================================*/
  327. $(function dw_set_cookie() {
  328. var COOKIE = 'windhamdavid-cookie';
  329. var dwcookie = $.cookie(COOKIE);
  330. if (dwcookie == null) {
  331. $.cookie(COOKIE, 'yum-cookies', { expires: 7, path: '/'});
  332. $('.terminal-welcome').modal('show');
  333. $('.welcome').typed({
  334. strings: ['Hey,', 'Hello,\n^10Welcome to ^10my domain ^10...^10', 'Hello, \nWelcome to my little corner of the internet. \n^10It is nice to ', 'Hello, \nWelcome to my little corner of the internet. \nWhat is your name?^200\n'],
  335. typeSpeed: 30,
  336. backSpeed: 50,
  337. startDelay: 0,
  338. backDelay: 0,
  339. loop: false,
  340. loopCount: false,
  341. attr: null,
  342. callback: function(){
  343. dw_terminal();
  344. }
  345. });
  346. }
  347. else {
  348. $('.terminal').modal('show');
  349. if (localStorage.getItem('person') === null) {
  350. var person = 'anonymous person';
  351. }
  352. else {
  353. var person = localStorage.getItem('person');
  354. }
  355. $('.welcome-back-text').typed({
  356. strings: ['...', 'Welcome back ' +person],
  357. typeSpeed: 30,
  358. backSpeed: 10,
  359. startDelay: 0,
  360. backDelay: 100,
  361. loop: false,
  362. loopCount: false,
  363. attr: null,
  364. callback: function(){
  365. dw_terminal();
  366. }
  367. });
  368. }
  369. });
  370. /*============================================
  371. Terminal
  372. ==============================================*/
  373. (function ($){
  374. $.fn.cli = function(handler, prompt, effect){
  375. if (!prompt) prompt = '<span class="u">you</span><span class="at">@</span><span class="mee">david</span>&nbsp;&gt;&nbsp;';
  376. if (!effect) effect = $.fn.text;
  377. return this.each(function(){
  378. var self = $(this);
  379. function newline(){
  380. self.
  381. append('<p class=input><span class=prompt>'+prompt+'</span><span style=outline:none contenteditable></span></p>');
  382. try {
  383. $('[contenteditable]', self)[0].focus();
  384. }catch(e){
  385. }
  386. }
  387. newline();
  388. self.on('keydown', '[contenteditable]', function(evt){
  389. if (evt.keyCode === 13){
  390. $(this).removeAttr('contenteditable');
  391. effect.call($('<p class=response>').appendTo(self),handler(this.textContent || this.innerText));
  392. newline();
  393. return false;
  394. }
  395. });
  396. });
  397. };
  398. })(jQuery);
  399. function dw_shift() {
  400. $('.terminal-welcome').delay(5500).queue(function(thanks){
  401. $(this).modal('hide');
  402. thanks();
  403. });
  404. }
  405. function dw_contact() {
  406. $('.terminal-welcome').delay(2500).queue(function(thanks){
  407. window.location.assign("/contact/");
  408. });
  409. }
  410. function dw_terminal(){
  411. function type(text){
  412. var span = $('<span>').appendTo(this).after('<span id="blinker" style="background:green">&nbsp;&nbsp;</span>');
  413. var style = $('<style>p.input {visibility: hidden} p.input.old {visibility: visible}</style>').appendTo('head');
  414. $('p.input').addClass('old');
  415. var progress = 0;
  416. var timer = setInterval (function(){
  417. span.text(text.substr(0, progress++));
  418. if (progress > text.length){
  419. $('#blinker').remove();
  420. style.remove();
  421. $('[contenteditable]')[0].focus();
  422. clearInterval(timer);
  423. }
  424. },100);
  425. }
  426. $('.thermo-intro').cli(function(text){
  427. if (text.length > 1) {
  428. localStorage.setItem('person', text),
  429. dw_shift();
  430. };
  431. return 'I will close this terminal now. Thank You ' +text;
  432. }, null, type);
  433. $('.thermo').cli(function(text){
  434. if (/exit/i.test(text)) {
  435. $('.terminal').modal('hide');
  436. }
  437. if (/contact/i.test(text)) {
  438. return "loading...",
  439. dw_contact();
  440. };
  441. if (/help/i.test(text)) return "I need somebody!";
  442. if (/hello/i.test(text)) return "Hello to you!";
  443. if (/what/i.test(text)) return "This is a website silly human";
  444. if (/and/i.test(text)) return "and what?";
  445. if (/who/i.test(text)) return "David A. Windham";
  446. if (/when/i.test(text)) return "Yesterday";
  447. if (/how/i.test(text)) return "JavaScript is a dynamic computer programming language";
  448. if (/why/i.test(text)) return "For fun";
  449. if (/you/i.test(text)) return "your mama";
  450. if (/cd ../i.test(text)) return "Oh, I see";
  451. if (/thermonuclear/i.test(text)) return "Wouldn't you prefer a nice game of chess?";
  452. if (text.length < 3) return "type 'exit' to close";
  453. return 'command not found: '+text;
  454. }, null, type);
  455. $('p.input').on('touchstart click', function(e) {
  456. e.preventDefault();
  457. $('[contenteditable]')[0].focus();
  458. });
  459. }
  460. });