3 Jappix - An open social platform
4 These are the common JS script for Jappix
6 -------------------------------------------------
9 Authors: Vanaryon, olivierm
10 Last revision: 24/06/11
14 // Checks if an element exists in the DOM
15 function exists(selector) {
16 if(jQuery(selector).size() > 0)
22 // Checks if Jappix is connected
23 function isConnected() {
24 if((typeof con != 'undefined') && con && con.connected())
30 // Checks if Jappix has focus
31 function isFocused() {
33 if(document.hasFocus())
44 // Generates the good XID
45 function generateXID(xid, type) {
46 // XID needs to be transformed
47 if(xid && (xid.indexOf('@') == -1)) {
49 if(type == 'groupchat')
50 return xid + '@' + HOST_MUC;
53 if(xid.indexOf('.') == -1)
54 return xid + '@' + HOST_MAIN;
56 // It might be a gateway?
60 // Nothing special (yet bare XID)
64 // Gets the asked translated string
69 // Replaces '%s' to a given value for a translated string
70 function printf(string, value) {
71 return string.replace('%s', value);
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);
79 // We split if necessary the string
82 toStr = toStr.substr(0, index);
84 toStr = toStr.substr(index + 1);
87 // We return the value
91 // Cuts the resource of a XID
92 function cutResource(aXID) {
93 return explodeThis('/', aXID, 0);
96 // Gets the resource of a XID
97 function thisResource(aXID) {
99 if(aXID.indexOf('/') != -1)
100 return explodeThis('/', aXID, 1);
106 // Does stringprep on a string
107 function stringPrep(string) {
108 // Replacement arrays
109 var invalid = new Array('Š', 'š', 'Đ', 'đ', 'Ž', 'ž', 'Č', 'č', 'Ć', 'ć', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ý', 'þ', 'ÿ', 'Ŕ', 'ŕ');
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');
113 // Compute a new string
115 string = string.replace(invalid[i], valid[i]);
120 // Encodes quotes in a string
121 function encodeQuotes(str) {
122 return (str + '').replace(/"/g, '"');
125 // Gets the bare XID from a XID
126 function bareXID(xid) {
128 xid = cutResource(xid);
130 // Launch the stringprep
131 xid = stringPrep(xid);
133 // Set the XID to lower case
134 xid = xid.toLowerCase();
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);
147 full += '/' + resource;
152 // Gets the nick from a XID
153 function getXIDNick(aXID) {
154 return explodeThis('@', aXID, 0);
157 // Gets the host from a XID
158 function getXIDHost(aXID) {
159 return explodeThis('@', aXID, 1);
162 // Checks if we are in developer mode
163 function isDeveloper() {
164 if(DEVELOPER == 'on')
170 // Checks if anonymous mode is allowed
171 function allowedAnonymous() {
172 if(ANONYMOUS == 'on')
178 // Checks if host is locked
179 function lockHost() {
180 if(LOCK_HOST == 'on')
186 // Gets the full XID of the user
188 // Return the XID of the user
189 if(con.username && con.domain)
190 return con.username + '@' + con.domain;
195 // Generates the colors for a given user XID
196 function generateColor(xid) {
197 var colors = new Array(
208 for(var i = 0; i < xid.length; i++)
209 number += xid.charCodeAt(i);
211 var color = '#' + colors[number % (colors.length)];
216 // Checks if the XID is a gateway
217 function isGateway(xid) {
218 if(xid.indexOf('@') != -1)
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();
228 // No from, we assume this is our XID
235 // Logs a given data in the console
236 function logThis(data, level) {
237 // Console not available
238 if(!isDeveloper() || (typeof(console) == 'undefined'))
241 // Switch the log level
277 // Gets the current Jappix app. location
278 function getJappixLocation() {
279 var url = window.location.href;
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];
288 if(!url.match(/(.+)\/$/))
294 // Removes spaces at the beginning & the end of a string
296 return str.replace(/^\s+/g,'').replace(/\s+$/g,'');
299 // Adds a zero to a date when needed
300 function padZero(i) {
301 // Negative number (without first 0)
303 return '-0' + (i * -1);
305 // Positive number (without first 0)