-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.terminaltype.min.js
1 lines (1 loc) · 1.72 KB
/
jquery.terminaltype.min.js
1
!function(e){e.fn.terminalType=function(n){var r=this.prop("tagName");if("TEXTAREA"!==r)throw new Error("The container needs to be a textarea. Check documentation for further help. ");var a=this,n=e.extend({introText:"Hello, I am some introduction text",typeText:!0,prependUser:!0,prependTerminal:!0,prependUserText:"> ",prependTerminalText:"$ ",focusOnFinish:!0,validCommands:{hello:"Hi there buddy! You checked the docs?"},error:["I AM AN ERROR"]},n),t=function(e){var r=e.split(""),t=0;n.prependTerminal&&a.val(a.val()+n.prependTerminalText);var o=function(){a.val(a.val()+e),a.val(a.val()+"\n"),n.prependUser&&a.val(a.val()+n.prependUserText),n.focusOnFinish&&a.focus()},i=function(){var e=70*Math.random()+10;return t<r.length?(a.val(a.val()+r[t]),t++,setTimeout(i,e),void 0):(a.val(a.val()+"\n"),n.prependUser&&a.val(a.val()+n.prependUserText),void(n.focusOnFinish&&a.focus()))};n.typeText?i():o()};t(n.introText);var o=function(e){var r=e.toLowerCase().replace(n.prependUserText,"").trim();void 0!==n.validCommands[r]?(t(n.validCommands[r]),void 0!=a._onSuccessCallback&&a._onSuccessCallback(r)):(t(n.error[Math.floor(Math.random()*n.error.length)]),void 0!=a._onFailureCallback&&a._onFailureCallback(r))};return this.keypress(function(n){if(13==n.which){n.preventDefault();var r=e(this).val().substr(e(this).val().lastIndexOf("\n")+1);e(this).val(e(this).val()+"\n"),o(r,e(this))}}),this},e.fn.onTerminalSuccess=function(e){if("function"!=typeof e)throw new Error("Provided callback for onTerminalSuccess is not a function.");return this._onSuccessCallback=e,this},e.fn.onTerminalFailure=function(e){if("function"!=typeof e)throw new Error("Provided callback for onTerminalFailure is not a function.");return this._onFailureCallback=e,this}}(jQuery);