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