!function(t){function H5PDataView(e,a,i,o,n,r,s,l){var d=this;d.$container=t(e).addClass("h5p-data-view").html(""),d.source=a,d.headers=i,d.l10n=o,d.classes=void 0===n?{}:n,d.filters=void 0===r?[]:r,d.loaded=s,d.order=l,d.limit=20,d.offset=0,d.filterOn=[],d.facets={},d.loadData()}H5PDataView.prototype.loadData=function(){var e=this;e.setMessage(H5PUtils.throbber(e.l10n.loading));var a,i=e.source;i+=(-1===i.indexOf("?")?"?":"&")+"offset="+e.offset+"&limit="+e.limit,void 0!==e.order&&(i+="&sortBy="+e.order.by+"&sortDir="+e.order.dir);for(var o=0;o<e.filterOn.length;o++)void 0!==e.filterOn[o]&&(a=!0,i+="&filters["+o+"]="+encodeURIComponent(e.filterOn[o]));for(var n in e.facets)e.facets.hasOwnProperty(n)&&(i+="&facets["+n+"]="+e.facets[n].id);t.ajax({dataType:"json",cache:!0,url:i}).fail((function(){e.setMessage(t("<p/>",{text:e.l10n.ajaxFailed}))})).done((function(i){i.rows.length?e.updateTable(i.rows):e.setMessage(t("<p/>",{text:a?e.l10n.noData:e.l10n.empty})),e.updatePagination(i.num),void 0!==e.loaded&&e.loaded()}))},H5PDataView.prototype.setMessage=function(t){var e=this;void 0===e.table?e.$container.html("").append(t):e.table.setBody(t)},H5PDataView.prototype.updateTable=function(e){var a=this;void 0===a.table&&(a.$container.html(""),a.addFilters(),a.$facets=t("<div/>",{class:"h5p-facet-wrapper",appendTo:a.$container}),a.table=new H5PUtils.Table(a.classes,a.headers),a.table.setHeaders(a.headers,(function(t){a.order=t,a.loadData()}),a.order),a.table.appendTo(a.$container));for(var i=0;i<a.headers.length;i++)if(!0===a.headers[i].facet)for(var o=0;o<e.length;o++)e[o][i]=a.createFacets(e[o][i],i);var n=a.table.setRows(e);t(".h5p-facet",n).click((function(){var e=t(this);a.filterByFacet(e.data("col"),e.data("id"),e.text())})).keypress((function(e){if(32===e.which){var i=t(this);a.filterByFacet(i.data("col"),i.data("id"),i.text())}}))},H5PDataView.prototype.createFacets=function(t,e){var a="";if(t instanceof Array)for(var i=0;i<t.length;i++)""!==a&&(a+=", "),a+='<span class="h5p-facet" role="button" tabindex="0" data-id="'+t[i].id+'" data-col="'+e+'">'+t[i].title+"</span>";else a+='<span class="h5p-facet" role="button" tabindex="0" data-id="'+t.id+'" data-col="'+e+'">'+t.title+"</span>";return""===a?"—":a},H5PDataView.prototype.filterByFacet=function(e,a,i){var o=this;if(void 0!==o.facets[e]){if(o.facets[e].id===a)return;o.facets[e].$tag.remove()}o.facets[e]={id:a,$tag:t("<span/>",{class:"h5p-facet-tag",text:i,appendTo:o.$facets})};var remove=function(){o.facets[e].$tag.remove(),delete o.facets[e],o.loadData()};t("<span/>",{role:"button",tabindex:0,appendTo:o.facets[e].$tag,text:o.l10n.remove,title:o.l10n.remove,on:{click:remove,keypress:function(t){32===t.which&&remove()}}}),o.loadData()},H5PDataView.prototype.updatePagination=function(e){var a=this;if(void 0===a.pagination){if(void 0===a.table)return;var i=t("<div/>",{class:"h5p-pagination"});a.pagination=new H5PUtils.Pagination(e,a.limit,(function(t){a.offset=t,a.loadData()}),a.l10n),a.pagination.appendTo(i),a.table.setFoot(i)}else a.pagination.update(e,a.limit)},H5PDataView.prototype.addFilters=function(){for(var t=this,e=0;e<t.filters.length;e++)!0===t.filters[e]&&t.addTextFilter(e)},H5PDataView.prototype.addTextFilter=function(e){var a,i=this,search=function(){var t=o.val().replace(/^\s+|\s+$/g,"");""===t&&(t=void 0),t!==i.filterOn[e]&&(i.filterOn[e]=t,i.loadData())},o=t("<input/>",{type:"text",placeholder:i.l10n.search,on:{blur:function(){clearTimeout(a),search()},keyup:function(t){if(13===t.keyCode)return clearTimeout(a),search(),!1;clearTimeout(a),a=setTimeout((function(){search()}),500)}}}).appendTo(i.$container)}}(H5P.jQuery);