var DBG = DBG || false var DBG_FAKE_ANIM = DBG_FAKE_ANIM || false if (!URL_FETCH_BI_AUDIT_PROGRESS) throw "Missing URL_FETCH_BI_AUDIT_PROGRESS" if (!jQuery('#BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA').length) return; var IS_STARTED = false var DBG_COUNTER = 0; function getTableAjaxStruct() { var tblNode = jQuery('#BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA > table.AjaxTable') var fieldCells = tblNode.find('> thead > tr.tblAjax__head__sort > th > span') var idCells = tblNode.find('> tbody > tr > td:nth-child(3)') // if(DBG)console.log('updateRaportProgress ('+DBG_COUNTER+') idCells: ', idCells); if (!idCells.length || !fieldCells.length) { return null; } // FILE_STATUS -> 'IN_PROGRESS' // FILE_STATUS_info -> 'W trakcie generowania powiązań' var fields = fieldCells.toArray().map(function (el) { // if(DBG)console.log('updateRaportProgress ('+DBG_COUNTER+') fields loop ('+idx+'): el', el.title); if (!el.title) return ''; var title = el.title var fieldName = '' if (')' === title.substr(-1)) { var pos = title.lastIndexOf('(') if (-1 !== pos) { fieldName = title.substring(pos + 1, title.length - 1) } } return fieldName; }) if(DBG)console.log('getTableAjaxStruct ('+DBG_COUNTER+') fields: ', fields); var fieldStatusIdx = fields.indexOf('FILE_STATUS'); var fieldStatusInfoIdx = fields.indexOf('FILE_STATUS_info'); if(DBG)console.log('getTableAjaxStruct ('+DBG_COUNTER+') idx('+fieldStatusIdx+','+fieldStatusInfoIdx+')'); var ids = idCells.toArray().map(function (el) { return parseInt(jQuery(el).text()); }) if(DBG)console.log('getTableAjaxStruct ('+DBG_COUNTER+') ids: ', ids); var statusNodes = (fieldStatusIdx > 0) ? tblNode.find('> tbody > tr > td:nth-child(' + (3 + fieldStatusIdx) + ')').toArray() : [] var statusInfoNodes = (fieldStatusInfoIdx > 0) ? tblNode.find('> tbody > tr > td:nth-child(' + (3 + fieldStatusInfoIdx) + ')').toArray() : [] var statuses = statusNodes.map(function (el) { return jQuery(el).text() }) return { fields: fields, fieldStatusIdx: fieldStatusIdx, fieldStatusInfoIdx: fieldStatusInfoIdx, ids: ids, statusNodes: statusNodes, statusInfoNodes: statusInfoNodes, statuses: statuses, } } function updateTableAjaxView(respJson) { if (!respJson.body || !respJson.body.ids || !respJson.body.progress) return; var tblStruct = getTableAjaxStruct() if (!tblStruct) return; if(DBG)console.log('DBG:updateTableAjaxView', {respJson: respJson, tblStruct: tblStruct}); tblStruct.statusNodes.forEach(function (el, idx) { var id = tblStruct.ids[idx] var respId = respJson.body.ids[idx] if (id !== respId) return; // view updated before response var progress = respJson.body.progress[idx] var percent = Math.round(100 * progress) if (DBG_FAKE_ANIM && percent > 0 && percent < 100) percent += (DBG_COUNTER * 10) if(DBG)console.log('DBG:updateTableAjaxView loop ('+idx+', el)', {el: el, id: id, progress: progress, pr: percent }); var status = (respJson.body.statuses[idx]) ? respJson.body.statuses[idx] : null if (DBG_FAKE_ANIM) status = (percent < 100) ? 'IN_PROGRES' : 'DONE' updatePercentView(el, { percent: percent, status: status, }) var statusInfo = (respJson.body.statusesInfo[idx]) ? respJson.body.statusesInfo[idx] : null if (statusInfo && tblStruct.statusInfoNodes && tblStruct.statusInfoNodes[idx]) updateStatusInfoView(tblStruct.statusInfoNodes[idx], statusInfo) }) } function updateStatusInfoView(el, statusInfo) { if (statusInfo) jQuery(el).find('span').text(statusInfo) } function updatePercentView(el, props) { var hrNode = jQuery(el).find('hr') if (!hrNode.length) hrNode = jQuery('
').appendTo(el) if (props.percent > 100) props.percent = 100 hrNode.attr('style', "margin:0; padding:0; border-bottom:3px solid #f00; width:" + props.percent + "%") if (props.status) jQuery(el).find('span').text(props.status) } function updateRaportProgress() { DBG_COUNTER += 1 if(DBG)console.log('updateRaportProgress ('+DBG_COUNTER+')'); var tblStruct = getTableAjaxStruct() if(DBG)console.log('updateRaportProgress ('+DBG_COUNTER+') tblStruct', tblStruct); if (!tblStruct) { global.setTimeout(updateRaportProgress, 3000) return; } global.fetch(URL_FETCH_BI_AUDIT_PROGRESS, { method: 'POST', credentials: 'same-origin', body: JSON.stringify({ ids: tblStruct.ids, }) }).then(function (response) { return response.json() }).then(function (respJson) { updateTableAjaxView(respJson) IS_STARTED = false; startUpdateRaportProgress() }).catch(function (e) { if(DBG)console.warn('e:', e) IS_STARTED = false }) IS_STARTED = false } function startUpdateRaportProgress() { if (IS_STARTED) return; IS_STARTED = true if(DBG)console.log('START updateRaportProgress ...'); global.setTimeout(updateRaportProgress, 1000) } jQuery('#BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA').on('TableAjax:render', startUpdateRaportProgress)