swaf/dist/components/FormHelperComponent.js

52 lines
6.3 KiB
JavaScript
Raw Normal View History

2020-04-22 15:52:17 +02:00
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import ApplicationComponent from "../ApplicationComponent";
export default class FormHelperComponent extends ApplicationComponent {
start(app, router) {
return __awaiter(this, void 0, void 0, function* () {
router.use((req, res, next) => {
if (!req.session) {
throw new Error('Session is unavailable.');
}
res.locals.query = req.query;
let _validation = null;
res.locals.validation = () => {
if (!_validation) {
const v = req.flash('validation');
_validation = v.length > 0 ? v[0] : null;
}
return _validation;
};
let _previousFormData = null;
res.locals.previousFormData = () => {
if (!_previousFormData) {
const v = req.flash('previousFormData');
_previousFormData = v.length > 0 ? v[0] : null;
}
return _previousFormData;
};
next();
});
router.use((req, res, next) => {
if (['GET', 'POST'].find(m => m === req.method)) {
if (typeof req.body === 'object') {
req.flash('previousFormData', req.body);
}
}
next();
});
});
}
stop() {
return __awaiter(this, void 0, void 0, function* () {
});
}
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiRm9ybUhlbHBlckNvbXBvbmVudC5qcyIsInNvdXJjZVJvb3QiOiIuLyIsInNvdXJjZXMiOlsiY29tcG9uZW50cy9Gb3JtSGVscGVyQ29tcG9uZW50LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7OztBQUFBLE9BQU8sb0JBQW9CLE1BQU0seUJBQXlCLENBQUM7QUFHM0QsTUFBTSxDQUFDLE9BQU8sT0FBTyxtQkFBb0IsU0FBUSxvQ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8dm9pZD4ge1xuICAgICAgICByb3V0ZXIudXNlKChyZXEsIHJlcywgbmV4dCkgPT4ge1xuICAgICAgICAgICAgaWYgKCFyZXEuc2Vzc2lvbikge1xuICAgICAgICAgICAgICAgIHRocm93IG5ldyBFcnJvcignU2Vzc2lvbiBpcyB1bmF2YWlsYWJsZS4nKTtcbiAgICAgICAgICAgIH1cblxuICAgICAgICAgICAgcmVzLmxvY2Fscy5xdWVyeSA9IHJlcS5xdWVyeTtcblxuICAgICAgICAgICAgbGV0IF92YWxpZGF0aW9uOiBhbnkgPSBudWxsO1xuICAgICAgICAgICAgcmVzLmxvY2Fscy52YWxpZGF0aW9uID0gKCkgPT4ge1xuICAgICAgICAgICAgICAgIGlmICghX3ZhbGlkYXRpb24pIHtcbiAgICAgICAgICAgICAgICAgICAgY29uc3QgdiA9IHJlcS5mbGFzaCgndmFsaWRhdGlvbicpO1xuICAgICAgICAgICAgICAgICAgICBfdmFsaWRhdGlvbiA9IHYubGVuZ3RoID4gMCA/IHZbMF0gOiBudWxsO1xuICAgICAgICAgICAgICAgIH1cblxuICAgICAgICAgICAgICAgIHJldHVybiBfdmFsaWRhdGlvbjtcbiAgICAgICAgICAgIH1cblxuICAgICAgICAgICAgbGV0IF9wcmV2aW91c0Zvcm1EYXRhOiBhbnkgPSBudWxsO1xuICAgICAgICAgICAgcmVzLmxvY2Fscy5wcmV2aW91c0Zvcm1EYXRhID0gKCkgPT4ge1xuICAgICAgICAgICAgICAgIGlmICghX3ByZXZpb3VzRm9ybURhdGEpIHtcbiAgICAgICAgICAgICAgICAgICAgY29uc3QgdiA9IHJlcS5mbGFzaCgncHJldmlvdXNGb3JtRGF0YScpO1xuICAgICAgICAgICAgICAgICAgICBfcHJldmlvdXNGb3JtRGF0YSA9IHYubGVuZ3RoID4gMCA/IHYgWzBdIDogbnVsbDtcbiAgICAgICAgICAgICAgICB9XG5cbiAgICAgICAgICAgICAgICByZXR1cm4gX3ByZXZpb3VzRm9ybURhdGE7XG4gICAgICAgICAgICB9O1xuICAgICAgICAgICAgbmV4dCgpO1xuICAgICAgICB9KTtcblxuICAgICAgICByb3V0ZXIudXNlKChyZXEsIHJlcywgbmV4dCkgPT4ge1xuICAgICAgICAgICAgaWYgKFsnR0VUJywgJ1BPU1QnXS5maW5kKG0gPT4gbSA9PT0gcmVxLm1ldGhvZCkpIHtcbiAgICAgICAgICAgICAgICBpZiAodHlwZW9mIHJlcS5ib2R5ID09PSAnb2JqZWN0Jykge1xuICAgICAgICAgICAgICAgICAgICByZXEuZmxhc2goJ3ByZXZpb3VzRm9ybURhdGEnLCByZXEuYm9keSk7XG4gICAgICAgICAgICAgICAgfVxuICAgICAgICAgICAgfVxuICAgICAgICAgICAgbmV4dCgpO1xuICAgICAgICB9KTtcbiAgICB9XG5cbiAgICBwdWJsaWMgYXN5bmMgc3RvcCgpOiBQcm9taXNlP