yoyo-ng-modulewindy
Version:
服务于52ABP模板的前端开源的相关组件内容。整合了ng-alain和你NG ZORRO的内容
230 lines • 8.22 kB
JavaScript
import { Injectable } from '@angular/core';
import { of, Subject } from 'rxjs';
import { LazyService } from 'yoyo-ng-module/util';
import { AdLodopConfig } from './lodop.config';
// TODO: zone
var LodopService = /** @class */ (function () {
function LodopService(defCog, scriptSrv) {
this.defCog = defCog;
this.scriptSrv = scriptSrv;
this.pending = false;
this._lodop = null;
this._init = new Subject();
this._events = new Subject();
this.printBuffer = [];
this.cog = defCog;
}
Object.defineProperty(LodopService.prototype, "cog", {
/**
* 获取或重新设置配置
*
* **注:**重新设置会倒置重新加载脚本资源
*/
get: function () {
return this._cog;
},
set: function (value) {
this._cog = Object.assign({
url: 'https://localhost:8443/CLodopfuncs.js',
name: 'CLODOP',
companyName: '',
checkMaxCount: 100,
}, this.defCog, value);
},
enumerable: true,
configurable: true
});
Object.defineProperty(LodopService.prototype, "events", {
/** 事件变更通知 */
get: function () {
return this._events.asObservable();
},
enumerable: true,
configurable: true
});
LodopService.prototype.check = function () {
if (!this._lodop)
throw new Error("\u8BF7\u52A1\u5FC5\u5148\u8C03\u7528 lodop \u83B7\u53D6\u5BF9\u8C61");
};
LodopService.prototype.request = function () {
var _this = this;
this.pending = true;
var url = this.cog.url + "?name=" + this.cog.name;
var checkMaxCount = this.cog.checkMaxCount;
var onResolve = function (status, error) {
_this._init.next({
ok: status === 'ok',
status: status,
error: error,
lodop: _this._lodop,
});
};
var checkStatus = function () {
--checkMaxCount;
if (_this._lodop.webskt && _this._lodop.webskt.readyState === 1) {
onResolve('ok');
}
else {
if (checkMaxCount < 0) {
onResolve('check-limit');
return;
}
setTimeout(function () { return checkStatus(); }, 100);
}
};
this.scriptSrv.load(url).then(function (res) {
if (res.length === 1 && res[0].status !== 'ok') {
_this.pending = false;
onResolve('script-load-error', res[0]);
return;
}
_this._lodop =
window.hasOwnProperty(_this.cog.name) &&
window[_this.cog.name];
if (_this._lodop === null) {
onResolve('load-variable-name-error', { name: _this.cog.name });
return;
}
_this._lodop.SET_LICENSES(_this.cog.companyName, _this.cog.license, _this.cog.licenseA, _this.cog.licenseB);
checkStatus();
});
};
/** 重置 lodop 对象 */
LodopService.prototype.reset = function () {
this._lodop = null;
this.pending = false;
this.request();
};
Object.defineProperty(LodopService.prototype, "lodop", {
/** 获取 lodop 对象 */
get: function () {
if (this._lodop)
return of({ ok: true, lodop: this._lodop });
if (this.pending)
return this._init.asObservable();
this.request();
return this._init.asObservable();
},
enumerable: true,
configurable: true
});
Object.defineProperty(LodopService.prototype, "printer", {
/** 获取打印机列表 */
get: function () {
this.check();
var ret = [];
var count = this._lodop.GET_PRINTER_COUNT();
for (var index = 0; index < count; index++) {
ret.push(this._lodop.GET_PRINTER_NAME(index));
}
return ret;
},
enumerable: true,
configurable: true
});
/**
* 附加代码至 `lodop` 对象上,字符串类支持 `{{key}}` 的动态参数
*
* **注:** 代码是指打印设计所产生字符串数据
*
* @param code 代码
* @param contextObj 动态参数上下文对象
* @param parser 自定义解析表达式,默认:`/LODOP\.([^(]+)\(([^\n]+)\);/i`
*/
LodopService.prototype.attachCode = function (code, contextObj, parser) {
var _this = this;
this.check();
if (!parser)
parser = /LODOP\.([^(]+)\(([^\n]+)\);/i;
code.split('\n').forEach(function (line) {
var res = parser.exec(line.trim());
if (!res)
return;
var fn = _this._lodop[res[1]];
if (fn) {
var arr = void 0;
try {
var fakeFn = new Function("return [" + res[2] + "]");
arr = fakeFn();
}
catch (_a) { }
if (Array.isArray(arr) && contextObj) {
for (var i = 0; i < arr.length; i++) {
if (typeof arr[i] === 'string') {
arr[i] = arr[i].replace(/{{(.*?)}}/g, function (match, key) { return contextObj[key.trim()] || ''; });
}
}
}
fn.apply(_this._lodop, arr);
}
});
};
/**
* 打开打印设计关闭后自动返回代码
*
* **注:** 自动监听 `On_Return` 事件,运行后会移除
*/
LodopService.prototype.design = function () {
var _this = this;
this.check();
var tid = this._lodop.PRINT_DESIGN();
return new Promise(function (resolve) {
_this._lodop.On_Return = function (taskID, value) {
if (tid !== taskID)
return;
_this._lodop.On_Return = null;
resolve('' + value);
};
});
};
LodopService.prototype.printDo = function () {
var _this = this;
var data = this.printBuffer.shift();
if (!data)
return;
this.attachCode(data.code, data.item, data.parser);
var tid = this._lodop.PRINT();
this._lodop.On_Return = function (taskID, value) {
if (tid !== taskID)
return;
_this._lodop.On_Return = null;
_this._events.next(Object.assign({
ok: value === true,
error: value === true ? null : value,
}, data));
_this.printDo();
};
};
/**
* 立即打印,一般用于批量套打
*
* @param code 代码
* @param contextObj 动态参数上下文对象
* @param parser 自定义解析表达式,默认:`/LODOP\.([^(]+)\(([^\n]+)\);/i`
*/
LodopService.prototype.print = function (code, contextObj, parser) {
this.check();
if (contextObj) {
(_a = this.printBuffer).push.apply(_a, (Array.isArray(contextObj) ? contextObj : [contextObj]).map(function (item) {
return { code: code, parser: parser, item: item };
}));
}
this.printDo();
var _a;
};
LodopService.prototype.ngOnDestroy = function () {
this._init.unsubscribe();
this._events.unsubscribe();
};
LodopService.decorators = [
{ type: Injectable },
];
/** @nocollapse */
LodopService.ctorParameters = function () { return [
{ type: AdLodopConfig },
{ type: LazyService }
]; };
return LodopService;
}());
export { LodopService };
//# sourceMappingURL=lodop.service.js.map