From 1b0f63698828c918df6676e6c9629335678ac0af Mon Sep 17 00:00:00 2001 From: cjd6568358 Date: Tue, 4 Jul 2017 11:09:03 +0800 Subject: [PATCH] fix --- plugin/directive.js | 13 +++++ plugin/discribe.js | 18 ++++++ plugin/filter.js | 31 ++++++++++ plugin/koa-view-ng.js | 133 ++++++++++++++++++------------------------ plugin/ngTemplate.js | 70 ++++++++++++++++++++++ 5 files changed, 190 insertions(+), 75 deletions(-) create mode 100644 plugin/directive.js create mode 100644 plugin/discribe.js create mode 100644 plugin/filter.js create mode 100644 plugin/ngTemplate.js diff --git a/plugin/directive.js b/plugin/directive.js new file mode 100644 index 0000000..05931f7 --- /dev/null +++ b/plugin/directive.js @@ -0,0 +1,13 @@ +(function() { + var directives = {}; + if (typeof exports === 'object' && typeof module === 'object') { + module.exports = directives; + } else if (typeof define === 'function' && define.amd) + define(function() { + return directives; + }); + else if (typeof exports === 'object') + exports["directives"] = directives; + else + window.directives = directives; +})(); \ No newline at end of file diff --git a/plugin/discribe.js b/plugin/discribe.js new file mode 100644 index 0000000..d84d5b2 --- /dev/null +++ b/plugin/discribe.js @@ -0,0 +1,18 @@ +(function() { + var discribes = {}; + if (typeof exports === 'object' && typeof module === 'object') { + var fs = require('fs'); + var path = require('path'); + discribes.nodeTplPath = function(htmlPath) { + return fs.readFileSync(path.resolve(__dirname, '../../../../common/templates', htmlPath), 'utf-8'); + }; + module.exports = discribes; + } else if (typeof define === 'function' && define.amd) { + define(function() { + return discribes; + }); + } else if (typeof exports === 'object') { + } else { + window.discribes = discribes; + } +})(); \ No newline at end of file diff --git a/plugin/filter.js b/plugin/filter.js new file mode 100644 index 0000000..3624af7 --- /dev/null +++ b/plugin/filter.js @@ -0,0 +1,31 @@ +(function() { + var filters = { + toFixed: function(input, number) { + input = Number(input) + if (isNaN(input)) { + return 0; + } + return input.toFixed(number) + }, + date: function(input, fmt) { + var type = typeof input + if (type == 'number' || (type == 'string' && new Date(input))) { + return new Date(input).Format(fmt) + } else if (type == 'object' && input.getTime) { + return input.Format(fmt) + } else { + return '无效的日期参数' + } + } + } + if (typeof exports === 'object' && typeof module === 'object') { + module.exports = filters; + } else if (typeof define === 'function' && define.amd) + define(function() { + return filters; + }); + else if (typeof exports === 'object') + exports["filters"] = filters; + else + window.filters = filters; +})(); \ No newline at end of file diff --git a/plugin/koa-view-ng.js b/plugin/koa-view-ng.js index 5bc6ea5..856355f 100644 --- a/plugin/koa-view-ng.js +++ b/plugin/koa-view-ng.js @@ -4,25 +4,25 @@ // 加载模板引擎 // ngViews(app, { // extension: ".html", -// filters: ngTemplateFilter, -// templateDir: path.join(__dirname, './common/templates/views'), +// viewsDir: path.join(__dirname, './common/templates/views'), // cache: LRU({ // max: 500, // The maximum number of items allowed in the cache // max_age: 1000 * 60 * 60 * 24 // The maximum life of a cached item in milliseconds // }), // beforeRender: (viewName, scope, ctx, setting) => { -// let { prefix, extension } = setting; // let combScope = Object.assign({ // HMC: Object.assign({}, ctx.state, { -// tplSettings: { prefix, extension }, // currPage: viewName, // jsRev: ctx.state.jsRevList[viewName + ".min.js"], -// pointList: ctx.state.pointData.common.concat(ctx.state.pointData.pages[viewName] || []) +// cssRev: ctx.state.cssRevList[viewName + ".min.css"], +// pointList: ctx.state.pointData.common.concat(ctx.state.pointData.pages[viewName] || []), +// data: scope && scope.data || {} // }) // }, scope, { // templates: scope && scope.templates || {} // }) // delete combScope.HMC.jsRevList; +// delete combScope.HMC.cssRevList; // delete combScope.HMC.pointData; // return { viewName, combScope, setting } // }, @@ -47,17 +47,14 @@ */ const fs = require('fs'); const path = require('path'); -const ngTemplate = require('sodajs/node'); +const ngTemplate = require('./ngTemplate'); const crypto = require('crypto'); /** * default render options * @type {Object} */ -const defaultSettings = { - prefix: 'ng', - templateDir: __dirname -}; +const defaultSettings = {}; let tplCache = new Map() @@ -71,75 +68,61 @@ let tplCache = new Map() * @param {Application} app koa application instance * @param {Object} settings user settings */ -exports = module.exports = function (app, settings) { - if (app.context.render) { - return; - } - - if (!settings || !settings.templateDir) { - throw new Error('settings.templateDir required'); - } - - settings = Object.assign({}, defaultSettings, settings); - - if (settings.cache) { - tplCache = settings.cache; - settings.cache = null; - } - +exports = module.exports = function(app, settings) { + if (app.context.render) { + return; + } - // 初始化ngTemplate设置 - ngTemplate.prefix(settings.prefix); - //ngTemplate.templateDir = settings.templateDir; + if (!settings || !settings.viewsDir) { + throw new Error('settings.viewsDir required'); + } - for (var key in settings.filters) { - ngTemplate.filter(key, settings.filters[key]) - } + settings = Object.assign({}, defaultSettings, settings); - /** - * generate html with view name and options - * @param {String} view - * @param {Object} options - * @return {String} html - */ - async function render(view, options, ctx, setting) { - const viewPath = path.join(setting.templateDir, view + setting.extension); - const tpl = await fs.readFileSync(viewPath, 'utf-8'); - let hashKey = ""; - let hash = crypto.createHmac('sha256', "hmc_secret"); - if (process.env.NODE_ENV === 'development') { - hashKey = hash.update(tpl + JSON.stringify(setting.serverCacheKey || options)).digest('hex'); - } - else { - hashKey = hash.update(viewPath + JSON.stringify(setting.serverCacheKey || options)).digest('hex'); - } - // 从缓存获取模版 - if (!setting.disabledCache && tplCache && tplCache.get(hashKey)) { - ctx.set("fromCache", true); - return tplCache.get(hashKey); + if (settings.cache) { + tplCache = settings.cache; + settings.cache = null; } - let template = ngTemplate(tpl, options) - // 加入缓存 - if (!setting.disabledCache && tplCache) { - tplCache.set(hashKey, template); + /** + * generate html with view name and state + * @param {String} view + * @param {Object} state + * @return {String} html + */ + async function render(viewName, scope, ctx, setting) { + const viewPath = path.join(setting.viewsDir, viewName + setting.extension); + const tpl = await fs.readFileSync(viewPath, 'utf-8'); + let hashKey = ""; + let hash = crypto.createHmac('sha256', "hmc_secret"); + if (process.env.NODE_ENV === 'development') { + hashKey = hash.update(tpl + JSON.stringify(setting.serverCacheKey || scope)).digest('hex'); + } else { + hashKey = hash.update(viewPath + JSON.stringify(setting.serverCacheKey || scope)).digest('hex'); + } + // 从缓存获取模版 + if (!setting.disabledCache && tplCache && tplCache.get(hashKey)) { + ctx.set("fromCache", true); + return tplCache.get(hashKey); + } + let template = ngTemplate(tpl, scope) + // 加入缓存 + if (!setting.disabledCache && tplCache) { + tplCache.set(hashKey, template); + } + return template; } - return template; - } - - app.context.render = async function (view, _context, setting) { - const ctx = this; - setting = Object.assign({}, settings, setting); - let { prefix, extension } = setting; - Object.assign(ctx.state, { - tplSettings: { prefix, extension } - }); - const context = Object.assign({}, ctx.state, _context); - - let html = await render(view, context, ctx, setting); - ctx.type = 'html'; - ctx.body = html.replace("", "").replace("", ""); - }; -}; -exports.ngTemplate = ngTemplate; + app.context.render = async function(viewName, scope, setting) { + const ctx = this; + // 合并配置 + setting = Object.assign({}, settings, setting); + // 合并state + let combScope = Object.assign({}, ctx.state, scope); + // 渲染前预处理 + setting.beforeRender && ({ viewName, combScope, setting } = setting.beforeRender(viewName, scope, ctx, setting)) + let html = await render(viewName, combScope, ctx, setting); + ctx.type = 'html'; + ctx.body = html.replace("", "").replace("", ""); + }; +}; \ No newline at end of file diff --git a/plugin/ngTemplate.js b/plugin/ngTemplate.js new file mode 100644 index 0000000..b65bc5f --- /dev/null +++ b/plugin/ngTemplate.js @@ -0,0 +1,70 @@ +(function() { + function init(soda, filters, discribes, directives) { + filters = filters || {}; + discribes = discribes || {}; + directives = directives || {}; + soda.prefix('ng-'); + for (var key in filters) { + soda.filter(key, filters[key]) + } + for (var key in discribes) { + soda.discribe(key, discribes[key]) + } + for (var key in directives) { + soda.directive(key, directives[key]) + } + return soda; + } + if (typeof exports === 'object' && typeof module === 'object') { + // CMD + var soda = require('sodajs/node'); + // var soda_old = require('../lib/soda.old'); + var filters = require('./filter'); + var discribes = require('./discribe'); + var directives = require('./directive'); + var ngTemplate = init(soda, filters, discribes, directives); + module.exports = ngTemplate; + } else if (typeof define === 'function' && define.amd) { + // AMD + define([], function() { + var engine = null; + var ngTemplate = { + render: function(str, scope) { + var dfd = $.Deferred(); + getTemplateEngine().then(function(engine) { + dfd.resolve(engine(str, scope)) + }, function(e) { + console.log(e) + }) + return dfd.promise(); + } + }; + // 获取模版引擎 + function getTemplateEngine() { + var dfd = $.Deferred(); + if (engine) { + dfd.resolve(engine) + } else { + require(['soda', 'filters', 'discribes', 'directives'], function(soda, filters, discribes, directives) { + if (soda) { + engine = init(soda, filters, discribes, directives); + dfd.resolve(engine) + } else { + dfd.reject('加载失败') + } + }, function(e) { + console.log(e) + dfd.reject('加载失败') + }) + } + return dfd.promise(); + } + window.ngTemplate = ngTemplate; + return ngTemplate; + }); + } else if (typeof exports === 'object') { + // ES6 + } else { + window.ngTemplate = init(soda, filters, discribes, directives); + } +})(); \ No newline at end of file