]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/common.js
Removed the word "Settings" from the settings. This information is redundant.
[friendica-addons.git] / jappixmini / jappix / js / common.js
1 /*
2
3 Jappix - An open social platform
4 These are the common JS script for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Authors: Vanaryon, olivierm
10 Last revision: 24/06/11
11
12 */
13
14 // Checks if an element exists in the DOM
15 function exists(selector) {
16         if(jQuery(selector).size() > 0)
17                 return true;
18         else
19                 return false;
20 }
21
22 // Checks if Jappix is connected
23 function isConnected() {
24         if((typeof con != 'undefined') && con && con.connected())
25                 return true;
26         
27         return false;
28 }
29
30 // Checks if Jappix has focus
31 function isFocused() {
32         try {
33                 if(document.hasFocus())
34                         return true;
35                 
36                 return false;
37         }
38         
39         catch(e) {
40                 return true;
41         }
42 }
43
44 // Generates the good XID
45 function generateXID(xid, type) {
46         // XID needs to be transformed
47         if(xid && (xid.indexOf('@') == -1)) {
48                 // Groupchat
49                 if(type == 'groupchat')
50                         return xid + '@' + HOST_MUC;
51                 
52                 // One-to-one chat
53                 if(xid.indexOf('.') == -1)
54                         return xid + '@' + HOST_MAIN;
55                 
56                 // It might be a gateway?
57                 return xid;
58         }
59         
60         // Nothing special (yet bare XID)
61         return xid;
62 }
63
64 // Gets the asked translated string
65 function _e(string) {
66         return string;
67 }
68
69 // Replaces '%s' to a given value for a translated string
70 function printf(string, value) {
71         return string.replace('%s', value);
72 }
73
74 // Properly explodes a string with a given character
75 function explodeThis(toEx, toStr, i) {
76         // Get the index of our char to explode
77         var index = toStr.indexOf(toEx);
78         
79         // We split if necessary the string
80         if(index != -1) {
81                 if(i == 0)
82                         toStr = toStr.substr(0, index);
83                 else
84                         toStr = toStr.substr(index + 1);
85         }
86         
87         // We return the value
88         return toStr;
89 }
90
91 // Cuts the resource of a XID
92 function cutResource(aXID) {
93         return explodeThis('/', aXID, 0);
94 }
95
96 // Gets the resource of a XID
97 function thisResource(aXID) {
98         // Any resource?
99         if(aXID.indexOf('/') != -1)
100                 return explodeThis('/', aXID, 1);
101         
102         // No resource
103         return '';
104 }
105
106 // Does stringprep on a string
107 function stringPrep(string) {
108         // Replacement arrays
109         var invalid = new Array('Š', 'š', 'Đ', 'đ', 'Ž', 'ž', 'Č', 'č', 'Ć', 'ć', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ý', 'þ', 'ÿ', 'Ŕ', 'ŕ');
110         
111         var valid   = new Array('S', 's', 'Dj', 'dj', 'Z', 'z', 'C', 'c', 'C', 'c', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'N', 'O', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 'B', 'Ss', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'o', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'y', 'b', 'y', 'R', 'r');
112         
113         // Compute a new string
114         for(i in invalid)
115                 string = string.replace(invalid[i], valid[i]);
116         
117         return string;
118 }
119
120 // Encodes quotes in a string
121 function encodeQuotes(str) {
122         return (str + '').replace(/"/g, '"');
123 }
124
125 // Gets the bare XID from a XID
126 function bareXID(xid) {
127         // Cut the resource
128         xid = cutResource(xid);
129         
130         // Launch the stringprep
131         xid = stringPrep(xid);
132         
133         // Set the XID to lower case
134         xid = xid.toLowerCase();
135         
136         return xid;
137 }
138
139 // Gets the full XID from a XID
140 function fullXID(xid) {
141         // Normalizes the XID
142         var full = bareXID(xid);
143         var resource = thisResource(xid);
144         
145         // Any resource?
146         if(resource)
147                 full += '/' + resource;
148         
149         return full;
150 }
151
152 // Gets the nick from a XID
153 function getXIDNick(aXID) {
154         return explodeThis('@', aXID, 0);
155 }
156
157 // Gets the host from a XID
158 function getXIDHost(aXID) {
159         return explodeThis('@', aXID, 1);
160 }
161
162 // Checks if we are in developer mode
163 function isDeveloper() {
164         if(DEVELOPER == 'on')
165                 return true;
166         
167         return false;
168 }
169
170 // Checks if anonymous mode is allowed
171 function allowedAnonymous() {
172         if(ANONYMOUS == 'on')
173                 return true;
174         
175         return false;
176 }
177
178 // Checks if host is locked
179 function lockHost() {
180         if(LOCK_HOST == 'on')
181                 return true;
182         
183         return false;
184 }
185
186 // Gets the full XID of the user
187 function getXID() {
188         // Return the XID of the user
189         if(con.username && con.domain)
190                 return con.username + '@' + con.domain;
191         
192         return '';
193 }
194
195 // Generates the colors for a given user XID
196 function generateColor(xid) {
197         var colors = new Array(
198                         'ac0000',
199                         'a66200',
200                         '007703',
201                         '00705f',
202                         '00236b',
203                         '4e005c'
204                      );
205         
206         var number = 0;
207         
208         for(var i = 0; i < xid.length; i++)
209                 number += xid.charCodeAt(i);
210         
211         var color = '#' + colors[number % (colors.length)];
212         
213         return color;
214 }
215
216 // Checks if the XID is a gateway
217 function isGateway(xid) {
218         if(xid.indexOf('@') != -1)
219                 return false;
220         
221         return true;
222 }
223
224 // Gets the from attribute of a stanza (overrides some servers like Prosody missing from attributes)
225 function getStanzaFrom(stanza) {
226         var from = stanza.getFrom();
227         
228         // No from, we assume this is our XID
229         if(!from)
230                 from = getXID();
231         
232         return from;
233 }
234
235 // Logs a given data in the console
236 function logThis(data, level) {
237         // Console not available
238         if(!isDeveloper() || (typeof(console) == 'undefined'))
239                 return false;
240         
241         // Switch the log level
242         switch(level) {
243                 // Debug
244                 case 0:
245                         console.debug(data);
246                         
247                         break;
248                 
249                 // Error
250                 case 1:
251                         console.error(data);
252                         
253                         break;
254                 
255                 // Warning
256                 case 2:
257                         console.warn(data);
258                         
259                         break;
260                 
261                 // Information
262                 case 3:
263                         console.info(data);
264                         
265                         break;
266                 
267                 // Default log level
268                 default:
269                         console.log(data);
270                         
271                         break;
272         }
273         
274         return true;
275 }
276
277 // Gets the current Jappix app. location
278 function getJappixLocation() {
279         var url = window.location.href;
280         
281         // If the URL has variables, remove them
282         if(url.indexOf('?') != -1)
283                 url = url.split('?')[0];
284         if(url.indexOf('#') != -1)
285                 url = url.split('#')[0];
286         
287         // No "/" at the end
288         if(!url.match(/(.+)\/$/))
289                 url += '/';
290         
291         return url;
292 }
293
294 // Removes spaces at the beginning & the end of a string
295 function trim(str) {
296         return str.replace(/^\s+/g,'').replace(/\s+$/g,'');
297 }
298
299 // Adds a zero to a date when needed
300 function padZero(i) {
301         // Negative number (without first 0)
302         if(i > -10 && i < 0)
303                 return '-0' + (i * -1);
304         
305         // Positive number (without first 0)
306         if(i < 10 && i >= 0)
307                 return '0' + i;
308         
309         // All is okay
310         return i;
311 }