]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/utilities.js
Statusnet: New intelligent shortening enabled.
[friendica-addons.git] / jappixmini / jappix / js / utilities.js
1 /*
2
3 Jappix - An open social platform
4 These are the utilities 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 a function exists
15 function functionExists(func) {
16         if(typeof func == 'function')
17                 return true;
18         
19         return false;
20 }
21
22 // Returns whether using HTTPS or not
23 function isHTTPS() {
24         if(window.location.href && (window.location.href).match(/^https/i))
25                 return true;
26         
27         return false;
28 }
29
30 // Generates the good storage URL
31 function generateURL(url) {
32         // HTTPS not allowed
33         if((HTTPS_STORAGE != 'on') && url.match(/^https(.+)/))
34                 url = 'http' + RegExp.$1;
35         
36         return url;
37 }
38
39 // Disables an input if needed
40 function disableInput(value, condition) {
41         if(value == condition)
42                 return ' disabled=""';
43         
44         return '';
45 }
46
47 // Cuts a string
48 function cut(string, limit) {
49         return string.substr(0, limit);
50 }
51
52 // Truncates a string
53 function truncate(string, limit) {
54         // Must truncate the string
55         if(string.length > limit)
56                 string = string.substr(0, limit) + '...';
57         
58         return string;
59 }
60
61 // Removes the new lines
62 function noLines(string) {
63         return string.replace(/\n/g, ' ');
64 }
65
66 // Encodes a string for onclick attribute
67 function encodeOnclick(str) {
68         return (encodeQuotes(str)).replace(/'/g, '\\$&');
69 }
70
71 // Checks if we are in the anonymous mode
72 function isAnonymous() {
73         if(allowedAnonymous() && LINK_VARS['r'])
74                 return true;
75         
76         return false;
77 }
78
79 // Checks if this is a private chat user
80 function isPrivate(xid) {
81         if(exists('[data-xid=' + escape(xid) + '][data-type=groupchat]'))
82                 return true;
83         
84         return false;
85 }
86
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;
92         
93         // No DOM storage
94         if(!hasDB() || !hasPersistent())
95                 return true;
96         
97         // Obsolete IE
98         if((browser_name == 'Explorer') && (browser_version < 8))
99                 return true;
100         
101         // Obsolete Chrome
102         if((browser_name == 'Chrome') && (browser_version < 7))
103                 return true;
104         
105         // Obsolete Safari
106         if((browser_name == 'Safari') && (browser_version < 4))
107                 return true;
108         
109         // Obsolete Firefox
110         if((browser_name == 'Firefox') && (browser_version < 3.5))
111                 return true;
112         
113         // Obsolete Opera
114         if((browser_name == 'Opera') && (browser_version < 9))
115                 return true;
116         
117         return false;
118 }
119
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');
123 }
124
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');
128 }
129
130 // Gets the server of the user
131 function getServer() {
132         // Return the domain of the user
133         return con.domain;
134 }
135
136 // Gets the password of the user
137 function getPassword() {
138         // Return the password of the user
139         return con.pass;
140 }
141
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();
146         });
147 }
148
149 // Escapes a string for a regex usage
150 function escapeRegex(query) {
151         return query.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
152 }
153
154 // Converts a XML document to a string
155 function xmlToString(xmlData) {
156         try {
157                 // For Mozilla, Firefox, Opera, etc.
158                 if(window.XMLSerializer)
159                         return (new XMLSerializer()).serializeToString(xmlData);
160                 
161                 // For Internet Explorer
162                 if(window.ActiveXObject)
163                         return xmlData.xml;
164                 
165                 return null;
166         }
167         
168         catch(e) {
169                 return null;
170         }
171 }
172
173 // Converts a string to a XML document
174 function XMLFromString(sXML) {
175         try {
176                 // No data?
177                 if(!sXML)
178                         return '';
179                 
180                 // Add the XML tag
181                 if(!sXML.match(/^<\?xml/i))
182                         sXML = '<?xml version="1.0"?>' + sXML;
183                 
184                 // Parse it!
185                 if(window.DOMParser)
186                         return (new DOMParser()).parseFromString(sXML, 'text/xml');
187                 
188                 if(window.ActiveXObject) {
189                         var oXML = new ActiveXObject('Microsoft.XMLDOM');
190                         oXML.loadXML(sXML);
191                         
192                         return oXML;
193                 }
194         }
195         
196         catch(e) {
197                 return '';
198         }
199 }
200
201 // Return the file category
202 function fileCategory(ext) {
203         var cat;
204         
205         switch(ext) {
206                 // Images
207                 case 'jpg':
208                 case 'jpeg':
209                 case 'png':
210                 case 'bmp':
211                 case 'gif':
212                 case 'tif':
213                 case 'svg':
214                 case 'psp':
215                 case 'xcf':
216                         cat = 'image';
217                         
218                         break;
219                 
220                 // Videos
221                 case 'ogv':
222                 case 'ogg':
223                 case 'mkv':
224                 case 'avi':
225                 case 'mov':
226                 case 'mp4':
227                 case 'm4v':
228                 case 'wmv':
229                 case 'asf':
230                 case 'mpg':
231                 case 'mpeg':
232                 case 'ogm':
233                 case 'rmvb':
234                 case 'rmv':
235                 case 'qt':
236                 case 'flv':
237                 case 'ram':
238                 case '3gp':
239                 case 'avc':
240                         cat = 'video';
241                         
242                         break;
243                 
244                 // Sounds
245                 case 'oga':
246                 case 'mka':
247                 case 'flac':
248                 case 'mp3':
249                 case 'wav':
250                 case 'm4a':
251                 case 'wma':
252                 case 'rmab':
253                 case 'rma':
254                 case 'bwf':
255                 case 'aiff':
256                 case 'caf':
257                 case 'cda':
258                 case 'atrac':
259                 case 'vqf':
260                 case 'au':
261                 case 'aac':
262                 case 'm3u':
263                 case 'mid':
264                 case 'mp2':
265                 case 'snd':
266                 case 'voc':
267                         cat = 'audio';
268                         
269                         break;
270                 
271                 // Documents
272                 case 'pdf':
273                 case 'odt':
274                 case 'ott':
275                 case 'sxw':
276                 case 'stw':
277                 case 'ots':
278                 case 'sxc':
279                 case 'stc':
280                 case 'sxi':
281                 case 'sti':
282                 case 'pot':
283                 case 'odp':
284                 case 'ods':
285                 case 'doc':
286                 case 'docx':
287                 case 'docm':
288                 case 'xls':
289                 case 'xlsx':
290                 case 'xlsm':
291                 case 'xlt':
292                 case 'ppt':
293                 case 'pptx':
294                 case 'pptm':
295                 case 'pps':
296                 case 'odg':
297                 case 'otp':
298                 case 'sxd':
299                 case 'std':
300                 case 'std':
301                 case 'rtf':
302                 case 'txt':
303                 case 'htm':
304                 case 'html':
305                 case 'shtml':
306                 case 'dhtml':
307                 case 'mshtml':
308                         cat = 'document';
309                         
310                         break;
311                 
312                 // Packages
313                 case 'tgz':
314                 case 'gz':
315                 case 'tar':
316                 case 'ar':
317                 case 'cbz':
318                 case 'jar':
319                 case 'tar.7z':
320                 case 'tar.bz2':
321                 case 'tar.gz':
322                 case 'tar.lzma':
323                 case 'tar.xz':
324                 case 'zip':
325                 case 'xz':
326                 case 'rar':
327                 case 'bz':
328                 case 'deb':
329                 case 'rpm':
330                 case '7z':
331                 case 'ace':
332                 case 'cab':
333                 case 'arj':
334                 case 'msi':
335                         cat = 'package';
336                         
337                         break;
338                 
339                 // Others
340                 default:
341                         cat = 'other';
342                         
343                         break;
344         }
345         
346         return cat;
347 }
348
349 // Registers Jappix as the default XMPP links handler
350 function xmppLinksHandler() {
351         try {
352                 navigator.registerProtocolHandler('xmpp', JAPPIX_LOCATION + '?x=%s', SERVICE_NAME);
353                 
354                 return true;
355         }
356         
357         catch(e) {
358                 return false;
359         }
360 }
361
362 // Checks if a value exists in an array
363 function existArrayValue(array, value) {
364         try {
365                 // Loop in the array
366                 for(i in array) {
367                         if(array[i] == value)
368                                 return true;
369                 }
370                 
371                 return false;
372         }
373         
374         catch(e) {
375                 return false;
376         }
377 }
378
379 // Removes a value from an array
380 function removeArrayValue(array, value) {
381         for(i in array) {
382                 // It matches, remove it!
383                 if(array[i] == value) {
384                         array.splice(i, 1);
385                         
386                         return true;
387                 }
388         }
389         
390         return false;
391 }
392
393 // Converts a string to an array
394 function stringToArray(string) {
395         var array = [];
396         
397         // Any string to convert?
398         if(string) {
399                 // More than one item
400                 if(string.match(/,/gi)) {
401                         var string_split = string.split(',');
402                         
403                         for(i in string_split) {
404                                 if(string_split[i])
405                                         array.push(string_split[i]);
406                                 else
407                                         array.push('');
408                         }
409                 }
410                 
411                 // Only one item
412                 else
413                         array.push(string);
414         }
415         
416         return array;
417 }
418
419 // Get the index of an array value
420 function indexArrayValue(array, value) {
421         // Nothing?
422         if(!array || !array.length)
423                 return 0;
424         
425         // Read the index of the value
426         var index = 0;
427         
428         for(var i = 0; i < array.length; i++) {
429                 if(array[i] == value) {
430                         index = i;
431                         
432                         break;
433                 }
434         }
435         
436         return index;
437 }