2010-02-03 10:27:53 +01:00
|
|
|
function ExpressionPreviewDialog(title, cellIndex, rowIndices, values, expression, onDone) {
|
|
|
|
this._onDone = onDone;
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
var frame = DialogSystem.createDialog();
|
2010-02-07 10:02:22 +01:00
|
|
|
frame.width("700px");
|
2010-02-03 10:27:53 +01:00
|
|
|
|
|
|
|
var header = $('<div></div>').addClass("dialog-header").text(title).appendTo(frame);
|
|
|
|
var body = $('<div></div>').addClass("dialog-body").appendTo(frame);
|
|
|
|
var footer = $('<div></div>').addClass("dialog-footer").appendTo(frame);
|
2010-03-03 19:08:35 +01:00
|
|
|
var html = $(ExpressionPreviewDialog.generateWidgetHtml()).appendTo(body);
|
2010-02-03 10:27:53 +01:00
|
|
|
|
2010-02-23 01:33:39 +01:00
|
|
|
this._elmts = DOM.bind(html);
|
2010-02-03 10:27:53 +01:00
|
|
|
|
|
|
|
$('<button></button>').html(" OK ").click(function() {
|
2010-02-07 10:02:22 +01:00
|
|
|
DialogSystem.dismissUntil(self._level - 1);
|
2010-03-03 22:21:38 +01:00
|
|
|
self._onDone(self._previewWidget.getExpression(true));
|
2010-02-03 10:27:53 +01:00
|
|
|
}).appendTo(footer);
|
|
|
|
|
|
|
|
$('<button></button>').text("Cancel").click(function() {
|
2010-02-07 10:02:22 +01:00
|
|
|
DialogSystem.dismissUntil(self._level - 1);
|
2010-02-03 10:27:53 +01:00
|
|
|
}).appendTo(footer);
|
2010-02-23 01:33:39 +01:00
|
|
|
|
|
|
|
this._level = DialogSystem.showDialog(frame);
|
2010-03-03 19:08:35 +01:00
|
|
|
this._previewWidget = new ExpressionPreviewDialog.Widget(
|
|
|
|
this._elmts,
|
|
|
|
cellIndex,
|
|
|
|
rowIndices,
|
|
|
|
values,
|
|
|
|
expression
|
|
|
|
);
|
2010-04-08 21:52:23 +02:00
|
|
|
}
|
2010-03-03 19:08:35 +01:00
|
|
|
|
|
|
|
ExpressionPreviewDialog.generateWidgetHtml = function() {
|
2010-03-13 08:13:18 +01:00
|
|
|
return '<div class="grid-layout layout-tight layout-full"><table rows="4" cols="2">' +
|
2010-03-03 19:08:35 +01:00
|
|
|
'<tr>' +
|
|
|
|
'<td>Expression</td>' +
|
|
|
|
'<td>Language</td>' +
|
|
|
|
'</tr>' +
|
|
|
|
'<tr>' +
|
2010-03-13 08:13:18 +01:00
|
|
|
'<td rowspan="2"><div class="input-container"><textarea class="expression-preview-code" bind="expressionPreviewTextarea" /></div></td>' +
|
2010-03-08 22:02:04 +01:00
|
|
|
'<td width="150" height="1">' +
|
2010-03-03 19:08:35 +01:00
|
|
|
'<select bind="expressionPreviewLanguageSelect">' +
|
2010-03-16 19:11:08 +01:00
|
|
|
'<option value="gel">Gridworks expression language (GEL)</option>' +
|
2010-03-03 19:08:35 +01:00
|
|
|
'<option value="jython">Jython</option>' +
|
|
|
|
'<option value="clojure">Clojure</option>' +
|
|
|
|
'</select>' +
|
|
|
|
'</td>' +
|
|
|
|
'</tr>' +
|
|
|
|
'<tr>' +
|
2010-03-04 01:00:46 +01:00
|
|
|
'<td class="expression-preview-error-container" bind="expressionPreviewErrorContainer" width="150" style="vertical-align: top;"></td>' +
|
2010-03-03 19:08:35 +01:00
|
|
|
'</tr>' +
|
|
|
|
'<tr>' +
|
|
|
|
'<td colspan="2">' +
|
2010-04-26 02:10:36 +02:00
|
|
|
'<div id="expression-preview-tabs" class="gridworks-tabs">' +
|
2010-03-03 19:08:35 +01:00
|
|
|
'<ul>' +
|
|
|
|
'<li><a href="#expression-preview-tabs-preview">Preview</a></li>' +
|
|
|
|
'<li><a href="#expression-preview-tabs-history">History</a></li>' +
|
|
|
|
'<li><a href="#expression-preview-tabs-help">Help</a></li>' +
|
|
|
|
'</ul>' +
|
|
|
|
'<div id="expression-preview-tabs-preview">' +
|
|
|
|
'<div class="expression-preview-container" bind="expressionPreviewPreviewContainer"></div>' +
|
|
|
|
'</div>' +
|
2010-03-04 01:00:46 +01:00
|
|
|
'<div id="expression-preview-tabs-history" style="display: none;">' +
|
2010-03-03 19:08:35 +01:00
|
|
|
'<div class="expression-preview-container" bind="expressionPreviewHistoryContainer"></div>' +
|
|
|
|
'</div>' +
|
|
|
|
'<div id="expression-preview-tabs-help" style="display: none;">' +
|
|
|
|
'<div class="expression-preview-help-container" bind="expressionPreviewHelpTabBody"></div>' +
|
|
|
|
'</div>' +
|
|
|
|
'</div>' +
|
|
|
|
'</td>' +
|
|
|
|
'</tr>' +
|
2010-03-13 08:13:18 +01:00
|
|
|
'</table></div>';
|
2010-03-03 19:08:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
ExpressionPreviewDialog.Widget = function(
|
|
|
|
elmts,
|
|
|
|
cellIndex,
|
|
|
|
rowIndices,
|
|
|
|
values,
|
|
|
|
expression
|
|
|
|
) {
|
2010-05-27 03:13:11 +02:00
|
|
|
var language = "gel";
|
|
|
|
if (!(expression)) {
|
|
|
|
language = $.cookie("scripting.lang");
|
|
|
|
if (language == "jython") {
|
|
|
|
this.expression = "return value";
|
|
|
|
} else if (language == "clojure") {
|
|
|
|
this.expression = "value";
|
|
|
|
} else {
|
|
|
|
language = "gel";
|
|
|
|
this.expression = "value";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.expression = expression;
|
|
|
|
|
|
|
|
var colon = expression.indexOf(":");
|
|
|
|
if (colon > 0) {
|
|
|
|
var l = expression.substring(0, colon);
|
|
|
|
if (l == "gel" || l == "jython" || l == "clojure") {
|
|
|
|
this.expression = expression.substring(colon + 1);
|
|
|
|
language = l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-03 19:08:35 +01:00
|
|
|
this._elmts = elmts;
|
|
|
|
this._cellIndex = cellIndex;
|
|
|
|
this._rowIndices = rowIndices;
|
|
|
|
this._values = values;
|
|
|
|
|
|
|
|
this._results = null;
|
|
|
|
this._timerID = null;
|
|
|
|
|
2010-02-23 01:33:39 +01:00
|
|
|
$("#expression-preview-tabs").tabs();
|
2010-03-04 01:00:46 +01:00
|
|
|
$("#expression-preview-tabs-history").css("display", "");
|
2010-02-23 01:33:39 +01:00
|
|
|
$("#expression-preview-tabs-help").css("display", "");
|
2010-03-04 03:45:54 +01:00
|
|
|
|
|
|
|
this._elmts.expressionPreviewLanguageSelect[0].value = language;
|
2010-05-27 03:13:11 +02:00
|
|
|
this._elmts.expressionPreviewLanguageSelect.bind("change", function() {
|
|
|
|
$.cookie("scripting.lang", this.value);
|
|
|
|
self.update();
|
|
|
|
});
|
2010-02-23 01:33:39 +01:00
|
|
|
|
2010-03-03 19:08:35 +01:00
|
|
|
var self = this;
|
|
|
|
this._elmts.expressionPreviewTextarea
|
|
|
|
.attr("value", this.expression)
|
|
|
|
.keyup(function(){
|
|
|
|
self._scheduleUpdate();
|
|
|
|
})
|
|
|
|
.select()
|
|
|
|
.focus();
|
|
|
|
|
2010-03-04 03:25:42 +01:00
|
|
|
this.update();
|
2010-03-03 22:21:38 +01:00
|
|
|
this._renderExpressionHistoryTab();
|
2010-02-23 01:33:39 +01:00
|
|
|
this._renderHelpTab();
|
2010-02-07 10:02:22 +01:00
|
|
|
};
|
|
|
|
|
2010-03-03 22:21:38 +01:00
|
|
|
ExpressionPreviewDialog.Widget.prototype.getExpression = function(commit) {
|
|
|
|
var s = $.trim(this.expression || "");
|
2010-04-08 22:16:08 +02:00
|
|
|
if (!s.length) {
|
2010-03-03 22:21:38 +01:00
|
|
|
return null;
|
|
|
|
}
|
2010-03-04 03:45:54 +01:00
|
|
|
|
|
|
|
s = this._getLanguage() + ":" + s;
|
2010-03-03 22:21:38 +01:00
|
|
|
if (commit) {
|
|
|
|
$.post(
|
|
|
|
"/command/log-expression?" + $.param({ project: theProject.id, expression: s }),
|
|
|
|
null,
|
|
|
|
function(data) {
|
|
|
|
},
|
|
|
|
"json"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
};
|
|
|
|
|
2010-03-04 03:45:54 +01:00
|
|
|
ExpressionPreviewDialog.Widget.prototype._getLanguage = function() {
|
|
|
|
return this._elmts.expressionPreviewLanguageSelect[0].value;
|
2010-04-08 21:52:23 +02:00
|
|
|
};
|
2010-03-04 03:45:54 +01:00
|
|
|
|
2010-03-03 19:08:35 +01:00
|
|
|
ExpressionPreviewDialog.Widget.prototype._renderHelpTab = function() {
|
2010-02-07 10:02:22 +01:00
|
|
|
var self = this;
|
|
|
|
$.getJSON(
|
|
|
|
"/command/get-expression-language-info",
|
|
|
|
null,
|
|
|
|
function(data) {
|
2010-02-23 01:33:39 +01:00
|
|
|
self._renderHelp(data);
|
2010-02-07 10:02:22 +01:00
|
|
|
},
|
|
|
|
"json"
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2010-03-03 19:08:35 +01:00
|
|
|
ExpressionPreviewDialog.Widget.prototype._renderHelp = function(data) {
|
|
|
|
var elmt = this._elmts.expressionPreviewHelpTabBody.empty();
|
2010-02-07 10:02:22 +01:00
|
|
|
|
2010-02-23 20:06:58 +01:00
|
|
|
$('<h3></h3>').text("Variables").appendTo(elmt);
|
|
|
|
var varTable = $('<table width="100%" cellspacing="5"></table>').appendTo(elmt)[0];
|
|
|
|
var vars = [
|
|
|
|
{ name: "cell",
|
|
|
|
description: "The current cell. It has a few fields: 'value' and 'recon'."
|
|
|
|
},
|
|
|
|
{ name: "value",
|
|
|
|
description: "The current cell's value. This is a shortcut for 'cell.value'."
|
|
|
|
},
|
|
|
|
{ name: "row",
|
|
|
|
description: "The current row. It has 4 fields: 'flagged', 'starred', 'index', and 'cells'."
|
|
|
|
},
|
|
|
|
{ name: "cells",
|
|
|
|
description: "The cells of the current row. This is a shortcut for 'row.cells'. " +
|
|
|
|
"A particular cell can be retrieved with 'cells.<column name>' if the <column name> is a single word, " +
|
|
|
|
"or with 'cells[\"<column name>\"] otherwise."
|
|
|
|
},
|
|
|
|
{ name: "rowIndex",
|
|
|
|
description: "The current row's index. This is a shortcut for 'row.index'."
|
|
|
|
}
|
|
|
|
];
|
|
|
|
for (var i = 0; i < vars.length; i++) {
|
|
|
|
var variable = vars[i];
|
|
|
|
var tr = varTable.insertRow(varTable.rows.length);
|
|
|
|
$(tr.insertCell(0)).addClass("expression-preview-doc-item-title").text(variable.name);
|
|
|
|
$(tr.insertCell(1)).addClass("expression-preview-doc-item-desc").text(variable.description);
|
|
|
|
}
|
|
|
|
|
2010-02-23 02:40:06 +01:00
|
|
|
var renderEntry = function(table, name, entry) {
|
|
|
|
var tr0 = table.insertRow(table.rows.length);
|
|
|
|
var tr1 = table.insertRow(table.rows.length);
|
|
|
|
var tr2 = table.insertRow(table.rows.length);
|
|
|
|
|
2010-02-23 20:06:58 +01:00
|
|
|
$(tr0.insertCell(0)).addClass("expression-preview-doc-item-title").text(name);
|
|
|
|
$(tr0.insertCell(1)).addClass("expression-preview-doc-item-params").text("(" + entry.params + ")");
|
2010-02-23 02:40:06 +01:00
|
|
|
|
|
|
|
$(tr1.insertCell(0));
|
2010-02-23 20:06:58 +01:00
|
|
|
$(tr1.insertCell(1)).addClass("expression-preview-doc-item-returns").text("returns: " + entry.returns);
|
2010-02-23 02:40:06 +01:00
|
|
|
|
|
|
|
$(tr2.insertCell(0));
|
2010-02-23 20:06:58 +01:00
|
|
|
$(tr2.insertCell(1)).addClass("expression-preview-doc-item-desc").text(entry.description);
|
|
|
|
};
|
|
|
|
var renderEntries = function(table, map) {
|
|
|
|
var names = [];
|
|
|
|
for (var n in map) {
|
|
|
|
if (map.hasOwnProperty(n)) {
|
|
|
|
names.push(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
names.sort();
|
|
|
|
|
|
|
|
for (var i = 0; i < names.length; i++) {
|
|
|
|
var name = names[i];
|
|
|
|
renderEntry(table, name, map[name]);
|
|
|
|
}
|
2010-02-23 02:40:06 +01:00
|
|
|
};
|
2010-02-07 10:02:22 +01:00
|
|
|
|
2010-02-23 02:40:06 +01:00
|
|
|
$('<h3></h3>').text("Functions").appendTo(elmt);
|
2010-02-23 20:06:58 +01:00
|
|
|
var functionTable = $('<table width="100%" cellspacing="5"></table>').appendTo(elmt)[0];
|
|
|
|
renderEntries(functionTable, data.functions);
|
2010-02-07 10:02:22 +01:00
|
|
|
|
|
|
|
$('<h3></h3>').text("Controls").appendTo(elmt);
|
2010-02-23 20:06:58 +01:00
|
|
|
var controlTable = $('<table width="100%" cellspacing="5"></table>').appendTo(elmt)[0];
|
|
|
|
renderEntries(controlTable, data.controls);
|
2010-02-07 10:02:22 +01:00
|
|
|
};
|
|
|
|
|
2010-03-03 22:21:38 +01:00
|
|
|
ExpressionPreviewDialog.Widget.prototype._renderExpressionHistoryTab = function() {
|
|
|
|
var self = this;
|
|
|
|
$.getJSON(
|
|
|
|
"/command/get-expression-history?" + $.param({ project: theProject.id }),
|
|
|
|
null,
|
|
|
|
function(data) {
|
|
|
|
self._renderExpressionHistory(data);
|
|
|
|
},
|
|
|
|
"json"
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ExpressionPreviewDialog.Widget.prototype._renderExpressionHistory = function(data) {
|
|
|
|
var self = this;
|
|
|
|
var elmt = this._elmts.expressionPreviewHistoryContainer.empty();
|
|
|
|
|
|
|
|
var table = $(
|
|
|
|
'<table width="100%" cellspacing="5">' +
|
2010-05-27 03:13:11 +02:00
|
|
|
'<tr><th></th><th>From</th><th colspan="2">Expression</th><th></th></tr>' +
|
2010-03-03 22:21:38 +01:00
|
|
|
'</table>'
|
|
|
|
).appendTo(elmt)[0];
|
|
|
|
|
|
|
|
var renderEntry = function(entry) {
|
|
|
|
var tr = table.insertRow(table.rows.length);
|
2010-03-04 08:18:23 +01:00
|
|
|
var o = Scripting.parse(entry.code);
|
2010-03-03 22:21:38 +01:00
|
|
|
|
2010-05-27 03:13:11 +02:00
|
|
|
$('<a href="javascript:{}">Reuse</a>').appendTo(tr.insertCell(0)).click(function() {
|
2010-03-04 08:18:23 +01:00
|
|
|
self._elmts.expressionPreviewTextarea[0].value = o.expression;
|
|
|
|
self._elmts.expressionPreviewLanguageSelect[0].value = o.language;
|
|
|
|
|
|
|
|
$("#expression-preview-tabs").tabs('option', 'selected', 0);
|
|
|
|
|
2010-03-03 22:21:38 +01:00
|
|
|
self._elmts.expressionPreviewTextarea.select().focus();
|
2010-03-04 08:18:23 +01:00
|
|
|
|
2010-03-04 03:25:42 +01:00
|
|
|
self.update();
|
2010-03-03 22:21:38 +01:00
|
|
|
});
|
2010-03-16 19:11:08 +01:00
|
|
|
|
2010-05-27 03:13:11 +02:00
|
|
|
$(tr.insertCell(1)).html(entry.global ? "Other projects" : "This project");
|
|
|
|
$(tr.insertCell(2)).text(o.language + ":");
|
|
|
|
$(tr.insertCell(3)).text(o.expression);
|
2010-03-03 22:21:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
for (var i = 0; i < data.expressions.length; i++) {
|
|
|
|
var entry = data.expressions[i];
|
|
|
|
renderEntry(entry);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-03-03 19:08:35 +01:00
|
|
|
ExpressionPreviewDialog.Widget.prototype._scheduleUpdate = function() {
|
2010-04-09 03:00:44 +02:00
|
|
|
if (this._timerID !== null) {
|
2010-02-03 10:27:53 +01:00
|
|
|
window.clearTimeout(this._timerID);
|
|
|
|
}
|
|
|
|
var self = this;
|
2010-03-04 03:25:42 +01:00
|
|
|
this._timerID = window.setTimeout(function() { self.update(); }, 300);
|
2010-02-03 10:27:53 +01:00
|
|
|
};
|
|
|
|
|
2010-03-04 03:25:42 +01:00
|
|
|
ExpressionPreviewDialog.Widget.prototype.update = function() {
|
2010-02-03 10:27:53 +01:00
|
|
|
var self = this;
|
2010-03-03 19:08:35 +01:00
|
|
|
var expression = this.expression = $.trim(this._elmts.expressionPreviewTextarea[0].value);
|
2010-03-04 03:25:42 +01:00
|
|
|
var params = {
|
|
|
|
project: theProject.id,
|
2010-03-04 03:45:54 +01:00
|
|
|
expression: this._getLanguage() + ":" + expression,
|
2010-03-04 03:25:42 +01:00
|
|
|
cellIndex: this._cellIndex
|
|
|
|
};
|
|
|
|
this._prepareUpdate(params);
|
2010-02-03 10:27:53 +01:00
|
|
|
|
|
|
|
$.post(
|
2010-03-04 03:25:42 +01:00
|
|
|
"/command/preview-expression?" + $.param(params),
|
2010-02-03 10:27:53 +01:00
|
|
|
{
|
|
|
|
rowIndices: JSON.stringify(this._rowIndices)
|
|
|
|
},
|
|
|
|
function(data) {
|
|
|
|
if (data.code != "error") {
|
|
|
|
self._results = data.results;
|
|
|
|
} else {
|
|
|
|
self._results = null;
|
|
|
|
}
|
2010-02-27 06:48:33 +01:00
|
|
|
self._renderPreview(expression, data);
|
2010-02-03 10:27:53 +01:00
|
|
|
},
|
|
|
|
"json"
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2010-03-04 03:25:42 +01:00
|
|
|
ExpressionPreviewDialog.Widget.prototype._prepareUpdate = function(params) {
|
|
|
|
};
|
|
|
|
|
2010-03-03 19:08:35 +01:00
|
|
|
ExpressionPreviewDialog.Widget.prototype._renderPreview = function(expression, data) {
|
|
|
|
var container = this._elmts.expressionPreviewPreviewContainer.empty();
|
2010-02-07 04:36:40 +01:00
|
|
|
var table = $('<table width="100%"></table>').appendTo(container)[0];
|
|
|
|
|
2010-02-03 10:27:53 +01:00
|
|
|
var tr = table.insertRow(0);
|
2010-02-07 04:36:40 +01:00
|
|
|
$(tr.insertCell(0)).addClass("expression-preview-heading").text("row");
|
|
|
|
$(tr.insertCell(1)).addClass("expression-preview-heading").text("value");
|
|
|
|
$(tr.insertCell(2)).addClass("expression-preview-heading").text(expression);
|
|
|
|
|
|
|
|
var renderValue = function(td, v) {
|
2010-02-07 10:02:22 +01:00
|
|
|
if (v !== null && v !== undefined) {
|
2010-04-06 07:35:48 +02:00
|
|
|
if ($.isPlainObject(v)) {
|
2010-03-01 01:21:13 +01:00
|
|
|
$('<span></span>').addClass("expression-preview-special-value").text("Error: " + v.message).appendTo(td);
|
2010-02-07 10:02:22 +01:00
|
|
|
} else {
|
2010-04-06 07:35:48 +02:00
|
|
|
td.text(v);
|
2010-02-07 10:02:22 +01:00
|
|
|
}
|
2010-02-07 04:36:40 +01:00
|
|
|
} else {
|
2010-03-01 01:21:13 +01:00
|
|
|
$('<span>null</span>').addClass("expression-preview-special-value").appendTo(td);
|
2010-02-07 04:36:40 +01:00
|
|
|
}
|
|
|
|
};
|
2010-02-03 10:27:53 +01:00
|
|
|
|
2010-04-09 03:00:44 +02:00
|
|
|
if (this._results !== null) {
|
2010-03-03 19:08:35 +01:00
|
|
|
this._elmts.expressionPreviewErrorContainer.empty();
|
|
|
|
} else {
|
|
|
|
var message = (data.type == "parser") ? data.message : "Internal error";
|
|
|
|
this._elmts.expressionPreviewErrorContainer.empty().text(message);
|
|
|
|
}
|
|
|
|
|
2010-02-03 10:27:53 +01:00
|
|
|
for (var i = 0; i < this._values.length; i++) {
|
|
|
|
var tr = table.insertRow(table.rows.length);
|
|
|
|
|
2010-02-07 04:36:40 +01:00
|
|
|
$(tr.insertCell(0)).attr("width", "1%").html((this._rowIndices[i] + 1) + ".");
|
|
|
|
|
|
|
|
renderValue($(tr.insertCell(1)), this._values[i]);
|
|
|
|
|
|
|
|
var tdValue = $(tr.insertCell(2));
|
2010-04-09 03:00:44 +02:00
|
|
|
if (this._results !== null) {
|
2010-02-03 10:27:53 +01:00
|
|
|
var v = this._results[i];
|
2010-02-07 04:36:40 +01:00
|
|
|
renderValue(tdValue, v);
|
2010-02-03 10:27:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|