UNPKG

5.4 kBJavaScriptView Raw
1var $;
2try
3{
4 $ = require('jquery');
5}
6catch(e)
7{
8 $ = function() { };
9}
10var mongoose = require('mongoose');
11
12function Renderer() {
13
14};
15
16/** Builds HTML for an entire document
17 *
18 * @param {Array} models
19 * @param {Object} fields
20 * @param {Object} options
21 * @param {Object} document
22 * @param {Function} onReady
23 *
24 * @return {String}
25 */
26Renderer.renderDocument = function(models, fields, options, document, onReady) {
27 var documentDiv = $('<div />');
28 var form = $('<form />').attr('id', 'document');
29
30 if (document) {
31 form.append($('<input />').attr('id', 'document_id').attr('name', 'document_id').attr('type', 'hidden').val(document._id));
32 } else {
33 form.append($('<input />').attr('id', 'document_id').attr('name', 'document_id').attr('type', 'hidden').val(''));
34 }
35
36 for (field in fields) {
37 if (!options.hidden || options.hidden.indexOf(field) == -1) {
38 form.append(Renderer.renderDocumentField(models, field, fields, options, document, false, ''));
39 }
40 }
41
42 documentDiv.append(form);
43 onReady(documentDiv.html());
44};
45
46/**
47 * Builds HTML component for a single field of a document
48 *
49 * @param {Array} models
50 * @param {String} field
51 * @param {Object} fields
52 * @param {Object} options
53 * @param {Object} document
54 * @param {Boolean} isChild
55 * @param{String} parentFieldName
56 *
57 * @return {Object}
58 */
59Renderer.renderDocumentField = function(models, field, fields, options, document, isChild, parentFieldName) {
60 var isRequired = fields[field].required;
61 var labelClassName = isRequired ? 'required_label' : 'optional_label';
62
63 var span = $('<span />').addClass('document_label');
64 if (isChild) {
65 span.addClass('inline_element');
66 }
67
68 var label = $('<label />').attr('for', field).addClass(labelClassName).html(field);
69 if (isChild) {
70 label.addClass('inline_element');
71 }
72 span.append(label);
73
74 var result = $('<p />');
75 result.append(span);
76
77 // nested objects
78 var fieldValue = '';
79 var fieldName = '';
80 if (isChild) {
81 fieldValue = (document && document[parentFieldName][field]) ? document[parentFieldName][field].toString() : '';
82 fieldName = parentFieldName + '[' + field + ']';
83 } else {
84 fieldValue = (document && document[field]) ? document[field].toString() : '';
85 fieldName = field;
86 }
87
88
89 if (fields[field].type === Date) {
90 // Objects defined in mongoose schema as type:Date
91 result.append($('<input />').attr('id', fieldName).attr('name', fieldName).attr('type', 'text').attr('placeholder', 'Date or Date & Time').attr('value', fieldValue).addClass('document_input'));
92 } else if (fields[field].type === Boolean){
93 // a total hack
94 var el = $('<input />').attr('type', 'text').attr('id', fieldName).attr('name', fieldName).addClass('document_input')
95 el.attr('value', fieldValue)
96 result.append(el);
97 } else if (fields[field].type === String || fields[field].type === Number) {
98 // Objects defined in mongoose schema as type:String
99 var el = {}
100 if (fields[field].widget && fields[field].widget == 'textarea'){
101 el = $('<textarea />')
102 }
103 else {
104 el = $('<input />').attr('type', 'text')
105 }
106 result.append(el.attr('id', fieldName).attr('name', fieldName).attr('value', fieldValue).addClass('document_input'));
107 } else if (fields[field].type === mongoose.Objectid) {
108 // Nested objects in mongoose schema
109 var childDiv = $('<div />').attr('id', fieldName).attr('name', fieldName).addClass('document_object');
110 for (childField in fields[field]) {
111 if (childField != '0') {
112 childDiv.append(Renderer.renderDocumentField(models, childField, fields[field], options, document, true, field));
113 }
114 }
115 childDiv.append($('<div />').addClass('clearfix'));
116 result.append(childDiv).append($('<div />').addClass('clearfix'));
117 } else if (fields[field].type.toString() == mongoose.Schema.ObjectId.toString()) {
118 // Objects defined in mongoose schema as type:mongoose.Schema.ObjectId
119 var modelSelect = $('<select />').addClass('linked_model').attr('id', fieldName + '_linked_model').attr('name', fieldName + '_linked_model').attr('rel', fieldName + '_linked_document');
120 modelSelect.append($('<option />').attr('value', '').html('Select type...'));
121 models.forEach(function(model) {
122 var option = $('<option />').attr('value', model.collection.name).html(model.modelName);
123 modelSelect.append(option);
124 });
125 var documentSelect = $('<select />').addClass('linked_document').attr('id', fieldName + '_linked_document').attr('name', fieldName + '_linked_document');
126 result.append(modelSelect);
127 result.append(documentSelect);
128 result.append($('<div />').attr('style', 'clear:both;'));
129 } else {
130 // Objects which we don't know what they are
131 result.append($('<span />').attr('id', fieldName).attr('name', fieldName).addClass('document_other').html(fieldValue.length === 0 ? '<em>Not set</em>' : fieldValue));
132 }
133
134 return result;
135};
136
137exports.Renderer = Renderer;