d6f3f9953281610bbea62b82045dee91b6e14b58
[myslice.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 function arrays_equal(a,b) { return !(a<b || b<a); }
19
20 function arrayInArray(elt,tab){
21     var flag=false;
22     jQuery.each(tab, function(i,x){
23         if(arrays_equal(x,elt)){flag=true;return;}            
24     });
25     return flag;
26 }
27
28 jQuery.fn.spin = function(opts) {
29   this.each(function() {
30     var $this = jQuery(this),
31         data = $this.data();
32
33     if (data.spinner) {
34       data.spinner.stop();
35       delete data.spinner;
36     }
37     if (opts !== false) {
38       data.spinner = new Spinner(jQuery.extend({color: $this.css('color')}, opts)).spin(this);
39     }
40   });
41   return this;
42 };
43
44 // FROM Triptych : http://stackoverflow.com/users/43089/triptych
45 // http://stackoverflow.com/questions/979256/how-to-sort-an-array-of-javascript-objects
46 // data.sort(sort_by('city', false, function(a){return a.toUpperCase()}));
47 var sort_by = function(field, reverse, primer){
48
49    var key = function (x) {return primer ? primer(x[field]) : x[field]};
50    //var key = primer ? function (x) { return primer(x[field]); } : function (x) { return x[field]; }
51    
52    return function (a,b) {
53        var A = key(a), B = key(b);
54        return (A < B ? -1 : (A > B ? 1 : 0)) * [1,-1][+!!reverse];
55        //return ((A < B) ? -1 :
56        //        (A > B) ? +1 : 0)) * [-1,1][+!!reverse];                  
57    }
58 }