SelectEntryFactory.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use strict';
  2. var escapeHTML = require('../Utils').escapeHTML;
  3. var domify = require('min-dom').domify;
  4. var forEach = require('lodash/forEach');
  5. var entryFieldDescription = require('./EntryFieldDescription');
  6. var isList = function(list) {
  7. return !(!list || Object.prototype.toString.call(list) !== '[object Array]');
  8. };
  9. var addEmptyParameter = function(list) {
  10. return list.concat([ { name: '', value: '' } ]);
  11. };
  12. var createOption = function(option) {
  13. return '<option value="' + option.value + '">' + option.name + '</option>';
  14. };
  15. /**
  16. * @param {Object} options
  17. * @param {string} options.id
  18. * @param {string} [options.label]
  19. * @param {Array<Object>} options.selectOptions
  20. * @param {string} options.modelProperty
  21. * @param {boolean} options.emptyParameter
  22. * @param {function} options.disabled
  23. * @param {function} options.hidden
  24. * @param {Object} defaultParameters
  25. *
  26. * @return {Object}
  27. */
  28. var selectbox = function(options, defaultParameters) {
  29. var resource = defaultParameters,
  30. label = options.label || resource.id,
  31. selectOptions = options.selectOptions || [ { name: '', value: '' } ],
  32. modelProperty = options.modelProperty,
  33. emptyParameter = options.emptyParameter,
  34. canBeDisabled = !!options.disabled && typeof options.disabled === 'function',
  35. canBeHidden = !!options.hidden && typeof options.hidden === 'function',
  36. description = options.description;
  37. if (emptyParameter) {
  38. selectOptions = addEmptyParameter(selectOptions);
  39. }
  40. resource.html =
  41. '<label for="activiti-' + escapeHTML(resource.id) + '"' +
  42. (canBeDisabled ? 'data-disable="isDisabled" ' : '') +
  43. (canBeHidden ? 'data-show="isHidden" ' : '') +
  44. '>' + escapeHTML(label) + '</label>' +
  45. '<select id="activiti-' + escapeHTML(resource.id) + '-select" name="' +
  46. escapeHTML(modelProperty) + '"' +
  47. (canBeDisabled ? 'data-disable="isDisabled" ' : '') +
  48. (canBeHidden ? 'data-show="isHidden" ' : '') +
  49. ' data-value>';
  50. if (isList(selectOptions)) {
  51. forEach(selectOptions, function(option) {
  52. resource.html += '<option value="' + escapeHTML(option.value) + '">' +
  53. (option.name ? escapeHTML(option.name) : '') + '</option>';
  54. });
  55. }
  56. resource.html += '</select>';
  57. // add description below select box entry field
  58. if (description && typeof options.showCustomInput !== 'function') {
  59. resource.html += entryFieldDescription(description);
  60. }
  61. /**
  62. * Fill the select box options dynamically.
  63. *
  64. * Calls the defined function #selectOptions in the entry to get the
  65. * values for the options and set the value to the inputNode.
  66. *
  67. * @param {djs.model.Base} element
  68. * @param {HTMLElement} entryNode
  69. * @param {EntryDescriptor} inputNode
  70. * @param {Object} inputName
  71. * @param {Object} newValue
  72. */
  73. resource.setControlValue = function(element, entryNode, inputNode, inputName, newValue) {
  74. if (typeof selectOptions === 'function') {
  75. var options = selectOptions(element, inputNode);
  76. if (options) {
  77. // remove existing options
  78. while (inputNode.firstChild) {
  79. inputNode.removeChild(inputNode.firstChild);
  80. }
  81. // add options
  82. forEach(options, function(option) {
  83. var template = domify(createOption(option));
  84. inputNode.appendChild(template);
  85. });
  86. }
  87. }
  88. // set select value
  89. if (newValue !== undefined) {
  90. inputNode.value = newValue;
  91. }
  92. };
  93. if (canBeDisabled) {
  94. resource.isDisabled = function() {
  95. return options.disabled.apply(resource, arguments);
  96. };
  97. }
  98. if (canBeHidden) {
  99. resource.isHidden = function() {
  100. return !options.hidden.apply(resource, arguments);
  101. };
  102. }
  103. resource.cssClasses = ['bpp-dropdown'];
  104. return resource;
  105. };
  106. module.exports = selectbox;