EntityUserDefineView.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. define([ "require", "backbone", "hbs!tmpl/entity/EntityUserDefineView_tmpl", "models/VEntity", "utils/Utils", "utils/Enums", "utils/Messages", "utils/CommonViewFunction" ], function(require, Backbone, EntityUserDefineView_tmpl, VEntity, Utils, Enums, Messages, CommonViewFunction) {
  2. "use strict";
  3. return Backbone.Marionette.LayoutView.extend({
  4. _viewName: "EntityUserDefineView",
  5. template: EntityUserDefineView_tmpl,
  6. templateHelpers: function() {
  7. return {
  8. customAttibutes: this.customAttibutes,
  9. readOnlyEntity: this.readOnlyEntity,
  10. swapItem: this.swapItem,
  11. saveAttrItems: this.saveAttrItems,
  12. divId_1: this.dynamicId_1,
  13. divId_2: this.dynamicId_2
  14. };
  15. },
  16. ui: {
  17. addAttr: "[data-id='addAttr']",
  18. saveAttrItems: "[data-id='saveAttrItems']",
  19. cancel: "[data-id='cancel']",
  20. addItem: "[data-id='addItem']",
  21. userDefineHeader: ".userDefinePanel .panel-heading"
  22. },
  23. events: function() {
  24. var events = {};
  25. return events["click " + this.ui.addAttr] = "onAddAttrClick", events["click " + this.ui.addItem] = "onAddAttrClick",
  26. events["click " + this.ui.saveAttrItems] = "onEditAttrClick", events["click " + this.ui.cancel] = "onCancelClick",
  27. events["click " + this.ui.userDefineHeader] = "onHeaderClick", events;
  28. },
  29. initialize: function(options) {
  30. _.extend(this, _.pick(options, "entity", "customFilter", "renderAuditTableLayoutView")),
  31. this.userDefineAttr = this.entity && this.entity.customAttributes || [], this.initialCall = !1,
  32. this.swapItem = !1, this.saveAttrItems = !1, this.readOnlyEntity = void 0 === this.customFilter ? Enums.entityStateReadOnly[this.entity.status] : this.customFilter,
  33. this.entityModel = new VEntity(this.entity), this.dynamicId_1 = CommonViewFunction.getRandomIdAndAnchor(),
  34. this.dynamicId_2 = CommonViewFunction.getRandomIdAndAnchor(), this.generateTableFields();
  35. },
  36. onRender: function() {},
  37. renderEntityUserDefinedItems: function() {
  38. var that = this;
  39. require([ "views/entity/EntityUserDefineItemView" ], function(EntityUserDefineItemView) {
  40. that.itemView = new EntityUserDefineItemView({
  41. items: that.customAttibutes,
  42. updateButtonState: that.updateButtonState.bind(that)
  43. }), that.REntityUserDefinedItemView.show(that.itemView);
  44. });
  45. },
  46. bindEvents: {},
  47. addChildRegion: function() {
  48. this.addRegions({
  49. REntityUserDefinedItemView: "#r_entityUserDefinedItemView"
  50. }), this.renderEntityUserDefinedItems();
  51. },
  52. onHeaderClick: function() {
  53. var that = this;
  54. $(".userDefinePanel").on("hidden.bs.collapse", function() {
  55. that.swapItem = !1, that.saveAttrItems = !1, that.initialCall = !1, that.render(),
  56. that.customAttibutes.length > 0 && ($(".userDefinePanel").find(that.ui.userDefineHeader.attr("href")).removeClass("in"),
  57. that.ui.userDefineHeader.addClass("collapsed").attr("aria-expanded", !1));
  58. });
  59. },
  60. onAddAttrClick: function() {
  61. this.swapItem = !this.swapItem, void 0 === this.customFilter ? this.saveAttrItems = this.swapItem === !0 : this.saveAttrItems = !1,
  62. this.initialCall = !0, this.render(), this.swapItem === !0 && this.addChildRegion();
  63. },
  64. generateTableFields: function() {
  65. var that = this;
  66. this.customAttibutes = [], _.each(Object.keys(that.userDefineAttr), function(key, i) {
  67. that.customAttibutes.push({
  68. key: key,
  69. value: that.userDefineAttr[key]
  70. });
  71. });
  72. },
  73. onEditAttrClick: function() {
  74. this.initialCall = !(this.customAttibutes.length > 0), this.setAttributeModal(this.itemView);
  75. },
  76. updateButtonState: function() {
  77. return 0 === this.customAttibutes.length && (this.swapItem = !1, this.saveAttrItems = !1,
  78. this.render(), void 0);
  79. },
  80. onCancelClick: function() {
  81. this.initialCall = !1, this.swapItem = !1, this.saveAttrItems = !1, this.render();
  82. },
  83. structureAttributes: function(list) {
  84. var obj = {};
  85. return list.map(function(o) {
  86. obj[o.key] = o.value;
  87. }), obj;
  88. },
  89. saveAttributes: function(list) {
  90. var that = this, entityJson = that.entityModel.toJSON(), properties = that.structureAttributes(list);
  91. entityJson.customAttributes = properties;
  92. var payload = {
  93. entity: entityJson
  94. };
  95. that.entityModel.createOreditEntity({
  96. data: JSON.stringify(payload),
  97. type: "POST",
  98. success: function() {
  99. var msg = that.initialCall ? "addSuccessMessage" : "editSuccessMessage", caption = "一个或多个用户定义属性";
  100. that.customAttibutes = list, 0 === list.length && (msg = "removeSuccessMessage",
  101. caption = "一个或多个用户定义属性"), Utils.notifySuccess({
  102. content: caption + Messages.getAbbreviationMsg(!0, msg)
  103. }), that.swapItem = !1, that.saveAttrItems = !1, that.render(), that.renderAuditTableLayoutView && that.renderAuditTableLayoutView();
  104. },
  105. error: function(e) {
  106. that.initialCall = !1, Utils.notifySuccess({
  107. content: e.message
  108. }), that.ui.saveAttrItems.attr("disabled", !1);
  109. },
  110. complete: function() {
  111. that.ui.saveAttrItems.attr("disabled", !1), that.initialCall = !1;
  112. }
  113. });
  114. },
  115. setAttributeModal: function(itemView) {
  116. var self = this;
  117. this.ui.saveAttrItems.attr("disabled", !0);
  118. var list = itemView.$el.find("[data-type]"), dataList = [];
  119. Array.prototype.push.apply(dataList, itemView.items);
  120. var field = CommonViewFunction.CheckDuplicateAndEmptyInput(list, dataList);
  121. field.validation && !field.hasDuplicate ? self.saveAttributes(itemView.items) : this.ui.saveAttrItems.attr("disabled", !1);
  122. }
  123. });
  124. });