EntityLabelDefineView.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. define([ "require", "backbone", "hbs!tmpl/entity/EntityLabelDefineView_tmpl", "models/VEntity", "utils/Utils", "utils/Messages", "utils/Enums", "utils/UrlLinks", "utils/CommonViewFunction" ], function(require, Backbone, EntityLabelDefineView_tmpl, VEntity, Utils, Messages, Enums, UrlLinks, CommonViewFunction) {
  2. "use strict";
  3. return Backbone.Marionette.LayoutView.extend({
  4. _viewName: "REntityLabelDefineView",
  5. template: EntityLabelDefineView_tmpl,
  6. templateHelpers: function() {
  7. return {
  8. swapItem: this.swapItem,
  9. labels: this.labels,
  10. saveLabels: this.saveLabels,
  11. readOnlyEntity: this.readOnlyEntity,
  12. div_1: this.dynamicId_1,
  13. div_2: this.dynamicId_2
  14. };
  15. },
  16. ui: {
  17. addLabelOptions: "[data-id='addLabelOptions']",
  18. addLabels: "[data-id='addLabels']",
  19. saveLabels: "[data-id='saveLabels']",
  20. cancel: "[data-id='cancel']",
  21. labelsHeader: ".labelsPanel .panel-heading"
  22. },
  23. events: function() {
  24. var events = {};
  25. return events["change " + this.ui.addLabelOptions] = "onChangeLabelChange", events["click " + this.ui.addLabels] = "handleBtnClick",
  26. events["click " + this.ui.saveLabels] = "saveUserDefinedLabels", events["click " + this.ui.cancel] = "onCancelClick",
  27. events["click " + this.ui.labelsHeader] = "onHeaderClick", events;
  28. },
  29. initialize: function(options) {
  30. _.extend(this, _.pick(options, "entity", "customFilter", "renderAuditTableLayoutView")),
  31. this.swapItem = !1, this.saveLabels = !1, this.readOnlyEntity = void 0 === this.customFilter ? Enums.entityStateReadOnly[this.entity.status] : this.customFilter,
  32. this.entityModel = new VEntity(this.entity), this.labels = this.entity.labels || [],
  33. this.dynamicId_1 = CommonViewFunction.getRandomIdAndAnchor(), this.dynamicId_2 = CommonViewFunction.getRandomIdAndAnchor();
  34. },
  35. onRender: function() {
  36. this.populateLabelOptions();
  37. },
  38. bindEvents: function() {},
  39. onHeaderClick: function() {
  40. var that = this;
  41. $(".labelsPanel").on("hidden.bs.collapse", function() {
  42. that.labels = that.entityModel.get("labels") || [], that.swapItem = !1, that.saveLabels = !1,
  43. that.render(), that.labels.length > 0 && ($(".labelsPanel").find(that.ui.labelsHeader.attr("href")).removeClass("in"),
  44. that.ui.labelsHeader.addClass("collapsed").attr("aria-expanded", !1));
  45. });
  46. },
  47. populateLabelOptions: function() {
  48. var str = this.labels.map(function(label) {
  49. return "<option selected > " + _.escape(label) + " </option>";
  50. });
  51. this.ui.addLabelOptions.html(str);
  52. var getLabelData = function(data, selectedData) {
  53. if (data.suggestions.length) return _.map(data.suggestions, function(name, index) {
  54. var findValue = _.find(selectedData, {
  55. id: name
  56. });
  57. return findValue ? findValue : {
  58. id: name,
  59. text: name
  60. };
  61. });
  62. var findValue = _.find(selectedData, {
  63. id: data.prefixString
  64. });
  65. return findValue ? [ findValue ] : [];
  66. };
  67. this.ui.addLabelOptions.select2({
  68. placeholder: "请选择标签",
  69. allowClear: !1,
  70. tags: !0,
  71. multiple: !0,
  72. ajax: {
  73. url: UrlLinks.searchApiUrl("suggestions"),
  74. dataType: "json",
  75. delay: 250,
  76. data: function(params) {
  77. return {
  78. prefixString: params.term,
  79. fieldName: "__labels"
  80. };
  81. },
  82. processResults: function(data, params) {
  83. return {
  84. results: getLabelData(data, this.$element.select2("data"))
  85. };
  86. },
  87. cache: !0
  88. },
  89. createTag: function(data) {
  90. var found = _.find(this.$element.select2("data"), {
  91. id: data.term
  92. });
  93. if (!found) return {
  94. id: data.term,
  95. text: data.term
  96. };
  97. },
  98. templateResult: this.formatResultSearch
  99. });
  100. },
  101. formatResultSearch: function(state) {
  102. return state.id ? state.element || "" === state.text.trim() ? void 0 : $("<span>Add<strong> '" + _.escape(state.text) + "'</strong></span>") : state.text;
  103. },
  104. onChangeLabelChange: function() {
  105. this.labels = this.ui.addLabelOptions.val();
  106. },
  107. handleBtnClick: function() {
  108. this.swapItem = !this.swapItem, void 0 === this.customFilter ? this.saveLabels = this.swapItem === !0 : this.saveLabels = !1,
  109. this.render();
  110. },
  111. onCancelClick: function() {
  112. this.labels = this.entityModel.get("labels") || [], this.swapItem = !1, this.saveLabels = !1,
  113. this.render();
  114. },
  115. saveUserDefinedLabels: function() {
  116. var that = this, entityJson = that.entityModel.toJSON();
  117. if (entityJson.labels && 0 !== entityJson.labels.length || 0 !== this.labels.length) {
  118. var payload = this.labels;
  119. that.entityModel.saveEntityLabels(entityJson.guid, {
  120. data: JSON.stringify(payload),
  121. type: "POST",
  122. success: function() {
  123. var msg = void 0 === entityJson.labels ? "addSuccessMessage" : "editSuccessMessage", caption = "一个或多个标签";
  124. 0 === payload.length ? (msg = "removeSuccessMessage", caption = "一个或多个标签",
  125. that.entityModel.unset("labels")) : that.entityModel.set("labels", payload), Utils.notifySuccess({
  126. content: caption + Messages.getAbbreviationMsg(!0, msg)
  127. }), that.swapItem = !1, that.saveLabels = !1, that.render(), that.renderAuditTableLayoutView && that.renderAuditTableLayoutView();
  128. },
  129. error: function(e) {
  130. that.ui.saveLabels && that.ui.saveLabels.length > 0 && that.ui.saveLabels[0].setAttribute("disabled", !1),
  131. Utils.notifySuccess({
  132. content: e.message
  133. });
  134. },
  135. complete: function() {
  136. that.ui.saveLabels && that.ui.saveLabels.length > 0 && that.ui.saveLabels[0].setAttribute("disabled", !1),
  137. that.render();
  138. }
  139. });
  140. }
  141. }
  142. });
  143. });