AssignTermLayoutView.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. define([ "require", "backbone", "hbs!tmpl/glossary/AssignTermLayoutView_tmpl", "utils/Utils", "utils/Enums", "utils/Messages", "utils/UrlLinks", "modules/Modal", "jquery-steps" ], function(require, Backbone, AssignTermLayoutViewTmpl, Utils, Enums, Messages, UrlLinks, Modal) {
  2. var AssignTermLayoutView = Backbone.Marionette.LayoutView.extend({
  3. _viewName: "AssignTermLayoutView",
  4. template: AssignTermLayoutViewTmpl,
  5. templateHelpers: function() {
  6. return {
  7. isAttributeRelationView: this.isAttributeRelationView,
  8. selectedTermAttributeList: Enums.termRelationAttributeList[this.selectedTermAttribute]
  9. };
  10. },
  11. regions: {
  12. RGlossaryTree: "#r_glossaryTree"
  13. },
  14. ui: {
  15. wizard: '[data-id="wizard"]'
  16. },
  17. events: function() {
  18. var events = {};
  19. return events;
  20. },
  21. initialize: function(options) {
  22. _.extend(this, _.pick(options, "glossaryCollection", "guid", "callback", "hideLoader", "isCategoryView", "categoryData", "isTermView", "termData", "isAttributeRelationView", "selectedTermAttribute", "associatedTerms", "multiple"));
  23. var that = this;
  24. this.options = options, this.isCategoryView || this.isTermView || this.isAttributeRelationView || (this.isEntityView = !0),
  25. this.glossary = {
  26. selectedItem: {}
  27. };
  28. var title = "";
  29. title = this.isCategoryView || this.isEntityView ? "指定术语给 " + (this.isCategoryView ? "类别" : "实体") : this.isAttributeRelationView ? "指定术语给 " + this.selectedTermAttribute : "指定类别给术语",
  30. this.modal = new Modal({
  31. title: title,
  32. content: this,
  33. cancelText: "取消",
  34. okText: "关联",
  35. allowCancel: !0,
  36. showFooter: !this.isAttributeRelationView,
  37. mainClass: "wizard-modal",
  38. okCloses: !1
  39. }), this.modal.open(), this.modal.$el.find("button.ok").attr("disabled", !0), this.modal.on("closeModal", function() {
  40. that.modal.trigger("cancel"), that.assignTermError && that.hideLoader && that.hideLoader(),
  41. options.onModalClose && options.onModalClose();
  42. }), this.modal.on("ok", function() {
  43. that.assignTerm();
  44. }), this.bindEvents();
  45. },
  46. bindEvents: function() {
  47. this.listenTo(this.glossaryCollection, "node_selected", function(skip) {
  48. this.modal.$el.find("button.ok").attr("disabled", !1);
  49. }, this);
  50. },
  51. onRender: function() {
  52. this.renderGlossaryTree();
  53. var that = this;
  54. this.isAttributeRelationView && this.ui.wizard.steps({
  55. headerTag: "h3",
  56. bodyTag: "section",
  57. transitionEffect: "slideLeft",
  58. autoFocus: !0,
  59. enableCancelButton: !0,
  60. transitionEffect: $.fn.steps.transitionEffect.none,
  61. transitionEffectSpeed: 200,
  62. labels: {
  63. cancel: "Cancel",
  64. finish: "Assign",
  65. next: "Next",
  66. previous: "Previous",
  67. loading: "Loading ..."
  68. },
  69. onStepChanging: function(event, currentIndex, newIndex) {
  70. var isMatch = "GlossaryTerm" == that.glossary.selectedItem.type;
  71. return isMatch || Utils.notifyWarn({
  72. content: "请选择关联的术语"
  73. }), isMatch;
  74. },
  75. onFinished: function(event, currentIndex) {
  76. var $assignBtn = $(this).find('a[href="#finish"]');
  77. $assignBtn.attr("disabled") || ($assignBtn.attr("disabled", !0).showButtonLoader(),
  78. $assignBtn.parent().attr("aria-disabled", "true").addClass("disabled"), that.assignTerm());
  79. },
  80. onCanceled: function(event) {
  81. that.modal.trigger("cancel");
  82. }
  83. });
  84. },
  85. assignTerm: function() {
  86. this.assignTermError = !1;
  87. var that = this, data = [], termAttributeFormData = [], selectedItem = this.glossary.selectedItem, selectedGuid = selectedItem.guid, termName = selectedItem.text, ajaxOptions = {
  88. success: function(rModel, response) {
  89. Utils.notifySuccess({
  90. content: (that.isCategoryView || that.isEntityView || that.isAttributeRelationView ? "Term" : "Category") + " is associated successfully "
  91. }), that.modal.trigger("closeModal"), that.callback && that.callback();
  92. },
  93. cust_error: function() {
  94. var $assignBtn = that.$el.find('a[href="#finish"]');
  95. $assignBtn.removeAttr("disabled").hideButtonLoader(), $assignBtn.parent().attr("aria-disabled", "false").removeClass("disabled"),
  96. that.assignTermError = !0;
  97. }
  98. }, model = new this.glossaryCollection.model();
  99. if (this.isCategoryView) data = $.extend(!0, {}, this.categoryData), data.terms ? data.terms.push({
  100. termGuid: selectedGuid
  101. }) : data.terms = [ {
  102. termGuid: selectedGuid
  103. } ], model.assignTermToCategory(_.extend(ajaxOptions, {
  104. data: JSON.stringify(data),
  105. guid: data.guid
  106. })); else if (this.isTermView) data = $.extend(!0, {}, this.termData), data.categories ? data.categories.push({
  107. categoryGuid: selectedGuid
  108. }) : data.categories = [ {
  109. categoryGuid: selectedGuid
  110. } ], model.assignCategoryToTerm(_.extend(ajaxOptions, {
  111. data: JSON.stringify(data),
  112. guid: data.guid
  113. })); else if (this.isAttributeRelationView) termAttributeFormData = this.$('[data-id="termAttributeForm"]').serializeArray().reduce(function(obj, item) {
  114. return obj[item.name] = item.value, obj;
  115. }, {}), data = $.extend(!0, {}, this.termData), data[this.selectedTermAttribute] ? data[this.selectedTermAttribute].push(_.extend({
  116. termGuid: selectedGuid
  117. }, termAttributeFormData)) : data[this.selectedTermAttribute] = [ _.extend({
  118. termGuid: selectedGuid
  119. }, termAttributeFormData) ], model.assignTermToAttributes(_.extend(ajaxOptions, {
  120. data: JSON.stringify(data),
  121. guid: data.guid
  122. })); else {
  123. var deletedEntity = [], skipEntity = [];
  124. if (this.multiple ? (_.each(that.multiple, function(entity, i) {
  125. var name = Utils.getName(entity.model);
  126. Enums.entityStateReadOnly[entity.model.status] ? deletedEntity.push(name) : _.indexOf(entity.model.meaningNames || _.pluck(entity.model.meanings, "displayText"), termName) === -1 ? data.push({
  127. guid: entity.model.guid
  128. }) : skipEntity.push(name);
  129. }), deletedEntity.length && Utils.notifyError({
  130. html: !0,
  131. content: "<b>" + deletedEntity.join(", ") + "</b> " + (1 === deletedEntity.length ? "entity " : "entities ") + Messages.assignTermDeletedEntity
  132. })) : data.push({
  133. guid: that.guid
  134. }), skipEntity.length) {
  135. var text = "<b>所选" +that.multiple.length + " 个实体中已有 "+skipEntity.length + "</b> 个实体已经与术语 <b>" + termName + "</b> 关联,是否将该术语与其他实体关联?", removeCancelButton = !1;
  136. skipEntity.length + deletedEntity.length === that.multiple.length && (text = (skipEntity.length > 1 ? "All selected" : "Selected") + " 实体已经与术语 <b>" + termName + "</b> 关联",
  137. removeCancelButton = !0);
  138. var notifyObj = {
  139. text: text,
  140. modal: !0,
  141. ok: function(argument) {
  142. data.length && model.assignTermToEntity(selectedGuid, _.extend(ajaxOptions, {
  143. data: JSON.stringify(data)
  144. }));
  145. },
  146. cancel: function(argument) {}
  147. };
  148. removeCancelButton && (notifyObj.confirm = {
  149. confirm: !0,
  150. buttons: [ {
  151. text: "Ok",
  152. addClass: "btn-atlas btn-md",
  153. click: function(notice) {
  154. notice.remove();
  155. }
  156. }, null ]
  157. }), Utils.notifyConfirm(notifyObj);
  158. } else data.length && model.assignTermToEntity(selectedGuid, _.extend(ajaxOptions, {
  159. data: JSON.stringify(data)
  160. }));
  161. }
  162. },
  163. renderGlossaryTree: function() {
  164. var that = this;
  165. require([ "views/glossary/GlossaryLayoutView" ], function(GlossaryLayoutView) {
  166. that.RGlossaryTree.show(new GlossaryLayoutView(_.extend({
  167. isAssignTermView: that.isCategoryView,
  168. isAssignCategoryView: that.isTermView,
  169. isAssignEntityView: that.isEntityView,
  170. isAssignAttributeRelationView: that.isAttributeRelationView,
  171. glossary: that.glossary,
  172. associatedTerms: that.associatedTerms
  173. }, that.options)));
  174. });
  175. }
  176. });
  177. return AssignTermLayoutView;
  178. });