ComboEntryFactory.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use strict';
  2. var assign = require('lodash/assign'),
  3. find = require('lodash/find');
  4. var domQuery = require('min-dom').query;
  5. var escapeHTML = require('../Utils').escapeHTML;
  6. var selectEntryFactory = require('./SelectEntryFactory'),
  7. entryFieldDescription = require('./EntryFieldDescription');
  8. /**
  9. * The combo box is a special implementation of the select entry and adds the option 'custom' to the
  10. * select box. If 'custom' is selected, an additional text input field is shown which allows to define
  11. * a custom value.
  12. *
  13. * @param {Object} options
  14. * @param {string} options.id
  15. * @param {string} options.label
  16. * @param {Array<Object>} options.selectOptions list of name/value pairs
  17. * @param {string} options.modelProperty
  18. * @param {function} options.get
  19. * @param {function} options.set
  20. * @param {string} [options.customValue] custom select option value (default: 'custom')
  21. * @param {string} [options.customName] custom select option name visible in the select box (default: 'custom')
  22. *
  23. * @return {Object}
  24. */
  25. var comboBox = function(options) {
  26. var selectOptions = options.selectOptions,
  27. modelProperty = options.modelProperty,
  28. customValue = options.customValue || 'custom',
  29. customName = options.customName || 'custom ' + modelProperty,
  30. description = options.description;
  31. // check if a value is not a built in value
  32. var isCustomValue = function(value) {
  33. if (typeof value[modelProperty] === 'undefined') {
  34. return false;
  35. }
  36. var isCustom = !find(selectOptions, function(option) {
  37. return value[modelProperty] === option.value;
  38. });
  39. return isCustom;
  40. };
  41. var comboOptions = assign({}, options);
  42. // true if the selected value in the select box is customValue
  43. comboOptions.showCustomInput = function(element, node) {
  44. var selectBox = domQuery('[data-entry="'+ options.id +'"] select', node.parentNode);
  45. if (selectBox) {
  46. return selectBox.value === customValue;
  47. }
  48. return false;
  49. };
  50. comboOptions.get = function(element, node) {
  51. var value = options.get(element, node);
  52. var modifiedValues = {};
  53. if (!isCustomValue(value)) {
  54. modifiedValues[modelProperty] = value[modelProperty] || '';
  55. return modifiedValues;
  56. }
  57. modifiedValues[modelProperty] = customValue;
  58. modifiedValues['custom-'+modelProperty] = value[modelProperty];
  59. return modifiedValues;
  60. };
  61. comboOptions.set = function(element, values, node) {
  62. var modifiedValues = {};
  63. // if the custom select option has been selected
  64. // take the value from the text input field
  65. if (values[modelProperty] === customValue) {
  66. modifiedValues[modelProperty] = values['custom-' + modelProperty] || '';
  67. }
  68. else if (options.emptyParameter && values[modelProperty] === '') {
  69. modifiedValues[modelProperty] = undefined;
  70. } else {
  71. modifiedValues[modelProperty] = values[modelProperty];
  72. }
  73. return options.set(element, modifiedValues, node);
  74. };
  75. comboOptions.selectOptions.push({ name: customName, value: customValue });
  76. var comboBoxEntry = assign({}, selectEntryFactory(comboOptions, comboOptions));
  77. comboBoxEntry.html += '<div class="bpp-field-wrapper bpp-combo-input" ' +
  78. 'data-show="showCustomInput"' +
  79. '>' +
  80. '<input id="activiti-' + escapeHTML(options.id) + '-input" type="text" name="custom-' +
  81. escapeHTML(modelProperty) + '" ' +
  82. ' />' +
  83. '</div>';
  84. // add description below combo box entry field
  85. if (description) {
  86. comboBoxEntry.html += entryFieldDescription(description);
  87. }
  88. return comboBoxEntry;
  89. };
  90. module.exports = comboBox;