ResourcesSelected Plugin working with URNs - http://trac.myslice.info/ticket/36
[myslice.git] / portal / static / js / common.functions.js
1 /*
2  * This file is included in tophat_render.php
3  */
4 // Escape special characters in jQuery Selector
5 function escape_id( myid ) {
6     return "#" + myid.replace( /(:|\.|\[|\])/g, "\\$1" );
7 }
8
9 function getKeySplitId(id,separator){
10     // id of elements must respect this rule
11     // split_id[0] = plugin_uuid
12     // split_id[1] = element
13     // split_id[2] = field or key
14     
15     var split_id = id.split(separator);
16     var key = split_id[2];
17     
18     return key;
19 }
20
21 function arrays_equal(a,b) { return !(a<b || b<a); }
22
23 function arrayInArray(elt,tab){
24     var flag=false;
25     jQuery.each(tab, function(i,x){
26         if(arrays_equal(x,elt)){flag=true;return;}            
27     });
28     return flag;
29 }
30
31 jQuery.fn.spin = function(opts) {
32   this.each(function() {
33     var $this = jQuery(this),
34         data = $this.data();
35
36     if (data.spinner) {
37       data.spinner.stop();
38       delete data.spinner;
39     }
40     if (opts !== false) {
41       data.spinner = new Spinner(jQuery.extend({color: $this.css('color')}, opts)).spin(this);
42     }
43   });
44   return this;
45 };
46
47 // FROM Triptych : http://stackoverflow.com/users/43089/triptych
48 // http://stackoverflow.com/questions/979256/how-to-sort-an-array-of-javascript-objects
49 // data.sort(sort_by('city', false, function(a){return a.toUpperCase()}));
50 var sort_by = function(field, reverse, primer){
51
52    var key = function (x) {return primer ? primer(x[field]) : x[field]};
53    //var key = primer ? function (x) { return primer(x[field]); } : function (x) { return x[field]; }
54    
55    return function (a,b) {
56        var A = key(a), B = key(b);
57        return (A < B ? -1 : (A > B ? 1 : 0)) * [1,-1][+!!reverse];
58        //return ((A < B) ? -1 :
59        //        (A > B) ? +1 : 0)) * [-1,1][+!!reverse];                  
60    }
61 }