TermRelationAttributeLayoutView.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. define([ "require", "backbone", "hbs!tmpl/glossary/TermRelationAttributeLayoutView_tmpl", "hbs!tmpl/glossary/TermRelationAttributeTable_tmpl", "utils/Enums", "utils/Utils", "utils/UrlLinks", "modules/Modal" ], function(require, Backbone, TermRelationAttributeLayoutViewTmpl, TermRelationAttributeTableTmpl, Enums, Utils, UrlLinks, Modal) {
  2. var TermRelationAttributeTable = Backbone.Marionette.LayoutView.extend({
  3. _viewName: "TermRelationAttributeTable",
  4. template: TermRelationAttributeTableTmpl,
  5. templateHelpers: function() {
  6. return {
  7. attributeValue: this.data[this.selectedTermAttribute],
  8. selectedTermAttribute: this.selectedTermAttribute,
  9. editMode: this.editMode,
  10. attributes: Enums.termRelationAttributeList[this.selectedTermAttribute]
  11. };
  12. },
  13. ui: {
  14. deleteAttribute: '[data-id="deleteAttribute"]',
  15. attributeUpdate: '[data-id="attributeUpdate"]'
  16. },
  17. events: function() {
  18. var events = {};
  19. return events["click " + this.ui.deleteAttribute] = "onModalDeleteAttribute", events["change " + this.ui.attributeUpdate] = "onAttributeUpdate",
  20. events;
  21. },
  22. initialize: function(options) {
  23. _.extend(this, _.pick(options, "glossaryCollection", "data", "callback", "selectedTermAttribute", "onDeleteAttribute", "editMode"));
  24. var that = this;
  25. this.updateObj = $.extend(!0, {}, this.data), this.modal = new Modal({
  26. title: (this.editMode ? "Edit attributes" : "Attributes") + " of " + this.selectedTermAttribute,
  27. content: this,
  28. okText: this.editMode ? "Update" : "ok",
  29. allowCancel: !!this.editMode,
  30. okCloses: !0,
  31. width: "80%"
  32. }), this.modal.open(), this.modal.on("closeModal", function() {
  33. that.modal.trigger("cancel");
  34. }), this.modal.on("ok", function() {
  35. that.editMode && that.updateAttributes();
  36. }), this.bindEvents();
  37. },
  38. bindEvents: function() {
  39. this.listenTo(this.glossaryCollection, "data:updated", function(data) {
  40. this.data = data, this.render();
  41. }, this);
  42. },
  43. onRender: function() {},
  44. onModalDeleteAttribute: function(e) {
  45. this.onDeleteAttribute(e);
  46. },
  47. onAttributeUpdate: function(e) {
  48. var $el = $(e.currentTarget), termGuid = $el.data("termguid"), name = $el.data("name");
  49. _.find(this.updateObj[this.selectedTermAttribute], function(obj) {
  50. obj.termGuid == termGuid && (obj[name] = $el.val());
  51. });
  52. },
  53. updateAttributes: function() {
  54. var that = this, model = new this.glossaryCollection.model(), ajaxOptions = {
  55. success: function(rModel, response) {
  56. Utils.notifySuccess({
  57. content: "Attributes updated successfully"
  58. }), that.callback && that.callback();
  59. }
  60. };
  61. model.createEditTerm(_.extend(ajaxOptions, {
  62. data: JSON.stringify(this.updateObj)
  63. }, {
  64. guid: this.updateObj.guid
  65. }));
  66. }
  67. }), TermRelationAttributeLayoutView = Backbone.Marionette.LayoutView.extend({
  68. _viewName: "TermRelationAttributeLayoutView",
  69. template: TermRelationAttributeLayoutViewTmpl,
  70. templateHelpers: function() {
  71. return {
  72. attributeList: Enums.termRelationAttributeList
  73. };
  74. },
  75. regions: {},
  76. ui: {
  77. showAttribute: '[data-id="showAttribute"]',
  78. addTermRelation: '[data-id="addTermRelation"]',
  79. termAttributeTable: '[data-id="termAttributeTable"]',
  80. deleteAttribute: '[data-id="deleteAttribute"]'
  81. },
  82. events: function() {
  83. var events = {};
  84. return events["click " + this.ui.addTermRelation] = "onAddTermRelation", events["click " + this.ui.deleteAttribute] = "onDeleteAttribute",
  85. events["click " + this.ui.showAttribute] = "onShowAttribute", events;
  86. },
  87. initialize: function(options) {
  88. _.extend(this, _.pick(options, "glossaryCollection", "data", "fetchCollection"));
  89. },
  90. bindEvents: function() {},
  91. onRender: function() {
  92. this.renderTermAttributeTable();
  93. },
  94. onShowAttribute: function(e) {
  95. var that = this, attributename = $(e.currentTarget).data("attributename");
  96. new TermRelationAttributeTable({
  97. data: that.data,
  98. editMode: "edit" == $(e.currentTarget).data("mode"),
  99. selectedTermAttribute: attributename,
  100. callback: function() {
  101. that.fetchCollection && that.fetchCollection();
  102. },
  103. onDeleteAttribute: that.onDeleteAttribute.bind(that),
  104. glossaryCollection: that.glossaryCollection
  105. });
  106. },
  107. onAddTermRelation: function(e) {
  108. var that = this, attributename = $(e.currentTarget).data("attributename");
  109. require([ "views/glossary/AssignTermLayoutView" ], function(AssignTermLayoutView) {
  110. new AssignTermLayoutView({
  111. isAttributeRelationView: !0,
  112. termData: that.data,
  113. selectedTermAttribute: attributename,
  114. callback: function() {
  115. that.fetchCollection && that.fetchCollection();
  116. },
  117. glossaryCollection: that.glossaryCollection
  118. });
  119. });
  120. },
  121. onDeleteAttribute: function(e) {
  122. e.stopPropagation();
  123. var that = this, notifyObj = {
  124. modal: !0,
  125. text: "Are you sure you want to remove term association",
  126. ok: function(argument) {
  127. var model = new that.glossaryCollection.model(), selectedGuid = $(e.currentTarget).data("termguid"), attributename = $(e.currentTarget).data("attributename"), ajaxOptions = {
  128. success: function(rModel, response) {
  129. Utils.notifySuccess({
  130. content: "Association removed successfully "
  131. }), that.fetchCollection && that.fetchCollection();
  132. }
  133. }, data = _.clone(that.data);
  134. data[attributename] = _.reject(data[attributename], function(obj) {
  135. return obj.termGuid == selectedGuid;
  136. }), model.removeTermFromAttributes(_.extend(ajaxOptions, {
  137. data: JSON.stringify(data)
  138. }, {
  139. guid: that.data.guid
  140. }));
  141. },
  142. cancel: function(argument) {}
  143. };
  144. Utils.notifyConfirm(notifyObj);
  145. },
  146. renderTermAttributeTable: function(e, options) {
  147. var that = this;
  148. this.ui.termAttributeTable.html(TermRelationAttributeTableTmpl({
  149. data: this.data,
  150. attributes: Enums.termRelationAttributeList,
  151. relationTypeTable: !0,
  152. getTerms: function(key) {
  153. var terms = _.map(that.data[key], function(obj) {
  154. var name = _.escape(obj.displayText);
  155. return '<span data-guid="' + obj.termGuid + '" class="btn btn-action btn-sm btn-icon btn-blue" data-id="termClick"><span>' + name + '</span><i class="fa fa-close" data-id="deleteAttribute" data-attributename="' + key + '" data-termguid="' + obj.termGuid + '" data-type="term" title="Remove Term"></i></span>';
  156. }).join(""), attributeButtons = "";
  157. return terms.length && (attributeButtons = '<div class="btn-inline"><button type="button" title="View Attribute" class="btn btn-action btn-sm" data-attributename="' + key + '" data-id="showAttribute"><i class="fa fa-eye fa-fw" aria-hidden="true"></i></button><button type="button" title="Edit Attribute" class="btn btn-action btn-sm" data-attributename="' + key + '" data-mode="edit" data-id="showAttribute"><i class="fa fa-pencil fa-fw" aria-hidden="true"></i></button></div>'),
  158. "<td>" + terms + '<button type="button" data-attributename="' + key + '" class="btn btn-action btn-sm" data-id="addTermRelation"><i class="fa fa-plus"></i></button></td><td>' + attributeButtons + "</td>";
  159. }
  160. }));
  161. }
  162. });
  163. return TermRelationAttributeLayoutView;
  164. });