@gpa-gemstone/common-pages
Version:
Common UI pages for GPA products
267 lines (266 loc) • 11.9 kB
JavaScript
"use strict";
//******************************************************************************************************
// ControllerFunctions.tsx - Gbtc
//
// Copyright (c) 2024, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA may license this file to you under the MIT License (MIT), the "License"; you may not use this
// file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 04/02/2024 - C. Lackner
// Generated original version of source code.
//
//******************************************************************************************************
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReadWriteControllerFunctions = exports.ReadOnlyControllerFunctions = void 0;
var jquery_1 = __importDefault(require("jquery"));
var ReadOnlyControllerFunctions = /** @class */ (function () {
function ReadOnlyControllerFunctions(APIPath) {
var _this = this;
this.GetPageInfo = function (filter, parentID) {
var route = parentID == null ? "".concat(_this.APIPath, "/PageInfo") : "".concat(_this.APIPath, "/PageInfo/").concat(parentID);
if (filter === undefined || filter.length === 0)
return jquery_1.default.ajax({
type: 'GET',
url: "".concat(route),
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
async: true
});
return jquery_1.default.ajax({
type: 'POST',
url: "".concat(route),
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify({ Searches: filter, OrderBy: '', Ascending: false }),
cache: false,
async: true
});
};
this.GetOne = function (id) {
return jquery_1.default.ajax({
type: 'GET',
url: "".concat(_this.APIPath, "/One/").concat(id),
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
async: true
});
};
this.GetPage = function (page, sortField, asc, parentID) {
if (sortField == null || asc == null) {
if (parentID != null) {
return jquery_1.default.ajax({
type: 'GET',
url: "".concat(_this.APIPath, "/").concat(page, "/").concat(parentID),
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
async: true
});
}
return jquery_1.default.ajax({
type: 'GET',
url: "".concat(_this.APIPath, "/").concat(page),
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
async: true
});
}
if (parentID != null)
return jquery_1.default.ajax({
type: 'GET',
url: "".concat(_this.APIPath, "/").concat(page, "/").concat(parentID, "/").concat(sortField.toString(), "/").concat(asc),
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
async: true
});
return jquery_1.default.ajax({
type: 'GET',
url: "".concat(_this.APIPath, "/").concat(page, "/").concat(sortField.toString(), "/").concat(asc),
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
async: true
});
};
/**
* Use with caution as this could be VERY network resource intensive.
*/
this.GetAll = function (sortField, asc, filter, parentID) {
var deferred = jquery_1.default.Deferred();
var pending = [];
// First, determine the number of pages.
var pagReq = _this.GetPageInfo(filter !== null && filter !== void 0 ? filter : [], parentID).done(function (pageInfo) {
var pageCount = pageInfo.PageCount;
if (pageCount <= 0) {
deferred.resolve([]);
return;
}
// Build an array of page requests.
var pageRequests = [];
for (var i = 0; i < pageCount; i++) {
var req = _this.SearchPage(i, sortField, asc, filter !== null && filter !== void 0 ? filter : [], parentID);
pageRequests.push(req);
pending.push(req);
}
// Combine all page requests.
jquery_1.default.when.apply(jquery_1.default, pageRequests).done(function () {
var _a;
var results = [];
for (var _i = 0; _i < arguments.length; _i++) {
results[_i] = arguments[_i];
}
// If there's only one page, 'arguments' is not an array of arrays.
var responses = pageRequests.length === 1 ? [results] : jquery_1.default.makeArray(results);
var pages = responses.map(function (result) { return result[0]; });
var allData = (_a = []).concat.apply(_a, pages);
deferred.resolve(allData);
}).fail(function (err) { return deferred.reject(err); });
}).fail(function (err) { return deferred.reject(err); });
pending.push(pagReq);
// Attach an abort handler to cancel all pending requests.
var promise = deferred.promise();
promise.abort = function () {
pending.forEach(function (req) { return req.abort(); });
deferred.reject('Aborted');
};
return promise;
};
this.SearchPage = function (page, sortField, asc, filter, parentID) {
if (parentID != null)
return jquery_1.default.ajax({
type: 'POST',
url: "".concat(_this.APIPath, "/Search/").concat(page, "/").concat(parentID),
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify({
Searches: filter,
OrderBy: sortField,
Ascending: asc
}),
cache: false,
async: true
});
return jquery_1.default.ajax({
type: 'POST',
url: "".concat(_this.APIPath, "/Search/").concat(page),
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify({
Searches: filter,
OrderBy: sortField,
Ascending: asc
}),
cache: false,
async: true
});
};
this.New = function () {
return jquery_1.default.ajax({
type: 'GET',
url: "".concat(_this.APIPath, "/New"),
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
async: true
});
};
this.GetMaxValue = function (fieldName) {
return jquery_1.default.ajax({
type: 'GET',
url: "".concat(_this.APIPath, "/Max/").concat(fieldName),
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
async: true
});
};
this.APIPath = APIPath;
}
return ReadOnlyControllerFunctions;
}());
exports.ReadOnlyControllerFunctions = ReadOnlyControllerFunctions;
var ReadWriteControllerFunctions = /** @class */ (function (_super) {
__extends(ReadWriteControllerFunctions, _super);
function ReadWriteControllerFunctions(APIPath) {
var _this = _super.call(this, APIPath) || this;
_this.Add = function (record) {
return jquery_1.default.ajax({
type: 'POST',
url: "".concat(_this.APIPath),
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify(record),
cache: false,
async: true
});
};
_this.Update = function (record) {
return jquery_1.default.ajax({
type: 'PATCH',
url: "".concat(_this.APIPath),
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify(record),
cache: false,
async: true
});
};
_this.Delete = function (record) {
return jquery_1.default.ajax({
type: 'DELETE',
url: "".concat(_this.APIPath),
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify(record),
cache: false,
async: true
});
};
_this.DeleteByID = function (id) {
return jquery_1.default.ajax({
type: 'DELETE',
url: "".concat(_this.APIPath, "/").concat(id),
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
async: true
});
};
return _this;
}
return ReadWriteControllerFunctions;
}(ReadOnlyControllerFunctions));
exports.ReadWriteControllerFunctions = ReadWriteControllerFunctions;