$(document).ready(function () { CEEPAppForm.OnInitForm(); }); var expenseitems = []; var CEEPAppForm = CEEPAppForm || {}; (function () { var ceepApplicationBudget = 0; var onInitForm = function () { //hide fields $("#mcm_fundingpackagerequestedamount").hide(); $("#mcm_singlepagedetaillines").hide(); $("#mcm_fundingpackagerequestedamount_label").hide(); $("#mcm_singlepagedetaillines_label").hide(); //set the package dropdown values setFundingPackageTypes(); //setExpenseCategoryLookupValues(); getCurrentCEEPApplicationBudget(); //build the expense items from existing values. setExpenseItemsFromExisting(); //DOB field - is positionally hard coded. var dobControl = $('div.control')[14]; $(dobControl).on("dp.change", function (e) { var age = getAge(e.date); if (age >= 0) { $("#mcm_applicantage").val(getAge(e.date)); } else { $("#mcm_applicantage").val(null); } }); //barriers //showHide_BarriersOther(); //var barriersControl = $('div.control')[23]; //$(barriersControl).on("change", function (e) { // showHide_BarriersOther(); //}); //start the form at the top - MS PCF multiselect controls move the focus of the entire form setTimeout(() => { window.focus(); document.activeElement.blur(); $('html,body').scrollTop(0); }, 1500); //custom grid init var gridDataNextID = expenseitems.length + 1; $(document).ready(function () { $("#grid").kendoGrid({ dataSource: { transport: { read: function (options) { options.success(expenseitems); }, update: function (options) { options.success(); }, create: function (options) { options.data.ID = gridDataNextID++; expenseitems.push(options.data); options.success(options.data); }, destroy: function (options) { options.success(); } }, schema: { model: { id: "ItemName", fields: { ExpenseType: { defaultValue: null, validation: { required: true } }, RequestedAmount: { type: "number", validation: { required: true } }, ItemName: { type: "string", validation: { required: true }, defaultValue: "Item Name" }, Details: { type: "string" } } } }, pageSize: 20, aggregate: [{ field: "RequestedAmount", aggregate: "sum" }], change: function () { // subscribe to the CHANGE event of the data source let data = JSON.stringify(this.data()); $("#mcm_singlepagedetaillines").val(data); //set the requested amount field let totalRequested = this.aggregates().RequestedAmount.sum; $("#mcm_fundingpackagerequestedamount").val(totalRequested); } }, toolbar: [{ name: "create", text: "Add new item"}], editable: { mode: "popup", template: $("#itemTemplate").html() }, edit: function (e) { initEditor(); }, height: 550, scrollable: true, sortable: true, //filterable: true, //pageable: { // input: true, // numeric: false //}, columns: [ { field: "ItemName", title: "Item Name" }, { field: "Details", title: "Details" }, { field: "RequestedAmount", title: "Requested Amount", format: "{0:c}", width: "130px", aggregates: ["sum"], footerTemplate: "Total: #=kendo.format('{0:c}', sum)#" }, { command: ["edit", "destroy"], title: " ", width: "250px" } ] }); }); }; var getAge = function (dateString) { var today = new Date(); var birthDate = new Date(dateString); var age = today.getFullYear() - birthDate.getFullYear(); var m = today.getMonth() - birthDate.getMonth(); if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) { age--; } return age; } var showHide_BarriersOther = function () { var barriersControl = $('div.control')[23]; var selectedItems = $("div").find(barriersControl).find('li[id^="mcm_applicantbarriers-selected-item"'); var selectedvalues = []; for (i = 0; i < selectedItems.length; i++) { selectedvalues.push(selectedItems[i].dataset.value); } if (selectedvalues.length > 0 && selectedvalues.some(x => x == '803970004')) //Other { console.log("found so showing"); //show the barriers other and make it required. $("#mcm_applicantbarriersother").show(); $("#mcm_applicantbarriersother_label").show(); } else { console.log("not found so hiding"); $("#mcm_applicantbarriersother").hide() $("#mcm_applicantbarriersother_label").hide(); } } var getCurrentCEEPApplicationBudget = function () { // request to our custom page with id as parameter $.getJSON("/ceepapplicationbudget-json", function (data) { if (data.results.length > 0) { //use the first result let budget = data.results[0]; ceepApplicationBudget = budget.CurrentBudgetAmount; //addbudget valiation addBudgetValidator("mcm_fundingpackagerequestedamount", ceepApplicationBudget, budget.CurrentBudgetAmountFormatted); //set budget amount $("#currentbudgetavailable").text("$" + budget.CurrentBudgetAmountFormatted); } } ); }; var setFundingPackageTypes = function () { //determine if the dropdown should be cleared let currentValue = $("#mcm_fundingpackagetypeid").val(); // request to our custom page with id as parameter $.getJSON("/ceepfundingpackagetypes-json", function (data) { if (data.results.length > 0) { $("#mcm_fundingpackagetypeid").empty(); //create option for each returned entity data.results.forEach(element => { let option = document.createElement("option"); option.value = element.Id; option.innerText = element.DisplayName; $("#mcm_fundingpackagetypeid").append(option); }); $("#mcm_fundingpackagetypeid").val(currentValue); } } ); }; var addBudgetValidator = function (fieldName, amount, formattedamount) { if (typeof (Page_Validators) == 'undefined') return; var newValidator = document.createElement('span'); newValidator.style.display = "none"; newValidator.id = "CustomRangeValidator" + fieldName; newValidator.controltovalidate = "casetypecode"; newValidator.errormessage = "The requested total must be within case plan budget of $" + formattedamount + ""; newValidator.validationGroup = ""; newValidator.initialvalue = ""; newValidator.evaluationfunction = function () { var value = $("#" + fieldName).val(); if (value == null || value == "") { return false; } else if (!isNaN(value) && Number(value) <= amount && Number(value) > 0) { return true; } else { return false; } }; // Add the new validator to the page validators array: Page_Validators.push(newValidator); // Wire-up the click event handler of the validation summary link $("a[href='#" + fieldName + "_label']").on("click", function () { scrollToAndFocus(fieldName + '_label', fieldName); }); }; function initEditor() { $("#ItemName").kendoTextBox({ }); $("#Details").kendoTextArea({ rows: 5, maxLength: 400, placeholder: "Enter your text here." }); $("#RequestedAmount").kendoNumericTextBox({ format: "{0:c}" }); $("#ExpenseType").kendoDropDownList({ dataTextField: "DisplayName", dataValueField: "Id", valuePrimitive: true, dataSource: { transport: { read: "/ceepsupportteamexpensecategories-json", }, schema: { data: "results" } }, change: onChangeExpenseItem, }).data("kendoDropDownList"); } function onChangeExpenseItem(e) { var dataItem = this.dataItem(); var container = $("#ExpenseType").closest(".k-popup-edit-form"); var uid = container.attr("data-uid"); //get the grid model item var grid = $("#grid").data("kendoGrid"); var model = grid.dataSource.getByUid(uid); model.set("RequestedAmount", dataItem.CurrentBudgetAmount); model.set("ItemName", dataItem.Name); } var setExpenseItemsFromExisting = function () { if($("#mcm_singlepagedetaillines").val() != "") { let jsonString = $("#mcm_singlepagedetaillines").val(); let itemArray = JSON.parse(jsonString); if (itemArray.length != undefined && itemArray.length > 0) { expenseitems = itemArray; } } }; /** * Public Members */ this.OnInitForm = onInitForm; }).apply(CEEPAppForm);