').text(this._data.error).addClass("facet-body-message"));
return;
}
var scrollTop = 0;
if (!resetScroll) {
try {
scrollTop = this._elmts.bodyInnerDiv[0].scrollTop;
} catch (e) {
}
}
this._elmts.bodyInnerDiv.empty();
//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);
this._elmts.choiceCountContainer.text(choices.length + " choices");
if (selectionCount > 0) {
this._elmts.resetButton.show();
} else {
this._elmts.resetButton.hide();
}
if (this._options.sort == "name") {
this._elmts.sortByNameLink.addClass("facet-mode-link-selected");
this._elmts.sortByCountLink.removeClass("facet-mode-link-selected");
} else {
this._elmts.sortByNameLink.removeClass("facet-mode-link-selected");
this._elmts.sortByCountLink.addClass("facet-mode-link-selected");
}
var html = [];
var temp = $('
');
var encodeHtml = function(s) {
return temp.text(s).html();
};
var renderEdit = this._config.expression == "value";
var renderChoice = function(index, choice, customLabel) {
var label = customLabel || choice.v.l;
var count = choice.c;
html.push('
');
};
for (var i = 0; i < choices.length; i++) {
renderChoice(i, choices[i]);
}
if (this._blankChoice !== null) {
renderChoice(-1, this._blankChoice, "(blank)");
}
if (this._errorChoice !== null) {
renderChoice(-2, this._errorChoice, "(error)");
}
this._elmts.bodyInnerDiv.html(html.join(''));
this._elmts.bodyInnerDiv[0].scrollTop = scrollTop;
var getChoice = function(elmt) {
var index = parseInt(elmt.attr("choiceIndex"));
if (index == -1) {
return self._blankChoice;
} else if (index == -2) {
return self._errorChoice;
} else {
return choices[index];
}
};
var findChoice = function(elmt) {
return getChoice(elmt.closest('.facet-choice'));
};
var select = function(choice) {
self._select(choice, false);
};
var selectOnly = function(choice) {
self._select(choice, true);
};
var deselect = function(choice) {
self._deselect(choice);
};
var wireEvents = function() {
var bodyInnerDiv = self._elmts.bodyInnerDiv;
bodyInnerDiv.find('.facet-choice-label').click(function() {
var choice = findChoice($(this));
if (choice.s) {
if (selectionCount > 1) {
selectOnly(choice);
} else {
deselect(choice);
}
} else if (selectionCount > 0) {
selectOnly(choice);
} else {
select(choice);
}
});
bodyInnerDiv.find('.facet-choice-edit').click(function() {
var choice = findChoice($(this));
self._editChoice(choice, $(this).closest('.facet-choice'));
});
bodyInnerDiv.find('.facet-choice').mouseenter(function() {
$(this).find('.facet-choice-edit').css("visibility", "visible");
var choice = getChoice($(this));
if (!choice.s) {
$(this).find('.facet-choice-toggle').css("visibility", "visible");
}
}).mouseleave(function() {
$(this).find('.facet-choice-edit').css("visibility", "hidden");
var choice = getChoice($(this));
if (!choice.s) {
$(this).find('.facet-choice-toggle').css("visibility", "hidden");
}
});
bodyInnerDiv.find('.facet-choice-toggle').click(function() {
var choice = findChoice($(this));
if (choice.s) {
deselect(choice);
} else {
select(choice);
}
});
}
window.setTimeout(wireEvents, 100);
};
ListFacet.prototype._doEdit = function() {
new ClusteringDialog(this._config.columnName, this._config.expression);
};
ListFacet.prototype._editChoice = function(choice, choiceDiv) {
var self = this;
var menu = MenuSystem.createMenu().addClass("data-table-cell-editor").width("400px");
menu.html(
'
' +
'' +
'' +
'' +
' | ' +
'
' +
'' +
'' +
' ' +
'Enter' +
' | ' +
'' +
' ' +
'Esc' +
' | ' +
'' +
' | ' +
'
' +
'
'
);
var elmts = DOM.bind(menu);
MenuSystem.showMenu(menu, function(){});
MenuSystem.positionMenuLeftRight(menu, choiceDiv);
var originalContent;
if (choice === this._blankChoice) {
originalContent = "(blank)";
} else if (choice === this._errorChoice) {
originalContent = "(error)";
} else {
originalContent = choice.v.v;
}
var commit = function() {
var text = elmts.textarea[0].value;
MenuSystem.dismissAll();
var edit = { to : text };
if (choice === self._blankChoice) {
edit.fromBlank = true;
} else if (choice === self._errorChoice) {
edit.fromError = true;
} else {
edit.from = [ originalContent ]
}
Gridworks.postProcess(
"mass-edit",
{},
{
columnName: self._config.columnName,
expression: "value",
edits: JSON.stringify([ edit ])
},
{
// limit edits to rows constrained only by the other facets
engineConfig: ui.browsingEngine.getJSON(false, self),
cellsChanged: true
},
{
onDone: function(o) {
var selection = [];
var gotSelection = false;
for (var i = 0; i < self._selection.length; i++) {
var choice = self._selection[i];
if (choice.v.v == originalContent) {
if (gotSelection) {
continue;
}
choice.v.v = text;
gotSelection = true; // eliminate duplicated selections due to changing one selected choice to another
}
selection.push(choice);
}
self._selection = selection;
}
}
);
};
elmts.okButton.click(commit);
elmts.textarea
.text(originalContent)
.keydown(function(evt) {
if (!evt.shiftKey) {
if (evt.keyCode == 13) {
commit();
} else if (evt.keyCode == 27) {
MenuSystem.dismissAll();
}
}
})
.select()
.focus();
elmts.cancelButton.click(function() {
MenuSystem.dismissAll();
});
};
ListFacet.prototype._select = function(choice, only) {
if (only) {
this._selection = [];
if (this._blankChoice !== null) {
this._blankChoice.s = false;
}
if (this._errorChoice !== null) {
this._errorChoice.s = false;
}
}
choice.s = true;
if (choice !== this._errorChoice && choice !== this._blankChoice) {
this._selection.push(choice);
}
this._updateRest();
};
ListFacet.prototype._deselect = function(choice) {
if (choice === this._errorChoice || choice === this._blankChoice) {
choice.s = false;
} else {
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._blankChoice = null;
this._errorChoice = null;
this._updateRest();
};
ListFacet.prototype._remove = function() {
ui.browsingEngine.removeFacet(this);
this._div = null;
this._config = null;
this._selection = null;
this._blankChoice = null;
this._errorChoice = null;
this._data = null;
};
ListFacet.prototype._updateRest = function() {
Gridworks.update({ engineChanged: true });
};
ListFacet.prototype._editExpression = function() {
var self = this;
var title = (this._config.columnName) ?
("Edit Facet's Expression based on Column " + this._config.columnName) :
"Edit Facet's Expression"
var column = Gridworks.columnNameToColumn(this._config.columnName);
var o = DataTableView.sampleVisibleRows(column);
new ExpressionPreviewDialog(
title,
column ? column.cellIndex : -1,
o.rowIndices,
o.values,
this._config.expression,
function(expr) {
if (expr != self._config.expression) {
self._config.expression = expr;
self._elmts.expressionDiv.text(self._config.expression);
if (self._config.expression == "value" || self._config.expression == "gel:value") {
self._elmts.clusterLink.show();
} else {
self._elmts.clusterLink.hide();
}
self.reset();
self._updateRest();
}
}
);
};