-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.preview.js
225 lines (178 loc) · 6.43 KB
/
jquery.preview.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
(function($) {
$.fn.formPreview = function(settings) {
var config = { "buttonText" : "Preview" };
if (settings) $.extend(config, settings);
return this.each(function() {
// element-specific code here
$(this).find(":submit").before("<input type='button' class='preview_button' value='" + config["buttonText"] + "' onclick='$.fn.formPreview.generatePreview(this.form)' />");
});
return this;
};
$.fn.formPreview.generatePreview = function(form) {
var formId = form.id;
var formToPreview = $("#" + formId);
//declare variables as required
var previewHtml = "";
var firstRun = true;
var fieldLabel = "";
var fieldId = "";
var fieldName = "";
var fieldFieldset = "abc-random-string-abc";
var fieldLegend = "xyz-random-string-xyz";
var currentFieldLabel = "";
var currentFieldName = "";
var currentFieldset = "";
var currentFieldLegend = "";
var fieldValue = "";
var fieldValueLength = "";
var strSeparator = "";
var submitButtonValue = $("#" + formId + " :submit:last").val();
//surrounding div
previewHtml = "<div class='preview_div' id='preview_div_" + formId + "'>";
//output form preview heading
if ($("#" + formId + "[title]").text().length > 0) {
previewHtml += "<h2 id='preview_heading'>" + formToPreview.attr("title") + " preview</h2>";
} else {
previewHtml += "<h2 id='preview_heading'>Form preview</h2>";
}
//loop through all input fields to be checked
$("#" + formId + " :input").not(":button, :reset, :submit, .nopreview").each(function(i, field){
currentFieldName = fieldName;
fieldId = field.id;
fieldName = field.name;
if (currentFieldName != fieldName) {
//use class="for_fieldname", then actual label tag using id & for attributes, then display N/A
if ($("#for_" + fieldName).html()) {
fieldLabel = $("#for_" + fieldName).html();
}
else if ($("label[for=" + fieldId + "]").text()) {
fieldLabel = $("label[for=" + fieldId + "]").html();
}
else {
fieldLabel = "N/A"
}
//make it easier to compare
if (fieldLegend == null) {
fieldLegend = "";
}
if (fieldFieldset == null) {
fieldFieldset = "";
}
//set values for fieldset, legend and label so that comparisons can be made
currentFieldset = fieldFieldset;
currentFieldLegend = fieldLegend;
currentFieldLabel = fieldLabel;
//find the legend tag if possible
fieldFieldset = $("#" + fieldId).closest("fieldset");
fieldLegend = fieldFieldset.find("legend").html();
fieldFieldset = fieldFieldset.html();
//make it easier to compare
if (fieldLegend == null) {
fieldLegend = "";
}
if (fieldFieldset == null) {
fieldFieldset = "";
}
//if change fieldsets then close table and re-open another, display legend as h3 tag if possible
if (currentFieldset != fieldFieldset) {
//no table to close on first run
if (!firstRun) {
previewHtml += "</table>";
}
//only display heading between tables/fieldsets if found
if (fieldLegend.length > 0) {
previewHtml += "<h3>" + fieldLegend + "</h3>";
}
//start new table of form results
previewHtml += "<table class='preview_table'>";
previewHtml += "<tr>";
previewHtml += "<th class='preview_th_field'>Field</th>";
previewHtml += "<th class='preview_th_value'>Value</th>";
previewHtml += "<th class='preview_th_edit'></th>";
previewHtml += "</tr>";
}
previewHtml += "<tr>";
previewHtml += "<td class='preview_td_field'>" + fieldLabel + "</td>";
//reset values
fieldValue = "";
fieldValueLength = "";
strSeparator = "";
// radio buttons and checkboxes
if ($("#" + fieldId).is("input:radio, input:checkbox"))
{
$("input[name='" + fieldName + "']:checked").each(function(){
if ($(this).attr("title"))
{
fieldValue += strSeparator + $(this).attr("title");
} else {
fieldValue += strSeparator + $("label[for=" + $(this).attr("id") + "]").text();
}
strSeparator = "<br />";
});
}
// selectboxes
else if ($("#" + fieldId).is("select"))
{
$("select[name='" + fieldName + "'] option:selected").each(function(){
fieldValue += strSeparator + $(this).text();
strSeparator = "<br />";
});
}
// password fields
else if ($("#" + fieldId).is("input:password"))
{
fieldValueLength = $("#" + fieldId).val().length;
var i=0;
for (i=0; i<fieldValueLength; i++)
{
fieldValue += "*";
}
}
else
{
fieldValue = field.value;
}
previewHtml += "<td class='preview_td_value'>" + fieldValue + "</td>";
previewHtml += "<td class='preview_td_edit'><a href='' onclick='$.fn.formPreview.editField(\"" + formId + "\",\"" + fieldId + "\"); return false;'>Edit</a></td>";
previewHtml += "</tr>";
}
firstRun = false;
});
previewHtml += "</table>";
//buttons as required
previewHtml += "<input type='button' id='preview_button_cancel' class='preview_button' value='Cancel' onclick='$.fn.formPreview.cancelPreview(\"" + formId + "\");' />";
previewHtml += "<input type='submit' id='preview_button_submit' class='preview_button' value='" + submitButtonValue + "' onclick='document.getElementById(\"" + formId + "\").submit();' />";
previewHtml += "</div>";
//hide the form
$("#" + formId).hide();
//output the preview - create a new div or re-use if available (already previewed and cancelled then previewed again)
if ($("#" + formId + "_preview").length > 0) {
$("#" + formId + "_preview").html(previewHtml);
$("#" + formId + "_preview").show();
} else {
formToPreview.after("<div id='" + formId + "_preview'>" + previewHtml + "</div>");
}
var previewOffset = $("#" + formId + "_preview").offset();
//scroll to top of page when click preview button
self.scrollTo(previewOffset.left, previewOffset.top);
};
$.fn.formPreview.cancelPreview = function(formId) {
$("#" + formId + "_preview").hide();
$("#" + formId).show();
var formOffset = $("#" + formId).offset();
//scroll to top of page when click preview button
self.scrollTo(formOffset.left, formOffset.top);
};
$.fn.formPreview.editField = function(formId, fieldId) {
$("#" + formId).show();
$("#" + formId + "_preview").hide();
var formOffset = $("#" + formId).offset();
//scroll to top of page when click preview button
self.scrollTo(formOffset.left, formOffset.top);
try {
$("#" + fieldId).focus();
}
catch(e) {
}
};
})(jQuery);