/** * Module for the Sample section (right side, bottom section) */ let Sample = (function() { "use strict"; // Success messages const SAMPLE1 = "sample1"; // Error messages const SAMPLE2 = "SAMPLE2"; const SAMPLE3 = "SAMPLE3"; // Validation messages const SAMPLE4 = "SAMPLE4"; const SAMPLE5 = "SAMPLE5"; const sDateForm = "DDMMMYYYY", _editBtn = 0, _cancelBtn = 1, _tDesc = "Create Dupl", _inlineEditableClass = "inlineEditable", _noEdit = "noEdit"; let _tReq; let _tSection, _server, _sK, _tableId = "Sample-dt", _table, _dt, _tEditor, _submitEditor, _changedRows = [], _UI, _exampleApp, _abbbData, _genNumber, _genDate, _lT, _rDelay, _fR = "N", _tDate, _pSample, _Options = [], _tOptions = [], _ptOptions = [], _eMap = new Object(), _rtMap = new Object(), _ptMap = new Object(), _editMode = false, _user, _userType, /** * Clear the Sample section */ clearSample = function() { stopLoadingSamples(); if ((_dt != null) && (_dt != undefined)) { _dt.clear().draw(); _dt.buttons([ _editBtn, _cancelBtn ]).disable(); } _exampleApp.messages.clear("Sample-message-display"); _changedRows = []; _abbbData = ""; _genNumber = ""; _genDate = ""; _lT = null; _rDelay = ""; _fR = "N"; _tDate = ""; _pSample = ""; _Options = []; _tOptions = []; _ptOptions = []; _eMap = new Object(); _rtMap = new Object(); _ptMap = new Object(); }, /** * Cancel loading samples */ stopLoadingSamples = function() { _tReq.abort(); }, /** * Confirm Save Sample */ confirmSaveSamples = function() { _exampleApp.messages.clear("confirm-Sample-message-display"); _rDelay = $("#rDelay").val(); _fR = $('input[name=fR]:checked').val(); _tDate = $('#tDatepic').val(); let tDMt = moment(_tDate, sDateForm, true); let toDMt = moment().startOf("day"); let uTDate = tDMt.toDate(); let pRCycle = _pSample.rC; if ((pRCycle > 0) && (_fR == "N") && (uTDate.getDay() === 0 || uTDate.getDay() === 6 || P.cHDate(uTDate) || (!tDMt.isValid()) || (tDMt.diff(toDMt, "day") < 0))) { _exampleApp.messages.error("confirm-Sample-message-display", SAMPLE4); return false; } if (!_rDelay && $("#rDelay:visible").length) { _exampleApp.messages.error('confirm-Sample-message-display', SAMPLE5); return false; } else { clearSaveModel(); _submitEditor.edit(_changedRows, false) // batch submit changes, do no display modal popup .submit(); } }, /** * Cancel Save Sample */ cSS = function() { clearSaveModel(); enableEditButton(); enableCancelButton(); }, /** * Display Target Date */ dTDate = function() { let fR = $('input[name=fR]:checked').val(); if (fR == 'Y') { $("#nRTDateDiv").hide(); } else { $("#nRTDateDiv").show(); } }, /** * Cancel Save Samples */ clearSaveModel = function() { _exampleApp.messages.clear("confirm-Sample-message-display"); $("#sTModal").modal('hide'); $("#pRDDiv").modal('hide'); $("#pRFDDiv").hide(); $("#pFlRDiv").hide(); $("#nRTDateDiv").hide(); $("#pFRN").prop("checked", true); $("#rDelay").val(""); $("#tDatepic").val(""); }, /** * Generate DataTables configuration */ config = function(params) { let cCBC = _exampleApp.getButtonColor(); return { "dom" : "frtipB", "ajax" : { dataType : "json", url : _server + "/pdh/rwc/" + params, beforeSend : function(xhr) { xhr.setRequestHeader("X-SA", _sK); } }, "destroy" : true, "fixedHeader" : true, "select" : { style : "single" }, "paging" : false, "scrollY" : "30vh", "language" : { "emptyTable" : "No data available." }, "rowId" : "dataTableRowId", "columns" : [ { "data" : "rTN" }, { "data" : "pDCDDS" }, { "data" : "pDD" }, { "data" : "uN" } ], "buttons" : [ { // Action button to remove "text" : "Date", "className" : "action-btn " + cCBC, "enabled" : true, "action" : function() { _tEditor.edit(_dt.row(_tEditor.modifier().row), false).set('cDDS', '').submit(); $("#pRDDiv").modal('hide'); } }, { // custom Cancel button "text" : "Cancel", "className" : "action-btn " + cCBC, "enabled" : true, "action" : function() { $("#pRDDiv").modal('hide'); } } ] }; }, /** * Generate DataTables configuration */ gTTC = function(ePSN, rN, pTN) { let tU = _server + "/Sample/P/" + ePSN+"/"+rN, cCBC = _exampleApp.getButtonColor(); return { "dom" : "frtipB", "ajax" : { dataType : "json", url : tU, beforeSend : function(xhr) { xhr.setRequestHeader("X-SA", _sK); } }, "order" : [], "destroy" : true, "fixedHeader" : true, "paging" : false, "scrollY" : "30vh", "scrollX" : true, "select" : { style : "single" }, "searching" : false, "ordering" : false, "scrollCollapse" : true, "language" : { "emptyTable" : "No data available." }, "rowId" : "dataTableRowId", "columns" : [ { "data" : "pTN", "className" : _noEdit, "editField" : "pTN" }, { "data" : "pTD", "className" : _noEdit, "editField" : "pTD" }, { "data" : function(row, type, val, meta) { if (type === 'display') { return row.tDDS; } return row.tDDS; }, "editField" : "tDDS", "className" : _noEdit }, { "data" : function(row, type, val, meta) { if (type === 'display') { return row.cDDS; } return row.cDS; }, "className" : _inlineEditableClass, "editField" : "cDDS" }, { "data" : "aTUN", "className" : _inlineEditableClass, "editField" : "aTUN" }, { "data" : "notes", "className" : _inlineEditableClass, "editField" : "notes" } ], // enable tabbing only for inline editable columns "keys" : { "columns" : ".inlineEditable", "keys" : [ 9 ], // 9 = tab "editor" : _tEditor, "editOnFocus" : true }, "buttons" : [ { // custom Edit button "text" : "Edit", "name" : "edit-Sample", "enabled" : false, "className" : "edit-Sample action-btn " + cCBC, "action" : function(e, dt, node, config) { //Clear all the messages _exampleApp.messages.clear("Sample-message-display"); if (this.buttons(".edit-Sample").length) { setDataTableStyle(_dt, true); disableOtherSections(); _editMode = true; this.buttons(".edit-Sample").nodes().removeClass('edit-Sample').addClass('save-Sample'); this.buttons([ _editBtn ]).text("Save"); enableCancelButton(); } else { this.buttons([ _editBtn, _cancelBtn ]).disable(); saveSamples(e, dt, node, config); } } }, { // custom Cancel button "text" : "Cancel", "className" : "action-btn " + cCBC, "enabled" : false, "action" : function() { _editMode = false; let ePSN; _dt.rows().every(function(rowIdx, tableLoop, rowLoop) { let data = this.data(); ePSN = data.tId.ePSN; return false; }); // Clear out the saved rows and disable the cancel button _changedRows.length = 0; _dt.buttons([ _cancelBtn ]).disable(); reT(ePSN, rN); enableOtherSections(); } } ], "initComplete" : function(row, data) { if (_dt.row().length > 0) { if (!pTN) { // simply select the first row _dt.rows(0).select(); } else { _dt.row("#t" + pTN).select(); } enableEditButton(); } else { _dt.buttons([ _editBtn, _cancelBtn ]).disable(); } setDataTableStyle(_dt, false); } }; }, /** * Generate a local Editor configuration */ gEC = function(tableId) { return { table : '#' + _tableId, idSrc : 'dataTableRowId', fields : [ { label : "C D", name : "cDDS", type : 'datetime', def : function() { return new Date(); }, opts : { firstDay : 0 }, format : sDateForm }, { label : "TEST!", type : 'select', name : "aTUN", }, { label : "Samp!", name : "samp!" } ], formOptions : { inline : { onBlur : 'submit' } } }; }, /** * Method to load the table */ lRT = function(cC, pN, ePSN, rTN) { if ((cC) && (pN) && (ePSN)) { let params = cC + "/" + pN + "/" + ePSN; if (rTN) { params = params + "/" + rTN; } _abbbData = $("#r-d-d").DataTable(config(params)); } else { console.log("C c or p n for the p is b"); } }, /** * Generate a submit Editor configuration for a given table */ gSEC = function(config, tU) { return { ajax : function(method, url, edits, success, error) { let sT = new Object(), samples = [], cC, pN, pI; for ( let rowId in edits.data) { let Sample, editData = edits.data[rowId], rowData = _dt.row('#' + rowId).data(); // copy over user's edits for ( let editField in editData) { rowData[editField] = editData[editField]; } Sample = rowData; cC = rowData.cC; pN = rowData.pN; pI = rowData.tId.ePSN; samples.push(Sample); } sT.samples = samples; sT.lTa = _lT; sT.rFD = _rDelay; sT.fR = _fR; sT.tD = _tDate; sT.rTN = _genNumber; sT.rCD = _genDate; sT.pdtLastUpdateDate = _pSample.rD; console.log("sT: " + JSON.stringify(sT)); if (sT.samples.length > 0) { $.ajax({ method : "PUT", contentType : "application/json; charset=utf-8", dataType : "json", url : tU, data : JSON.stringify(sT), beforeSend : function(xhr) { xhr.setRequestHeader("X-SA", _sK); xhr.setRequestHeader("loginUserId", _UI); } }).done(function(data) { $.when(P.r(cC, pN, pI)).always(function() { enableOtherSections(); _exampleApp.messages.success("Sample-message-display", SAMPLE1); // Clear out any saved rows and disable the save / cancel buttons _changedRows.length = 0; _dt.buttons([ _editBtn ]).text("Edit"); _dt.buttons([ _editBtn ]).nodes().removeClass('save-Sample').addClass('edit-Sample'); }) }).fail(function(jqXHR, textStatus, errorThrown) { error(jqXHR, textStatus, errorThrown); // allow the table to continue working after processing the failure if (jqXHR.responseJSON) { _exampleApp.messages.error("Sample-message-display", jqXHR.responseJSON.messages, SAMPLE2); } else { _exampleApp.messages.error("Sample-message-display", SAMPLE2); } // enable the Edit / Cancel buttons enableEditButton(); enableCancelButton(); }); } else { error(); _exampleApp.messages.error("Sample-message-display", SAMPLE3); // enable the Edit / Cancel buttons enableEditButton(); enableCancelButton(); } } }; }, /** * samples section */ reT = function(ePSN, rN, pTN) { _editMode = false; clearSample(); clearSaveModel(); loadTa(ePSN, rN, pTN); //Load the Sample assignee lists lUO("rr", _eMap, _Options); lUO("ss", _rtMap, _tOptions); lUO("yy", _ptMap, _ptOptions); _pSample = P.findProduct(ePSN); }, /** * Load the Sample table */ loadTa = function(ePSN, rN, pTN) { let tU = _server + "/samples"; _tEditor = new $.fn.dataTable.Editor(gEC(_tableId)); _dt = _table.DataTable(gTTC(ePSN, rN, pTN)); _submitEditor = genSubmitEditor(_dt, tU); _tEditor.on('preSubmit', function(e, o, action) { let editor = this, cD = editor.field('cDDS'); }); // Keep track of user edits _tEditor.on('postEdit', function(e, json, data) { let testingT = _eMap[data.aTUN]; if (!testingT) { testingT = _rtMap[data.aTUN]; } if (!testingT) { testingT = _ptMap[data.aTUN]; } if (testingT) { data.testingT = testingT; } _changedRows.push('#' + data.dataTableRowId); // enable the Save / Cancel buttons }); _tEditor.on('preOpen', function(e, mode, action) { let row = _dt.row(_tEditor.modifier().row), rowData = row.data(), fieldName = e.cT.s.includeFields[0], sfun = rowData.sfun; _dt.row("#t" + rowData.pTN).select(); if (!_userType || !_editMode) { return false; } else if ((_userType != 'rr') && (_userType != sfun)) { return false; } let testingO = []; if (rowData.sfun == 'rr') { $.extend(testingO, _Options); } else if (rowData.sfun == 'ss') { $.extend(testingO, _tOptions); } else if (rowData.sfun == 'yy') { $.extend(testingO, _ptOptions); } if (testingO.indexOf(rowData.aTUN) < 0) { testingO.push(rowData.aTUN); } _tEditor.field('aTUN').update(testingO); if ((rowData.pTD == _tDesc) && (fieldName === 'cDDS')) { if (_pSample.id.rN > 0) { lRT(_pSample.cC, _pSample.pN, _pSample.id.pI, _pSample.rTN); } else { lRT(_pSample.cC, _pSample.pN, _pSample.id.pI); } $("#pRDDiv").modal('show'); return false; } else { $("#pRDDiv").modal('hide'); } }); }, /** * Generate submit editor */ genSubmitEditor = function(config, sU) { leteO = gEC(config.tableId), sEC = gSEC(config, sU); return new $.fn.dataTable.Editor($.extend(true, sEC,eO)); }, enableEditButton = function(config) { if ((_userType == "rr") || (_userType == "ss") || (_userType == "yy")) { _dt.buttons([ _editBtn ]).enable(); } else { _dt.buttons([ _editBtn ]).disable(); } }, enableCancelButton = function(config) { if ((_userType == "rr") || (_userType == "ss") || (_userType == "yy")) { _dt.buttons([ _cancelBtn ]).enable(); } else { _dt.buttons([ _cancelBtn ]).disable(); } }, setDataTableStyle = function(datatable, editable) { if (editable) { $(datatable.column(3).nodes()).removeClass(_noEdit).addClass(_inlineEditableClass); $(datatable.column(4).nodes()).removeClass(_noEdit).addClass(_inlineEditableClass); $(datatable.column(5).nodes()).removeClass(_noEdit).addClass(_inlineEditableClass); } else { $(datatable.rows().nodes()).children().removeClass(_inlineEditableClass).addClass(_noEdit); } }, disableOtherSections = function() { $('#hO').addClass("hO"); $('#s-t-h').addClass("disabled-s-t-h-div"); $('#m-m-l').addClass("disabled-div"); $('.a-e-p').addClass("disabled-div"); $('.P-section').addClass("disabled-div"); }, enableOtherSections = function() { $('#hO').removeClass("hO"); $('#s-t-h').removeClass("disabled-s-t-h-div"); $('#m-m-l').removeClass("disabled-div"); $('.a-e-p').removeClass("disabled-div"); $('.P-section').removeClass("disabled-div"); }, /** * This method is invoked on click of save from edit Sample */ saveSamples = function(e, dt, node, config) { //If the user clicks save without tabbing out, then submit the editor. _tEditor.submit(); //Push the last updated row into the _changedRows if it is no already in there if (_tEditor.modifier()) { let lastUpdatedRow = _dt.row(_tEditor.modifier().row), lastUpdatedRowData = lastUpdatedRow.data(); let dataTableRowId = '#t' + lastUpdatedRowData.pTN; if (_changedRows.indexOf(dataTableRowId) == -1) { _changedRows.push(dataTableRowId); } } clearSaveModel(); let cDs = [], lTN, rowData, ePSN; _dt.rows().every(function(rowIdx, tableLoop, rowLoop) { let data = this.data(); cDs[data.pTN] = data.cDDS; ePSN = data.tId.ePSN; if (!lTN) { lTN = data.pTN; } else if (lTN < data.pTN) { lTN = data.pTN; } }); if (!vCD(cDs)) { enableEditButton(); enableCancelButton(); return false; } //Load the Sample and add Data if Sample and has been modified let rowId; let sSTM = false; for ( let i in _changedRows) { rowId = _changedRows[i]; rowData = _dt.row(rowId).data(); if (rowData.pTN == lTN) { _lT = rowData; } } //Processing the logic when set in the last Sample if (_lT) { if (_lT.cDDS) { var cDM = moment(_lT.cDDS, sDateForm, true); var tDMt = moment(_lT.tDDS, sDateForm, true); let pRCycle = _pSample.rC; let pTDDStr = _pSample.tDDS; let nRDTDa = P.cTD(_lT.date, pRCycle); if (cDM.diff(tDMt, "day") > 0) { $("#pRFDDiv").show(); sSTM = true; } else { $("#pRFDDiv").hide(); } if (pRCycle > 0) { $("#" + "pLADDTS").html($("#" + "l-t-d").val()); $("#tDatepic").val(nRDTDa); $("#pFlRDiv").show(); $("#nRTDateDiv").show(); sSTM = true; } else { $("#pFlRDiv").hide(); $("#nRTDateDiv").hide(); } } } if (sSTM) { P.lHABTD(); $('#tDatepic').datepicker({ beforeShowDay : P.hD, showOtherMonths : false, numberOfMonths : 1, dateFormat : "ddMyy", }); $("#sTModal").modal('show'); return false; } _submitEditor.edit(_changedRows, false) // batch submit changes, do no display modal popup .submit(); // Clear out the saved rows and disable the save / cancel buttons _changedRows.length = 0; }, /* * Validates whether are in chronological order. */ vCD = function(cDs) { let iD = [], fD = [], nSD = [],nCD = [], cDVM = ""; for (var i = 0, len = cDs.length; i < len; i++) { let pV, cV = cDs[i]; var cDM = moment(cV, sDateForm, true); var toDMt = moment().startOf("day"); if (!cDM.isValid()) { if (cV) { iD.push(cV); } } else if (cDM.diff(toDMt, "day") > 0) { fD.push(cV); } else if (i > 1) { pV = cDs[i - 1]; if ((!pV) && (cV)) { nSD.push(cV); } else if ((pV) && (cV) && (moment(pV, sDateForm) > moment(cV, sDateForm))) { nCD.push(cV); } } } if (iD.length > 0) { cDVM = "Samp1:" + iD; } if (fD.length > 0) { if (cDVM) { cDVM = cDVM + "\n"; } cDVM = cDVM + "Samp2:" + fD; } if (nSD.length > 0) { if (cDVM) { cDVM = cDVM + "\n"; } cDVM = cDVM + "Samp3:" + nSD; } if (noCD.length > 0) { if (cDVM) { cDVM = cDVM + "\n"; } cDVM = cDVM + "Samp4:" +nCD } if (cDVM) { _exampleApp.messages.error("Sample-message-display", cDVM); return false; } else { return true; } }, getTableId = function() { return _tableId; }, getDataTable = function() { return _dt; }, sRD = function(dtConfig) { let tr = $(this).closest('tr'); let row = dtConfig.row(tr).data(); if (row) { _genNumber = row.rTN; _genDate = row.pDCD; let cD = moment(_genDate).format(sDateForm); $("#pRDDiv").modal('hide'); _tEditor.edit(_dt.row(_tEditor.modifier().row), false).set('cDDS', cD).submit(); } }, /** * Load the user */ lUO = function(sfun, uM, uL) { return $.ajax({ dataType : "json", url : _server + "/accau/sfun/" + sfun, beforeSend : function(xhr) { xhr.setRequestHeader("X-SA", _sK); } }).done(function(accau, textStatus, jqXHR) { $.each(accau, function(i, aA) { let user = new Object() if (uL.indexOf(aA.uN) < 0) { uM[aA.uN] = aA.uId uL.push(aA.uN); } }); }).fail(function(jqXHR, textStatus, errorThrown) { console.log("Unable to load user lists"); }).always(function(data, textStatus, jqXHR) { }); }, init = function(exampleApp) { _tSection = $("#ms-Sample-list"); _server = exampleApp.restApi(); _sK = exampleApp.aK(); _table = $("#" + _tableId); _UI = exampleApp.user().uId; _exampleApp = exampleApp; _user = exampleApp.user(); if (_user.iE) { _userType = "rr"; } else if (_user.iT) { _userType = "ss"; } else if (_user.iST) { _userType = "yy"; } $('input[name=fR]').click(dTDate); $("#sTMO").click(cST); $("#sTMC").click(cSS); $('#r-d-d tbody').on('click', 'tr', function() { sRD.call(this, _abbbData); }); // setup to capture requests related to the Sample table so they can be cancelled when necessary _tReq = ms.requests(["/Sample/P/", "/accau/sfun/"]).capture(); } return { init : init, r : reT, clear : clearSample, getTableId : getTableId, getDataTable : getDataTable }; })(); $(document).ready(function() { "use strict"; let tableT = $("#" + Sample.getTableId()); let tableBody = $("#" + Sample.getTableId() + " tbody"); // setup empty table tableT.DataTable({ "dom" : "tr", "destroy" : true, "fixedHeader" : true, "searching" : false, "ordering" : false, "language" : { "emptyTable" : "No data available." } }); });