Giter Site home page Giter Site logo

console.js's Introduction

Console.js

Console.js is a tiny lib for building game-console-like popups in browser.

Console.js Screenshot

Check the Live Demo, or Basic Usage / Advance Usage / API.

Basic Usage

  1. Install console.js, either:
  • use npm: npm install console.js
  • use bower: bower install console.js
  • directly include console.min.js in your page: <script src="http://amio.github.io/console.js/lib/console.min.js"></script>
  1. Create a Console:
new Console({
    "addbots": function (num) {
        // add some bots,
        // then tell player:
        return num + ' bots added.'
    }
});

Advance Usage

Init Console with options

var cnsl = new Console({}, {
    hotkey: 27, // <kbd>ESC</kbd> ('~' for default)
    welcome: 'Hello User.',
    caseSensitive: true,
    defaultHandler: function(){}
    onShow: function(){},
    onHide: function(){}
});
  • hotkey : {Number|boolean} The keyCode of hotkey. Hint: If you want to manually put up console(cnsl.toggle("on")), set to a falsy value. 192 by default, the "~".
  • welcome: {String} The welcome message. '' by default.
  • caseSensitive: {Boolean} If you want to. false by default.
  • defaultHandler: {Function} the default handler for any unspecified command. null by default.
  • onShow : {Function} On show callback. null by default.
  • onHide : {Function} On hide callback. null by default.

Alias

new Console({
    "add": "addbots",
    "addbots": function (num) {
        // add some bots,
        // then tell player:
        return num + ' bots added.'
    }
});

Late register command

.register(command, commandHandler)

var playerName = 'Player';

var cnsl = new Console();

cnsl.register('setname',function(name){
    playerName = name;
    return 'Player name is' + playerName + ' now.';
});

Late register command with extra (any) config

.register(command, commandHandler, commandConfig)

var playerName = 'Player';

var cnsl = new Console();

cnsl.register('setname',function(name){
    playerName = name;
    return 'Player name is' + playerName + ' now.';
}, {
    usage: 'SETNAME &lt;newname&gt; || NAME &lt;newname&gt;',
    desc: 'Change your name (works in network play too).'
})

.register('help', function () {
    var cmds = cnsl.commands;
    for (var name in cmds) {
        if (cmds.hasOwnProperty(name)) {
        cmds[name].desc && cnsl.log(' -', cmds[name].usage + ':', cmds[name].desc);
        }
    }
},{
    usage: 'HELP',
    desc: 'Show help messages.'
});

API

Create a Console

  • new Console() Create a console (with default options).
  • new Console(cmdsObj) Create a console with command handlers. (see Basic Usage)
  • new Console(cmdsObj, optionsObj) Create a console with options. (see Init console with options)

Instance Methods

.register(cmd, fn[, config])

  • .register(cmd, fn) Register a fn to cmd
  • .register(cmd, fn, config) Register a fn to cmd with a config object

.toggle([switch])

  • .toggle() Toggle the console
  • .toggle("on") Show the console
  • .toggle("off") Hide the console

.printHelp([filterFn])

  • .printHelp() List available commands to console.
  • .printHelp( function(x){ return x.indexOf('se') === 0 } ) List commands which starts with "se" to.

.destroy()

  • .destroy() Suicide.

console.js's People

Contributors

amio avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.