3 Jappix - An open social platform
4 These are the utilities JS script for Jappix
6 -------------------------------------------------
9 Authors: Vanaryon, olivierm
10 Last revision: 24/06/11
14 // Checks if a function exists
15 function functionExists(func) {
16 if(typeof func == 'function')
22 // Returns whether using HTTPS or not
24 if(window.location.href && (window.location.href).match(/^https/i))
30 // Generates the good storage URL
31 function generateURL(url) {
33 if((HTTPS_STORAGE != 'on') && url.match(/^https(.+)/))
34 url = 'http' + RegExp.$1;
39 // Disables an input if needed
40 function disableInput(value, condition) {
41 if(value == condition)
42 return ' disabled=""';
48 function cut(string, limit) {
49 return string.substr(0, limit);
53 function truncate(string, limit) {
54 // Must truncate the string
55 if(string.length > limit)
56 string = string.substr(0, limit) + '...';
61 // Removes the new lines
62 function noLines(string) {
63 return string.replace(/\n/g, ' ');
66 // Encodes a string for onclick attribute
67 function encodeOnclick(str) {
68 return (encodeQuotes(str)).replace(/'/g, '\\$&');
71 // Checks if we are in the anonymous mode
72 function isAnonymous() {
73 if(allowedAnonymous() && LINK_VARS['r'])
79 // Checks if this is a private chat user
80 function isPrivate(xid) {
81 if(exists('[data-xid=' + escape(xid) + '][data-type=groupchat]'))
87 // Checks if the user browser is obsolete
88 function isObsolete() {
89 // Get browser name & version
90 var browser_name = BrowserDetect.browser;
91 var browser_version = BrowserDetect.version;
94 if(!hasDB() || !hasPersistent())
98 if((browser_name == 'Explorer') && (browser_version < 8))
102 if((browser_name == 'Chrome') && (browser_version < 7))
106 if((browser_name == 'Safari') && (browser_version < 4))
110 if((browser_name == 'Firefox') && (browser_version < 3.5))
114 if((browser_name == 'Opera') && (browser_version < 9))
120 // Gets a MUC user XID
121 function getMUCUserXID(room, nick) {
122 return $('div.chat[data-xid=' + escape(room) + '] div[data-nick=' + escape(nick) + ']').attr('data-xid');
125 // Gets a MUC user read XID
126 function getMUCUserRealXID(room, nick) {
127 return $('div.chat[data-xid=' + escape(room) + '] div[data-nick=' + escape(nick) + ']').attr('data-realxid');
130 // Gets the server of the user
131 function getServer() {
132 // Return the domain of the user
136 // Gets the password of the user
137 function getPassword() {
138 // Return the password of the user
142 // Quotes the nick of an user
143 function quoteMyNick(hash, nick) {
144 $(document).oneTime(10, function() {
145 $('#page-engine #' + hash + ' .message-area').val(nick + ', ').focus();
149 // Escapes a string for a regex usage
150 function escapeRegex(query) {
151 return query.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
154 // Converts a XML document to a string
155 function xmlToString(xmlData) {
157 // For Mozilla, Firefox, Opera, etc.
158 if(window.XMLSerializer)
159 return (new XMLSerializer()).serializeToString(xmlData);
161 // For Internet Explorer
162 if(window.ActiveXObject)
173 // Converts a string to a XML document
174 function XMLFromString(sXML) {
181 if(!sXML.match(/^<\?xml/i))
182 sXML = '<?xml version="1.0"?>' + sXML;
186 return (new DOMParser()).parseFromString(sXML, 'text/xml');
188 if(window.ActiveXObject) {
189 var oXML = new ActiveXObject('Microsoft.XMLDOM');
201 // Return the file category
202 function fileCategory(ext) {
349 // Registers Jappix as the default XMPP links handler
350 function xmppLinksHandler() {
352 navigator.registerProtocolHandler('xmpp', JAPPIX_LOCATION + '?x=%s', SERVICE_NAME);
362 // Checks if a value exists in an array
363 function existArrayValue(array, value) {
367 if(array[i] == value)
379 // Removes a value from an array
380 function removeArrayValue(array, value) {
382 // It matches, remove it!
383 if(array[i] == value) {
393 // Converts a string to an array
394 function stringToArray(string) {
397 // Any string to convert?
399 // More than one item
400 if(string.match(/,/gi)) {
401 var string_split = string.split(',');
403 for(i in string_split) {
405 array.push(string_split[i]);
419 // Get the index of an array value
420 function indexArrayValue(array, value) {
422 if(!array || !array.length)
425 // Read the index of the value
428 for(var i = 0; i < array.length; i++) {
429 if(array[i] == value) {