1 | var $;
|
2 | try
|
3 | {
|
4 | $ = require('jquery');
|
5 | }
|
6 | catch(e)
|
7 | {
|
8 | $ = function() { };
|
9 | }
|
10 | var mongoose = require('mongoose');
|
11 |
|
12 | function Renderer() {
|
13 |
|
14 | };
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | Renderer.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 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | Renderer.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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
137 | exports.Renderer = Renderer;
|