UNPKG

gd-sprest-js

Version:

SharePoint 2013/Online js components.

545 lines (544 loc) 23.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var gd_sprest_1 = require("gd-sprest"); var __1 = require(".."); /** * Field */ exports.Field = function (props) { // Method to generate the choice dropdown options var getChoiceOptions = function (fieldinfo, selectedValues) { var options = []; // Get the current value var values = selectedValues || null; values = values && values.results ? values.results : [values]; // Parse the options for (var i = 0; i < fieldinfo.choices.length; i++) { var choice = fieldinfo.choices[i]; var isSelected = false; // Determine if this choice is selected for (var j = 0; j < values.length; j++) { // See if this choice is selected if (choice == values[j]) { // Set the flag and break from the loop isSelected = true; break; } } // Add the option options.push({ isSelected: isSelected, text: choice, value: choice }); } // Return the options return options; }; // Method to generate the lookup dropdown options var getLookupOptions = function (fieldinfo, selectedValues) { var options = []; // Get the current value var values = props.value || null; values = values && values.results ? values.results : [values]; // Parse the selected values for (var i = 0; i < selectedValues.length; i++) { var item = selectedValues[i]; var isSelected = false; // See if this is a multi-lookup field if (fieldinfo.multi) { // Determine if this lookup is selected for (var j = 0; j < values.length; j++) { var id = values[j] ? values[j].Id : null; // See if this choice is selected if (item.Id == id) { // Set the flag and break from the loop isSelected = true; break; } } } // Add the option options.push({ isSelected: isSelected, text: item[fieldinfo.lookupField], value: item.Id.toString() }); } // Return the options return options; }; // Method to get the lookup values var getLookupValues = function (fieldinfo, value) { var values = []; // See if this is a multi-lookup if (fieldinfo.multi) { var results = value && value.results ? value.results : []; // Parse the values for (var i = 0; i < results.length; i++) { // Add the value values.push(results[i].Id ? results[i].Id : results[i]); } } else { // Set the value values.push(value && value.Id > 0 ? value.Id : value); } // Return the values return values; }; // Method to get the mms dropdown options var getMMSOptions = function (term, selectedValues) { if (selectedValues === void 0) { selectedValues = []; } var options = []; // See if information exists if (term.info) { var isSelected = false; // Parse the selected values for (var i = 0; i < selectedValues.length; i++) { // See if this item is selected if (selectedValues[i] == term.info.id) { isSelected = true; break; } } // Add the heading options.push({ isSelected: isSelected, text: term.info.name, value: term.info.id }); } // Parse the terms for (var termName in term) { var child = term[termName]; var isSelected = false; // Skip the info and parent properties if (termName == "info" || termName == "parent") { continue; } // Get the child options var childOptions = getMMSOptions(child, selectedValues); // Parse the selected values for (var i = 0; i < selectedValues.length; i++) { // See if this item is selected if (selectedValues[i] == child.info.id) { isSelected = true; break; } } // Add the option options.push({ isSelected: isSelected, options: childOptions.length > 1 ? childOptions : null, text: child.info.name, value: child.info.id }); } // Return the options return options; }; // Method to get the MMS values var getMMSValues = function (value) { var values = []; // Parse the MMS values var mmsValues = (value || "").split(";#"); for (var i = 0; i < values.length; i++) { var mmsValue = mmsValues[i].split("|"); if (mmsValue[1]) { // Add the value values.push(mmsValue[1]); } } // Return the values return values; }; // Method to get the user values var getUserValues = function (value) { var users = []; // See if a value exists if (value) { var userValues = value.results ? value.results : [value]; for (var i = 0; i < userValues.length; i++) { var userValue = userValues[i]; // Add the user users.push({ DisplayText: userValue.Title, EntityData: { Email: userValue.EMail, SPUserID: userValue.Id.toString() }, Key: userValue.Id.toString() }); } } // Return the users return users; }; // Method to update the value var _value = props.value; var updateValue = function (value) { // Update the value _value = value; // Call the change event props.onChange ? props.onChange(value) : null; }; // Render a loading message var _spinner = __1.Fabric.Spinner({ el: props.el, text: "Loading the field..." }); // Return a promise return new Promise(function (resolve, reject) { // See if we are displaying the field if (props.controlMode == gd_sprest_1.SPTypes.ControlMode.Display) { // Update the value, based on the type var value = props.value || ""; switch (props.fieldInfo.field.FieldTypeKind) { // Lookup case gd_sprest_1.SPTypes.FieldType.Lookup: var results = value.results ? value.results : [value]; var values = []; // Parse the results for (var i = 0; i < results.length; i++) { var result = results[i] ? results[i][props.fieldInfo.field.LookupField] : null; // Ensure the value exists if (result) { // Add the value values.push(result); } } // Update the value value = values.join(", "); break; // Multi-Choice case gd_sprest_1.SPTypes.FieldType.MultiChoice: // Update the values value = value.results ? value.results.join(", ") : value; break; // URL case gd_sprest_1.SPTypes.FieldType.URL: // Resolve the promise resolve({ fieldInfo: props.fieldInfo, element: __1.Fabric.LinkField({ className: props.className, description: props.fieldInfo.field.Description, disable: true, el: props.el, label: props.fieldInfo.field.Title, required: props.fieldInfo.required, value: value }) }); return; // User case gd_sprest_1.SPTypes.FieldType.User: var userNames = []; var userValues = value && value.results ? value.results : [value]; // Parse the user values for (var i = 0; i < userValues.length; i++) { // Ensure the name exists if (userValues[i] && userValues[i].Title) { // Add the user name userNames.push(userValues[i].Title); } } // Update the value value = userNames.join(", "); return; } // See if this is a taxonomy field if (props.fieldInfo.field.TypeAsString.startsWith("TaxonomyFieldType")) { var mmsValues = (value || "").split(";#"); value = []; // Parse the values for (var i = 0; i < mmsValues.length; i++) { // Add the term label value.push(mmsValues[i].split("|")[0]); } // Update the value value = value.join(", "); } // Resolve the promise resolve({ fieldInfo: props.fieldInfo, element: __1.Fabric.TextField({ className: props.className, description: props.fieldInfo.field.Description, disable: true, el: props.el, label: props.fieldInfo.field.Title, required: props.fieldInfo.required, type: __1.Fabric.TextFieldTypes.Underline, value: value }) }); // Return return; } // Load the field information gd_sprest_1.Helper.ListFormField.create(props.fieldInfo).then(function (fieldInfo) { // Set the value var value = props.controlMode == gd_sprest_1.SPTypes.ControlMode.New ? props.fieldInfo.defaultValue : props.value; // Render the field based on the type switch (fieldInfo.type) { // Boolean Field case gd_sprest_1.SPTypes.FieldType.Boolean: resolve({ fieldInfo: fieldInfo, element: __1.Fabric.Toggle({ className: props.className, description: fieldInfo.field.Description, disable: props.disabled, el: props.el, label: fieldInfo.title, onChange: updateValue, value: value }) }); break; // Calculated Field case gd_sprest_1.SPTypes.FieldType.Calculated: resolve({ fieldInfo: fieldInfo, element: __1.Fabric.TextField({ className: props.className, description: fieldInfo.field.Description, disable: true, el: props.el, label: fieldInfo.title, onChange: updateValue, required: fieldInfo.required, type: __1.Fabric.TextFieldTypes.Underline, value: value }) }); break; // Choice Field case gd_sprest_1.SPTypes.FieldType.Choice: resolve({ fieldInfo: fieldInfo, element: __1.Fabric.Dropdown({ className: props.className, description: fieldInfo.field.Description, disable: props.disabled, el: props.el, label: fieldInfo.title, onChange: updateValue, options: getChoiceOptions(fieldInfo, value), required: fieldInfo.required, value: value }) }); break; // Date/Time case gd_sprest_1.SPTypes.FieldType.DateTime: resolve({ fieldInfo: fieldInfo, element: __1.Fabric.DatePicker({ className: props.className, description: fieldInfo.field.Description, disable: props.disabled, el: props.el, label: fieldInfo.title, onChange: updateValue, required: fieldInfo.required, showTime: fieldInfo.showTime, value: value }) }); break; // Lookup Field case gd_sprest_1.SPTypes.FieldType.Lookup: // Get the drop down information gd_sprest_1.Helper.ListFormField.loadLookupData(fieldInfo, 500).then(function (items) { resolve({ fieldInfo: fieldInfo, element: __1.Fabric.Dropdown({ className: props.className, description: fieldInfo.field.Description, disable: props.disabled, el: props.el, label: fieldInfo.title, multi: fieldInfo.multi, onChange: updateValue, options: getLookupOptions(fieldInfo, items), required: fieldInfo.required, value: getLookupValues(fieldInfo, value) }) }); }); break; // Multi-Choice Field case gd_sprest_1.SPTypes.FieldType.MultiChoice: resolve({ fieldInfo: fieldInfo, element: __1.Fabric.Dropdown({ className: props.className, description: fieldInfo.field.Description, disable: props.disabled, el: props.el, label: fieldInfo.title, multi: true, onChange: updateValue, options: getChoiceOptions(fieldInfo, value), required: fieldInfo.required, value: value ? value.results : value }) }); break; // Note Field case gd_sprest_1.SPTypes.FieldType.Note: resolve({ fieldInfo: fieldInfo, element: __1.Fabric.TextField({ className: props.className, description: fieldInfo.field.Description, disable: props.disabled, el: props.el, label: fieldInfo.title, onChange: updateValue, required: fieldInfo.required, type: __1.Fabric.TextFieldTypes.Multi, value: value }) }); break; // Number or Currency Field case gd_sprest_1.SPTypes.FieldType.Number: case gd_sprest_1.SPTypes.FieldType.Currency: var numberInfo = fieldInfo; resolve({ fieldInfo: numberInfo, element: __1.Fabric.NumberField({ className: props.className, decimals: numberInfo.decimals, description: numberInfo.field.Description, disable: props.disabled, el: props.el, label: numberInfo.title, maxValue: numberInfo.maxValue, minValue: numberInfo.minValue, onChange: updateValue, required: numberInfo.required, type: numberInfo.showAsPercentage ? __1.Fabric.NumberFieldTypes.Percentage : (numberInfo.decimals == 0 ? __1.Fabric.NumberFieldTypes.Integer : __1.Fabric.NumberFieldTypes.Number), value: value }) }); break; // Text Field case gd_sprest_1.SPTypes.FieldType.Text: resolve({ fieldInfo: fieldInfo, element: __1.Fabric.TextField({ className: props.className, description: fieldInfo.field.Description, disable: props.disabled, el: props.el, label: fieldInfo.title, onChange: updateValue, required: fieldInfo.required, type: __1.Fabric.TextFieldTypes.Underline, value: value }) }); break; // Url Field case gd_sprest_1.SPTypes.FieldType.URL: resolve({ fieldInfo: fieldInfo, element: __1.Fabric.LinkField({ className: props.className, description: fieldInfo.field.Description, disable: props.disabled, el: props.el, label: fieldInfo.title, onChange: updateValue, required: fieldInfo.required, value: value }) }); break; // User Field case gd_sprest_1.SPTypes.FieldType.User: var userInfo = fieldInfo; resolve({ fieldInfo: userInfo, element: __1.Fabric.PeoplePicker({ allowGroups: userInfo.allowGroups, allowMultiple: userInfo.multi, description: userInfo.field.Description, el: props.el, label: userInfo.title, required: userInfo.required, searchLocalFl: true, value: getUserValues(value) }) }); break; // Default default: // See if this is a taxonomy field if (fieldInfo.typeAsString.startsWith("TaxonomyFieldType")) { var mmsInfo_1 = fieldInfo; // See if this is a new form if (props.controlMode == gd_sprest_1.SPTypes.ControlMode.New) { // Clear the value value = []; // Get the default values var values = (mmsInfo_1.defaultValue || props.value || "").split(";#"); for (var i = 0; i < values.length; i++) { var value_1 = values[i].split("|"); if (value_1.length == 2) { // Add the term id value_1.push(value_1[1]); } } } else { // Parse the values var values = value && value.results ? value.results : [value]; value = []; for (var i = 0; i < values.length; i++) { // Ensure the value exists if (values[i] && values[i].TermGuid) { // Add the value value.push(values[i].TermGuid); } } } // Load the terms gd_sprest_1.Helper.ListFormField.loadMMSData(mmsInfo_1).then(function (terms) { // Load the value field gd_sprest_1.Helper.ListFormField.loadMMSValueField(mmsInfo_1).then(function (valueField) { // Set the value field mmsInfo_1.valueField = valueField; // Resolve the promise resolve({ fieldInfo: mmsInfo_1, element: __1.Fabric.Dropdown({ className: props.className, description: mmsInfo_1.field.Description, disable: props.disabled, el: props.el, label: mmsInfo_1.title, multi: mmsInfo_1.multi, onChange: updateValue, options: getMMSOptions(gd_sprest_1.Helper.Taxonomy.toObject(terms), value), required: mmsInfo_1.required, value: value }) }); }); }); } else { // Log console.log("[gd-sprest] The field type '" + fieldInfo.typeAsString + "' is not supported."); } break; } }); }); };