oak-frontend-base/lib/page.common.js

400 lines
18 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.destroyNode = exports.callPicker = exports.execute = exports.loadMore = exports.refresh = exports.reRender = exports.onPathSet = exports.unsubscribe = exports.subscribe = void 0;
var tslib_1 = require("tslib");
var assert_1 = require("oak-domain/lib/utils/assert");
var types_1 = require("oak-domain/lib/types");
var Feature_1 = require("./types/Feature");
var lodash_1 = require("oak-domain/lib/utils/lodash");
function subscribe() {
var _this = this;
if (!this.subscribed) {
this.subscribed = (0, Feature_1.subscribe)(function () { return _this.reRender(); });
}
}
exports.subscribe = subscribe;
function unsubscribe() {
if (this.subscribed) {
this.subscribed();
this.subscribed = undefined;
}
}
exports.unsubscribe = unsubscribe;
function onPathSet(option) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _a, props, state, oakPath, oakProjection, oakIsPicker, oakFilters, oakSorters, oakId, entity, path, projection, isList, filters, sorters, pagination, features, oakPath2, entity2, filters2, oakFilters2, _loop_1, filters_1, filters_1_1, ele, proj, sorters2, oakSorters2, _loop_2, sorters_1, sorters_1_1, ele;
var e_1, _b, e_2, _c;
var _this = this;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
_a = this, props = _a.props, state = _a.state;
oakPath = props.oakPath, oakProjection = props.oakProjection, oakIsPicker = props.oakIsPicker, oakFilters = props.oakFilters, oakSorters = props.oakSorters, oakId = props.oakId;
entity = option.entity, path = option.path, projection = option.projection, isList = option.isList, filters = option.filters, sorters = option.sorters, pagination = option.pagination;
features = this.features;
oakPath2 = oakPath || path;
if (!entity) return [3 /*break*/, 2];
entity2 = entity instanceof Function ? entity.call(this) : entity;
filters2 = [];
if (oakFilters) {
oakFilters2 = typeof oakFilters === 'string' ? JSON.parse(oakFilters) : oakFilters;
filters2.push.apply(filters2, tslib_1.__spreadArray([], tslib_1.__read(oakFilters2), false));
}
else if (filters) {
_loop_1 = function (ele) {
var _e;
var filter = ele.filter, name_1 = ele["#name"];
filters2.push((_e = {
filter: typeof filter === 'function'
? function () {
return filter.call(_this, {
features: features,
props: _this.props,
state: _this.state,
});
}
: filter
},
_e['#name'] = name_1,
_e));
};
try {
for (filters_1 = tslib_1.__values(filters), filters_1_1 = filters_1.next(); !filters_1_1.done; filters_1_1 = filters_1.next()) {
ele = filters_1_1.value;
_loop_1(ele);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (filters_1_1 && !filters_1_1.done && (_b = filters_1.return)) _b.call(filters_1);
}
finally { if (e_1) throw e_1.error; }
}
}
proj = oakProjection && (typeof oakProjection === 'string' ? JSON.parse(oakProjection) : oakProjection);
if (!proj && projection) {
proj = typeof projection === 'function'
? function () {
return projection.call(_this, {
features: features,
props: _this.props,
state: _this.state,
});
}
: projection;
}
sorters2 = [];
if (oakSorters) {
oakSorters2 = typeof oakSorters === 'string' ? JSON.parse(oakSorters) : oakSorters;
sorters2.push.apply(sorters2, tslib_1.__spreadArray([], tslib_1.__read(oakSorters2), false));
}
else if (sorters) {
_loop_2 = function (ele) {
var _f;
var sorter = ele.sorter, name_2 = ele["#name"];
sorters2.push((_f = {
sorter: typeof sorter === 'function'
? function () {
return sorter.call(_this, {
features: features,
props: _this.props,
state: _this.state,
});
}
: sorter
},
_f['#name'] = name_2,
_f));
};
try {
for (sorters_1 = tslib_1.__values(sorters), sorters_1_1 = sorters_1.next(); !sorters_1_1.done; sorters_1_1 = sorters_1.next()) {
ele = sorters_1_1.value;
_loop_2(ele);
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (sorters_1_1 && !sorters_1_1.done && (_c = sorters_1.return)) _c.call(sorters_1);
}
finally { if (e_2) throw e_2.error; }
}
}
(0, assert_1.assert)(oakPath2, '没有正确的path信息请检查是否配置正确');
return [4 /*yield*/, features.runningTree.createNode({
path: oakPath2,
entity: entity2,
isList: isList,
isPicker: oakIsPicker,
projection: proj,
pagination: pagination,
filters: filters2,
sorters: sorters2,
id: oakId,
})];
case 1:
_d.sent();
Object.assign(this.state, {
oakEntity: entity2,
oakFullpath: oakPath2,
});
return [3 /*break*/, 4];
case 2:
Object.assign(this.state, {
oakFullpath: oakPath2,
});
// 创建virtualNode
return [4 /*yield*/, features.runningTree.createNode({
path: oakPath2,
})];
case 3:
// 创建virtualNode
_d.sent();
_d.label = 4;
case 4: return [4 /*yield*/, this.refresh()];
case 5:
_d.sent();
return [2 /*return*/];
}
});
});
}
exports.onPathSet = onPathSet;
function reRender(option, extra) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var features, formData, rows, oakDirty, oakLoading, oakPullDownRefreshLoading, oakLoadingMore, oakExecuting, oakLegalActions, actions, testResult, data, _a, k, oakAllowExecuting, err_1, data, _b;
var _c;
var _this = this;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
features = this.features;
formData = option.formData;
if (!(this.state.oakEntity && this.state.oakFullpath)) return [3 /*break*/, 11];
return [4 /*yield*/, this.features.runningTree.getFreshValue(this.state.oakFullpath)];
case 1:
rows = _d.sent();
oakDirty = this.features.runningTree.isDirty(this.state.oakFullpath);
oakLoading = !this.pullDownRefresh && this.features.runningTree.isLoading(this.state.oakFullpath);
oakPullDownRefreshLoading = this.pullDownRefresh && this.features.runningTree.isLoading(this.state.oakFullpath);
oakLoadingMore = this.features.runningTree.isLoadingMore(this.state.oakFullpath);
oakExecuting = this.features.runningTree.isExecuting(this.state.oakFullpath);
oakLegalActions = [];
actions = this.props.oakActions || option.actions;
if (!(actions && actions.length > 0)) return [3 /*break*/, 3];
(0, assert_1.assert)(this.props.oakId); // actions必须配合id来使用
return [4 /*yield*/, Promise.all(actions.map(function (ele) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var _a;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = {
action: ele
};
return [4 /*yield*/, this.checkOperation(this.state.oakEntity, ele, { id: this.props.oakId }, ['user', 'row'])];
case 1: return [2 /*return*/, (_a.result = _b.sent(),
_a)];
}
});
}); }))];
case 2:
testResult = _d.sent();
oakLegalActions = testResult.filter(function (ele) { return ele.result; }).map(function (ele) { return ele.action; });
_d.label = 3;
case 3:
if (!formData) return [3 /*break*/, 5];
return [4 /*yield*/, formData.call(this, {
data: rows,
features: features,
props: this.props,
legalActions: oakLegalActions,
})];
case 4:
_a = _d.sent();
return [3 /*break*/, 6];
case 5:
_a = {};
_d.label = 6;
case 6:
data = _a;
Object.assign(data, {
oakLegalActions: oakLegalActions,
});
for (k in data) {
if (data[k] === undefined) {
Object.assign(data, (_c = {},
_c[k] = null,
_c));
}
}
Object.assign(data, {
oakDirty: oakDirty,
oakLoading: oakLoading,
oakLoadingMore: oakLoadingMore,
oakExecuting: oakExecuting,
oakPullDownRefreshLoading: oakPullDownRefreshLoading,
});
if (extra) {
Object.assign(data, extra);
}
oakAllowExecuting = false;
_d.label = 7;
case 7:
_d.trys.push([7, 9, , 10]);
return [4 /*yield*/, this.features.runningTree.tryExecute(this.state.oakFullpath)];
case 8:
oakAllowExecuting = _d.sent();
return [3 /*break*/, 10];
case 9:
err_1 = _d.sent();
if (err_1 instanceof types_1.OakUserException) {
oakAllowExecuting = err_1;
}
else {
oakAllowExecuting = false;
throw err_1;
}
return [3 /*break*/, 10];
case 10:
Object.assign(data, {
oakAllowExecuting: oakAllowExecuting,
});
this.setState(data);
return [3 /*break*/, 15];
case 11:
if (!formData) return [3 /*break*/, 13];
return [4 /*yield*/, formData.call(this, {
features: features,
props: this.props,
})];
case 12:
_b = _d.sent();
return [3 /*break*/, 14];
case 13:
_b = {
__now: Date.now(), // 如果没有任何state被set可能会不触发重渲染
};
_d.label = 14;
case 14:
data = _b;
if (extra) {
Object.assign(data, extra);
}
this.setState(data);
_d.label = 15;
case 15: return [2 /*return*/];
}
});
});
}
exports.reRender = reRender;
function refresh() {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var err_2;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this.state.oakFullpath) return [3 /*break*/, 4];
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.features.runningTree.refresh(this.state.oakFullpath)];
case 2:
_a.sent();
return [3 /*break*/, 4];
case 3:
err_2 = _a.sent();
this.setMessage({
type: 'error',
content: err_2.message,
});
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
});
}
exports.refresh = refresh;
function loadMore() {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var err_3;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(this.state.oakEntity && this.state.oakFullpath)) return [3 /*break*/, 4];
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.features.runningTree.loadMore(this.state.oakFullpath)];
case 2:
_a.sent();
return [3 /*break*/, 4];
case 3:
err_3 = _a.sent();
this.setMessage({
type: 'error',
content: err_3.message,
});
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
});
}
exports.loadMore = loadMore;
function execute(data, path) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var fullpath, result;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this.state.oakExecuting) {
throw new Error('请仔细设计按钮状态,不要允许重复点击!');
}
fullpath = path ? "".concat(this.state.oakFullpath, ".").concat(path) : this.state.oakFullpath;
return [4 /*yield*/, this.features.runningTree.execute(fullpath, data)];
case 1:
result = _a.sent();
return [4 /*yield*/, this.setMessage({
type: 'success',
content: '操作成功',
})];
case 2:
_a.sent();
return [2 /*return*/, result];
}
});
});
}
exports.execute = execute;
function callPicker(attr, params) {
if (params === void 0) { params = {}; }
if (this.state.oakExecuting) {
return;
}
var relation = this.features.cache.judgeRelation(this.state.oakEntity, attr);
var subEntity;
if (relation === 2) {
subEntity = attr;
}
else {
(0, assert_1.assert)(typeof relation === 'string');
subEntity = relation;
}
var url = "/pickers/".concat(subEntity, "?oakIsPicker=true&oakParentEntity=").concat(this.state.oakEntity, "&oakParent=").concat(this.state.oakFullpath, "&oakPath=").concat(attr);
for (var k in params) {
url += "&".concat(k, "=").concat(JSON.stringify(params[k]));
}
this.navigateTo({
url: url,
});
}
exports.callPicker = callPicker;
function destroyNode() {
(0, assert_1.assert)(this.state.oakFullpath);
this.features.runningTree.destroyNode(this.state.oakFullpath);
(0, lodash_1.unset)(this.state, ['oakFullpath', 'oakEntity']);
}
exports.destroyNode = destroyNode;