[525] | 1 | var fs = require('fs') |
---|
| 2 | , path = require('path') |
---|
| 3 | ; |
---|
| 4 | |
---|
| 5 | function readModule(name) { |
---|
| 6 | var filename; |
---|
| 7 | if ( name.indexOf('.') === 0 ) { |
---|
| 8 | filename = path.join(path.dirname(module.filename),name+'.js'); |
---|
| 9 | } else { |
---|
| 10 | var dirname = path.join(path.dirname(module.filename), |
---|
| 11 | '../../node_modules',name); |
---|
| 12 | filename = path.join(dirname,name+'.js'); |
---|
| 13 | var pkgfile = path.join(dirname,'package.json'); |
---|
| 14 | if ( fs.exists(pkgfile) ) { |
---|
| 15 | var pkg = require(pkgfile); |
---|
| 16 | if ( pkg.main ) { |
---|
| 17 | filename = path.join(dirname,pkg.main); |
---|
| 18 | } |
---|
| 19 | } |
---|
| 20 | } |
---|
| 21 | var src = fs.readFileSync(filename, 'utf8'); |
---|
| 22 | src = src.replace(/require\s*\(\s*(['\"])([^'"]*)(['\"])\s*\)/g, |
---|
| 23 | 'require($1views/lib/$2$3)'); |
---|
| 24 | return src; |
---|
| 25 | } |
---|
| 26 | |
---|
[455] | 27 | module.exports = { |
---|
[464] | 28 | |
---|
[519] | 29 | "schemaInfo": { |
---|
| 30 | _id: "schemaInfo", |
---|
[523] | 31 | version: "4", |
---|
| 32 | viewsVersion: "1" |
---|
[480] | 33 | }, |
---|
| 34 | |
---|
[519] | 35 | "_design/protectPublished": { |
---|
[455] | 36 | __configAction: "replace", |
---|
| 37 | language: "javascript", |
---|
| 38 | validate_doc_update: function(newDoc, oldDoc, userCtx, secObj) { |
---|
[487] | 39 | if ( oldDoc && oldDoc.publicationDate ) { |
---|
| 40 | throw({forbidden:'Published documents cannot be modified.'}); |
---|
| 41 | } |
---|
[516] | 42 | } |
---|
| 43 | }, |
---|
| 44 | |
---|
[519] | 45 | "_design/default": { |
---|
[516] | 46 | __configAction: "replace", |
---|
| 47 | language: "javascript", |
---|
[455] | 48 | views: { |
---|
| 49 | by_type: { |
---|
| 50 | map: function(doc){ |
---|
| 51 | emit(doc.type, doc); |
---|
| 52 | } |
---|
[477] | 53 | }, |
---|
| 54 | typeless: { |
---|
| 55 | map: function(doc){ |
---|
| 56 | if ( !doc.type ) { |
---|
| 57 | emit(doc._id, doc); |
---|
| 58 | } |
---|
| 59 | } |
---|
[455] | 60 | } |
---|
| 61 | } |
---|
| 62 | }, |
---|
[464] | 63 | |
---|
[519] | 64 | "_design/questions": { |
---|
[455] | 65 | __configAction: "replace", |
---|
| 66 | language: "javascript", |
---|
| 67 | views: { |
---|
| 68 | all: { |
---|
| 69 | map: function(doc){ |
---|
| 70 | if ( doc.type !== 'Question' ) { return; } |
---|
| 71 | if ( doc.categories && doc.categories.length > 0 ) { |
---|
| 72 | for ( var i = 0; i < doc.categories.length; i++ ) { |
---|
[487] | 73 | emit([doc.categories[i],doc.topic||"(default)"],1); |
---|
[455] | 74 | } |
---|
| 75 | } else { |
---|
[487] | 76 | emit(["(default)","(default)"],1); |
---|
[455] | 77 | } |
---|
| 78 | }, |
---|
| 79 | reduce: function(keys,values){ return sum(values); } |
---|
| 80 | }, |
---|
| 81 | published: { |
---|
| 82 | map: function(doc){ |
---|
| 83 | if ( doc.type!=='Question' || !doc.publicationDate ) { return; } |
---|
| 84 | if ( doc.categories && doc.categories.length > 0 ) { |
---|
| 85 | for ( var i = 0; i < doc.categories.length; i++ ) { |
---|
[487] | 86 | emit([doc.categories[i],doc.topic||"(default)"],1); |
---|
[455] | 87 | } |
---|
| 88 | } else { |
---|
[487] | 89 | emit(["(default)","(default)"],1); |
---|
[455] | 90 | } |
---|
| 91 | }, |
---|
| 92 | reduce: function(keys,values){ return sum(values); } |
---|
| 93 | }, |
---|
| 94 | all_topics: { |
---|
| 95 | map: function(doc){ |
---|
| 96 | if( doc.type !== 'Question' ){ return; } |
---|
[487] | 97 | emit(doc.topic||"(default)",1); |
---|
[455] | 98 | }, |
---|
[487] | 99 | reduce: function(key, values, rereduce) { return sum(values); } |
---|
[455] | 100 | }, |
---|
| 101 | published_topics: { |
---|
| 102 | map: function(doc){ |
---|
| 103 | if ( doc.type !== 'Question' || !doc.publicationDate ) { return; } |
---|
[487] | 104 | emit(doc.topic||"(default)",1); |
---|
[455] | 105 | }, |
---|
[487] | 106 | reduce: function(key, values, rereduce) { return sum(values); } |
---|
| 107 | }, |
---|
[525] | 108 | all_variables: { |
---|
| 109 | map: function(doc) { |
---|
| 110 | if ( doc.type !== 'Question' ) { return; } |
---|
| 111 | var _ = require('views/lib/underscore'); |
---|
| 112 | var objF = require('views/lib/object'); |
---|
| 113 | _.chain(objF.collectFields('subcode',doc.content)) |
---|
| 114 | .map(function(subcode){ |
---|
| 115 | return doc.code+subcode; |
---|
| 116 | }) |
---|
| 117 | .each(function(variable){ |
---|
| 118 | emit(variable,doc._id); |
---|
| 119 | }); |
---|
| 120 | } |
---|
| 121 | }, |
---|
| 122 | published_variables: { |
---|
| 123 | map: function(doc) { |
---|
| 124 | if ( doc.type !== 'Question' || !doc.publicationDate ) { return; } |
---|
| 125 | var _ = require('views/lib/underscore'); |
---|
| 126 | var objF = require('views/lib/object'); |
---|
| 127 | _.chain(objF.collectFields('subcode',doc.content)) |
---|
| 128 | .map(function(subcode){ |
---|
| 129 | return doc.code+subcode; |
---|
| 130 | }) |
---|
| 131 | .each(function(variable){ |
---|
| 132 | emit(variable,doc._id); |
---|
| 133 | }); |
---|
| 134 | } |
---|
| 135 | }, |
---|
[501] | 136 | all_by_code: { |
---|
[487] | 137 | map: function(doc){ |
---|
| 138 | if ( doc.type !== 'Question' ) { return; } |
---|
| 139 | emit(doc.code,doc); |
---|
| 140 | } |
---|
[501] | 141 | }, |
---|
| 142 | published_by_code: { |
---|
| 143 | map: function(doc){ |
---|
| 144 | if ( doc.type !== 'Question' || !doc.publicationDate ) { return; } |
---|
| 145 | emit(doc.code,doc); |
---|
| 146 | } |
---|
[525] | 147 | }, |
---|
| 148 | lib: { |
---|
| 149 | underscore: readModule('underscore'), |
---|
| 150 | object: readModule('../util/object') |
---|
[455] | 151 | } |
---|
| 152 | } |
---|
| 153 | }, |
---|
[464] | 154 | |
---|
[519] | 155 | "_design/surveys": { |
---|
[455] | 156 | __configAction: "replace", |
---|
| 157 | language: "javascript", |
---|
| 158 | views: { |
---|
| 159 | drafts: { |
---|
| 160 | map: function(doc){ |
---|
| 161 | if ( doc.type !== 'Survey' || doc.publicationDate ) { return; } |
---|
| 162 | emit(doc._id,doc); |
---|
| 163 | } |
---|
| 164 | }, |
---|
| 165 | published: { |
---|
| 166 | map: function(doc){ |
---|
| 167 | if ( doc.type !== 'Survey' || !doc.publicationDate ) { return; } |
---|
| 168 | emit(doc._id,doc); |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | }, |
---|
[464] | 173 | |
---|
[519] | 174 | "_design/surveyRuns": { |
---|
[492] | 175 | __configAction: "replace", |
---|
| 176 | language: "javascript", |
---|
| 177 | views: { |
---|
| 178 | by_dates: { |
---|
| 179 | map: function(doc){ |
---|
| 180 | if ( doc.type !== 'SurveyRun' ) { return; } |
---|
| 181 | var startDate = doc.startDate || ""; |
---|
| 182 | var endDate = doc.endDate || {}; |
---|
| 183 | emit([startDate,endDate,doc.liveName||null],doc); |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | }, |
---|
| 188 | |
---|
[519] | 189 | "_design/responses": { |
---|
[455] | 190 | __configAction: "replace", |
---|
| 191 | language: "javascript", |
---|
| 192 | views: { |
---|
| 193 | by_surveyrun: { |
---|
| 194 | map: function(doc){ |
---|
| 195 | if ( doc.type !== 'Response' ) { return; } |
---|
| 196 | emit(doc.surveyRunId, doc); |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | } |
---|
[464] | 201 | |
---|
[455] | 202 | }; |
---|