diff --git a/src/main/webapp/scripts/facets/list-facet.js b/src/main/webapp/scripts/facets/list-facet.js index 829c5cbe1..c689b7a86 100644 --- a/src/main/webapp/scripts/facets/list-facet.js +++ b/src/main/webapp/scripts/facets/list-facet.js @@ -13,7 +13,8 @@ function ListFacet(div, config, options, selection) { this._data = null; - this.render(); + this._initializeUI(); + this._update(); } ListFacet.reconstruct = function(div, uiState) { @@ -87,7 +88,7 @@ ListFacet.prototype.updateState = function(data) { this._errorChoice = data.errorChoice || null; } - this.render(); + this._update(); }; ListFacet.prototype._reSortChoices = function() { @@ -101,113 +102,120 @@ ListFacet.prototype._reSortChoices = function() { ); }; -ListFacet.prototype.render = function() { +ListFacet.prototype._initializeUI = function() { var self = this; - var scrollTop = 0; - try { - scrollTop = this._div[0].childNodes[1].scrollTop; - } catch (e) { - } - - var container = this._div.empty().show().html( + this._div.empty().show().html( '
' + '' + '' + - '
' + '' + + '' + + '' + + '
' ); - var elmts = DOM.bind(container); + this._elmts = DOM.bind(this._div); - elmts.titleSpan.text(this._config.name); - elmts.removeButton.click(function() { self._remove(); }); - - var bodyDiv = $('
').addClass("facet-body"); - if (!("scroll" in this._options) || this._options.scroll) { - bodyDiv.addClass("facet-body-scrollable"); + this._elmts.titleSpan.text(this._config.name); + this._elmts.removeButton.click(function() { self._remove(); }); + this._elmts.resetButton.click(function() { self._reset(); }); + + this._elmts.sortByCountLink.click(function() { + if (self._options.sort != "count") { + self._options.sort = "count"; + self._reSortChoices(); + self._update(true); + } + }); + this._elmts.sortByNameLink.click(function() { + if (self._options.sort != "name") { + self._options.sort = "name"; + self._reSortChoices(); + self._update(true); + } + }); + + if (this._config.expression == "value") { + this._elmts.clusterLink.click(function() { self._doEdit(); }); + } else { + this._elmts.clusterLink.hide(); } + if (!("scroll" in this._options) || this._options.scroll) { + this._elmts.bodyDiv.addClass("facet-body-scrollable"); + } +}; + +ListFacet.prototype._update = function(resetScroll) { + var self = this; + + var scrollTop = 0; + if (!resetScroll) { + try { + scrollTop = this._elmts.bodyDiv[0].scrollTop; + } catch (e) { + } + } + + this._elmts.bodyDiv.empty(); + if (!this._data) { - $('
').text("Loading...").addClass("facet-body-message").appendTo(bodyDiv); - bodyDiv.appendTo(container); - + this._elmts.statusDiv.hide(); + this._elmts.controlsDiv.hide(); + + $('
').text("Loading...").addClass("facet-body-message").appendTo(this._elmts.bodyDiv); } else if ("error" in this._data) { - $('
').text(this._data.error).addClass("facet-body-message").appendTo(bodyDiv); - bodyDiv.appendTo(container); + this._elmts.statusDiv.hide(); + this._elmts.controlsDiv.hide(); + $('
').text(this._data.error).addClass("facet-body-message").appendTo(this._elmts.bodyDiv); } else { + this._elmts.statusDiv.show(); + this._elmts.controlsDiv.show(); + var choices = this._data.choices; var selectionCount = this._selection.length + (this._blankChoice !== null && this._blankChoice.s ? 1 : 0) + (this._errorChoice !== null && this._errorChoice.s ? 1 : 0); - - /* - * Status - */ - var statusDiv = $( - '
' + - '' + - '' + - '' + - '
' + choices.length + ' choicesCluster
' + - '
' - ).appendTo(container); - - var statusElmts = DOM.bind(statusDiv); - if (this._config.expression == "value") { - statusElmts.clusterLink.click(function() { self._doEdit(); }); - } else { - statusElmts.clusterLink.hide(); - } - - /* - * Controls - */ - var controlsDiv = $( - '
' + - '' + - '' + - '' + - '
Sort by ' + - 'Name ' + - 'Count' + - '' + - 'Reset' + - '
' + - '
' - ).appendTo(container); - - var controlsElmts = DOM.bind(controlsDiv); + + this._elmts.choiceCountContainer.text(choices.length + " choices"); if (selectionCount > 0) { - controlsElmts.resetButton.click(function() { self._reset(); }); + this._elmts.resetButton.show(); } else { - controlsElmts.resetButton.hide(); + this._elmts.resetButton.hide(); } if (this._options.sort == "name") { - controlsElmts.sortByNameLink.addClass("facet-mode-link-selected"); - controlsElmts.sortByCountLink.click(function() { - self._options.sort = "count"; - self._reSortChoices(); - self.render(); - }); + this._elmts.sortByNameLink.addClass("facet-mode-link-selected"); + this._elmts.sortByCountLink.removeClass("facet-mode-link-selected"); } else { - controlsElmts.sortByCountLink.addClass("facet-mode-link-selected"); - controlsElmts.sortByNameLink.click(function() { - self._options.sort = "name"; - self._reSortChoices(); - self.render(); - }); + this._elmts.sortByNameLink.removeClass("facet-mode-link-selected"); + this._elmts.sortByCountLink.addClass("facet-mode-link-selected"); } - /* - * Body - */ + var choiceContainer = $('
'); + var renderEdit = this._config.expression == "value"; var renderChoice = function(choice, customLabel) { var label = customLabel || choice.v.l; var count = choice.c; - var choiceDiv = $('
').addClass("facet-choice").appendTo(bodyDiv); + var choiceDiv = $('
').addClass("facet-choice").appendTo(choiceContainer); if (choice.s) { choiceDiv.addClass("facet-choice-selected"); } @@ -225,16 +233,16 @@ ListFacet.prototype.render = function() { self._deselect(choice); }; + var editLink = $('') + .addClass("facet-choice-link") + .text("edit") + .css("visibility", "hidden") + .prependTo(choiceDiv); + if (renderEdit && customLabel === undefined) { - // edit link - var editLink = $('') - .addClass("facet-choice-link") - .text("edit") - .css("visibility", "hidden") - .click(function() { - self._editChoice(choice, choiceDiv); - }) - .prependTo(choiceDiv); + editLink.click(function() { + self._editChoice(choice, choiceDiv); + }) choiceDiv .mouseenter(function() { @@ -245,6 +253,13 @@ ListFacet.prototype.render = function() { }); } + var includeExcludeLink = $('') + .addClass("facet-choice-link") + .text("include") + .css("visibility", "hidden") + .click(deselect) + .prependTo(choiceDiv); + if (choice.s) { // selected if (selectionCount > 1) { // select only @@ -254,30 +269,22 @@ ListFacet.prototype.render = function() { a.click(deselect); } - // exclude link - $('') - .addClass("facet-choice-link") + includeExcludeLink .text("exclude") - .click(deselect) - .prependTo(choiceDiv); + .css("visibility", "visible") + .click(deselect); } else if (selectionCount > 0) { a.click(selectOnly); - // include link - var includeLink = $('') - .addClass("facet-choice-link") - .text("include") - .css("visibility", "hidden") - .click(select) - .prependTo(choiceDiv); + includeExcludeLink.click(select); choiceDiv .mouseenter(function() { - includeLink.css("visibility", "visible"); + includeExcludeLink.css("visibility", "visible"); }) .mouseleave(function() { - includeLink.css("visibility", "hidden"); + includeExcludeLink.css("visibility", "hidden"); }); } else { a.click(select); @@ -294,8 +301,8 @@ ListFacet.prototype.render = function() { renderChoice(this._errorChoice, "(error)"); } - bodyDiv.appendTo(container); - bodyDiv[0].scrollTop = scrollTop; + this._elmts.bodyDiv.append(choiceContainer); + this._elmts.bodyDiv[0].scrollTop = scrollTop; } };