Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/webroot/rsrc/externals/javelin/lib/behavior.js
12242 views
1
/**
2
* @provides javelin-behavior
3
* @requires javelin-magical-init
4
* javelin-util
5
*
6
* @javelin-installs JX.behavior
7
* @javelin-installs JX.initBehaviors
8
*
9
* @javelin
10
*/
11
12
/**
13
* Define a Javelin behavior, which holds glue code in a structured way. See
14
* @{article:Concepts: Behaviors} for a detailed description of Javelin
15
* behaviors.
16
*
17
* To define a behavior, provide a name and a function:
18
*
19
* JX.behavior('win-a-hog', function(config, statics) {
20
* alert("YOU WON A HOG NAMED " + config.hogName + "!");
21
* });
22
*
23
* @param string Behavior name.
24
* @param function Behavior callback/definition.
25
* @return void
26
*/
27
JX.behavior = function(name, control_function) {
28
if (__DEV__) {
29
if (JX.behavior._behaviors.hasOwnProperty(name)) {
30
JX.$E(
31
'JX.behavior("' + name + '", ...): '+
32
'behavior is already registered.');
33
}
34
if (!control_function) {
35
JX.$E(
36
'JX.behavior("' + name + '", <nothing>): '+
37
'initialization function is required.');
38
}
39
if (typeof control_function != 'function') {
40
JX.$E(
41
'JX.behavior("' + name + '", <garbage>): ' +
42
'initialization function is not a function.');
43
}
44
// IE does not enumerate over these properties
45
var enumerables = {
46
toString: true,
47
hasOwnProperty: true,
48
valueOf: true,
49
isPrototypeOf: true,
50
propertyIsEnumerable: true,
51
toLocaleString: true,
52
constructor: true
53
};
54
if (enumerables[name]) {
55
JX.$E(
56
'JX.behavior("' + name + '", <garbage>): ' +
57
'do not use this property as a behavior.'
58
);
59
}
60
}
61
JX.behavior._behaviors[name] = control_function;
62
JX.behavior._statics[name] = {};
63
};
64
65
66
/**
67
* Execute previously defined Javelin behaviors, running the glue code they
68
* contain to glue stuff together. See @{article:Concepts: Behaviors} for more
69
* information on Javelin behaviors.
70
*
71
* Normally, you do not call this function yourself; instead, your server-side
72
* library builds it for you.
73
*
74
* @param dict Map of behaviors to invoke: keys are behavior names, and values
75
* are lists of configuration dictionaries. The behavior will be
76
* invoked once for each configuration dictionary.
77
* @return void
78
*/
79
JX.initBehaviors = function(map) {
80
var missing_behaviors = [];
81
for (var name in map) {
82
if (!(name in JX.behavior._behaviors)) {
83
missing_behaviors.push(name);
84
continue;
85
}
86
var configs = map[name];
87
if (!configs.length) {
88
if (JX.behavior._initialized.hasOwnProperty(name)) {
89
continue;
90
}
91
configs = [null];
92
}
93
for (var ii = 0; ii < configs.length; ii++) {
94
try {
95
JX.behavior._behaviors[name](configs[ii], JX.behavior._statics[name]);
96
} catch (behavior_exception) {
97
JX.log(
98
'JX.initBehaviors(...): behavior "%s" raised an error during setup.',
99
name);
100
JX.log(behavior_exception);
101
}
102
}
103
JX.behavior._initialized[name] = true;
104
}
105
if (missing_behaviors.length) {
106
JX.$E(
107
'JX.initBehavior(map): behavior(s) not registered: ' +
108
missing_behaviors.join(', ')
109
);
110
}
111
};
112
113
JX.behavior._behaviors = {};
114
JX.behavior._statics = {};
115
JX.behavior._initialized = {};
116
JX.flushHoldingQueue('behavior', JX.behavior);
117
118