Added filterable to summary and histogram controllers
[ninja.git] / application / views / js / global_search.js
blob47d081574dbf1626ed174850dc453d9a24be3af6
1 $(document).ready(function() {
2 var query = $('#query');
3 if(!query.length) {
4 // fix to avoid autocomplete if element is not present.
5 // the autocomplete source is minified and we've got a demo to run..
6 // new-host wizard fails without this
7 return;
9 query.autocomplete({
10 serviceUrl:_site_domain + _index_page + '/ajax/global_search/',
11 minChars:2,
12 //delimiter: /(,|;)\s*/, // regex or character
13 maxHeight:500,
14 width:269,
15 deferRequestBy: 300, //miliseconds
16 // callback function:
17 onSelect: function(value, data){ do_redirect(value, data); }
18 });
21 var search_old_refresh = 0;
22 query.focus(function() {
23 search_old_refresh = current_interval;
24 ninja_refresh(0);
25 $("#ninja_refresh_control").attr('checked', true);
26 $('#ninja_refresh_lable').css('font-weight', 'bold');
27 });
29 query.blur(function() {
30 if (current_interval === 0 && search_old_refresh !== 0) {
31 current_interval = search_old_refresh;
32 ninja_refresh(current_interval);
33 $("#ninja_refresh_control").attr('checked', false);
34 $('#ninja_refresh_lable').css('font-weight', '');
36 });
37 });
39 // function from http://forums.devshed.com/t39065/s84ded709f924610aa44fff827511aba3.html
40 // author appears to be Robert Pollard
42 function sprintf()
44 if (!arguments || arguments.length < 1 || !RegExp)
46 return;
48 var str = arguments[0];
49 var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
50 var a = b = [], numSubstitutions = 0, numMatches = 0;
51 while (a = re.exec(str))
53 var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
54 var pPrecision = a[5], pType = a[6], rightPart = a[7];
56 numMatches++;
57 if (pType == '%')
59 subst = '%';
61 else
63 numSubstitutions++;
64 if (numSubstitutions >= arguments.length)
66 alert('Error! Not enough function arguments (' + (arguments.length - 1)
67 + ', excluding the string)\n'
68 + 'for the number of substitution parameters in string ('
69 + numSubstitutions + ' so far).');
71 var param = arguments[numSubstitutions];
72 var pad = '';
73 if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
74 else if (pPad) pad = pPad;
75 var justifyRight = true;
76 if (pJustify && pJustify === "-") justifyRight = false;
77 var minLength = -1;
78 if (pMinLength) minLength = parseInt(pMinLength);
79 var precision = -1;
80 if (pPrecision && pType == 'f')
81 precision = parseInt(pPrecision.substring(1));
82 var subst = param;
83 switch (pType)
85 case 'b':
86 subst = parseInt(param).toString(2);
87 break;
88 case 'c':
89 subst = String.fromCharCode(parseInt(param));
90 break;
91 case 'd':
92 subst = parseInt(param) ? parseInt(param) : 0;
93 break;
94 case 'u':
95 subst = Math.abs(param);
96 break;
97 case 'f':
98 subst = (precision > -1)
99 ? Math.round(parseFloat(param) * Math.pow(10, precision))
100 / Math.pow(10, precision)
101 : parseFloat(param);
102 break;
103 case 'o':
104 subst = parseInt(param).toString(8);
105 break;
106 case 's':
107 subst = param;
108 break;
109 case 'x':
110 subst = ('' + parseInt(param).toString(16)).toLowerCase();
111 break;
112 case 'X':
113 subst = ('' + parseInt(param).toString(16)).toUpperCase();
114 break;
116 var padLeft = minLength - subst.toString().length;
117 if (padLeft > 0)
119 var arrTmp = new Array(padLeft+1);
120 var padding = arrTmp.join(pad?pad:" ");
122 else
124 var padding = "";
127 str = leftpart + padding + subst + rightPart;
129 return str;
134 function do_redirect(value, data)
136 // if user clicked on information message about
137 // nr of rows returned, we shouldn't do anything
138 if (data[0] == '') {
139 $('#query').val('');
140 return false;
143 var match = value.split(';');
144 if (match.length) {
145 value = match[1];
148 var str = sprintf(data[0], encodeURIComponent(data[1]), value);
149 self.location.href = _site_domain + _index_page + str;