dashboard redesign
[unfold.git] / portal / static / js / common.functions.js
1 // Escape special characters in jQuery Selector
2 function escape_id( myid ) {
3     return "#" + myid.replace( /(:|\.|\[|\])/g, "\\$1" );
4 }
5
6 function getKeySplitId(id,separator){
7     // id of elements must respect this rule
8     // split_id[0] = plugin_uuid
9     // split_id[1] = element
10     // split_id[2] = field or key
11     
12     var split_id = id.split(separator);
13     var key = split_id[2];
14     
15     return key;
16 }
17
18 // Adding startsWith & endsWith to the string prototype
19 if ( typeof String.prototype.startsWith != 'function' ) {
20   String.prototype.startsWith = function( str ) {
21     return this.substring( 0, str.length ) === str;
22   };
23 };
24
25 if ( typeof String.prototype.endsWith != 'function' ) {
26   String.prototype.endsWith = function( str ) {
27     return this.substring( this.length - str.length, this.length ) === str;
28   };
29 };
30 // http://stackoverflow.com/questions/646628/javascript-startswith
31
32 function arrays_equal(a,b) { return !(a<b || b<a); }
33
34 function arrayInArray(elt,tab){
35     var flag=false;
36     jQuery.each(tab, function(i,x){
37         if(arrays_equal(x,elt)){flag=true;return;}            
38     });
39     return flag;
40 }
41
42 jQuery.fn.spin = function(opts) {
43   this.each(function() {
44     var $this = jQuery(this),
45         data = $this.data();
46
47     if (data.spinner) {
48       data.spinner.stop();
49       delete data.spinner;
50     }
51     if (opts !== false) {
52       data.spinner = new Spinner(jQuery.extend({color: $this.css('color')}, opts)).spin(this);
53     }
54   });
55   return this;
56 };
57
58 // FROM Triptych : http://stackoverflow.com/users/43089/triptych
59 // http://stackoverflow.com/questions/979256/how-to-sort-an-array-of-javascript-objects
60 // data.sort(sort_by('city', false, function(a){return a.toUpperCase()}));
61 var sort_by = function(field, reverse, primer) {
62
63    var key = function(x) { return primer ? primer(x[field]) : x[field]; };
64    //var key = primer ? function (x) { return primer(x[field]); } : function (x) { return x[field]; }
65    
66    return function(a,b) {
67        var A = key(a), B = key(b);
68        return (A < B ? -1 : (A > B ? 1 : 0)) * [1,-1][+!!reverse];
69        //return ((A < B) ? -1 :
70        //        (A > B) ? +1 : 0)) * [-1,1][+!!reverse];                  
71    };
72 };
73
74 function escapeRegExp(str) {
75   return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
76 }