@easyquery/core
Version:
EasyQuery.JS core modules
260 lines • 9.59 kB
JavaScript
import { eqi18n } from "../i18n/i18n";
import { DataModel } from "../dm/data_model";
import { Query } from "../query/query";
var EqContext = /** @class */ (function () {
function EqContext() {
this.internalListCache = {};
this.useBootstrap = false;
this.loadModelOnStart = true;
this.loadQueryOnStart = false;
this.dataModel = new DataModel();
this.query = new Query(this.dataModel);
this.widgets = new Array();
}
EqContext.prototype.init = function (options) {
var _this = this;
options = options || {};
if (options.defaultQueryId) {
this.query.setId(options.defaultQueryId);
}
if (options.listCache) {
this.setExternalListCache(options.listCache);
}
if (options.broker) {
this.broker = options.broker.resolver(this);
}
if (options.defaultQueryId) {
this.query.setId(options.defaultQueryId);
}
if (options.locale) {
eqi18n.setLocale(options.locale);
}
/*
if (options.menuOptions) {
var mmo = this.menuOptions;
var omo = options.menuOptions;
for (var attr in mmo) {
if (mmo.hasOwnProperty(attr) && omo.hasOwnProperty(attr))
mmo[attr] = omo[attr];
}
}
*/
this.loadQueryOnStart = false;
if (options.loadQueryOnStart) {
this.loadQueryOnStart = options.loadQueryOnStart;
}
this.loadModelOnStart = true;
if (typeof options.loadModelOnStart !== 'undefined') {
this.loadModelOnStart = options.loadModelOnStart;
}
if (options.handlers) {
if (options.handlers.onInit) {
this.onInit = options.handlers.onInit;
}
if (options.handlers.onLoadModel) {
this.onLoadModel = options.handlers.onLoadModel;
}
if (options.handlers.onLoadQuery) {
this.onLoadQuery = options.handlers.onLoadQuery;
}
if (options.handlers.onInitialModelLoad) {
this.onInitialModelLoad = options.handlers.onInitialModelLoad;
}
if (options.handlers.onProcessStart) {
this.onProcessStart = options.handlers.onProcessStart;
}
if (options.handlers.onProcessEnd) {
this.onProcessEnd = options.handlers.onProcessEnd;
}
if (options.handlers.onError) {
this.onError = options.handlers.onError;
}
if (options.handlers.listRequestHandler) {
this.clientListRequestHandler = options.handlers.listRequestHandler;
}
}
this.initialQuery = options.initialQuery;
this.defaultQueryId = options.defaultQueryId || "";
this.defaultModelId = options.defaultModelId || "";
if (options.broker) {
this.broker.init(options.broker);
}
//init widgets
this.widgets.forEach(function (widget) {
var widgetOptions = options.widgets[widget.widgetType] || {};
widget.init(_this, widgetOptions);
widget.refresh();
});
if (this.loadModelOnStart) {
this.loadDefaultModel();
}
};
EqContext.prototype.addWidget = function (widget) {
this.widgets.push(widget);
};
EqContext.prototype.refreshWidgets = function () {
this.widgets.forEach(function (widget) {
widget.refresh();
});
};
EqContext.prototype.startLoadingWidgets = function () {
this.widgets.forEach(function (widget) {
widget.startLoading();
});
};
EqContext.prototype.finishLoadingWidgets = function () {
this.widgets.forEach(function (widget) {
widget.finishLoading();
});
};
EqContext.prototype.addQueryChangedCallback = function (callback) {
this.query.addChangedCallback(callback);
};
EqContext.prototype.removeQueryChangedCallback = function (callback) {
this.query.removeChangedCallback(callback);
};
EqContext.prototype.getBroker = function () {
return this.broker;
};
EqContext.prototype.getQuery = function () {
return this.query;
};
EqContext.prototype.getModel = function () {
return this.dataModel;
};
EqContext.prototype.setModel = function (dataModel) {
this.query.loadModelData(dataModel);
this.dataModel = this.query.getModel();
};
EqContext.prototype.setDefaultModelId = function (modelId) {
this.defaultModelId = modelId;
};
EqContext.prototype.fireProcessStart = function (message) {
if (this.onProcessStart) {
this.onProcessStart(message);
}
};
EqContext.prototype.fireProcessEnd = function (message) {
if (this.onProcessEnd) {
this.onProcessEnd(message);
}
};
EqContext.prototype.throwError = function (error) {
if (this.onError) {
this.onError(error);
}
else {
var msgTitle = error && error.type ? error.type + " error" : "Error";
var msgText = error && error.text ? error.text : "";
console.error(msgTitle + ": " + msgText);
}
};
EqContext.prototype.loadDefaultModel = function () {
var self = this;
this.broker.loadModel({
modelId: self.defaultModelId,
beforeSend: function () {
self.startLoadingWidgets();
},
success: function (data) {
self.finishLoadingWidgets();
self.refreshWidgets();
var query = self.getQuery();
if (self.initialQuery) {
query.setData(self.initialQuery);
}
if (self.loadQueryOnStart) {
self.broker.loadQuery({
id: self.defaultQueryId,
modelId: self.defaultModelId,
success: function () {
//if we load query on start - then we should call onInit only after its successful loading
if (self.onInit) {
self.onInit();
}
}
});
}
else {
if (self.onInit) {
self.onInit();
}
}
if (self.onInitialModelLoad) {
self.onInitialModelLoad(data);
}
}
});
};
EqContext.prototype.clearQuery = function () {
this.query.clear();
this.refreshWidgets();
};
EqContext.prototype.setExternalListCache = function (cache) {
this.externalListCache = cache;
};
EqContext.prototype.getListFromCache = function (params) {
var key = params.listName == "SQL" ? "SQL_" + params.editorId : params.listName;
if (this.externalListCache) {
return this.externalListCache.get(key);
}
else {
return this.internalListCache[key];
}
};
EqContext.prototype.addListToCache = function (params, list) {
var key = params.listName == "SQL" ? "SQL_" + params.editorId : params.listName;
if (this.externalListCache) {
this.externalListCache.set(key, list);
}
else {
this.internalListCache[key] = list;
}
};
EqContext.prototype.resetListCache = function () {
if (this.externalListCache) {
this.externalListCache.clear();
}
else {
this.internalListCache = {};
}
};
EqContext.prototype.getListRequestHandler = function () {
var self = this;
var handler = function (params, onResult) {
if (params == null || !params.listName)
return;
var dynamicParam = params.listName.match(/{{(.+?)}}/);
if (dynamicParam) {
var paramValue = self.query.getOneValueForAttr(dynamicParam[1]);
if (!paramValue)
paramValue = "";
params.listName = params.listName.replace(/{{.+?}}/, paramValue);
}
var list = self.getListFromCache(params);
if (list && list.length > 0) {
onResult(list);
return;
}
var processed = false;
if (self.clientListRequestHandler) {
processed = self.clientListRequestHandler(params, onResult);
}
if (!processed) {
self.broker.getRequestList({
params: params,
success: function (data) {
if (data) {
self.addListToCache(params, data);
}
onResult(data);
}
});
}
};
return handler;
};
return EqContext;
}());
export { EqContext };
//# sourceMappingURL=context.js.map