EntryFactory.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. 'use strict';
  2. var getBusinessObject = require('bpmn-js/lib/util/ModelUtil').getBusinessObject;
  3. // input entities
  4. var textInputField = require('./TextInputEntryFactory'),
  5. checkboxField = require('./CheckboxEntryFactory'),
  6. selectBoxField = require('./SelectEntryFactory'),
  7. comboBoxField = require('./ComboEntryFactory'),
  8. textBoxField = require('./TextBoxEntryFactory'),
  9. validationAwareTextInputField = require('./ValidationAwareTextInput'),
  10. tableField = require('./TableEntryFactory'),
  11. labelEntry = require('./LabelFactory'),
  12. link = require('./LinkEntryFactory');
  13. var cmdHelper = require('../helper/CmdHelper');
  14. // helpers ////////////////////////////////////////
  15. function ensureNotNull(prop) {
  16. if (!prop) {
  17. throw new Error(prop + ' must be set.');
  18. }
  19. return prop;
  20. }
  21. /**
  22. * sets the default parameters which are needed to create an entry
  23. *
  24. * @param options
  25. * @returns {{id: *, description: (*|string), get: (*|Function), set: (*|Function),
  26. * validate: (*|Function), html: string}}
  27. */
  28. var setDefaultParameters = function(options) {
  29. // default method to fetch the current value of the input field
  30. var defaultGet = function(element) {
  31. var bo = getBusinessObject(element),
  32. res = {},
  33. prop = ensureNotNull(options.modelProperty);
  34. res[prop] = bo.get(prop);
  35. return res;
  36. };
  37. // default method to set a new value to the input field
  38. var defaultSet = function(element, values) {
  39. var res = {},
  40. prop = ensureNotNull(options.modelProperty);
  41. if (values[prop] !== '') {
  42. res[prop] = values[prop];
  43. } else {
  44. res[prop] = undefined;
  45. }
  46. return cmdHelper.updateProperties(element, res);
  47. };
  48. // default validation method
  49. var defaultValidate = function() {
  50. return {};
  51. };
  52. return {
  53. id : options.id,
  54. description : (options.description || ''),
  55. get : (options.get || defaultGet),
  56. set : (options.set || defaultSet),
  57. validate : (options.validate || defaultValidate),
  58. html: ''
  59. };
  60. };
  61. function EntryFactory() {
  62. }
  63. /**
  64. * Generates an text input entry object for a property panel.
  65. * options are:
  66. * - id: id of the entry - String
  67. *
  68. * - description: description of the property - String
  69. *
  70. * - label: label for the input field - String
  71. *
  72. * - set: setter method - Function
  73. *
  74. * - get: getter method - Function
  75. *
  76. * - validate: validation mehtod - Function
  77. *
  78. * - modelProperty: name of the model property - String
  79. *
  80. * - buttonAction: Object which contains the following properties: - Object
  81. * ---- name: name of the [data-action] callback - String
  82. * ---- method: callback function for [data-action] - Function
  83. *
  84. * - buttonShow: Object which contains the following properties: - Object
  85. * ---- name: name of the [data-show] callback - String
  86. * ---- method: callback function for [data-show] - Function
  87. *
  88. * @param options
  89. * @returns the propertyPanel entry resource object
  90. */
  91. EntryFactory.textField = function(options) {
  92. return textInputField(options, setDefaultParameters(options));
  93. };
  94. EntryFactory.validationAwareTextField = function(options) {
  95. return validationAwareTextInputField(options, setDefaultParameters(options));
  96. };
  97. /**
  98. * Generates a checkbox input entry object for a property panel.
  99. * options are:
  100. * - id: id of the entry - String
  101. *
  102. * - description: description of the property - String
  103. *
  104. * - label: label for the input field - String
  105. *
  106. * - set: setter method - Function
  107. *
  108. * - get: getter method - Function
  109. *
  110. * - validate: validation method - Function
  111. *
  112. * - modelProperty: name of the model property - String
  113. *
  114. * @param options
  115. * @returns the propertyPanel entry resource object
  116. */
  117. EntryFactory.checkbox = function(options) {
  118. return checkboxField(options, setDefaultParameters(options));
  119. };
  120. EntryFactory.textBox = function(options) {
  121. return textBoxField(options, setDefaultParameters(options));
  122. };
  123. EntryFactory.selectBox = function(options) {
  124. return selectBoxField(options, setDefaultParameters(options));
  125. };
  126. EntryFactory.comboBox = function(options) {
  127. return comboBoxField(options);
  128. };
  129. EntryFactory.table = function(options) {
  130. return tableField(options);
  131. };
  132. EntryFactory.label = function(options) {
  133. return labelEntry(options);
  134. };
  135. EntryFactory.link = function(options) {
  136. return link(options);
  137. };
  138. module.exports = EntryFactory;