diff --git a/js/h5p-data-view.js b/js/h5p-data-view.js index 2f708f8..3d628bb 100644 --- a/js/h5p-data-view.js +++ b/js/h5p-data-view.js @@ -53,7 +53,17 @@ var H5PDataView = (function ($) { self.filterOn = []; self.facets = {}; - self.loadData(); + // Index of column with author name; could be made more general by passing database column names and checking for position + self.columnIdAuthor = 2; + + // Future option: Create more general solution for filter presets + if (H5PIntegration.user && parseInt(H5PIntegration.user.canToggleViewOthersH5PContents) === 1) { + self.updateTable([]); + self.filterByFacet(self.columnIdAuthor, H5PIntegration.user.id, H5PIntegration.user.name || ''); + } + else { + self.loadData(); + } } /** @@ -151,6 +161,12 @@ var H5PDataView = (function ($) { // Add filters self.addFilters(); + // Add toggler for others' content + if (H5PIntegration.user && parseInt(H5PIntegration.user.canToggleViewOthersH5PContents) > 0) { + // canToggleViewOthersH5PContents = 1 is setting for only showing current user's contents + self.addOthersContentToggler(parseInt(H5PIntegration.user.canToggleViewOthersH5PContents) === 1); + } + // Add facets self.$facets = $('
', { 'class': 'h5p-facet-wrapper', @@ -246,13 +262,17 @@ var H5PDataView = (function ($) { appendTo: self.$facets, }) }; - /** * Callback for removing filter. * * @private */ var remove = function () { + // Uncheck toggler for others' H5P contents + if ( self.$othersContentToggler && self.facets.hasOwnProperty( self.columnIdAuthor ) ) { + self.$othersContentToggler.prop('checked', false ); + } + self.facets[col].$tag.remove(); delete self.facets[col]; self.loadData(); @@ -374,5 +394,49 @@ var H5PDataView = (function ($) { }).appendTo(self.$container); }; + /** + * Add toggle for others' H5P content. + * @param {boolean} [checked=false] Initial check setting. + */ + H5PDataView.prototype.addOthersContentToggler = function (checked) { + var self = this; + + checked = (typeof checked === 'undefined') ? false : checked; + + // Checkbox + this.$othersContentToggler = $('', { + type: 'checkbox', + 'class': 'h5p-others-contents-toggler', + 'id': 'h5p-others-contents-toggler', + 'checked': checked, + 'click': function () { + if ( this.checked ) { + // Add filter on current user + self.filterByFacet( self.columnIdAuthor, H5PIntegration.user.id, H5PIntegration.user.name ); + } + else { + // Remove facet indicator and reload full data view + if ( self.facets.hasOwnProperty( self.columnIdAuthor ) && self.facets[self.columnIdAuthor].$tag ) { + self.facets[self.columnIdAuthor].$tag.remove(); + } + delete self.facets[self.columnIdAuthor]; + self.loadData(); + } + } + }); + + // Label + var $label = $('