Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
jaz303 committed Dec 4, 2013
0 parents commit 42185b4
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
62 changes: 62 additions & 0 deletions README.md
@@ -0,0 +1,62 @@
# hudkit-action

An action is a wrapped, observable function that can be enabled/disabled. Actions are called just like normal functions but act as no-ops when disabled.

## Install

$ npm install hudkit-action

## Usage

var createAction = require('hudkit-action');

var myAction = createAction(function() {
console.log("HELLO WORLD");
});

myAction(); // => "HELLO WORLD"
myAction.disable();
myAction(); // (nothing)

## API

#### `action(callback, [opts])`

Create a new action with the given `callback`.

Option keys:

* `title`: action title, default: `''`
* `enabled`: action enabled, default: `true`

#### `action.getTitle()`

Returns the action's title.

#### `action.setTitle(title)`

Set's the action title.

#### `action.isEnabled()`

Returns `true` if action is enabled, `false` otherwise.

#### `action.toggleEnabled()`

Toggle action enabled.

#### `action.disable()`

Disable action.

#### `action.enable()`

Enable action.

#### `action.setEnabled(isEnabled)`

Set action enabled explicitly.

#### `action.onchange`

[http://github.com/jaz303/signalkit](Signalkit) signal that will be emitted when the action's state is changed.
38 changes: 38 additions & 0 deletions index.js
@@ -0,0 +1,38 @@
var signal = require('signalkit');

var ActionProto = Object.create(Function.prototype);

ActionProto.getTitle = function() { return this._title; };
ActionProto.setTitle = function(t) { this._title = ('' + t); this.onchange.emit(); };

ActionProto.isEnabled = function() { return this._enabled; };
ActionProto.toggleEnabled = function() { this.setEnabled(!this._enabled); };
ActionProto.enable = function() { this.setEnabled(true); };
ActionProto.disable = function() { this.setEnabled(false); };

ActionProto.setEnabled = function(en) {
en = !!en;
if (en != this._enabled) {
this._enabled = en;
this.onchange.emit();
}
}

module.exports = function(fn, opts) {

var actionFun = function() {
if (actionFun._enabled) {
fn.apply(null, arguments);
}
}

opts = opts || {};

actionFun._title = ('title' in opts) ? ('' + opts.title) : '';
actionFun._enabled = ('enabled' in opts) ? (!!opts.enabled) : true;
actionFun.onchange = signal('onchange');
actionFun.__proto__ = ActionProto;

return actionFun;

}
25 changes: 25 additions & 0 deletions package.json
@@ -0,0 +1,25 @@
{
"name": "hudkit-action",
"version": "0.0.0",
"description": "Actions for hudkit",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git://github.com/jaz303/hudkit-action.git"
},
"keywords": [
"hudkit",
"action",
"ui",
"binding"
],
"author": "Jason Frame <jason@onehackoranother.com> (http://jasonframe.co.uk)",
"license": "MIT",
"readmeFilename": "README.md",
"dependencies": {
"signalkit": "~0.2.3"
}
}

0 comments on commit 42185b4

Please sign in to comment.