| 1 | 1 | 'use strict'; |
| 2 | | |
| 3 | 1 | module.exports = Behavior(function() { |
| 4 | 1 | var fs = require('fs'); |
| 5 | 1 | var ejs = require('ejs'); |
| 6 | 1 | var thinkEjs = thinkRequire('ejs'); |
| 7 | | |
| 8 | 1 | var filterRe = ['^(']; |
| 9 | 1 | for (var n in ejs.filters) { |
| 10 | 23 | if (filterRe.length > 1) { |
| 11 | 22 | filterRe.push('|'); |
| 12 | | } |
| 13 | 23 | filterRe.push(n); |
| 14 | | } |
| 15 | 1 | filterRe.push(')$'); |
| 16 | 1 | filterRe = new RegExp(filterRe.join('')); |
| 17 | | |
| 18 | 1 | return { |
| 19 | | options: { |
| 20 | | 'ejs_behavior_actions': [] |
| 21 | | }, |
| 22 | | |
| 23 | | init: function(http) { |
| 24 | 13 | this.http = http; |
| 25 | 13 | for (var name in this.options) { |
| 26 | 13 | if (C(name) !== undefined) { |
| 27 | 13 | this.options[name] = C(name); |
| 28 | | } |
| 29 | | } |
| 30 | 13 | if (isArray(this.options.ejs_behavior_actions)) { |
| 31 | 1 | this.includeActions = this.options.ejs_behavior_actions; |
| 32 | 12 | } else if (this.options.ejs_behavior_actions) { |
| 33 | 11 | this.includeActions = [this.options.ejs_behavior_actions]; |
| 34 | | } else { |
| 35 | 1 | this.includeActions = []; |
| 36 | | } |
| 37 | 13 | this.templatePath = [VIEW_PATH, '/', this.http.group, '/', 'tpl', '/'].join(''); |
| 38 | | }, |
| 39 | | |
| 40 | | checkAction: function() { |
| 41 | 5 | var action = (this.http.group + '/' + this.http.controller + '/' + this.http.action).toLowerCase(); |
| 42 | 5 | var includeActions = this.includeActions; |
| 43 | 5 | var isInclude = false; |
| 44 | 5 | for (var i = 0, len = includeActions.length; i < len; i++) { |
| 45 | 5 | if ((isRegexp(includeActions[i]) && includeActions[i].test(action)) || (includeActions[i] == action)) { |
| 46 | 3 | isInclude = true; |
| 47 | 3 | break; |
| 48 | | } |
| 49 | | } |
| 50 | 5 | return isInclude; |
| 51 | | }, |
| 52 | | |
| 53 | | listIncludeTemplate: function() { |
| 54 | 5 | var self = this; |
| 55 | 5 | var deferred = getDefer(); |
| 56 | 5 | var includeRe = new RegExp(['^', self.http.controller, C('tpl_file_depr'), self.http.action, C('tpl_file_depr'), '(.+)', C('tpl_file_suffix'), '$'].join('').toLowerCase()); |
| 57 | 5 | fs.readdir(self.templatePath, function(err, files) { |
| 58 | 5 | if (err) { |
| 59 | 1 | deferred.reject(err); |
| 60 | | } else { |
| 61 | 4 | var includeTemplateFiles = []; |
| 62 | 4 | files.forEach(function(file) { |
| 63 | 12 | var matches = file.match(includeRe); |
| 64 | 12 | if (matches && matches[0]) { |
| 65 | 6 | includeTemplateFiles.push([matches[1], file]); |
| 66 | | } |
| 67 | | }); |
| 68 | 4 | deferred.resolve(includeTemplateFiles); |
| 69 | | } |
| 70 | | }) |
| 71 | 5 | return deferred.promise; |
| 72 | | }, |
| 73 | | |
| 74 | | fetchTemplate: function(templateFile) { |
| 75 | 5 | var self = this; |
| 76 | 5 | var deferred = getDefer(); |
| 77 | 5 | fs.readFile(self.templatePath + templateFile[1], { encoding: C('encoding') }, function(err, content) { |
| 78 | 5 | if (err) { |
| 79 | 1 | deferred.reject(err); |
| 80 | | } else { |
| 81 | 4 | deferred.resolve([templateFile[0], content]); |
| 82 | | } |
| 83 | | }) |
| 84 | 5 | return deferred.promise; |
| 85 | | }, |
| 86 | | |
| 87 | | buildFilters: function() { |
| 88 | 2 | var filters = thinkEjs.filters; |
| 89 | 2 | var exFilters = []; |
| 90 | 2 | var ret = []; |
| 91 | 2 | for (var n in filters) { |
| 92 | 48 | if (!filterRe.test(n)) { |
| 93 | 2 | exFilters.push([n, filters[n].toString()]); |
| 94 | | } |
| 95 | | } |
| 96 | 2 | if (exFilters.length > 0) { |
| 97 | 2 | ret.push('<script>'); |
| 98 | 2 | ret.push('(function() {'); |
| 99 | 2 | exFilters.forEach(function(f) { |
| 100 | 2 | ret.push('ejs.filters.' + f[0] + ' = ' + f[1] + ';'); |
| 101 | | }); |
| 102 | 2 | ret.push('}());'); |
| 103 | 2 | ret.push('</script>'); |
| 104 | | } |
| 105 | 2 | return ret; |
| 106 | | }, |
| 107 | | |
| 108 | | run: function(data) { |
| 109 | 3 | var self = this; |
| 110 | 3 | if (self.checkAction()) { |
| 111 | 2 | var outTpls = [].concat(self.buildFilters()); |
| 112 | 2 | return self.listIncludeTemplate().then(function(includeTemplateFiles) { |
| 113 | 2 | var includeTemplates = []; |
| 114 | 2 | includeTemplateFiles.forEach(function(file) { |
| 115 | 3 | includeTemplates.push(self.fetchTemplate(file)); |
| 116 | | }); |
| 117 | 2 | return Promise.all(includeTemplates).then(function(contents) { |
| 118 | 2 | contents.forEach(function(content) { |
| 119 | 3 | outTpls.push('<script id="_ejs_tpl_' + (self.http.controller + '_' + self.http.action + '_' + content[0]).toLowerCase() + '" type="text/template">'); |
| 120 | 3 | outTpls.push(content[1]); |
| 121 | 3 | outTpls.push('</script>'); |
| 122 | | }); |
| 123 | 2 | data.var._ejs_templates = outTpls.join('\n'); |
| 124 | 2 | return data; |
| 125 | | }); |
| 126 | | }); |
| 127 | | } else { |
| 128 | 1 | return data; |
| 129 | | } |
| 130 | | } |
| 131 | | } |
| 132 | | }); |
| 133 | | |