1/** * Created by Stefan on 06.04.2016. */ var SWFormulaAssistant = null; (function($){ SWFormulaAssistant = function() { this.saveUrl = 'index.php?module=Workflow2&action=FormulaSave&parent=Settings'; this.loadUrl = 'index.php?module=Workflow2&action=FormulaLoad&parent=Settings'; this.parentEle = null; this.dfd = null; this.ShowSaveOnly = true; this.formulaId = 0; this.start = function(value) { this.dfd = jQuery.Deferred(); this.show(); if(typeof value != 'undefined' && value != '') { window.setTimeout($.proxy(function() { var regex = /\$\[FORMULA,([0-9]+)\]/; var match = regex.exec(value); if(match && typeof match[1] != 'undefined') { $.post(this.loadUrl, {formulaId:match[1]}, function(response) { $('[name="formulaId"]', this.parentEle).val(response.id); $('[name="formulaName"]', this.parentEle).val(response.name); $('[name="formula"]', this.parentEle).val(response.formula).trigger('blur'); if(typeof response.variables != 'undefined') { $.each(response.variables, $.proxy(function(index, value) { $('.FA_VarField[data-varname="' + index + '"]').val(value); }, this)); } }, 'json'); } }, this), 500); } return this.dfd.promise(); }; this.save = function() { var dfd = jQuery.Deferred(); if($('[name="formula"]', this.parentEle).val() == '') { return false; } var data = { 'formulaId' : $('[name="formulaId"]', this.parentEle).val(), 'formula' : $('[name="formula"]', this.parentEle).val(), 'formulaName' : $('[name="formulaName"]', this.parentEle).val(), 'variables' : {} }; $('.FA_VarField', this.parentEle).each(function(index, ele) { data['variables'][$(ele).data('varname')] = $(ele).val(); }); $.post(this.saveUrl, data, $.proxy(function(response) { $('[name="formulaId"]', this.parentEle).val(response['id']); dfd.resolve(); }, this), 'json'); return dfd.promise(); }; this.hideSaveOnly = function() { this.ShowSaveOnly = false; }; this.show = function() { var html = '