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