Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 42185b4
Showing
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |