trc-sheet
Version:
trc sheet support
315 lines (314 loc) • 12.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var core = require("trc-core/core");
var XC = require("trc-httpshim/xclient");
var sheetContents_1 = require("./sheetContents");
var Validators = (function () {
function Validators() {
}
Validators.ValidateColumnName = function (name) {
if (name == null) {
throw 'Column Name is missing';
}
if (!Validators._columnNameRegex.test(name)) {
throw 'Column Name is not valid: ' + name;
}
};
Validators.ValidateHelper = function (columnName, description, possibleValues) {
Validators.ValidateColumnName(columnName);
if (description != null) {
if (description.length > 400) {
throw "Description is too long for '" + columnName + "'";
}
}
if (possibleValues != null) {
if (possibleValues.length > 15) {
throw "Too many possible values in question '" + columnName + "'";
}
for (var k in possibleValues) {
var item = possibleValues[k];
if (!item) {
throw "possible values can't be null in question '" + columnName + "'";
}
if (item.length > 75) {
throw "Possible value is too long for '" + columnName + "'";
}
}
}
};
Validators.ValidateColumnInfo = function (info) {
Validators.ValidateHelper(info.Name, info.Description, info.PossibleValues);
if (!!info.Expression && !!info.Semantic) {
throw "Can't send both Expression and Semantic properties on a column: '" + info.Name + "'.";
}
if (!!info.Expression && !info.IsReadOnly) {
throw "Expression columns must be read-only: '" + info.Name + "'.";
}
};
Validators.ValidateAddColumn = function (payload) {
Validators.ValidateHelper(payload.ColumnName, payload.Description, payload.PossibleValues);
};
Validators._columnNameRegex = new RegExp("^[a-zA-Z0-9_]+$");
return Validators;
}());
exports.Validators = Validators;
exports.PolygonKind = "_polygon";
var NextLinkEnumerable = (function () {
function NextLinkEnumerable(firstSegment, http) {
this._firstSegment = firstSegment;
this._http = http;
}
NextLinkEnumerable.prototype.ForEach = function (handler) {
var _this = this;
return new Promise(function (resolve, reject) {
var worker = function (de) {
var results = de.Results;
if (results != null) {
for (var i in results) {
var item = results[i];
try {
handler(item);
}
catch (e) {
reject(e);
}
}
}
if (de.NextLink != null) {
_this._http.getAsync(de.NextLink).then(function (segment) {
worker(segment);
}).catch(function (error) { return reject(error); });
}
else {
resolve();
}
};
worker(_this._firstSegment);
});
};
return NextLinkEnumerable;
}());
var SheetClient = (function () {
function SheetClient(http, sheetId) {
this._http = http;
this._sheetId = sheetId;
}
SheetClient.prototype.getId = function () {
return this._sheetId;
};
SheetClient.prototype.getSheetById = function (idChild) {
return new SheetClient(this._http, idChild);
};
SheetClient.prototype.getUrlBase = function (path) {
if (path === void 0) { path = ""; }
var url = new XC.UrlBuilder("/sheets/" + this._sheetId + path);
return url;
};
SheetClient.prototype.getInfoAsync = function () {
var url = this.getUrlBase("/info");
return this._http.getAsync(url);
};
SheetClient.prototype.getSheetContentsAsync = function (whereExpression, selectColumns, version) {
var url = this.getUrlBase();
url.addQuery("filter", whereExpression);
if (selectColumns != null && selectColumns != undefined) {
url.addQuery("select", selectColumns.join());
}
url.addQuery("version", version);
return this._http.getAsync(url);
};
SheetClient.prototype.getRecIdsAsync = function () {
var selectColumns = [sheetContents_1.ColumnNames.RecId];
return this.getSheetContentsAsync(null, selectColumns).then(function (contents) {
return contents[sheetContents_1.ColumnNames.RecId];
});
};
SheetClient.prototype.postUpdateSingleRowAsync = function (recId, columnNames, newValues) {
var body = sheetContents_1.SheetContents.FromRow(recId, columnNames, newValues);
return this.postUpdateAsync(body);
};
SheetClient.prototype.postUpdateSingleCellAsync = function (recId, columnName, newValue) {
var body = sheetContents_1.SheetContents.FromSingleCell(recId, columnName, newValue);
return this.postUpdateAsync(body);
};
SheetClient.prototype.postUpdateAsync = function (values) {
var url = this.getUrlBase();
return this._http.sendAsync("POST", url, values);
};
SheetClient.prototype.postExport = function (kind) {
var url = this.getUrlBase("/offline?kind=" + kind);
return this._http.sendAsync("POST", url);
};
SheetClient.prototype.getDeltaRangeAsync = function (startVersion, endVersion) {
var _this = this;
var uri = this.getUrlBase("/deltas");
uri.addQuery("start", startVersion);
uri.addQuery("end", endVersion);
return this._http.getAsync(uri).then(function (segment) {
return new NextLinkEnumerable(segment, _this._http);
});
};
SheetClient.prototype.getDeltaAsync = function (version) {
var uri = this.getUrlBase("/history/" + version);
return this._http.getAsync(uri);
};
SheetClient.prototype.findVersionAsync = function (timestamp) {
var uri = this.getUrlBase("/history/find?timestamp=" + timestamp.toISOString());
return this._http.getAsync(uri).
then(function (result) { return result.VersionNumber; });
};
SheetClient.prototype.getRebaseLogAsync = function () {
var _this = this;
var uri = this.getUrlBase("/history/rebase");
return this._http.getAsync(uri).
then(function (segment) { return new NextLinkEnumerable(segment, _this._http); });
};
SheetClient.prototype.getChildrenAsync = function () {
var uri = this.getUrlBase("/child");
return this._http.getAsync(uri).
then(function (result) { return result.ChildrenIds; });
};
SheetClient.prototype.createChildSheet = function (body) {
var _this = this;
var uri = this.getUrlBase("/child");
return this._http.postAsync(uri, body).
then(function (result) { return _this.getSheetById(result.Id); });
};
SheetClient.prototype.createChildSheetFromFilterAsync = function (name, whereExpression, sharesSandbox) {
var body = {
Name: name,
Filter: {
WhereExpression: whereExpression
},
RecIds: null,
ShareSandbox: sharesSandbox,
};
return this.createChildSheet(body);
};
SheetClient.prototype.deleteChildSheetAsync = function (childSheetId) {
var uri = this.getUrlBase("/child/" + childSheetId);
return this._http.deleteAsync(uri);
};
SheetClient.prototype.postCustomDataAsync = function (kind, dataId, body) {
var url = this.getUrlBase("/data/" + kind + "/" + dataId);
return this._http.sendAsync("POST", url, body);
};
SheetClient.prototype.getCustomDataAsync = function (kind, dataId) {
var url = this.getUrlBase("/data/" + kind + "/" + dataId);
return this._http.getAsync(url);
};
SheetClient.prototype.deleteCustomDataAsync = function (kind, dataId) {
var url = this.getUrlBase("/data/" + kind + "/" + dataId);
return this._http.deleteAsync(url);
};
SheetClient.prototype.listCustomDataAsync = function (kind) {
var url = this.getUrlBase("/data/" + kind);
return this._http.getAsync(url).then(function (list) {
return new core.ArrayEnumerable(list.Entries);
});
};
return SheetClient;
}());
exports.SheetClient = SheetClient;
var SheetAdminClient = (function () {
function SheetAdminClient(sheetClient) {
this._client = sheetClient;
}
SheetAdminClient.prototype.postNewExpressionAsync = function (name, expression) {
try {
Validators.ValidateColumnName(name);
var body = {
Name: name,
IsReadOnly: true,
Expression: expression
};
var uri = this._client.getUrlBase("/columns/" + name);
return this._client._http.postAsync(uri, body);
}
catch (error) {
return Promise.reject(error);
}
};
SheetAdminClient.prototype.getOpStatusAsync = function () {
var uri = this._client.getUrlBase("/ops");
return this._client._http.getAsync(uri);
};
SheetAdminClient.prototype.postOpAsync = function (kind, payload) {
var body = {
SheetId: this._client._sheetId,
Kind: kind,
Cookie: null,
Payload: payload
};
var uri = this._client.getUrlBase("/ops");
return this._client._http.putAsync(uri, body);
};
SheetAdminClient.prototype.postOpRefreshAsync = function () {
var payload = {};
return this.postOpAsync("RefreshContents", payload);
};
SheetAdminClient.prototype.postOpResetAllPins = function () {
var payload = {};
return this.postOpAsync("ResetPins", payload);
};
SheetAdminClient.prototype.postOpResetSomePins = function (recId) {
var payload = { RecIds: recId };
return this.postOpAsync("ResetPins", payload);
};
SheetAdminClient.prototype.postOpSetTopologyAsync = function (topology) {
return this.postOpAsync("SetTopology", topology);
};
SheetAdminClient.prototype.postOpCreateOrUpdateColumnsAsync = function (columns) {
var payload = {
Infos: columns
};
try {
columns.forEach(function (item) { return Validators.ValidateColumnInfo(item); });
}
catch (error) {
return Promise.reject(error);
}
return this.postOpAsync("AddColumns", payload);
};
SheetAdminClient.prototype.postOpAddQuestionAsync = function (questions) {
var payload = {
Columns: questions
};
try {
for (var i in questions) {
var item = questions[i];
Validators.ValidateAddColumn(item);
}
}
catch (error) {
return Promise.reject(error);
}
return this.postOpAsync("AddColumns", payload);
};
SheetAdminClient.prototype.postOpDeleteQuestionAsync = function (columnName) {
var payload = {
ColumnName: columnName
};
return this.postOpAsync("DeleteColumn", payload);
};
SheetAdminClient.prototype.WaitAsync = function () {
var _this = this;
return new Promise(function (resolve, reject) {
_this.waitHelper(resolve, reject);
});
};
SheetAdminClient.prototype.waitHelper = function (resolve, reject) {
var _this = this;
this.getOpStatusAsync().then(function (value) {
if (value.CurrentOp == null) {
resolve();
return;
}
setTimeout(function () {
_this.waitHelper(resolve, reject);
}, 5 * 1000);
}).catch(function (err) { return reject(err); });
};
return SheetAdminClient;
}());
exports.SheetAdminClient = SheetAdminClient;