@angular-jz/jz
Version:
jim jz
207 lines • 9.11 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
var router_1 = require("@angular/router");
var ng_bootstrap_1 = require("@ng-bootstrap/ng-bootstrap");
var angular2_toaster_1 = require("angular2-toaster");
var service_1 = require("../../service");
var utility_1 = require("../../utility");
var JZPageComponentConfig = /** @class */ (function () {
function JZPageComponentConfig() {
this.needLoadPage = true;
}
return JZPageComponentConfig;
}());
exports.JZPageComponentConfig = JZPageComponentConfig;
var JZPageComponent = /** @class */ (function () {
///constructor(public injector: Injector, config: JZPageComponentConfig),需要子类传入config来初始化页面
function JZPageComponent(injector, config) {
var _this = this;
this.injector = injector;
this.operating = false;
this.isPageChanged = true;
///配置页面使用的服务pageService以及页面加载时是否自动调用loadPage方法来加载页面初始化数据
this.config = new JZPageComponentConfig();
///标识页面运行时是否以对话框形成展现
this.isDialog = false;
///页面的queryString集合
this.queryParams = {};
this._criteria = {};
///页面上用到的字典项,key value形式,会在loadPage页面初始化事件中加载 编辑删除
this.dics = {};
///操作成功后的回调函数,默认行为是弹出“操作成功”提示框
this.operationSucess = function (data) {
_this.toasterService.pop('success', '', '操作成功!');
};
///操作失败后的回调函数,默认行为是弹出“操作失败”提示框
this.operationFailed = function (error) {
//this.toasterService.pop('error', '', error);
_this.toasterService.pop('error', '', error.json().error.message);
};
this.handleError = function (error) {
//服务器端返回的UserFriendlyException信息,被Failed回调函数接收,如果Failed回调存在的话。
if (!error)
return;
switch (error.status) {
case 401:
_this.toasterService.pop('error', '', '用户无权限');
break;
case 500:
if (console)
console.warn(error);
var errorMessage = error.json().error.message;
if (errorMessage && errorMessage == 'InternalServerError') {
_this.toasterService.pop('error', '', '服务器异常');
}
else {
_this.toasterService.pop('error', '', errorMessage); //用户友好错误信息
}
break;
default:
_this.toasterService.pop('error', '', '服务器异常');
}
};
this.pageService = config.pageService;
this.activeModal = injector.get(ng_bootstrap_1.NgbActiveModal);
this.modalService = injector.get(ng_bootstrap_1.NgbModal);
this.toasterService = injector.get(angular2_toaster_1.ToasterService);
this.permissionService = injector.get(service_1.PermissionService);
this.route = injector.get(router_1.ActivatedRoute);
this.router = injector.get(router_1.Router);
this.config = __assign({}, this.config, config);
}
Object.defineProperty(JZPageComponent.prototype, "criteria", {
///页面上用到的与服务器端交互的查询条件key value形式
get: function () {
return this._criteria;
},
set: function (value) {
this._criteria = __assign({}, value);
if (!this._defaultCriteria)
this._defaultCriteria = __assign({}, this._criteria);
},
enumerable: true,
configurable: true
});
///订阅页面QueryString到queryParams中
JZPageComponent.prototype.subscribeQueryParams = function () {
var _this = this;
this.routeSub = this.route.queryParams.subscribe(function (params) {
if (utility_1.ObjectUtility.eq(params, _this.queryParams))
_this.criteria = __assign({}, _this.criteria, params);
else
_this.criteria = __assign({}, _this._defaultCriteria, params);
_this.queryParams = __assign({}, params);
if (!_this.isPageChanged) {
if (_this.loadPage)
_this.loadPage();
_this.isPageChanged = false;
}
});
if (this.isPageChanged)
this.isPageChanged = false;
};
///页面与服务器端访问数据进行初始化
JZPageComponent.prototype.loadPage = function () {
var _this = this;
if (this.config.needLoadPage) {
this.pageService.loadPage(this.criteria).then(function (data) {
utility_1.PageUtility.fillDics(data, _this.dics);
if (_this.onAfterLoadPage)
_this.onAfterLoadPage.call(_this, data);
});
}
};
///angular页面初始化事件,参见帮助:https://www.angular.cn/guide/lifecycle-hooks
JZPageComponent.prototype.ngOnInit = function () {
if (!this.isDialog)
this.subscribeQueryParams();
this.loadPage();
};
///加载数据成功后回调函数
JZPageComponent.prototype.onAfterLoadPage = function (data) { };
///angular页面销毁事件,参见帮助:https://www.angular.cn/guide/lifecycle-hooks
JZPageComponent.prototype.ngOnDestory = function () {
if (this.routeSub)
this.routeSub.unsubscribe();
};
JZPageComponent.prototype.openDialog = function (component, item, successCallback, failedCallback, config, extraData) {
var _this = this;
config = config || { backdrop: 'static' };
//item = item || {};
var modalRef = this.modalService.open(component, config);
if (item) {
modalRef.componentInstance.item = __assign({}, modalRef.componentInstance.item, item);
}
if (extraData) {
modalRef.componentInstance.extraData = __assign({}, modalRef.componentInstance.extraData, extraData);
}
modalRef.componentInstance.dics = __assign({}, modalRef.componentInstance.dics, this.dics);
modalRef.result.then(function (result) {
if (!result || !result.success) {
if (failedCallback)
failedCallback.call(_this, result);
}
else {
if (successCallback)
successCallback.call(_this, result.data);
}
});
};
JZPageComponent.prototype.createRouterLink = function (url) {
var urlLower = url.toLowerCase();
var origin = window.location.origin.toLowerCase();
if (urlLower.startsWith(origin)) {
url = url.substring(origin.length);
}
var strs = url.split('?');
if (strs.length == 1)
return { routerLink: [url], queryParams: {} };
var routerLink = strs[0];
var queryParams = {};
strs = strs[1].split('&');
for (var i = 0; i < strs.length; i++) {
var keyvalue = strs[i].split('=');
queryParams[keyvalue[0]] = keyvalue[1];
}
return { routerLink: [routerLink], queryParams: queryParams };
};
JZPageComponent.prototype.go = function (url, isNewWindow) {
if (isNewWindow) {
window.open(url);
return;
}
var urlLower = url.toLowerCase();
var origin = window.location.origin.toLowerCase();
if (urlLower.startsWith(origin)) {
url = url.substring(origin.length);
}
else if (urlLower.startsWith("http")) {
window.location.href = url;
return;
}
var strs = url.split('?');
if (strs.length == 1) {
this.router.navigate([url]);
return;
}
var routerLink = strs[0];
var queryParams = {};
strs = strs[1].split('&');
for (var i = 0; i < strs.length; i++) {
var keyvalue = strs[i].split('=');
queryParams[keyvalue[0]] = keyvalue[1];
}
this.router.navigate([routerLink], { queryParams: queryParams });
};
return JZPageComponent;
}());
exports.JZPageComponent = JZPageComponent;
//# sourceMappingURL=jz-page.component.js.map