-
Notifications
You must be signed in to change notification settings - Fork 41
/
turing.plugins.js
68 lines (63 loc) · 1.77 KB
/
turing.plugins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*!
* Turing Plugins
* Copyright (C) 2011 Alex R. Young
* MIT Licensed
*/
/**
* The Turing plugin module.
*
* This is an example plugin:
*
* turing.plugins.register('turnRed', {
* name: 'Turn Things Red',
* version: '1.0.0',
* description: 'Turns the background red',
* author: 'Alex Young <[email protected]>',
* licenses: [ { type: 'MIT' } ],
*
* turnRed: function() {
* this[0].style.backgroundColor = '#ff0000';
* return this;
* }
* });
*
*/
define('turing.plugins', ['turing.core'], function(turing) {
var plugins = {};
plugins.registered = {};
plugins.AlreadyRegistered = Error;
plugins.NotFound = Error;
/**
* Registers a plugin, making it available for
* chained DOM calls.
*
* Throws turing.plugins.AlreadyRegistered if a
* plugin with the same name has been registered.
*
* @param {String} methodName The name of your plugin method
* @param {Object} metadata Your plugin
*/
plugins.register = function(methodName, metadata) {
if (plugins.registered[methodName]) {
throw new plugins.AlreadyRegistered('Already registered a plugin called: ' + methodName);
}
plugins.registered[methodName] = metadata;
turing.domChain[methodName] = metadata[methodName];
};
/**
* Removes a plugin. Throws turing.plugins.NotFound if
* the plugin could not be found.
*
* @param {String} methodName The name of the plugin
*/
plugins.remove = function(methodName) {
if (!plugins.registered.hasOwnProperty(methodName)) {
throw new plugins.NotFound('Plugin not found: ' + methodName);
} else {
delete plugins.registered[methodName]
delete turing.domChain[methodName];
}
};
turing.plugins = plugins;
return plugins;
});