Licitator 1.0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
6.7 KiB

5 years ago
  1. /**
  2. * Created by george.calcea on 12/27/2016.
  3. */
  4. /**
  5. * Class that handles the condition tree (condition type, operator, value)
  6. * @param {Definitions} fieldDefinitions
  7. * @param {Object} ruleData
  8. * @param {callable} changeCallback
  9. * @constructor
  10. */
  11. var Condition = function (fieldDefinitions, ruleData) {
  12. this.fieldDefinitions = fieldDefinitions;
  13. this.ruleData = ruleData;
  14. this.id = guid();
  15. this.init();
  16. };
  17. /**
  18. * Init internal parameters
  19. */
  20. Condition.prototype.init = function () {
  21. var descriptionIcon = $("<a href='#' class='help-icon' data-toggle='tooltip' data-placement='right'><i class='glyphicon glyphicon-question-sign'></i></a>");
  22. this.wrapper = $('<div>', {class: 'condition-wrapper row'});
  23. this.parameterSelectWrapper = $('<div>', {class: 'condition-select-wrapper col-sm-3'});
  24. this.wrapper.append(this.parameterSelectWrapper);
  25. this.wrapper.append(descriptionIcon);
  26. this.operatorSelectWrapper = $('<div>', {class: 'condition-operators-wrapper col-sm-3'});
  27. this.wrapper.append(this.operatorSelectWrapper);
  28. this.valueFieldWrapper = $('<div>', {class: 'condition-values-wrapper col-sm-3'});
  29. this.wrapper.append(this.valueFieldWrapper);
  30. this.help = descriptionIcon;
  31. if ($.isEmptyObject(this.ruleData)) {
  32. this.selectedCondition = this.fieldDefinitions.getFirstConditionDefinition();
  33. this.selectedOperator = this.selectedCondition.getFirstOperatorDefinition();
  34. } else {
  35. this.selectedCondition = this.fieldDefinitions.getParameterDefinition(this.ruleData.name);
  36. this.selectedOperator = this.selectedCondition.getOperatorDefinition(this.ruleData.operator);
  37. }
  38. this.help.attr("title",this.selectedCondition.getDescription());
  39. this.help.tooltip();
  40. this.generateFieldObject();
  41. };
  42. /**
  43. * Returns the HTML for current condition manager
  44. * @param {jQuery} parentWrapper
  45. */
  46. Condition.prototype.appendHtml = function (parentWrapper) {
  47. parentWrapper.append(this.wrapper);
  48. this.wrapper.prepend(this.getRemoveButtonHtml());
  49. this.appendConditionsSelect(this.parameterSelectWrapper.empty());
  50. this.appendOperatorsSelect(this.operatorSelectWrapper.empty());
  51. this.field.appendHtml(this.valueFieldWrapper.empty());
  52. };
  53. /**
  54. * Returns an Jquery object for conditions select
  55. * @param {jQuery} wrapper
  56. */
  57. Condition.prototype.appendConditionsSelect = function (wrapper) {
  58. var select = $('<select>', {class: 'conditions-select'});
  59. var propertiesList = this.fieldDefinitions.getParametersList();
  60. for (var name in propertiesList) {
  61. var optionOptions = {selected: this.selectedCondition.getName() === name};
  62. var option = $('<option>', optionOptions).val(name).text(propertiesList[name]);
  63. select.append(option);
  64. }
  65. select.on('change', function (e) {
  66. this.updateParameter($(e.target).val());
  67. this.appendOperatorsSelect(this.operatorSelectWrapper.empty());
  68. this.field.appendHtml(this.valueFieldWrapper.empty());
  69. var conditionDescription = this.selectedCondition.getDescription();
  70. this.help.attr({
  71. "title":conditionDescription,
  72. "data-original-title":conditionDescription
  73. }).tooltip();
  74. if (typeof this.changeCallback === 'function') {
  75. this.changeCallback(this.id);
  76. }
  77. }.bind(this));
  78. wrapper.append(select);
  79. select.select2();
  80. };
  81. /**
  82. * Returns an Jquery object for operators select
  83. * @param {jQuery} wrapper
  84. */
  85. Condition.prototype.appendOperatorsSelect = function (wrapper) {
  86. var operators = this.selectedCondition.getOperatorsList();
  87. var select = $('<select>', {class: 'operator-select'});
  88. for (var operatorName in operators) {
  89. var optionOptions = {selected: this.selectedOperator.getName() === operatorName};
  90. var option = $('<option>', optionOptions).val(operatorName).text(operators[operatorName]);
  91. select.append(option);
  92. }
  93. select.on('change', function (e) {
  94. this.updateOperator($(e.target).val());
  95. this.field.appendHtml(this.valueFieldWrapper.empty());
  96. if (typeof this.changeCallback === 'function') {
  97. this.changeCallback(this.id);
  98. }
  99. }.bind(this));
  100. wrapper.append(select);
  101. select.select2();
  102. };
  103. /**
  104. * Initializes the Field object.
  105. */
  106. Condition.prototype.generateFieldObject = function () {
  107. this.field = fieldsTypesFactory.factory(
  108. this.selectedOperator.getFieldType(),
  109. this.selectedOperator.getFieldOptions()
  110. );
  111. if (typeof this.changeCallback === 'function') {
  112. this.field.setChangeCallback(this.changeCallback);
  113. }
  114. if (!$.isEmptyObject(this.ruleData)) {
  115. this.field.setValue(this.ruleData.value);
  116. }
  117. };
  118. /**
  119. * Callback for operator change
  120. * @param {string} newOperator
  121. */
  122. Condition.prototype.updateOperator = function (newOperator) {
  123. this.selectedOperator = this.selectedCondition.getOperatorDefinition(newOperator);
  124. this.generateFieldObject();
  125. };
  126. /**
  127. * Callback for condition change
  128. * @param {string} newParameter
  129. */
  130. Condition.prototype.updateParameter = function (newParameter) {
  131. this.selectedCondition = this.fieldDefinitions.getParameterDefinition(newParameter);
  132. this.selectedOperator = this.selectedCondition.getFirstOperatorDefinition();
  133. this.generateFieldObject();
  134. };
  135. /**
  136. * Returns an Jquery object for remove condition button
  137. * @returns {*|jQuery|HTMLElement}
  138. */
  139. Condition.prototype.getRemoveButtonHtml = function () {
  140. var removeEl = $('<a>', {
  141. class: 'remove-condition-button remove',
  142. id: this.id,
  143. href: 'javascript://'
  144. });
  145. removeEl.append("<i class='glyphicon glyphicon-remove'>");
  146. removeEl.on('click', function (e) {
  147. this.remove();
  148. }.bind(this));
  149. return removeEl;
  150. };
  151. /**
  152. *
  153. * @param {function} callback
  154. */
  155. Condition.prototype.setRemoveCallback = function (callback) {
  156. this.removeCallback = callback;
  157. };
  158. /**
  159. *
  160. * @param {function} callback
  161. */
  162. Condition.prototype.setChangeCallback = function (callback) {
  163. this.changeCallback = callback;
  164. this.field.setChangeCallback(callback);
  165. };
  166. /**
  167. * Callback that is executed on remove condition
  168. */
  169. Condition.prototype.remove = function () {
  170. this.wrapper.remove();
  171. if (typeof this.removeCallback === 'function') {
  172. this.removeCallback(this.id);
  173. }
  174. };
  175. /**
  176. * Returns the manager id
  177. * @returns {*|null}
  178. */
  179. Condition.prototype.getId = function () {
  180. return this.id;
  181. };
  182. /**
  183. * Serialized the current manager
  184. * @returns {*}
  185. */
  186. Condition.prototype.serialize = function () {
  187. if (this.selectedOperator === null || this.selectedCondition === null) {
  188. return {};
  189. }
  190. return {
  191. name: this.selectedCondition.getName(),
  192. operator: this.selectedOperator.getName(),
  193. value: this.field.getValue()
  194. };
  195. };