(function($) { $('.AddRowBtn').on('click', function(e) { Decisiontable.addRow(); }); $('.AddDecisionBtn').on('click', function(e) { Decisiontable.addDecision(); }); $('.AddSetterBtn').on('click', function(e) { Decisiontable.addSetter(); }); window.Decisiontable = { _ColType: { 'decision': [], 'setter':[] }, _Columns:{}, _ColCounter:0, _Data:[], _DelayDraw:false, addEvents:function() { $('.RemoveRow').on('click', function(e) { var target = $(e.currentTarget); var row = target.closest('.RowAction'); var rowid = row.data('rowid'); Decisiontable._Data[rowid] = undefined; row.closest('tr').remove(); }); $('.form-control').on('focus', function(e) { $(e.currentTarget).closest('tr').addClass('Focused'); }); $('.form-control').on('blur', function(e) { $(e.currentTarget).closest('tr').removeClass('Focused'); }); $('.RemoveColumn').on('click', function(e) { var target = $(e.currentTarget); var col = target.closest('.HeadAction'); var rowid = col.data('columnid'); var type = col.data('type'); Decisiontable._Columns[rowid] = undefined; $.each(Decisiontable._ColType[type], function(index, ele) { if(ele == rowid) { Decisiontable._ColType[type][index] = undefined; return false; } }); if($('#th_' + type).attr('colspan') == '1') { if(type == 'setter') { Decisiontable.addSetter(); } else { Decisiontable.addDecision(); } } $('#th_' + type).attr('colspan', $('#th_' + type).attr('colspan') - 1); var totalColNumber = Number($('#th_decision').attr('colspan')) + Number($('#th_setter').attr('colspan')) + 1; $('#th_decision').css('width', ((100 / totalColNumber) * (Number($('#th_decision').attr('colspan')) + 1)) + '%'); $('#th_setter').css('width', ((100 / totalColNumber) * $('#th_setter').attr('colspan')) + '%'); Decisiontable.redrawTable(); }); $('.SetterTypeSelect').on('change', function(e) { var target = $(e.currentTarget); var type = target.val(); var parent = target.parent(); var currentValue = $('select.SetterOptionValue, input.SetterOptionValue', parent).val(); var currentName = $('select.SetterOptionValue, input.SetterOptionValue', parent).attr('name'); var html = Decisiontable.getSetterInput(type, currentName, currentValue); $('.SetterOptionContainer', parent).html(html); }); }, getSetterInput:function(type, inputName, currentValue) { if(type == '' || typeof type == 'undefined') type = 'envvar'; switch(type) { case 'envvar': var html = ''; break; case 'field': var html = ''; break; } return html; }, delayRedraw:function(value) { Decisiontable._DelayDraw = value; }, redrawTable:function() { if(Decisiontable._DelayDraw === true) { return; } var structureHTML = ''; $('.DecisionTableStructure').remove(); var headActions = ''; var head = ''; var count = {'decision':0,'setter':0}; var ColCounter = 0; $.each(Decisiontable._ColType.decision, function(index, data) { if(typeof data == 'undefined') return; count.decision++; structureHTML += ''; headActions += ''; head += ''; head += '
' + Decisiontable._Columns[data].value + '
'; head += ''; }); var ColCounter = 0; $.each(Decisiontable._ColType.setter, function(index, data) { if(typeof data == 'undefined') return; count.setter++; structureHTML += ''; headActions += ''; head += '
' + Decisiontable.getSetterInput(Decisiontable._Columns[data].type, 'task[structure][_Columns][' + data + '][value]', Decisiontable._Columns[data].value) + '
'; }); head += ''; headActions += ''; html = headActions; html += head; $.each(Decisiontable._Data, function(rowIndex, rowData) { if(typeof rowData == 'undefined') return; var rowHTML = ''; rowHTML += ''; $.each(Decisiontable._ColType.decision, function(colIndex, colData) { if(typeof colData == 'undefined') return; if(typeof rowData[colData] == 'undefined') rowData[colData] = {value:'',type:'equal'}; rowHTML += '
'; rowHTML += ''; rowHTML += ''; rowHTML += '
'; }); $.each(Decisiontable._ColType.setter, function(colIndex, colData) { if(typeof colData == 'undefined') return; if(typeof rowData[colData] == 'undefined') rowData[colData] = {value:'',type:'envvar'}; rowHTML += ''; rowHTML += '
' + htmlEntities(rowData[colData].value) + '
'; rowHTML += ''; }); rowHTML += ''; html += rowHTML; }); $('#mainTaskForm').append(structureHTML); $('#th_decision').attr('colspan', count.decision); $('#th_setter').attr('colspan', count.setter); var totalColNumber = Number($('#th_decision').attr('colspan')) + Number($('#th_setter').attr('colspan')) + 1; $('#th_decision').css('width', ((100 / totalColNumber) * (Number($('#th_decision').attr('colspan')) + 1)) + '%'); $('#th_setter').css('width', ((100 / totalColNumber) * $('#th_setter').attr('colspan')) + '%') $('#DecisionTable tbody').html(html); createTemplateFields('#DecisionTable'); $('.MakeSelect2').each(function(index, ele) { $(ele).removeClass('MakeSelect2'); $(ele).select2(); }); Decisiontable.addEvents(); }, addRow:function() { var row = {}; $.each(Decisiontable._Columns, function(index, data) { row[data] = ''; }); Decisiontable._Data.push(row); Decisiontable.redrawTable(); }, addDecision:function() { do { Decisiontable._ColCounter++; key = 'col_' + Decisiontable._ColCounter; } while(typeof Decisiontable._Columns[key] != 'undefined'); Decisiontable._Columns[key] = { 'value': '' }; Decisiontable._ColType['decision'].push(key); Decisiontable.redrawTable(); }, addSetter:function() { do { Decisiontable._ColCounter++; key = 'col_' + Decisiontable._ColCounter; } while(typeof Decisiontable._Columns[key] != 'undefined'); Decisiontable._Columns[key] = { 'value': '' }; Decisiontable._ColType['setter'].push(key); Decisiontable.redrawTable(); }, init:function(structure, data) { if(typeof structure._Columns != 'undefined') { Decisiontable._Columns = structure._Columns; } if(typeof structure._ColType != 'undefined') { Decisiontable._ColType = structure._ColType; } Decisiontable.delayRedraw(true); if(Decisiontable._ColType.decision.length == 0) { Decisiontable.addDecision(); } if(Decisiontable._ColType.setter.length == 0) { Decisiontable.addSetter(); } if(Decisiontable._Data.length == 0) { Decisiontable.addRow(); } Decisiontable._Data = []; $.each(data, function(index, data) { Decisiontable._Data.push(data); }); Decisiontable._ColCounter = Object.keys(Decisiontable._Columns).length+ 1; Decisiontable.delayRedraw(false); Decisiontable.redrawTable(); $.each(data, function(rowIndex, rowData) { //var parent = }); } }; })(jQuery);