EnumCreateUpdateItemView.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. define([ "require", "backbone", "hbs!tmpl/business_metadata/EnumCreateUpdateItemView_tmpl", "utils/Utils", "utils/UrlLinks" ], function(require, Backbone, EnumCreateUpdateItemViewTmpl, Utils, UrlLinks) {
  2. "use strict";
  3. return Backbone.Marionette.ItemView.extend({
  4. template: EnumCreateUpdateItemViewTmpl,
  5. regions: {},
  6. ui: {
  7. enumTypeSelectorContainer: "[data-id='enumTypeSelectorContainer']",
  8. enumSelector: "[data-id='enumSelector']",
  9. enumValueSelectorContainer: "[data-id='enumValueSelectorContainer']",
  10. valueSelector: "[data-id='valueSelector']",
  11. enumCancleBtn: "[data-id='enumCancleBtn']",
  12. enumOkBtn: "[data-id='enumOkBtn']"
  13. },
  14. events: function() {
  15. var events = {};
  16. return events["change " + this.ui.enumSelector] = function(e) {
  17. this.model.set({
  18. enumValues: e.target.value.trim()
  19. });
  20. }, events["change " + this.ui.enumSelector] = function(e) {
  21. var emumValue = this.ui.enumSelector.select2("data")[0] ? this.ui.enumSelector.select2("data")[0].text : this.ui.enumSelector.val();
  22. "" == emumValue || null == emumValue ? this.ui.enumValueSelectorContainer.hide() : (this.ui.enumValueSelectorContainer.show(),
  23. this.showEnumValues(emumValue));
  24. }, events["change " + this.ui.valueSelector] = function(e) {}, events["click " + this.ui.enumCancleBtn] = function(e) {
  25. return this.options.closeModal ? void this.options.closeModal() : (this.ui.enumValueSelectorContainer.hide(),
  26. this.ui.enumSelector.val("").trigger("change"), void this.ui.enumCancleBtn.attr("disabled", "true"));
  27. }, events["click " + this.ui.enumOkBtn] = function(e) {
  28. this.ui.enumCancleBtn.attr("disabled", "true"), this.onUpdateEnum();
  29. }, events;
  30. },
  31. initialize: function(options) {
  32. _.extend(this, _.pick(options, "businessMetadataDefCollection", "enumDefCollection"));
  33. },
  34. onRender: function() {
  35. this.ui.enumValueSelectorContainer.hide(), this.bindEvents(), this.emumTypeSelectDisplay(),
  36. this.options.closeModal || (this.ui.enumCancleBtn.attr("disabled", "true"), this.ui.enumCancleBtn.text("Clear"));
  37. },
  38. bindEvents: function() {
  39. var that = this;
  40. this.listenTo(this.enumDefCollection, "reset", function() {
  41. that.emumTypeSelectDisplay();
  42. });
  43. },
  44. showEnumValues: function(enumName) {
  45. var enumValues = "", selectedValues = [], selectedEnum = this.enumDefCollection.fullCollection.findWhere({
  46. name: enumName
  47. }), selectedEnumValues = selectedEnum ? selectedEnum.get("elementDefs") : null;
  48. _.each(selectedEnumValues, function(enumVal, index) {
  49. selectedValues.push(enumVal.value), enumValues += "<option>" + _.escape(enumVal.value) + "</option>";
  50. }), this.ui.enumCancleBtn.removeAttr("disabled"), this.ui.valueSelector.empty(),
  51. this.ui.valueSelector.append(enumValues), this.ui.valueSelector.val(selectedValues),
  52. this.ui.valueSelector.select2({
  53. placeholder: "Select Enum value",
  54. allowClear: !1,
  55. tags: !0,
  56. multiple: !0
  57. });
  58. },
  59. emumTypeSelectDisplay: function() {
  60. var enumTypes = "";
  61. this.enumDefCollection.fullCollection.each(function(model, index) {
  62. enumTypes += "<option>" + _.escape(model.get("name")) + "</option>";
  63. }), this.ui.enumSelector.empty(), this.ui.enumSelector.append(enumTypes), this.ui.enumSelector.val(""),
  64. this.ui.enumSelector.select2({
  65. placeholder: "Select Enum name",
  66. tags: !0,
  67. allowClear: !0,
  68. multiple: !1,
  69. templateResult: this.formatSearchResult
  70. });
  71. },
  72. formatSearchResult: function(state) {
  73. return state.id ? state.element ? $("<span>" + _.escape(state.text) + "</span>") : $("<span>Create new enum : <strong> " + _.escape(state.text) + "</strong></span>") : state.text;
  74. },
  75. validationEnum: function() {
  76. var selectedEnumName = this.ui.enumSelector.val(), selectedEnumValues = this.ui.valueSelector.val();
  77. return "" == selectedEnumName || null == selectedEnumName ? (this.ui.enumOkBtn.hideButtonLoader(),
  78. Utils.notifyInfo({
  79. content: "Please enter the Enumeration Name"
  80. }), !0) : "" == selectedEnumValues || null == selectedEnumValues ? (this.ui.enumOkBtn.hideButtonLoader(),
  81. Utils.notifyInfo({
  82. content: "Please enter the Enum values"
  83. }), !0) : void 0;
  84. },
  85. onUpdateEnum: function(view, modal) {
  86. var that = this, selectedEnumName = this.ui.enumSelector.val(), selectedEnumValues = this.ui.valueSelector.val(), enumName = this.enumDefCollection.fullCollection.findWhere({
  87. name: selectedEnumName
  88. }), isPutCall = !1, isPostCallEnum = !1, enumDefs = [];
  89. if (!this.validationEnum()) {
  90. if (this.ui.enumOkBtn.showButtonLoader(), this.ui.enumSelector.attr("disabled", "true"),
  91. this.ui.valueSelector.attr("disabled", "true"), this.ui.enumCancleBtn.attr("disabled", "true"),
  92. enumName) {
  93. var enumDef = enumName.get("elementDefs");
  94. enumDef.length === selectedEnumValues.length ? _.each(enumDef, function(enumVal, index) {
  95. selectedEnumValues.indexOf(enumVal.value) === -1 && (isPutCall = !0);
  96. }) : isPutCall = !0;
  97. } else isPostCallEnum = !0;
  98. var elementValues = [];
  99. _.each(selectedEnumValues, function(inputEnumVal, index) {
  100. elementValues.push({
  101. ordinal: index + 1,
  102. value: inputEnumVal
  103. });
  104. }), enumDefs.push({
  105. name: selectedEnumName,
  106. elementDefs: elementValues
  107. }), this.json = {
  108. enumDefs: enumDefs
  109. };
  110. var apiObj = {
  111. sort: !1,
  112. success: function(model, response) {
  113. if (that.ui.enumValueSelectorContainer.hide(), isPostCallEnum) that.enumDefCollection.add(model.enumDefs[0]),
  114. Utils.notifySuccess({
  115. content: "Enumeration " + selectedEnumName + " added successfully"
  116. }); else {
  117. var foundEnum = that.enumDefCollection.fullCollection.find({
  118. guid: model.enumDefs[0].guid
  119. });
  120. foundEnum && foundEnum.set(model.enumDefs[0]), Utils.notifySuccess({
  121. content: "Enumeration " + selectedEnumName + " updated successfully"
  122. });
  123. }
  124. that.enumDefCollection.fetch({
  125. reset: !0
  126. }), that.options.onUpdateEnum && that.options.onUpdateEnum(), that.ui.enumCancleBtn.attr("disabled", "true");
  127. },
  128. silent: !0,
  129. reset: !0,
  130. complete: function(model, status) {
  131. that.emumTypeSelectDisplay(), that.ui.enumOkBtn.hideButtonLoader(), that.ui.enumSelector.removeAttr("disabled"),
  132. that.ui.valueSelector.removeAttr("disabled"), that.options.closeModal && that.options.closeModal();
  133. }
  134. };
  135. $.extend(apiObj, {
  136. contentType: "application/json",
  137. dataType: "json",
  138. data: JSON.stringify(this.json)
  139. }), isPostCallEnum ? this.businessMetadataDefCollection.constructor.nonCrudOperation.call(this, UrlLinks.typedefsUrl().defs, "POST", apiObj) : isPutCall ? this.businessMetadataDefCollection.constructor.nonCrudOperation.call(this, UrlLinks.typedefsUrl().defs, "PUT", apiObj) : (Utils.notifySuccess({
  140. content: "No updated values"
  141. }), that.ui.enumOkBtn.hideButtonLoader(), that.ui.enumSelector.removeAttr("disabled"),
  142. that.ui.valueSelector.removeAttr("disabled"), that.options.closeModal && that.options.closeModal());
  143. }
  144. }
  145. });
  146. });