'use strict'; var domQuery = require('min-dom').query, escapeHTML = require('../../../../Utils').escapeHTML, utils = require('../../../../Utils'); function getScriptType(node) { return utils.selectedType('select[name=scriptType]', node.parentElement); } module.exports = function(scriptLanguagePropName, scriptValuePropName, isFormatRequired, translate) { return { template: '
' + '' + '
' + '' + '' + '
' + '
' + '
' + '' + '
' + '' + '
' + '
' + '
' + '' + '
' + '' + '' + '
' + '
' + '
' + '' + '
' + '' + '
'+ '
', get: function(element, bo) { var values = {}; // read values from xml: var boScriptResource = bo.get('activiti:resource'), boScript = bo.get(scriptValuePropName), boScriptFormat = bo.get(scriptLanguagePropName); if (typeof boScriptResource !== 'undefined') { values.scriptResourceValue = boScriptResource; values.scriptType = 'scriptResource'; } else { values.scriptValue = boScript; values.scriptType = 'script'; } values.scriptFormat = boScriptFormat; return values; }, set: function(element, values, containerElement) { var scriptFormat = values.scriptFormat, scriptType = values.scriptType, scriptResourceValue = values.scriptResourceValue, scriptValue = values.scriptValue; // init update var update = { 'activiti:resource': undefined }; update[scriptValuePropName] = undefined; update[scriptLanguagePropName] = undefined; if (isFormatRequired) { // always set language update[scriptLanguagePropName] = scriptFormat || ''; } else // set language only when scriptFormat has a value if (scriptFormat !== '') { update[scriptLanguagePropName] = scriptFormat; } // set either inline script or resource if ('scriptResource' === scriptType) { update['activiti:resource'] = scriptResourceValue || ''; } else { update[scriptValuePropName] = scriptValue || ''; } return update; }, validate: function(element, values) { var validationResult = {}; if (values.scriptType === 'script' && !values.scriptValue) { validationResult.scriptValue = translate('Must provide a value'); } if (values.scriptType === 'scriptResource' && !values.scriptResourceValue) { validationResult.scriptResourceValue = translate('Must provide a value'); } if (isFormatRequired && (!values.scriptFormat || values.scriptFormat.length === 0)) { validationResult.scriptFormat = translate('Must provide a value'); } return validationResult; }, clearScriptFormat: function(element, inputNode, btnNode, scopeNode) { domQuery('input[name=scriptFormat]', scopeNode).value=''; return true; }, canClearScriptFormat: function(element, inputNode, btnNode, scopeNode) { var input = domQuery('input[name=scriptFormat]', scopeNode); return input.value !== ''; }, clearScriptResource: function(element, inputNode, btnNode, scopeNode) { domQuery('input[name=scriptResourceValue]', scopeNode).value=''; return true; }, canClearScriptResource: function(element, inputNode, btnNode, scopeNode) { var input = domQuery('input[name=scriptResourceValue]', scopeNode); return input.value !== ''; }, clearScript: function(element, inputNode, btnNode, scopeNode) { domQuery('textarea[name=scriptValue]', scopeNode).value=''; return true; }, canClearScript: function(element, inputNode, btnNode, scopeNode) { var input = domQuery('textarea[name=scriptValue]', scopeNode); return input.value !== ''; }, isScriptResource: function(element, inputNode, btnNode, scopeNode) { var scriptType = getScriptType(scopeNode); return scriptType === 'scriptResource'; }, isScript: function(element, inputNode, btnNode, scopeNode) { var scriptType = getScriptType(scopeNode); return scriptType === 'script'; } }; };