2010-02-27 01:16:44 +01:00
|
|
|
function ListFacet(div, config, options, selection) {
|
2010-01-30 02:05:30 +01:00
|
|
|
this._div = div;
|
|
|
|
this._config = config;
|
2010-02-02 02:36:02 +01:00
|
|
|
|
|
|
|
this._options = options || {};
|
|
|
|
if (!("sort" in this._options)) {
|
|
|
|
this._options.sort = "name";
|
|
|
|
}
|
|
|
|
|
2010-02-27 01:16:44 +01:00
|
|
|
this._selection = selection || [];
|
2010-01-30 02:05:30 +01:00
|
|
|
this._data = null;
|
|
|
|
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
2010-02-27 01:16:44 +01:00
|
|
|
ListFacet.reconstruct = function(div, uiState) {
|
|
|
|
return new ListFacet(div, uiState.c, uiState.o, uiState.s);
|
|
|
|
};
|
|
|
|
|
|
|
|
ListFacet.prototype.getUIState = function() {
|
|
|
|
var json = {
|
|
|
|
c: this.getJSON(),
|
|
|
|
o: this._options
|
|
|
|
};
|
|
|
|
|
|
|
|
json.s = json.c.selection;
|
|
|
|
delete json.c.selection;
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2010-01-30 02:05:30 +01:00
|
|
|
ListFacet.prototype.getJSON = function() {
|
2010-02-19 00:27:40 +01:00
|
|
|
var o = {
|
|
|
|
type: "list",
|
|
|
|
name: this._config.name,
|
|
|
|
columnName: this._config.columnName,
|
|
|
|
expression: this._config.expression,
|
|
|
|
selection: []
|
|
|
|
}
|
2010-01-30 02:05:30 +01:00
|
|
|
for (var i = 0; i < this._selection.length; i++) {
|
2010-02-19 00:27:40 +01:00
|
|
|
var choice = {
|
|
|
|
v: cloneDeep(this._selection[i].v)
|
|
|
|
};
|
2010-01-30 02:05:30 +01:00
|
|
|
o.selection.push(choice);
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
};
|
|
|
|
|
2010-02-01 01:19:41 +01:00
|
|
|
ListFacet.prototype.hasSelection = function() {
|
|
|
|
return this._selection.length > 0;
|
|
|
|
};
|
|
|
|
|
2010-01-30 02:05:30 +01:00
|
|
|
ListFacet.prototype.updateState = function(data) {
|
|
|
|
this._data = data;
|
|
|
|
|
|
|
|
var selection = [];
|
|
|
|
var choices = data.choices;
|
|
|
|
for (var i = 0; i < choices.length; i++) {
|
|
|
|
var choice = choices[i];
|
|
|
|
if (choice.s) {
|
|
|
|
selection.push(choice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._selection = selection;
|
2010-02-02 02:36:02 +01:00
|
|
|
this._reSortChoices();
|
2010-01-30 02:05:30 +01:00
|
|
|
|
|
|
|
this.render();
|
|
|
|
};
|
|
|
|
|
2010-02-02 02:36:02 +01:00
|
|
|
ListFacet.prototype._reSortChoices = function() {
|
|
|
|
this._data.choices.sort(this._options.sort == "name" ?
|
|
|
|
function(a, b) {
|
|
|
|
return a.v.l.localeCompare(b.v.l);
|
|
|
|
} :
|
|
|
|
function(a, b) {
|
|
|
|
return b.c - a.c;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2010-01-30 02:05:30 +01:00
|
|
|
ListFacet.prototype.render = function() {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var scrollTop = 0;
|
|
|
|
try {
|
|
|
|
scrollTop = this._div[0].childNodes[1].scrollTop;
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
var container = this._div.empty();
|
|
|
|
|
|
|
|
var headerDiv = $('<div></div>').addClass("facet-title").appendTo(container);
|
|
|
|
$('<span></span>').text(this._config.name).appendTo(headerDiv);
|
|
|
|
|
2010-02-26 23:24:25 +01:00
|
|
|
var removeButton = $('<img>')
|
|
|
|
.attr("src", "images/close.png")
|
|
|
|
.attr("title", "Remove this facet")
|
|
|
|
.addClass("facet-choice-link")
|
|
|
|
.click(function() {
|
|
|
|
self._remove();
|
|
|
|
}).prependTo(headerDiv);
|
2010-02-01 01:19:41 +01:00
|
|
|
|
2010-01-30 02:05:30 +01:00
|
|
|
var bodyDiv = $('<div></div>').addClass("facet-body").appendTo(container);
|
2010-02-02 02:36:02 +01:00
|
|
|
if (!("scroll" in this._options) || this._options.scroll) {
|
|
|
|
bodyDiv.addClass("facet-body-scrollable");
|
|
|
|
}
|
|
|
|
|
2010-01-30 02:05:30 +01:00
|
|
|
if (this._data == null) {
|
|
|
|
bodyDiv.html("Loading...");
|
|
|
|
} else {
|
|
|
|
var selectionCount = this._selection.length;
|
|
|
|
if (selectionCount > 0) {
|
|
|
|
var reset = function() {
|
|
|
|
self._reset();
|
|
|
|
};
|
2010-02-01 01:19:41 +01:00
|
|
|
removeButton.after(
|
|
|
|
$('<a href="javascript:{}"></a>').addClass("facet-choice-link").text("reset").click(reset)
|
|
|
|
);
|
2010-01-30 02:05:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var renderChoice = function(choice) {
|
|
|
|
var label = choice.v.l;
|
|
|
|
var count = choice.c;
|
|
|
|
|
|
|
|
var choiceDiv = $('<div></div>').addClass("facet-choice").appendTo(bodyDiv);
|
|
|
|
if (choice.s) {
|
|
|
|
choiceDiv.addClass("facet-choice-selected");
|
|
|
|
}
|
|
|
|
|
|
|
|
var a = $('<a href="javascript:{}"></a>').addClass("facet-choice-label").text(label).appendTo(choiceDiv);
|
|
|
|
$('<span></span>').addClass("facet-choice-count").text(count).appendTo(choiceDiv);
|
|
|
|
|
|
|
|
var select = function() {
|
|
|
|
self._select(choice, false);
|
|
|
|
};
|
|
|
|
var selectOnly = function() {
|
|
|
|
self._select(choice, true);
|
|
|
|
};
|
|
|
|
var deselect = function() {
|
|
|
|
self._deselect(choice);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (choice.s) { // selected
|
|
|
|
if (selectionCount > 1) {
|
|
|
|
// select only
|
|
|
|
a.click(selectOnly);
|
|
|
|
} else {
|
|
|
|
// deselect
|
|
|
|
a.click(deselect);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove link
|
|
|
|
$('<a href="javascript:{}"></a>').addClass("facet-choice-link").text("remove").click(deselect).prependTo(choiceDiv);
|
|
|
|
} else if (selectionCount > 0) {
|
|
|
|
a.click(selectOnly);
|
|
|
|
|
|
|
|
// include link
|
|
|
|
$('<a href="javascript:{}"></a>').addClass("facet-choice-link").text("include").click(select).appendTo(choiceDiv);
|
|
|
|
} else {
|
|
|
|
a.click(select);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var choices = this._data.choices;
|
|
|
|
for (var i = 0; i < choices.length; i++) {
|
|
|
|
renderChoice(choices[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
bodyDiv[0].scrollTop = scrollTop;
|
2010-02-02 02:36:02 +01:00
|
|
|
|
|
|
|
var footerDiv = $('<div></div>').addClass("facet-footer").appendTo(container);
|
2010-02-26 23:24:25 +01:00
|
|
|
|
|
|
|
$('<span>').text(choices.length + " choices: ").appendTo(footerDiv);
|
2010-02-02 02:36:02 +01:00
|
|
|
if (this._options.sort == "name") {
|
2010-02-23 21:20:14 +01:00
|
|
|
$('<a href="javascript:{}"></a>').addClass("action").text("re-sort by count").click(function() {
|
2010-02-02 02:36:02 +01:00
|
|
|
self._options.sort = "count";
|
|
|
|
self._reSortChoices();
|
|
|
|
self.render();
|
|
|
|
}).appendTo(footerDiv);
|
|
|
|
} else {
|
2010-02-23 21:20:14 +01:00
|
|
|
$('<a href="javascript:{}"></a>').addClass("action").text("re-sort by name").click(function() {
|
2010-02-02 02:36:02 +01:00
|
|
|
self._options.sort = "name";
|
|
|
|
self._reSortChoices();
|
|
|
|
self.render();
|
|
|
|
}).appendTo(footerDiv);
|
|
|
|
}
|
2010-01-30 02:05:30 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ListFacet.prototype._select = function(choice, only) {
|
|
|
|
if (only) {
|
|
|
|
this._selection = [];
|
|
|
|
}
|
|
|
|
this._selection.push(choice);
|
|
|
|
this._updateRest();
|
|
|
|
};
|
|
|
|
|
|
|
|
ListFacet.prototype._deselect = function(choice) {
|
|
|
|
for (var i = this._selection.length - 1; i >= 0; i--) {
|
|
|
|
if (this._selection[i] == choice) {
|
|
|
|
this._selection.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._updateRest();
|
|
|
|
};
|
|
|
|
|
|
|
|
ListFacet.prototype._reset = function() {
|
|
|
|
this._selection = [];
|
|
|
|
this._updateRest();
|
|
|
|
};
|
|
|
|
|
2010-02-01 01:19:41 +01:00
|
|
|
ListFacet.prototype._remove = function() {
|
|
|
|
ui.browsingEngine.removeFacet(this);
|
|
|
|
|
|
|
|
this._div = null;
|
|
|
|
this._config = null;
|
|
|
|
this._selection = null;
|
|
|
|
this._data = null;
|
|
|
|
};
|
|
|
|
|
2010-01-30 02:05:30 +01:00
|
|
|
ListFacet.prototype._updateRest = function() {
|
2010-02-26 22:56:41 +01:00
|
|
|
Gridworks.update({ engineChanged: true });
|
2010-02-01 01:19:41 +01:00
|
|
|
};
|