| | 6 | |
|---|
| | 7 | /** |
|---|
| | 8 | * Constructor for filter object. |
|---|
| | 9 | */ |
|---|
| | 10 | function Filter(filter, type, param) { |
|---|
| | 11 | logger.info("1.filter:" + filter) |
|---|
| | 12 | logger.info("2.filter:" + filter.filter) |
|---|
| | 13 | this.filter = filter.filter ? filter.filter : filter; |
|---|
| | 14 | this.param = param || {}; |
|---|
| | 15 | if (this.param.only && !Object.isArray(this.param.only)) this.param.only = [this.param.only]; |
|---|
| | 16 | if (this.param.except && !Object.isArray(this.param.except)) this.param.except = [this.param.except]; |
|---|
| | 17 | this.type = type; |
|---|
| | 18 | } |
|---|
| | 19 | |
|---|
| | 20 | Filter.before = "before"; |
|---|
| | 21 | Filter.after = "after"; |
|---|
| | 22 | |
|---|
| | 23 | |
|---|
| | 24 | /** |
|---|
| | 25 | * Calls all before filters and returns true, if all filters return true, |
|---|
| | 26 | * or if there are no before filters. |
|---|
| | 27 | * @return {Boolean} |
|---|
| | 28 | */ |
|---|
| | 29 | function beforeFiltersPass() { |
|---|
| | 30 | for (var i=0; i<this.beforeFilters.length; i++) { |
|---|
| | 31 | var f = this.beforeFilters[i]; |
|---|
| | 32 | if (f.param.only && !f.param.only.include(req.route.action)) continue; |
|---|
| | 33 | if (f.param.except && f.param.except.include(req.route.action)) continue; |
|---|
| | 34 | if (f.filter.apply(this) === false) return false; |
|---|
| | 35 | } |
|---|
| | 36 | return true; |
|---|
| | 37 | } |
|---|
| | 38 | |
|---|
| | 39 | |
|---|
| | 40 | /** |
|---|
| | 41 | * Calls all after filters and returns the manipulated content. |
|---|
| | 42 | * @param {Object} controller Controller instance |
|---|
| | 43 | * @param {Object} req Request object |
|---|
| | 44 | * @param {Object} res Response object |
|---|
| | 45 | * @param {Object} session Session object |
|---|
| | 46 | * @return {String} |
|---|
| | 47 | */ |
|---|
| | 48 | function applyAfterFilters(content, req, res, session) { |
|---|
| | 49 | for (var i=0; i<this.afterFilters.length; i++) { |
|---|
| | 50 | var f = this.afterFilters[i]; |
|---|
| | 51 | if (f.param.only && !f.param.only.include(req.route.action)) continue; |
|---|
| | 52 | if (f.param.except && f.param.except.include(req.route.action)) continue; |
|---|
| | 53 | content = f.filter.call(this, content, req, res, session) || content; |
|---|
| | 54 | } |
|---|
| | 55 | return content; |
|---|
| | 56 | } |
|---|
| | 57 | |
|---|
| | 58 | |
|---|
| | 59 | /** |
|---|
| | 60 | * @ignore |
|---|
| | 61 | */ |
|---|
| | 62 | function createFilterObjects(args, type) { |
|---|
| | 63 | var filters = $A(args); |
|---|
| | 64 | var param = {}; |
|---|
| | 65 | var last = filters[filters.length-1]; |
|---|
| | 66 | if (!last.filter && !Object.isFunction(last)) { |
|---|
| | 67 | param = filters.pop(); |
|---|
| | 68 | } |
|---|
| | 69 | filters = filters.collect(function(f) { |
|---|
| | 70 | return new Filter(f, type, param) |
|---|
| | 71 | }); |
|---|
| | 72 | return filters; |
|---|
| | 73 | } |
|---|
| | 74 | |
|---|
| 28 | | for (var i=0; i<arguments.length; i++) { |
|---|
| 29 | | this.beforeFilters.push(arguments[i]); |
|---|
| 30 | | } |
|---|
| 31 | | return this.beforeFilters; |
|---|
| | 96 | Array.prototype.push.apply( |
|---|
| | 97 | this.beforeFilters, |
|---|
| | 98 | createFilterObjects(arguments, Filter.before) |
|---|
| | 99 | ); |
|---|
| | 100 | } |
|---|
| | 101 | |
|---|
| | 102 | |
|---|
| | 103 | /** |
|---|
| | 104 | * The passed filters will be prepended to the array of filters that |
|---|
| | 105 | * run before actions on this controller are performed. |
|---|
| | 106 | * @param {Function} filter A filter function which returns true, if the filter passed. |
|---|
| | 107 | * @see beforeFilter |
|---|
| | 108 | */ |
|---|
| | 109 | function prependBeforeFilter() { |
|---|
| | 110 | Array.prototype.unshift.apply( |
|---|
| | 111 | this.beforeFilters, |
|---|
| | 112 | createFilterObjects(arguments, Filter.before) |
|---|
| | 113 | ); |
|---|
| | 133 | * @param {Function} filter |
|---|
| | 134 | * A filter function which returns the filtered output string. |
|---|
| | 135 | * output, req, res, session will be passed as arguments to the filter function. |
|---|
| | 136 | * @see afterFilter |
|---|
| | 137 | */ |
|---|
| | 138 | function appendAfterFilter() { |
|---|
| | 139 | Array.prototype.push.apply( |
|---|
| | 140 | this.afterFilters, |
|---|
| | 141 | createFilterObjects(arguments, Filter.after) |
|---|
| | 142 | ); |
|---|
| | 143 | } |
|---|
| | 144 | |
|---|
| | 145 | |
|---|
| | 146 | /** |
|---|
| | 147 | * The passed filters will be prepended to the array of filters that |
|---|
| | 148 | * run after actions on this controller are performed. |
|---|