@midwayjs/view
Version:
Midway Component for render view
139 lines • 4.95 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ViewManager = void 0;
const core_1 = require("@midwayjs/core");
const assert = require("assert");
const path = require("path");
const fs_1 = require("fs");
let ViewManager = class ViewManager extends Map {
constructor() {
super(...arguments);
this.extMap = new Map();
this.fileMap = new Map();
this.localsMap = {};
}
init() {
this.config = this.viewConfig;
const rootSet = new Set(Object.values(this.config.rootDir));
if (this.config.root) {
this.config.root.split(/\s*,\s*/g).forEach(filepath => {
rootSet.add(filepath);
});
}
this.config.root = Array.from(rootSet.values()).filter(filepath => {
return (0, fs_1.existsSync)(filepath);
});
this.extMap = new Map();
this.fileMap = new Map();
for (const ext of Object.keys(this.config.mapping)) {
this.extMap.set(ext, this.config.mapping[ext]);
}
}
/**
* This method can register view engine.
*
* You can define a view engine class contains two method, `render` and `renderString`
*
* ```js
* class View {
* render() {}
* renderString() {}
* }
* ```
* @param {String} name - the name of view engine
* @param {Object} viewEngine - the class of view engine
*/
use(name, viewEngine) {
assert(name, 'name is required');
assert(!this.has(name), `${name} has been registered`);
assert(viewEngine, 'viewEngine is required');
assert(viewEngine.prototype.render, 'viewEngine should implement `render` method');
assert(viewEngine.prototype.renderString, 'viewEngine should implement `renderString` method');
this.set(name, viewEngine);
}
/**
* Resolve the path based on the given name,
* if the name is `user.html` and root is `view` (by default),
* it will return `view/user.html`
* @param {String} name - the given path name, it's relative to config.root
* @return {String} filename - the full path
*/
async resolve(name) {
const config = this.config;
// check cache
let filename = this.fileMap.get(name);
if (config.cache && filename)
return filename;
// try find it with default extension
filename = await resolvePath([name, name + config.defaultExtension], config.root);
assert(filename, `Can't find ${name} from ${config.root.join(',')}`);
// set cache
this.fileMap.set(name, filename);
return filename;
}
/**
* add a global data for all views
* @param key
* @param localValue
*/
addLocals(key, localValue) {
this.localsMap[key] = localValue;
}
/**
* get global locals data
*/
getLocals() {
return this.localsMap;
}
findEngine(ext) {
return this.extMap.get(ext);
}
};
__decorate([
(0, core_1.App)(),
__metadata("design:type", Object)
], ViewManager.prototype, "app", void 0);
__decorate([
(0, core_1.Config)('view'),
__metadata("design:type", Object)
], ViewManager.prototype, "viewConfig", void 0);
__decorate([
(0, core_1.Init)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], ViewManager.prototype, "init", null);
ViewManager = __decorate([
(0, core_1.Provide)(),
(0, core_1.Scope)(core_1.ScopeEnum.Singleton)
], ViewManager);
exports.ViewManager = ViewManager;
async function resolvePath(names, root) {
for (const name of names) {
for (const dir of root) {
const filename = path.join(dir, name);
try {
await fs_1.promises.access(filename, fs_1.constants.R_OK);
if (inpath(dir, filename)) {
return filename;
}
}
catch (err) {
// ignore
}
}
}
}
function inpath(parent, sub) {
return sub.indexOf(parent) > -1;
}
//# sourceMappingURL=viewManager.js.map