Last active
March 19, 2022 14:06
-
-
Save ChecksumFailed/d5694e43bfb132974e70e1087bd671ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var recordToHTML = (function() { | |
//class variables | |
var exclusions = { | |
"sections": [], | |
"fields": [] | |
}; | |
/******************************** | |
* Private Methods | |
*********************************/ | |
/** | |
* Gets the default form of a table | |
* @returns {string} | |
*/ | |
function _getDefaultView(tbl) { | |
var gr = new GlideRecord('sys_ui_form'); | |
gr.addQuery("name=" + tbl + "^view=Default view"); | |
gr.setLimit(1); | |
gr.query(); | |
return gr.next() ? gr.getValue('sys_id') : ''; | |
} | |
/** | |
* Retrieves all the sections in a form | |
* @param {sys_id} form | |
* @returns Object | |
*/ | |
function _getSections(form, excludes) { | |
var sectionID = []; | |
var sectionName = []; | |
var grSection = new GlideRecord('sys_ui_form_section'); | |
var exclusions = _addExclusionsToQry(grSection.getRecordClassName(), 'sys_ui_section.caption', excludes); | |
if (exclusions) { | |
grSection.addEncodedQuery(exclusions); | |
} | |
grSection.addQuery('sys_ui_form', form); | |
grSection.orderBy('position'); | |
grSection.query(); | |
while (grSection.next()) { | |
sectionID.push(grSection.getValue('sys_ui_section')); | |
sectionName.push(grSection.sys_ui_section.caption.toString()); | |
} | |
//return sections; | |
return { | |
"sysid": sectionID, | |
"names": sectionName | |
}; | |
} | |
/** | |
* Build encoded query of exclusions | |
* @param {GlideRecord} rec | |
* @param {string} field | |
* @param {array} arr | |
* @returns {string} | |
*/ | |
function _addExclusionsToQry(tbl, field, arr) { | |
if (!tbl || !field || !arr) { | |
return; | |
} | |
var grQry = new GlideRecord(tbl); | |
arr.forEach(function(val) { | |
grQry.addQuery(field, '!=', val).addOrCondition('sys_ui_section.caption', ''); | |
}); | |
return grQry.getEncodedQuery(); | |
} | |
/** | |
* Retrieves all fields in a form by section(s) | |
* @param {array} sections | |
* @returns {Array} | |
*/ | |
function _getFields(sections, excludes) { | |
var fields = []; | |
var grField = new GlideRecord('sys_ui_element'); | |
var exclusions = _addExclusionsToQry(grField.getRecordClassName(), 'element', excludes); | |
if (exclusions) { | |
grField.addEncodedQuery(exclusions); | |
} | |
grField.addQuery('sys_ui_section', 'IN', sections); | |
grField.addNullQuery('type'); | |
grField.orderBy('position'); | |
grField.query(); | |
while (grField.next()) { | |
fields.push({ | |
"field": grField.getValue('element'), | |
"sys_ui_section": grField.getValue('sys_ui_section') | |
}); | |
} | |
return fields; | |
} | |
/** | |
* Builds object of name:value pairs. Field Label and and field value | |
* @param {array} fields | |
* @param {GlideRecord} rec | |
* @returns Object | |
*/ | |
function _buildObject(fields, rec) { | |
var tmpObj = {}; | |
fields.forEach(function(val) { | |
tmpObj[rec[val].getLabel()] = rec[val].getDisplayValue(); | |
}); | |
return tmpObj; | |
} | |
/** | |
* Builds an HTML table based off a name:value pair object | |
* @param {object} obj | |
* @returns {string} | |
*/ | |
function _objToHTMLTable(obj) { | |
if (typeof obj === 'undefined') { | |
return ''; | |
} | |
try { | |
var tblCSS = ' border: 1px solid black;table-layout: auto;word-wrap: break-word;width: 95%;'; | |
var evenTRCSS = 'background-color: lightgray;'; | |
var tdCSS = ' border: 1px solid black;'; | |
var leftTDCSS = tdCSS + 'font-weight: bold;width:30%'; | |
var trCSS; | |
var tblString = '<table style="' + tblCSS + '"><tbody>'; | |
var rowClass; | |
var count = 0; | |
for (var key in obj) { | |
count++; | |
trCSS = count % 2 == 0 ? ' style="' + evenTRCSS + '"' : ''; | |
tblString += '<tr id="CM_recordToHTML"' + trCSS + '>'; | |
tblString += '<td style="' + leftTDCSS + '">' + key + "</td>"; | |
tblString += '<td style="' + tdCSS + '">' + obj[key].toString() + "</td>"; | |
tblString += "</tr>"; | |
} | |
tblString += "</tbody></table>"; | |
return tblString; | |
} catch (ex) { | |
return "Error: " + ex.message; | |
} | |
} | |
/** | |
* Builds HTMl body based on form sections/fields | |
* @param {array} sections | |
* @param {array} fields | |
* @param {GlideRecord} rec | |
* @returns {string} | |
*/ | |
function _buildBody(rec, sections, fields) { | |
//CSS for table rendering | |
/* var cssStyle = "table.ObjArray {\ | |
border: 1px solid black;\ | |
table-layout: auto;\ | |
word-wrap: break-word;\ | |
width: 95%;\ | |
}\ | |
\ | |
table.ObjArray th,\ | |
table.ObjArray td {\ | |
border: 1px solid black;\ | |
}\ | |
tr:nth-child(even) { \ | |
background-color: lightgray; \ | |
}\ | |
table.ObjArray td.left {font-weight: bold;width:30%;}"; | |
var tblString = '<style>' + cssStyle + ' </style>'; | |
*/ | |
//var tblString = "<h1>" + rec.getClassDisplayValue() + "</h1>"; | |
var tblString = '<div id="CM_recordToHTML">'; | |
var sectionIDs = sections.sysid; | |
var sectionNames = sections.names; | |
sectionIDs.forEach(function(val, el) { | |
tblString += "<h2>" + sectionNames[el] + "</h2>"; | |
var tmpFields = fields.filter(function(element) { //Filter to only the fields in the current section | |
return element.sys_ui_section == val; | |
}).map(function(element) { | |
return element.field; | |
}); | |
tblString += _objToHTMLTable(_buildObject(tmpFields, rec)); //Build Object and convert to an HTML table | |
}); | |
tblString += '</div>'; | |
return tblString; | |
} | |
/******************************** | |
* Public Methods | |
*********************************/ | |
/** | |
* Converts a GlideRecord to HTML. Each form section is a seperate table | |
* @param {GlideRrecord} rec | |
* @param {string} view | |
* @returns {string} | |
*/ | |
function convertRecordToHTML(rec, view) { | |
view = view || _getDefaultView(rec.getRecordClassName()); | |
var sections = _getSections(view, exclusions.sections); | |
var fields = _getFields(sections.sysid, exclusions.fields); | |
return _buildBody(rec, sections, fields); | |
} | |
/** | |
* Sets Sections to Exclude from | |
* @param strArray | |
*/ | |
function setSectionExcludes(strArray) { | |
if (!Array.isArray(strArray)) { | |
throw "setSectionExcludes requires an array as an argument"; | |
} | |
exclusions.sections = strArray; | |
return this; | |
} | |
/** | |
* Sets Fields to Exclude from | |
* @param strArray | |
*/ | |
function setFieldExcludes(strArray) { | |
if (!Array.isArray(strArray)) { | |
throw "setFieldExcludes requires an array as an argument"; | |
} | |
exclusions.fields = strArray; | |
return this; | |
} | |
return { //Public Methods to expose | |
"convertRecordToHTML": convertRecordToHTML, | |
"setSectionExcludes": setSectionExcludes, | |
"setFieldExcludes": setFieldExcludes, | |
"type": "recordToHTML" | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment