TextInputEntryFactory.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict';
  2. var escapeHTML = require('../Utils').escapeHTML;
  3. var domQuery = require('min-dom').query;
  4. var entryFieldDescription = require('./EntryFieldDescription');
  5. var textField = function(options, defaultParameters) {
  6. // Default action for the button next to the input-field
  7. var defaultButtonAction = function(element, inputNode) {
  8. var input = domQuery('input[name="' + options.modelProperty + '"]', inputNode);
  9. input.value = '';
  10. return true;
  11. };
  12. // default method to determine if the button should be visible
  13. var defaultButtonShow = function(element, inputNode) {
  14. var input = domQuery('input[name="' + options.modelProperty + '"]', inputNode);
  15. return input.value !== '';
  16. };
  17. var resource = defaultParameters,
  18. label = options.label || resource.id,
  19. dataValueLabel = options.dataValueLabel,
  20. buttonLabel = (options.buttonLabel || 'X'),
  21. actionName = (typeof options.buttonAction != 'undefined') ? options.buttonAction.name : 'clear',
  22. actionMethod = (typeof options.buttonAction != 'undefined') ? options.buttonAction.method : defaultButtonAction,
  23. showName = (typeof options.buttonShow != 'undefined') ? options.buttonShow.name : 'canClear',
  24. showMethod = (typeof options.buttonShow != 'undefined') ? options.buttonShow.method : defaultButtonShow,
  25. canBeDisabled = !!options.disabled && typeof options.disabled === 'function',
  26. canBeHidden = !!options.hidden && typeof options.hidden === 'function',
  27. description = options.description;
  28. resource.html =
  29. '<label for="activiti-' + escapeHTML(resource.id) + '" ' +
  30. (canBeDisabled ? 'data-disable="isDisabled" ' : '') +
  31. (canBeHidden ? 'data-show="isHidden" ' : '') +
  32. (dataValueLabel ? 'data-value="' + escapeHTML(dataValueLabel) + '"' : '') + '>'+ escapeHTML(label) +'</label>' +
  33. '<div class="bpp-field-wrapper" ' +
  34. (canBeDisabled ? 'data-disable="isDisabled"' : '') +
  35. (canBeHidden ? 'data-show="isHidden"' : '') +
  36. '>' +
  37. '<input id="activiti-' + escapeHTML(resource.id) + '" type="text" name="' + escapeHTML(options.modelProperty) + '" ' +
  38. (canBeDisabled ? 'data-disable="isDisabled"' : '') +
  39. (canBeHidden ? 'data-show="isHidden"' : '') +
  40. ' />' +
  41. '<button class="' + escapeHTML(actionName) + '" data-action="' + escapeHTML(actionName) + '" data-show="' + escapeHTML(showName) + '" ' +
  42. (canBeDisabled ? 'data-disable="isDisabled"' : '') +
  43. (canBeHidden ? ' data-show="isHidden"' : '') + '>' +
  44. '<span>' + escapeHTML(buttonLabel) + '</span>' +
  45. '</button>' +
  46. '</div>';
  47. // add description below text input entry field
  48. if (description) {
  49. resource.html += entryFieldDescription(description);
  50. }
  51. resource[actionName] = actionMethod;
  52. resource[showName] = showMethod;
  53. if (canBeDisabled) {
  54. resource.isDisabled = function() {
  55. return options.disabled.apply(resource, arguments);
  56. };
  57. }
  58. if (canBeHidden) {
  59. resource.isHidden = function() {
  60. return !options.hidden.apply(resource, arguments);
  61. };
  62. }
  63. resource.cssClasses = ['bpp-textfield'];
  64. return resource;
  65. };
  66. module.exports = textField;