EntityBusinessMetaDataView.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. define([ "require", "backbone", "hbs!tmpl/entity/EntityBusinessMetaDataView_tmpl", "views/entity/EntityBusinessMetaDataItemView", "models/VEntity", "utils/Utils", "utils/Messages", "utils/Enums", "utils/CommonViewFunction", "moment", "utils/Globals" ], function(require, Backbone, EntityBusinessMetaDataView_tmpl, EntityBusinessMetaDataItemView, VEntity, Utils, Messages, Enums, CommonViewFunction, moment, Globals) {
  2. "use strict";
  3. return Backbone.Marionette.CompositeView.extend({
  4. _viewName: "EntityBusinessMetaDataView",
  5. template: EntityBusinessMetaDataView_tmpl,
  6. childView: EntityBusinessMetaDataItemView,
  7. childViewContainer: "[data-id='itemView']",
  8. childViewOptions: function() {
  9. return {
  10. editMode: this.editMode,
  11. entity: this.entity,
  12. businessMetadataCollection: this.businessMetadataCollection,
  13. enumDefCollection: this.enumDefCollection
  14. };
  15. },
  16. templateHelpers: function() {
  17. return {
  18. readOnlyEntity: this.readOnlyEntity
  19. };
  20. },
  21. ui: {
  22. addItem: "[data-id='addItem']",
  23. addBusinessMetadata: "[data-id='addBusinessMetadata']",
  24. saveBusinessMetadata: "[data-id='saveBusinessMetadata']",
  25. businessMetadataTree: "[data-id='businessMetadataTree']",
  26. cancel: "[data-id='cancel']",
  27. businessMetadataHeader: ".businessMetaDataPanel .panel-heading.main-parent"
  28. },
  29. events: function() {
  30. var events = {};
  31. return events["click " + this.ui.addItem] = "createNameElement", events["click " + this.ui.addBusinessMetadata] = "onAddBusinessMetadata",
  32. events["click " + this.ui.saveBusinessMetadata] = "onSaveBusinessMetadata", events["click " + this.ui.cancel] = "onCancel",
  33. events["click " + this.ui.businessMetadataHeader] = "onHeaderClick", events;
  34. },
  35. initialize: function(options) {
  36. var that = this;
  37. _.extend(this, _.pick(options, "entity", "businessMetadataCollection", "enumDefCollection", "guid", "fetchCollection")),
  38. this.editMode = !1, this.readOnlyEntity = Enums.entityStateReadOnly[this.entity.status],
  39. this.$("editBox").hide(), this.actualCollection = new Backbone.Collection(_.map(this.entity.businessAttributes, function(val, key) {
  40. var foundBusinessMetadata = that.businessMetadataCollection[key];
  41. return foundBusinessMetadata && _.each(val, function(aVal, aKey) {
  42. var foundAttr = _.find(foundBusinessMetadata, function(o) {
  43. return o.name === aKey;
  44. });
  45. foundAttr && (val[aKey] = {
  46. value: aVal,
  47. typeName: foundAttr.typeName
  48. });
  49. }), _.extend({}, val, {
  50. __internal_UI_businessMetadataName: key
  51. });
  52. })), this.collection = new Backbone.Collection(), this.entityModel = new VEntity();
  53. },
  54. onHeaderClick: function() {
  55. var that = this;
  56. $(".businessMetaDataPanel").on("hidden.bs.collapse", function(e) {
  57. that.ui.cancel.hide(), that.ui.saveBusinessMetadata.hide(), that.ui.addBusinessMetadata.show(),
  58. that.editMode = !1, that.ui.businessMetadataTree.show(), that.$(".editBox").hide(),
  59. that.updateToActualData(), that.collection && 0 === that.collection.length ? that.ui.addBusinessMetadata.text("Add") : that.ui.addBusinessMetadata.text("Edit");
  60. });
  61. },
  62. updateToActualData: function(options) {
  63. var silent = options && options.silent || !1;
  64. this.collection.reset($.extend(!0, [], this.actualCollection.toJSON()), {
  65. silent: silent
  66. });
  67. },
  68. onAddBusinessMetadata: function() {
  69. this.ui.addBusinessMetadata.hide(), this.ui.saveBusinessMetadata.show(), this.ui.cancel.show(),
  70. this.editMode = !0, this.ui.businessMetadataTree.hide(), this.$(".editBox").show(),
  71. this.updateToActualData({
  72. silent: !0
  73. }), 0 === this.collection.length ? this.createNameElement() : this.collection.trigger("reset"),
  74. this.panelOpenClose();
  75. },
  76. onCancel: function() {
  77. this.ui.cancel.hide(), this.ui.saveBusinessMetadata.hide(), this.ui.addBusinessMetadata.show(),
  78. this.editMode = !1, this.ui.businessMetadataTree.show(), this.$(".editBox").hide(),
  79. this.updateToActualData(), this.panelOpenClose();
  80. },
  81. panelOpenClose: function() {
  82. var collection = this.editMode ? this.collection : this.actualCollection, headingEl = this.$el.find(".panel-heading.main-parent");
  83. collection && 0 === collection.length ? this.ui.addBusinessMetadata.text("Add") : (this.ui.addBusinessMetadata.text("Edit"),
  84. headingEl.hasClass("collapsed") && headingEl.click());
  85. },
  86. validate: function() {
  87. var validation = !0;
  88. return this.$el.find('.custom-col-1[data-id="value"] [data-key]').each(function(el) {
  89. var val = $(this).val(), elIsSelect2 = $(this).hasClass("select2-hidden-accessible");
  90. _.isString(val) && (val = val.trim()), _.isEmpty(val) ? (validation && (validation = !1),
  91. elIsSelect2 ? $(this).siblings(".select2").find(".select2-selection").attr("style", "border-color : red !important") : $(this).css("borderColor", "red")) : elIsSelect2 ? $(this).siblings(".select2").find(".select2-selection").attr("style", "") : $(this).css("borderColor", "");
  92. }), validation;
  93. },
  94. onSaveBusinessMetadata: function() {
  95. var that = this;
  96. if (this.validate()) {
  97. var nData = this.generateData();
  98. return 0 === this.actualCollection.length && _.isEmpty(nData) ? void this.onCancel() : void this.entityModel.saveBusinessMetadataEntity(this.guid, {
  99. data: JSON.stringify(nData),
  100. type: "POST",
  101. success: function(data) {
  102. Utils.notifySuccess({
  103. content: "One or more Business Metadada attributes" + Messages.getAbbreviationMsg(!0, "editSuccessMessage")
  104. }), that.entity.businessAttributes = data, that.ui.businessMetadataTree.html(""),
  105. that.editMode = !1, that.fetchCollection(), that.onCancel();
  106. },
  107. complete: function(model, response) {}
  108. });
  109. }
  110. },
  111. generateData: function() {
  112. var finalObj = {};
  113. return this.collection.forEach(function(model) {
  114. if (!model.has("addAttrButton")) {
  115. var businessMetadataName = model.get("__internal_UI_businessMetadataName"), modelObj = model.toJSON();
  116. _.each(modelObj, function(o, k) {
  117. return "isNew" === k || "__internal_UI_businessMetadataName" === k ? void delete modelObj[k] : void (_.isObject(o) && void 0 !== o.value && (modelObj[k] = o.value));
  118. }), void 0 !== businessMetadataName && (finalObj[businessMetadataName] ? finalObj[businessMetadataName] = _.extend(finalObj[businessMetadataName], modelObj) : finalObj[businessMetadataName] = modelObj);
  119. }
  120. }), _.isEmpty(finalObj) && this.actualCollection.forEach(function(model) {
  121. var businessMetadataName = model.get("__internal_UI_businessMetadataName");
  122. businessMetadataName && (finalObj[businessMetadataName] = {});
  123. }), finalObj;
  124. },
  125. createNameElement: function(options) {
  126. var modelObj = {
  127. isNew: !0
  128. };
  129. this.collection.unshift(modelObj);
  130. },
  131. renderBusinessMetadata: function() {
  132. var that = this, li = "";
  133. this.actualCollection.forEach(function(obj) {
  134. var attrLi = "";
  135. _.each(obj.attributes, function(val, key) {
  136. if ("__internal_UI_businessMetadataName" !== key) {
  137. var newVal = val;
  138. _.isObject(val) && !_.isUndefinedNull(val.value) && (newVal = val.value, newVal.length > 0 && val.typeName.indexOf("date") > -1 && (newVal = _.map(newVal, function(dates) {
  139. return Utils.formatDate({
  140. date: dates,
  141. zone: !1,
  142. dateFormat: Globals.dateFormat
  143. });
  144. })), "date" === val.typeName && (newVal = Utils.formatDate({
  145. date: newVal,
  146. zone: !1,
  147. dateFormat: Globals.dateFormat
  148. }))), attrLi += "<tr><td class='business-metadata-detail-attr-key'>" + _.escape(key) + " (" + _.escape(val.typeName) + ")</td><td>" + _.escape(newVal) + "</td></tr>";
  149. }
  150. }), li += that.associateAttributePanel(obj, attrLi);
  151. });
  152. var html = li;
  153. "" === html && this.readOnlyEntity === !1 && (html = '<div class="col-md-12"> No business metadata have been created yet. To add a business metadata, click <a href="javascript:void(0)" data-id="addBusinessMetadata">here</a></div>'),
  154. this.ui.businessMetadataTree.html(html);
  155. },
  156. associateAttributePanel: function(obj, tableBody) {
  157. return '<div class="panel panel-default custom-panel expand_collapse_panel-icon no-border business-metadata-detail-attr"><div class="panel-heading" data-toggle="collapse" href="#' + _.escape(obj.get("__internal_UI_businessMetadataName")) + '" aria-expanded="true" style="width: 90%;"><h4 class="panel-title"> <a>' + _.escape(obj.get("__internal_UI_businessMetadataName")) + '</a></h4><div class="btn-group pull-left"> <button type="button" title="Collapse"><i class="ec-icon fa"></i></button></div></div><div id="' + _.escape(obj.get("__internal_UI_businessMetadataName")) + '" class="panel-collapse collapse in"><div class="panel-body"><table class="table bold-key">' + tableBody + "</table></div></div></div>";
  158. },
  159. onRender: function() {
  160. this.actualCollection && this.actualCollection.length && (this.$el.find(".panel-heading.main-parent").removeClass("collapsed").attr("aria-expanded", "true"),
  161. this.$el.find("#businessMetadataCollapse").addClass("in").removeAttr("style"), this.ui.addBusinessMetadata.text("Edit")),
  162. this.renderBusinessMetadata();
  163. }
  164. });
  165. });