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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
/**
*
*
*/
/**
* @namespace Namespace for elgg javascript functions
*/
var elgg = elgg || {};
elgg.assertTypeOf = function(type, param) {
if (typeof param !== type) {
throw new TypeError("Expecting param to be a(n) " + type + ". Was a(n) " + typeof param + ".");
}
};
/**
* Pointer to the global context
* {@see elgg.require} and {@see elgg.provide}
*/
elgg.global = this;
/**
* Throw an error if the required package isn't present
*
* @param {String} pkg The required package (e.g., 'elgg.package')
*/
elgg.require = function(pkg) {
elgg.assertTypeOf('string', pkg);
var parts = pkg.split('.'),
cur = elgg.global,
part;
for (var i = 0; i < parts.length; i++) {
part = parts[i];
cur = cur[part];
if(typeof cur == 'undefined') {
throw new Error("Missing package: " + pkg);
}
}
};
/**
* Generate the skeleton for a package.
*
* <pre>
* elgg.provide('elgg.package.subpackage');
* </pre>
*
* is equivalent to
*
* <pre>
* elgg = elgg || {};
* elgg.package = elgg.package || {};
* elgg.package.subpackage = elgg.package.subpackage || {};
* </pre>
*
* @example elgg.provide('elgg.config.translations')
*
* @param {string} pkg The package name.
*/
elgg.provide = function(pkg) {
elgg.assertTypeOf('string', pkg);
var parts = pkg.split('.'),
cur = elgg.global,
part;
for (var i = 0; i < parts.length; i++) {
part = parts[i];
cur[part] = cur[part] || {};
cur = cur[part];
}
};
/**
* Inherit the prototype methods from one constructor into another.
*
* @example
* <pre>
* function ParentClass(a, b) { }
*
* ParentClass.prototype.foo = function(a) { alert(a); }
*
* function ChildClass(a, b, c) {
* //equivalent of parent::__construct(a, b); in PHP
* ParentClass.call(this, a, b);
* }
*
* elgg.inherit(ChildClass, ParentClass);
*
* var child = new ChildClass('a', 'b', 'see');
* child.foo('boo!'); // alert('boo!');
* </pre>
*
* @param {Function} childCtor Child class.
* @param {Function} parentCtor Parent class.
*/
elgg.inherit = function(Child, Parent) {
Child.prototype = new Parent();
Child.prototype.constructor = Child;
};
/**
* Prepend elgg.config.wwwroot to a url if the url doesn't already have it.
*
* @param {String} url The url to extend
* @return {String} The extended url
* @private
*/
elgg.normalize_url = function(url) {
url = url || '';
elgg.assertTypeOf('string', url);
if(/(^(https?:)?\/\/)/.test(url)) {
return url;
}
return elgg.config.wwwroot + url;
};
/**
* Displays system messages via javascript rather than php.
*
* @param {String} msgs The message we want to display
* @param {Number} delay The amount of time to display the message in milliseconds. Defaults to 6 seconds.
* @param {String} type The type of message (typically 'error' or 'message')
* @private
*/
elgg.system_messages = function(msgs, delay, type) {
if (msgs == undefined) {
return;
}
//validate delay. Must be a positive integer.
delay = parseInt(delay);
if (isNaN(delay) || delay <= 0) {
delay = 6000;
}
classes = ['elgg_system_message', 'radius8'];
if (type == 'error') {
classes.push('messages_error');
}
//Handle non-arrays
if (msgs.constructor.toString().indexOf("Array") == -1) {
msgs = [msgs];
}
var messages_html = [];f
for (var i in msgs) {
messages_html.push('<div class="' + classes.join(' ') + '"><p>' + msgs[i] + '</p></div>');
}
$(messages_html.join('')).appendTo('#elgg_system_messages').animate({opacity:'1.0'},delay).fadeOut('slow');
};
/**
* Wrapper function for system_messages. Specifies "messages" as the type of message
* @param {String} msg The message to display
* @param {Number} delay How long to display the message (milliseconds)
*/
elgg.system_message = function(msgs, delay) {
elgg.system_messages(msgs, delay, "message");
};
/**
* Wrapper function for system_messages. Specifies "errors" as the type of message
* @param {String} error The error message to display
* @param {Number} delay How long to dispaly the error message (milliseconds)
*/
elgg.register_error = function(errors, delay) {
elgg.system_messages(errors, delay, "error");
};
/**
* Meant to mimic the php forward() function by simply redirecting the
* user to another page.
*
* @param {String} url The url to forward to
*/
elgg.forward = function(url) {
location.href = elgg.normalize_url(url);
};
|