| 1 | | /*jslint node: true,nomen: true, vars: true */ |
| 2 | | /*jshint maxcomplexity: 5 */ |
| 3 | 1 | 'use strict'; |
| 4 | | |
| 5 | 1 | var async = require('async'); |
| 6 | 1 | var _ = require('lodash'); |
| 7 | 1 | var schemaResolver = require('./schema.resolver'); |
| 8 | 1 | var SchemaDependencies = require('./schema.dependencies'); |
| 9 | 1 | var defaultFormPack = require('./pack/handlebars/handlebars.pack'); |
| 10 | 1 | var util = require('./schema.util'); |
| 11 | | |
| 12 | 1 | var expId = /-([0-9]+)/g; // id abstraction |
| 13 | 1 | var expBracket = /\[([0-9]+)\]/g; // array key abstraction |
| 14 | 1 | var expIdDummy = /id\=\"/g; |
| 15 | 1 | var idxReplace = "--index--"; |
| 16 | | |
| 17 | | /** |
| 18 | | * @class Schema2Html |
| 19 | | * |
| 20 | | * @param {object} schema Json Schema object to render into form |
| 21 | | * @param {object} data Data object to render |
| 22 | | * @param {Schema2HtmlOptions} options Configuration object for rendering loop |
| 23 | | */ |
| 24 | | |
| 25 | | /** |
| 26 | | * @typedef Schema2HtmlOptions |
| 27 | | * @description Configuration object for render loop |
| 28 | | * |
| 29 | | * @param {string} schemaBasePath If loading $ref schema files from local fs, path should be specified |
| 30 | | * @param {string} dataPrefix If your data needs prefixing, assign the a prefix to each variable and name |
| 31 | | * @param {object} pack Form pack to use |
| 32 | | */ |
| 33 | | |
| 34 | 1 | function Schema2Html(schema, data, options) { |
| 35 | 15 | this.schema = schema || {}; |
| 36 | 15 | this.data = data || {}; |
| 37 | 15 | this.options = options || {}; |
| 38 | 15 | this.options.dataPrefix = this.options.dataPrefix || null; |
| 39 | 15 | this.templates = {}; |
| 40 | 15 | this.funcs = []; |
| 41 | 15 | this.html = []; |
| 42 | 15 | this.htmlTemplate = []; |
| 43 | 15 | this.dependencyCache = {}; |
| 44 | 15 | this.additionalData = {}; |
| 45 | 15 | this.schemaBasePath = this.options.schemaBasePath; |
| 46 | 15 | this.pack = this.options.pack || defaultFormPack; |
| 47 | 15 | this.pack.build(); |
| 48 | 15 | this.renderMode = this.pack.renderMode || 1; |
| 49 | 15 | this.pos = 0; |
| 50 | 15 | this.arrayLoopCount = 0; |
| 51 | 15 | this.rawData = this.options.rawData || false; |
| 52 | 15 | this.lookupTable = { |
| 53 | | object: this.renderLoopObject.bind(this), |
| 54 | | string: this.renderLoopString.bind(this), |
| 55 | | array: this.renderLoopArray.bind(this), |
| 56 | | number: this.renderLoopNumber.bind(this), |
| 57 | | integer: this.renderLoopInteger.bind(this), |
| 58 | | boolean: this.renderLoopBoolean.bind(this), |
| 59 | | ref: this.renderLoopRef.bind(this) |
| 60 | | }; |
| 61 | | } |
| 62 | | |
| 63 | | /** |
| 64 | | * @memberOf Schema2Html |
| 65 | | * @description Add a data resolving function |
| 66 | | * |
| 67 | | * @param {string} key |
| 68 | | * @param {function} func |
| 69 | | */ |
| 70 | | |
| 71 | 1 | Schema2Html.prototype.addResolver = function(key, func) { |
| 72 | 0 | schemaResolver[key] = func; |
| 73 | | }; |
| 74 | | |
| 75 | | /** |
| 76 | | * @memberOf Schema2Html |
| 77 | | * @description Load all dependencies and output html form |
| 78 | | * |
| 79 | | * @param {function} callback |
| 80 | | */ |
| 81 | | |
| 82 | 1 | Schema2Html.prototype.buildForm = function(callback) { |
| 83 | 3 | var _that = this, |
| 84 | | schemaDependencies = new SchemaDependencies(this.options.schemaBasePath); |
| 85 | | |
| 86 | | |
| 87 | | |
| 88 | 3 | schemaDependencies.resolveDependencies(this.schema, function(err, dependencies) { |
| 89 | | |
| 90 | | |
| 91 | | |
| 92 | 3 | if (err !== null) { |
| 93 | 1 | callback(err); |
| 94 | 1 | return null; |
| 95 | | } |
| 96 | | |
| 97 | 2 | _that.dependencyCache = dependencies; |
| 98 | 2 | _that.addRenderTask(_that.addFormOpen(_that.schema.id, _that.options.endpoint, _that.options.method)); |
| 99 | 2 | _that.renderLoop(_that.schema, 0, _that.options.dataPrefix, false); |
| 100 | 2 | _that.addRenderTask(_that.addFormClose()); |
| 101 | | |
| 102 | 2 | async.series(_that.funcs, function(err) { |
| 103 | 2 | callback(err, _that.html.join(""), _that.htmlTemplate.join("").replace(expId,'-' + idxReplace).replace(expBracket, '[' + idxReplace + ']').split('id="').join('id="tmpl-').split("id='").join("id='tmpl-")); |
| 104 | | }); |
| 105 | | }); |
| 106 | | |
| 107 | | }; |
| 108 | | |
| 109 | | /** |
| 110 | | * @memberOf Schema2Html |
| 111 | | * @description Main circular render loop, all objects are passed through the loop, with render functions then being executed through the aysnc library |
| 112 | | * |
| 113 | | * @param {object} schema |
| 114 | | * @param {integer} depth |
| 115 | | * @param {string} scopeName |
| 116 | | */ |
| 117 | | |
| 118 | 1 | Schema2Html.prototype.renderLoop = function(schema, depth, scopeName) { |
| 119 | 28 | schema = schema || {}; |
| 120 | 28 | var type = schema.$ref ? 'ref' : schema.type, |
| 121 | | id = schema.id || scopeName, |
| 122 | | execProcess; |
| 123 | | |
| 124 | 28 | id = this.generateId(id); |
| 125 | | |
| 126 | 28 | execProcess = this.lookupTable[type] || null; |
| 127 | | |
| 128 | 28 | if (execProcess !== null && this.pack.security(schema) === true) { |
| 129 | 27 | execProcess(id, scopeName, depth, schema); |
| 130 | | } |
| 131 | | }; |
| 132 | | |
| 133 | | /** |
| 134 | | * @memberOf Schema2Html |
| 135 | | * @description adds an individual render task for the string schema type |
| 136 | | * |
| 137 | | * @param {string} id html id string |
| 138 | | * @param {string} name html name string |
| 139 | | * @param {integer} depth how deep the item is nested |
| 140 | | * @param {object} schema |
| 141 | | */ |
| 142 | | |
| 143 | 1 | Schema2Html.prototype.renderLoopString = function(id, name, depth, schema) { |
| 144 | 17 | var label = schema.title || name, |
| 145 | | options = schema.options || {}, |
| 146 | | val = util.retrieveValue(this.data, name, false); |
| 147 | | |
| 148 | 17 | options.depth = depth; |
| 149 | 17 | options.key = util.dotSyntax(name); |
| 150 | | |
| 151 | | |
| 152 | 17 | this.addRenderTask(this.addString(id, name, val, label, null, options)); |
| 153 | | }; |
| 154 | | |
| 155 | | /** |
| 156 | | * @memberOf Schema2Html |
| 157 | | * |
| 158 | | * @param {string} id html id string |
| 159 | | * @param {string} name html name string |
| 160 | | * @param {integer} depth how deep the item is nested |
| 161 | | * @param {object} schema |
| 162 | | * |
| 163 | | */ |
| 164 | | |
| 165 | 1 | Schema2Html.prototype.renderLoopRef = function(id, name, depth, schema) { |
| 166 | | //this.dependencyCache[schema.$ref].type || null; // try ref if no type |
| 167 | 1 | this.renderLoop(this.dependencyCache[schema.$ref], depth, name); |
| 168 | | }; |
| 169 | | |
| 170 | | /** |
| 171 | | * @memberOf Schema2Html |
| 172 | | * @description renders schema#object |
| 173 | | * |
| 174 | | * @param {string} id html id string |
| 175 | | * @param {string} scopeName html name string |
| 176 | | * @param {integer} depth how deep the item is nested |
| 177 | | * @param {object} schema |
| 178 | | */ |
| 179 | | |
| 180 | 1 | Schema2Html.prototype.renderLoopObject = function(id, scopeName, depth, schema) { |
| 181 | 7 | var _that = this, name, match, options; |
| 182 | | |
| 183 | 7 | options = schema.options || {}; |
| 184 | 7 | depth += 1; |
| 185 | | |
| 186 | 7 | this.addRenderTask(this.addGroupOpen(id, 0, depth)); // open a group |
| 187 | | |
| 188 | 7 | _.forOwn(schema.properties, function(property, key) { |
| 189 | 23 | name = _that.generateName(scopeName, key); |
| 190 | 23 | property.id = _that.generateId(name); |
| 191 | 23 | if (property.anyOf !== undefined) { |
| 192 | 0 | if (_.isArray(property.anyOf)) { |
| 193 | 0 | match = options.matchOn; |
| 194 | | |
| 195 | 0 | _that.renderLoopArrayAnyOf(id, scopeName, depth, 0, match, property.anyOf); |
| 196 | | |
| 197 | | } |
| 198 | | } else { |
| 199 | 23 | _that.renderLoop(property, depth + 1, name); // send property back in the loop |
| 200 | | } |
| 201 | | }); |
| 202 | | |
| 203 | 7 | this.addRenderTask(this.addGroupClose(id, depth)); // close group |
| 204 | | |
| 205 | | |
| 206 | | }; |
| 207 | | |
| 208 | | /** |
| 209 | | * @memberOf Schema2Html |
| 210 | | * @description adds an individual render task for the array schema type |
| 211 | | * |
| 212 | | * @param {string} id |
| 213 | | * @param {string} name |
| 214 | | * @param {integer} depth |
| 215 | | * @param {integer} index |
| 216 | | * @param {string} match |
| 217 | | * @param {object} schema |
| 218 | | */ |
| 219 | | |
| 220 | 1 | Schema2Html.prototype.renderLoopArrayAnyOf = function(id, name, depth, index, match, schema) { |
| 221 | 0 | var _that = this, |
| 222 | | ref, |
| 223 | | matchVal; |
| 224 | | |
| 225 | 0 | _that.arrayLoopCount += 1; |
| 226 | | |
| 227 | 0 | schema.anyOf.forEach(function(val) { |
| 228 | 0 | ref = val.$ref; |
| 229 | | |
| 230 | 0 | matchVal = util.retrieveValue(_that.data, (name + '[' + index + ']' + '[' + match + ']')); |
| 231 | | |
| 232 | 0 | if (ref === matchVal && _that.renderMode !== 2) { |
| 233 | 0 | _that.renderLoop(_that.dependencyCache[ref], (depth + 1), (name + '[' + index + ']')); |
| 234 | 0 | } else if (_that.renderMode === 2) { |
| 235 | | // render mode is 2, means were rendering a template based output |
| 236 | 0 | var indexVal = _that.pack.engine.index; |
| 237 | 0 | var backtickVal = _that.pack.engine.backTick; |
| 238 | 0 | var open = _that.pack.engine.open; |
| 239 | 0 | var close = _that.pack.engine.close; |
| 240 | | |
| 241 | 0 | _that.addRenderTask(_that.addAnyOfOpen(ref)); |
| 242 | 0 | _that.renderLoop(_that.dependencyCache[ref], (depth + 1), (name + '[' + open + util.repeat(_that.arrayLoopCount, backtickVal) + indexVal + close + ']')); |
| 243 | 0 | _that.addRenderTask(_that.addAnyOfClose(ref)); |
| 244 | | } |
| 245 | | |
| 246 | | }); |
| 247 | | |
| 248 | 0 | _that.arrayLoopCount += -1; |
| 249 | | }; |
| 250 | | |
| 251 | | /** |
| 252 | | * @memberOf Schema2Html |
| 253 | | * @description adds render tasks for an item within an array |
| 254 | | * |
| 255 | | * @param {string} id |
| 256 | | * @param {string} name |
| 257 | | * @param {integer} depth |
| 258 | | * @param {integer} index |
| 259 | | * @param {object} schema |
| 260 | | */ |
| 261 | | |
| 262 | 1 | Schema2Html.prototype.renderLoopArrayItem = function(id, name, depth, index, schema) { |
| 263 | | |
| 264 | | |
| 265 | | // plain old item definition |
| 266 | 2 | if (this.renderMode === 2) { |
| 267 | | |
| 268 | 0 | var indexVal = this.pack.engine.index; |
| 269 | 0 | var open = this.pack.engine.open; |
| 270 | 0 | var close = this.pack.engine.close; |
| 271 | | |
| 272 | 0 | this.addRenderTask(this.addGroupTag('groupItemOpen', id + '-group-' + open + indexVal + close + '-' + name + '[' + open + indexVal + close + ']', depth + 1)); // render tag open |
| 273 | 0 | this.renderLoop(schema, depth + 1, name + '[' + open + indexVal + close + ']'); |
| 274 | 0 | this.addRenderTask(this.addGroupTag('groupItemClose', id + '-group-' + open + indexVal + close, name + '[' + open + indexVal + close + ']', depth + 1)); // render tag close |
| 275 | | |
| 276 | 0 | return id + '-group-' + open + indexVal + close + '-' + name + '[' + open + indexVal + close + ']'; |
| 277 | | |
| 278 | | } else { |
| 279 | 2 | this.addRenderTask(this.addGroupTag('groupItemOpen', id + '-group-' + index, name + '[' + index + ']', depth + 1)); // render tag open |
| 280 | 2 | this.renderLoop(schema, depth + 1, name + '[' + index + ']'); |
| 281 | 2 | this.addRenderTask(this.addGroupTag('groupItemClose', id + '-group-' + index, name + '[' + index + ']', depth + 1)); // render tag close |
| 282 | | |
| 283 | 2 | return id + '-group-' + index; |
| 284 | | } |
| 285 | | |
| 286 | | |
| 287 | | }; |
| 288 | | |
| 289 | | /** |
| 290 | | * @memberOf Schema2Html |
| 291 | | * @description adds render tasks for schema#array |
| 292 | | * |
| 293 | | * @param {string} id |
| 294 | | * @param {string} name |
| 295 | | * @param {integer} depth |
| 296 | | * @param {object} schema |
| 297 | | */ |
| 298 | | |
| 299 | | |
| 300 | 1 | Schema2Html.prototype.renderLoopArray = function(id, name, depth, schema) { |
| 301 | 2 | var _that = this, |
| 302 | | insertId = "", |
| 303 | | minItems = schema.minItems || 1, |
| 304 | | maxItems = schema.maxItems, |
| 305 | | uniqueItems = schema.uniqueItems, |
| 306 | | itemsToRender, |
| 307 | | options, |
| 308 | | dataArr, |
| 309 | | match, |
| 310 | | ref, |
| 311 | | i; |
| 312 | | |
| 313 | | |
| 314 | 2 | options = schema.items.options || {}; |
| 315 | 2 | dataArr = util.retrieveValue(this.data, name); |
| 316 | | |
| 317 | 2 | itemsToRender = _.isArray(dataArr) ? dataArr.length : minItems; |
| 318 | 2 | itemsToRender = itemsToRender || 1; |
| 319 | 2 | itemsToRender = this.renderMode === 2 ? 1 : itemsToRender; |
| 320 | | |
| 321 | 2 | this.addRenderTask(this.addGroupTag('groupArrayOpen', id + '-group-many', name, depth, schema)); // open a group |
| 322 | | |
| 323 | 2 | for (i = 0; i < itemsToRender; i += 1) { |
| 324 | 2 | if (schema.items.$ref) { |
| 325 | 0 | this.renderLoopArrayItem(id, name, depth, i, _that.dependencyCache[schema.items.$ref]); |
| 326 | 2 | } else if (schema.items.anyOf) { |
| 327 | 0 | match = options.matchOn; |
| 328 | | |
| 329 | 0 | this.renderLoopArrayAnyOf(id, name, depth, i, match, schema.items); |
| 330 | | |
| 331 | 2 | } else if (schema.items.oneOf) { |
| 332 | 0 | match = options.matchOn; |
| 333 | | |
| 334 | 0 | schema.items.oneOf.forEach(function(val, index) { |
| 335 | 0 | ref = val.$ref; |
| 336 | | |
| 337 | 0 | if (ref === match) { |
| 338 | 0 | this.renderLoop(_that.dependencyCache[ref], depth + 1, name + '[' + index + ']'); |
| 339 | | } |
| 340 | | |
| 341 | | }); |
| 342 | | |
| 343 | | } else { |
| 344 | 2 | insertId = this.renderLoopArrayItem(id, name, depth, i, schema.items); |
| 345 | | } |
| 346 | | } |
| 347 | | |
| 348 | 2 | insertId = insertId.replace(expId, '-' + idxReplace).replace(expBracket, '[' + idxReplace + ']'); |
| 349 | | |
| 350 | | |
| 351 | | |
| 352 | 2 | this.addRenderTask(this.addGroupTag('groupArrayClose', id + '-group-many', name, depth, {insertTemplate: 'tmpl-' + insertId})); // open a group |
| 353 | | |
| 354 | | }; |
| 355 | | |
| 356 | | /** |
| 357 | | * @memberOf Schema2Html |
| 358 | | * @description adds an individual render task for the number schema type |
| 359 | | * |
| 360 | | * @param {string} id html id string |
| 361 | | * @param {string} name html name string |
| 362 | | * @param {integer} depth how deep the item is nested |
| 363 | | * @param {object} schema |
| 364 | | */ |
| 365 | | |
| 366 | 1 | Schema2Html.prototype.renderLoopNumber = function(id, name, depth, schema) { |
| 367 | 1 | this.renderLoopString(id, name, depth, schema); |
| 368 | | }; |
| 369 | | |
| 370 | | /** |
| 371 | | * @memberOf Schema2Html |
| 372 | | * @description adds an individual render task for the integer schema type |
| 373 | | * |
| 374 | | * @param {string} id html id string |
| 375 | | * @param {string} name html name string |
| 376 | | * @param {integer} depth how deep the item is nested |
| 377 | | * @param {object} schema |
| 378 | | */ |
| 379 | | |
| 380 | 1 | Schema2Html.prototype.renderLoopInteger = function(id, name, depth, schema) { |
| 381 | 1 | this.renderLoopString(id, name, depth, schema); |
| 382 | | }; |
| 383 | | |
| 384 | | /** |
| 385 | | * @memberOf Schema2Html |
| 386 | | * @description adds an individual render task for the boolean schema type |
| 387 | | * |
| 388 | | * @param {string} id html id string |
| 389 | | * @param {string} name html name string |
| 390 | | * @param {integer} depth how deep the item is nested |
| 391 | | * @param {object} schema |
| 392 | | */ |
| 393 | | |
| 394 | 1 | Schema2Html.prototype.renderLoopBoolean = function(id, name, depth, schema) { |
| 395 | 1 | this.renderLoopString(id, name, depth, schema); |
| 396 | | }; |
| 397 | | |
| 398 | | /** |
| 399 | | * @memberOf Schema2Html |
| 400 | | * @description return a function adding a formOpenTag with a single callback that can be added to the control flow, or executed standalone |
| 401 | | * |
| 402 | | * @param {string} id |
| 403 | | * @param {string} endpoint |
| 404 | | * @param {string} method |
| 405 | | */ |
| 406 | | |
| 407 | 1 | Schema2Html.prototype.addFormOpen = function(id, endpoint, method) { |
| 408 | 3 | var _that = this, |
| 409 | | pos = this.pos; |
| 410 | | |
| 411 | 3 | id = this.generateId(id); |
| 412 | 3 | this.pos += 1; |
| 413 | | |
| 414 | 3 | return function(callback) { |
| 415 | 3 | _that.renderFormTag('formOpen', id, endpoint, method, function(err, result) { |
| 416 | 3 | _that.appendHtml(result, pos); |
| 417 | 3 | _that.appendDummyHtml(result, pos); |
| 418 | 3 | callback(err, result); |
| 419 | | }); |
| 420 | | }; |
| 421 | | }; |
| 422 | | |
| 423 | | /** |
| 424 | | * @memberOf Schema2Html |
| 425 | | * @description |
| 426 | | * |
| 427 | | * @param {string} dataRef |
| 428 | | */ |
| 429 | | |
| 430 | 1 | Schema2Html.prototype.addAnyOfOpen = function(dataRef) { |
| 431 | 0 | var _that = this, |
| 432 | | pos = this.pos; |
| 433 | 0 | this.pos += 1; |
| 434 | 0 | return function(callback) { |
| 435 | 0 | _that.renderAnyOfOpenTag(dataRef, function(err, result) { |
| 436 | 0 | _that.appendHtml(result, pos); |
| 437 | 0 | _that.appendDummyHtml(result, pos); |
| 438 | 0 | callback(err, result); |
| 439 | | }); |
| 440 | | }; |
| 441 | | }; |
| 442 | | |
| 443 | | /** |
| 444 | | * @memberOf Schema2Html |
| 445 | | * @description |
| 446 | | * |
| 447 | | * @param {string} dataRef |
| 448 | | */ |
| 449 | | |
| 450 | 1 | Schema2Html.prototype.addAnyOfClose = function(dataRef) { |
| 451 | 0 | var _that = this, |
| 452 | | pos = this.pos; |
| 453 | 0 | this.pos += 1; |
| 454 | 0 | return function(callback) { |
| 455 | 0 | _that.renderAnyOfCloseTag(dataRef, function(err, result) { |
| 456 | 0 | _that.appendHtml(result, pos); |
| 457 | 0 | _that.appendDummyHtml(result, pos); |
| 458 | 0 | callback(err, result); |
| 459 | | }); |
| 460 | | }; |
| 461 | | }; |
| 462 | | |
| 463 | | /** |
| 464 | | * @memberOf Schema2Html |
| 465 | | * @description |
| 466 | | * |
| 467 | | * @param {string} dataRef |
| 468 | | * @param {function} callback |
| 469 | | */ |
| 470 | | |
| 471 | 1 | Schema2Html.prototype.renderAnyOfOpenTag = function(dataRef, callback) { |
| 472 | 0 | var params = { |
| 473 | | dataRef: dataRef |
| 474 | | }; |
| 475 | | |
| 476 | | |
| 477 | | |
| 478 | 0 | this.renderTemplate('anyOfOpen', params, function(err, result) { |
| 479 | 0 | callback(err, result); |
| 480 | | }); |
| 481 | | |
| 482 | | }; |
| 483 | | |
| 484 | | /** |
| 485 | | * @memberOf Schema2Html |
| 486 | | * @description |
| 487 | | * |
| 488 | | * @param {string} dataRef |
| 489 | | * @param {function} callback |
| 490 | | */ |
| 491 | | |
| 492 | 1 | Schema2Html.prototype.renderAnyOfCloseTag = function(dataRef, callback) { |
| 493 | 0 | var params = { |
| 494 | | dataRef: dataRef |
| 495 | | }; |
| 496 | | |
| 497 | 0 | this.renderTemplate('anyOfClose', params, function(err, result) { |
| 498 | 0 | callback(err, result); |
| 499 | | }); |
| 500 | | |
| 501 | | }; |
| 502 | | |
| 503 | | /** |
| 504 | | * @memberOf Schema2Html |
| 505 | | * @description Renders the opening and closing tags |
| 506 | | * |
| 507 | | * @param {string[formOpen,formClose]} type specify if type is opening or closing |
| 508 | | * @param {number} id html id assigned to form |
| 509 | | * @param {string} form endpoint submission endpoint |
| 510 | | * @param {string} method http method |
| 511 | | * @param {callback} |
| 512 | | */ |
| 513 | | |
| 514 | 1 | Schema2Html.prototype.renderFormTag = function(type, id, endpoint, method, callback) { |
| 515 | 10 | var params = {}; |
| 516 | | |
| 517 | 10 | params.id = id; |
| 518 | 10 | params.endpoint = endpoint; |
| 519 | 10 | params.method = method; |
| 520 | | |
| 521 | 10 | this.renderTemplate(type, params, function(err, result) { |
| 522 | 10 | callback(err, result); |
| 523 | | }); |
| 524 | | |
| 525 | | }; |
| 526 | | |
| 527 | | /** |
| 528 | | * @memberOf Schema2Html |
| 529 | | * @description return a function adding a formCloseTag with a single callback that can be added to the control flow, or executed standalone |
| 530 | | */ |
| 531 | | |
| 532 | 1 | Schema2Html.prototype.addFormClose = function() { |
| 533 | 3 | var _that = this, |
| 534 | | pos = this.pos; |
| 535 | | |
| 536 | 3 | this.pos += 1; |
| 537 | | |
| 538 | 3 | return function(callback) { |
| 539 | 3 | _that.renderFormTag('formClose', null, null, null, function(err, result) { |
| 540 | 3 | _that.appendHtml(result, pos); |
| 541 | 3 | _that.appendDummyHtml(result, pos); |
| 542 | 3 | callback(err, result); |
| 543 | | }); |
| 544 | | }; |
| 545 | | }; |
| 546 | | |
| 547 | | /** |
| 548 | | * @memberOf Schema2Html |
| 549 | | * @description Renders a template based on a path of a given key in the configuration |
| 550 | | * |
| 551 | | * @param {string} key |
| 552 | | * @param {object} params |
| 553 | | * @param {callback} callback |
| 554 | | */ |
| 555 | | |
| 556 | 1 | Schema2Html.prototype.renderTemplate = function(key, params, callback) { |
| 557 | 72 | var result = null, |
| 558 | | template = null, |
| 559 | | _that = this; |
| 560 | | |
| 561 | 72 | if (this.pack.templates[key] === undefined) { |
| 562 | 1 | callback(new Error("Template has no valid path:" + key)); |
| 563 | 1 | return null; // exit this is not going to work |
| 564 | | } |
| 565 | | |
| 566 | 71 | if (typeof (this.pack.templates[key]) !== 'function') { |
| 567 | 10 | this.pack.loadTemplate(key, function(err, compiled) { |
| 568 | 10 | if (err === null) { |
| 569 | 10 | try { |
| 570 | 10 | _that.pack.templates[key] = compiled; |
| 571 | | // result = compiled(params); |
| 572 | 10 | _that.pack.renderTemplate(compiled, params, function(pErr, pResult) { |
| 573 | 10 | callback(pErr, pResult); |
| 574 | | }); |
| 575 | | |
| 576 | | } catch (templateErr) { |
| 577 | 0 | callback(templateErr, result); |
| 578 | | } |
| 579 | | } else { |
| 580 | 0 | callback(err, result); |
| 581 | | } |
| 582 | | |
| 583 | | }); |
| 584 | | } else { |
| 585 | 61 | template = this.pack.templates[key]; |
| 586 | 61 | _that.pack.renderTemplate(template, params, function(pErr, pResult) { |
| 587 | 61 | callback(pErr, pResult); |
| 588 | | }); |
| 589 | | //result = template(params); |
| 590 | | //callback(null, result); |
| 591 | | } |
| 592 | | }; |
| 593 | | |
| 594 | | /** |
| 595 | | * @memberOf Schema2Html |
| 596 | | * @description changes a template path |
| 597 | | * |
| 598 | | * @param {string} key |
| 599 | | * @param {string} path |
| 600 | | */ |
| 601 | | |
| 602 | 1 | Schema2Html.prototype.changeTemplate = function(key, path) { |
| 603 | 0 | this.config.templates[key] = path; |
| 604 | | }; |
| 605 | | |
| 606 | 1 | Schema2Html.prototype.renderCustomHandler = function() {}; |
| 607 | | |
| 608 | | /** |
| 609 | | * @memberOf Schema2Html |
| 610 | | * @description render group tag open |
| 611 | | * |
| 612 | | * @param {string} id |
| 613 | | * @param {integer} total |
| 614 | | * @param {integer} depth |
| 615 | | */ |
| 616 | | |
| 617 | 1 | Schema2Html.prototype.addGroupOpen = function(id, total, depth) { |
| 618 | 8 | var _that = this, |
| 619 | | pos = this.pos; |
| 620 | | |
| 621 | 8 | this.pos += 1; |
| 622 | 8 | id = this.generateId(id); |
| 623 | | |
| 624 | 8 | return function(callback) { |
| 625 | 8 | _that.renderGroupOpen(id, total, depth, function(err, result) { |
| 626 | 8 | _that.appendHtml(result, pos); |
| 627 | 8 | _that.appendDummyHtml(result, pos); |
| 628 | 8 | callback(err, result); |
| 629 | | }); |
| 630 | | }; |
| 631 | | }; |
| 632 | | |
| 633 | | /** |
| 634 | | * @memberOf Schema2Html |
| 635 | | * @description render group tag open |
| 636 | | * |
| 637 | | * @param {string} id |
| 638 | | * @param {integer} total |
| 639 | | * @param {integer} depth |
| 640 | | * @param {function} callback |
| 641 | | */ |
| 642 | | |
| 643 | 1 | Schema2Html.prototype.renderGroupOpen = function(id, total, depth, callback) { |
| 644 | 9 | var params = {}; |
| 645 | | |
| 646 | 9 | params.id = id; |
| 647 | 9 | params.total = total || 0; |
| 648 | 9 | params.options = { |
| 649 | | depth: depth |
| 650 | | }; |
| 651 | | |
| 652 | 9 | this.renderTemplate('startGroup', params, function(err, result) { |
| 653 | 9 | callback(err, result); |
| 654 | | }); |
| 655 | | }; |
| 656 | | |
| 657 | | /** |
| 658 | | * @memberOf Schema2Html |
| 659 | | * @description add group tag close |
| 660 | | * |
| 661 | | * @param {string} id |
| 662 | | * @param {integer} depth |
| 663 | | */ |
| 664 | | |
| 665 | 1 | Schema2Html.prototype.addGroupClose = function(id, depth) { |
| 666 | 8 | var _that = this, |
| 667 | | pos = this.pos, |
| 668 | | params = { |
| 669 | | id: id |
| 670 | | }; |
| 671 | | |
| 672 | 8 | params.options = { |
| 673 | | |
| 674 | | depth: depth |
| 675 | | }; |
| 676 | | |
| 677 | 8 | this.pos += 1; |
| 678 | | |
| 679 | 8 | return function(callback) { |
| 680 | 8 | _that.renderGroupClose(params, function(err, result) { |
| 681 | 8 | _that.appendHtml(result, pos); |
| 682 | 8 | _that.appendDummyHtml(result, pos); |
| 683 | 8 | callback(err, result); |
| 684 | | }); |
| 685 | | }; |
| 686 | | }; |
| 687 | | |
| 688 | | /** |
| 689 | | * @memberOf Schema2Html |
| 690 | | * @description render group tag close |
| 691 | | * |
| 692 | | * @param {object} params |
| 693 | | * @param {function} callback |
| 694 | | */ |
| 695 | | |
| 696 | 1 | Schema2Html.prototype.renderGroupClose = function(params, callback) { |
| 697 | 9 | this.renderTemplate('endGroup', params, function(err, result) { |
| 698 | 9 | callback(err, result); |
| 699 | | }); |
| 700 | | }; |
| 701 | | |
| 702 | | /** |
| 703 | | * @memberOf Schema2Html |
| 704 | | * @description returns a render function for a open/close tag for generic groups |
| 705 | | * |
| 706 | | * @param {string} type |
| 707 | | * @param {string} id |
| 708 | | * @param {string} name |
| 709 | | * @param {integer} depth |
| 710 | | * @param {object} options |
| 711 | | */ |
| 712 | | |
| 713 | 1 | Schema2Html.prototype.addGroupTag = function(type, id, name, depth, options) { |
| 714 | 8 | var _that = this, |
| 715 | | pos = this.pos; |
| 716 | | |
| 717 | 8 | id = this.generateId(id); |
| 718 | | |
| 719 | 8 | options = options || {}; |
| 720 | 8 | options.key = util.dotSyntax(name); |
| 721 | 8 | options.keyName = util.rawName(name); |
| 722 | 8 | options.keyInner = util.innerName(name); |
| 723 | 8 | options.arrayDepth = this.arrayLoopCount; |
| 724 | 8 | this.pos += 1; |
| 725 | | |
| 726 | 8 | return function(callback) { |
| 727 | 8 | _that.renderGroupTag(id, name, depth, options, type, function(err, result) { |
| 728 | 8 | _that.appendHtml(result, pos); |
| 729 | 8 | _that.appendDummyHtml(result, pos); |
| 730 | 8 | callback(err, result); |
| 731 | | }); |
| 732 | | }; |
| 733 | | }; |
| 734 | | |
| 735 | | /** |
| 736 | | * @description renders are group open/close tag |
| 737 | | * |
| 738 | | * @param {string} id |
| 739 | | * @param {string} name |
| 740 | | * @param {integer} depth |
| 741 | | * @param {object} options |
| 742 | | * @param {string} type |
| 743 | | * @param {function} callback |
| 744 | | * |
| 745 | | */ |
| 746 | | |
| 747 | 1 | Schema2Html.prototype.renderGroupTag = function(id, name, depth, options, type, callback) { |
| 748 | 8 | var params = { |
| 749 | | id: id, |
| 750 | | name: name, |
| 751 | | depth: depth, |
| 752 | | options: options || {} |
| 753 | | }; |
| 754 | | |
| 755 | 8 | params.options.depth = depth; |
| 756 | | |
| 757 | 8 | this.renderTemplate(type, params, function(err, result) { |
| 758 | 8 | callback(err, result); |
| 759 | | }); |
| 760 | | }; |
| 761 | | |
| 762 | | /** |
| 763 | | * @memberOf Schema2Html |
| 764 | | * @description Appends rendered html to the internal html variable |
| 765 | | * |
| 766 | | * @param {string} html |
| 767 | | * @param {integer} pos |
| 768 | | */ |
| 769 | | |
| 770 | 1 | Schema2Html.prototype.appendHtml = function(html, pos) { |
| 771 | 47 | this.html[pos] = html; |
| 772 | | |
| 773 | | //this.appendDummyHtml(html, pos); |
| 774 | | }; |
| 775 | | |
| 776 | | /** |
| 777 | | * @memberOf Schema2Html |
| 778 | | * @description Append a string to the dummy html output |
| 779 | | */ |
| 780 | | |
| 781 | 1 | Schema2Html.prototype.appendDummyHtml = function(html, pos) { |
| 782 | 47 | this.htmlTemplate[pos] = html; |
| 783 | | }; |
| 784 | | |
| 785 | | /** |
| 786 | | * @memberOf Schema2Html |
| 787 | | * @description return a function that renders string template based on input variables |
| 788 | | * |
| 789 | | * @param {string} id |
| 790 | | * @param {string} name |
| 791 | | * @param {*} val |
| 792 | | * @param {string} label |
| 793 | | * @param {array} src |
| 794 | | * @param {object} options |
| 795 | | * @param {boolean} required |
| 796 | | */ |
| 797 | | |
| 798 | 1 | Schema2Html.prototype.addString = function(id, name, val, label, src, options, required) { |
| 799 | 17 | var _that = this, |
| 800 | | pos = this.pos; |
| 801 | | |
| 802 | 17 | this.pos += 1; |
| 803 | | |
| 804 | 17 | return function(callback) { |
| 805 | 17 | _that.renderString(id, name, val, label, src, options, required, function(err, result) { |
| 806 | 17 | _that.appendHtml(result, pos); |
| 807 | | //callback(err, result); |
| 808 | 17 | _that.renderString(id, name, null, label, src, options, required, function(err, resultTwo) { |
| 809 | 17 | _that.appendDummyHtml(resultTwo, pos); |
| 810 | 17 | callback(err, result); |
| 811 | | }); |
| 812 | | }); |
| 813 | | |
| 814 | | |
| 815 | | }; |
| 816 | | }; |
| 817 | | |
| 818 | | /** |
| 819 | | * @memberOf Schema2Html |
| 820 | | * @description Render string template |
| 821 | | * |
| 822 | | * @param {string} id |
| 823 | | * @param {string} name |
| 824 | | * @param {*} val |
| 825 | | * @param {string} label |
| 826 | | * @param {array} src |
| 827 | | * @param {object} options |
| 828 | | * @param {boolean} required |
| 829 | | * @param {function} callback |
| 830 | | */ |
| 831 | | |
| 832 | 1 | Schema2Html.prototype.renderString = function(id, name, val, label, src, options, required, callback) { |
| 833 | 35 | var params = {}, |
| 834 | | template = 'textfield'; |
| 835 | | |
| 836 | 35 | params.id = this.generateId(id); |
| 837 | 35 | params.name = name; |
| 838 | 35 | params.val = val; |
| 839 | 35 | params.src = src; |
| 840 | 35 | params.label = label; |
| 841 | 35 | params.options = options || {}; |
| 842 | 35 | params.required = required || false; |
| 843 | 35 | params.options.keyName = util.rawName(name); |
| 844 | 35 | params.options.keyInner = util.innerName(name, params.options.depth || 0); |
| 845 | 35 | params.options.arrayDepth = this.arrayLoopCount; |
| 846 | 35 | params.datasrc = params.options.datasrc ? this.dependencyCache[params.options.datasrc] : null; |
| 847 | | |
| 848 | 35 | template = params.options.format || template; |
| 849 | | |
| 850 | 35 | this.renderTemplate(template, params, function(err, result) { |
| 851 | 35 | callback(err, result); |
| 852 | | }); |
| 853 | | }; |
| 854 | | |
| 855 | | /** |
| 856 | | * @memberOf Schema2Html |
| 857 | | * @description Push a task into what will make up the rendering loop |
| 858 | | * |
| 859 | | * @param {function} func |
| 860 | | */ |
| 861 | | |
| 862 | 1 | Schema2Html.prototype.addRenderTask = function(func) { |
| 863 | 43 | this.funcs.push(func); |
| 864 | | }; |
| 865 | | |
| 866 | | /** |
| 867 | | * @memberOf Schema2Html |
| 868 | | * @description Generate a valid html id for use in the dom |
| 869 | | * |
| 870 | | * @param {string} scope |
| 871 | | */ |
| 872 | | |
| 873 | 1 | Schema2Html.prototype.generateId = function(scope) { |
| 874 | 106 | scope = scope || ""; |
| 875 | 106 | return scope !== null ? scope.toLowerCase().split('../').join('-@-').split('[').join('-').split(']').join('').split('.').join('-').split(' ').join('-').split('-@-').join('../') : null; |
| 876 | | }; |
| 877 | | |
| 878 | | /** |
| 879 | | * @memberOf Schema2Html |
| 880 | | * @description Genetate a valid form name |
| 881 | | * |
| 882 | | * @param {string} preScope |
| 883 | | * @param {string} newScope |
| 884 | | * |
| 885 | | */ |
| 886 | | |
| 887 | 1 | Schema2Html.prototype.generateName = function(preScope, newScope) { |
| 888 | 23 | var name; |
| 889 | | |
| 890 | 23 | if (preScope !== null) { |
| 891 | 10 | name = preScope + '[' + newScope + ']'; |
| 892 | | } else { |
| 893 | 13 | name = newScope; |
| 894 | | } |
| 895 | | |
| 896 | 23 | return name; |
| 897 | | }; |
| 898 | | |
| 899 | | |
| 900 | 1 | module.exports = Schema2Html; |