text: /*
   2015 (c) Sahana Software Foundation
   @license MIT
  
   requires jQuery 1.9.1+
   requires jQuery UI 1.10 widget factory
  */
  (function(e,r){var q=0;e.widget("s3.groupedItems",{options:{ajaxURL:null,renderGroupHeaders:!1,totalsLabel:"TOTAL"},_create:function(){e(this.element);this.id=q;q+=1;this.namespace=".groupeditems"},_init:function(){var a=e(this.element);this.widget_id=a.attr("id");this.items=a.find(".gi-data").first();this.refresh()},_destroy:function(){e.Widget.prototype.destroy.call(this)},refresh:function(){var a=e(this.element);a.find(".gi-throbber").show();this._unbindEvents();this.data||this._deserialize();
  var b=this.data,d=a.find(".gi-table");b.e?(d.hide(),d.empty(),a.find(".gi-empty").show()):(a.find(".gi-empty").hide(),d.empty(),b=this._renderTable(b),d.append(b).show());this._bindEvents();a.find(".gi-throbber").css("visibility","hidden")},_renderTable:function(a){var b=e('<table class="groupeditems">');this._renderTableHeader(a).appendTo(b);this._renderGroup(b,a);this._renderTableFooter(a).appendTo(b);return b},_renderTableHeader:function(a){var b=e("<thead>"),d=e('<tr class="gi-column-headers">').appendTo(b),
  c=a.c;a=a.l;for(var l,k=0,g=c.length;k<g;k++)l=a[c[k]],e("<th>").html(l).appendTo(d);return b},_renderTableFooter:function(a){var b=this.options,d=e("<tfoot>"),c=e('<tr class="gi-column-totals">'),l=a.t,k=!1,g;for(g in l){k=!0;break}if(k){a=a.c;for(var f=null,m=0,p=0,h=a.length;p<h;p++){if(!f){if(!l.hasOwnProperty(a[p])){m++;continue}1<m&&(f=e('<td class="gi-column-totals-label" colspan="'+m+'">'),e("<span> "+b.totalsLabel+"</span>").appendTo(f),f.appendTo(c))}g=e("<td>").appendTo(c);(k=l[a[p]])&&
  g.html(k)}c.appendTo(d)}return d},_renderGroup:function(a,b,d,c){"undefined"==typeof d&&(d=b);"undefined"==typeof c&&(c=0);this.options.renderGroupHeaders&&0<c&&this._renderGroupHeader(a,b,d,c);var e=d.d,k=d.i,g,f;if(e)for(g=0,f=e.length;g<f;g++)this._renderGroup(a,b,e[g],c+1);else if(k)for(g=0,f=k.length;g<f;g++)this._renderItem(a,b,k[g],c);0<c&&this._renderGroupFooter(a,b,d,c)},_renderGroupHeader:function(a,b,d,c){b=b.c;d=d.v;c=e('<tr class="gi-group-header gi-level-'+c+'">');e('<td colspan="'+
  b.length+'">').html(d).appendTo(c);c.appendTo(a)},_renderGroupFooter:function(a,b,d,c){var l=this.options,k=d.t,g=!1,f;for(f in k){g=!0;break}b=b.c;d=d.v;c=e('<tr class="gi-group-footer gi-level-'+c+'">');if(!g&&!l.renderGroupHeaders)e('<td colspan="'+b.length+'">').html(d).appendTo(c),c.appendTo(a);else if(g){g=0;f=null;for(var m,p,h=0,n=b.length;h<n;h++){if(!f){if(!k.hasOwnProperty(b[h])){g++;continue}1<g&&(f=e('<td class="gi-group-footer-label" colspan="'+g+'">'),l.renderGroupHeaders||f.html(d),
  e('<span class="gi-group-footer-inline-label"> '+l.totalsLabel+"</span>").appendTo(f),f.appendTo(c))}p=e("<td>").appendTo(c);(m=k[b[h]])&&p.html(m)}c.appendTo(a)}},_renderItem:function(a,b,d,c){c=e('<tr class="gi-item gi-level-'+c+'">');b=b.c;for(var l,k,g=0,f=b.length;g<f;g++)k=e("<td>"),(l=d[b[g]])&&k.html(l),k.appendTo(c);c.appendTo(a)},reload:function(a,b,d){d="undefined"!=typeof d?d:!0;"undefined"==typeof b&&(b=this._getFilters());var c=this,l=!1;e(this.element).find(".gi-throbber").css("visibility",
  "visible");if(a||b)l=this._updateAjaxURL(a,b);l||d?e.ajax({url:this.options.ajaxURL,dataType:"json"}).done(function(a){c.items.val(JSON.stringify(a));c.data=null;c.refresh()}).fail(function(a,b,c){msg="UNAUTHORIZED"==c?i18n.gis_requires_login:a.responseText;console.log(msg)}):c.refresh()},_getFilters:function(){var a=e("#"+this.widget_id+"-filter-form");try{return a.length?S3.search.getCurrentFilters(a.first()):null}catch(b){return null}},_updateAjaxURL:function(a,b){var d=this.options.ajaxURL;if(!d)return!1;
  var c,l=d.split("?"),k;1<l.length?(c=l[1],k=c.split("&")):k=[];var g=[],d=!1;if(a)for(var f in a)c=a[f],c=f+"="+c,d||-1!=e.inArray(c,k)||(d=!0),g.push(c);f={};c={};var m,p,h,n;if(b)for(m=0,p=b.length;m<p;m++)n=b[m],h=n[0],n=n[1],null===n?f[h]||(c[h]=!0):(c[h]&&(c[h]=!1),f[h]?f[h].push(h+"="+n):f[h]=[h+"="+n]);m=0;for(p=k.length;m<p;m++)n=k[m].split("="),1<n.length&&(h=decodeURIComponent(n[0]),n=decodeURIComponent(n[1]),c[h]?d=!0:f[h]?d||-1!=e.inArray(h+"="+n,f[h])||(d=!0):a&&a.hasOwnProperty(h)||
  g.push(k[m]));for(h in f)for(m=0,p=f[h].length;m<p;m++)d||-1!=e.inArray(f[h][m],k)||(d=!0),g.push(f[h][m]);h=g.join("&");l=l[0];h&&(l=l+"?"+h);this.options.ajaxURL=l;return d},_serialize:function(){var a=this.items;if(a){var b="";this.data&&(b=JSON.stringify(this.data));a.val(b)}return b},_deserialize:function(){this.data={};var a=this.items;a&&(a=a.val())&&(this.data=JSON.parse(a));return this.data},_bindEvents:function(){var a=this;e(this.element).delegate(".gi-export","click"+this.namespace,function(){var b=
  e(this).data("url"),d=a._getFilters();d&&(b=S3.search.filterURL(b,d));window.open(b)})},_unbindEvents:function(){e(this.element).undelegate(this.namespace)}})})(jQuery);
  