SaveModalLayoutView.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. define([ "require", "backbone", "hbs!tmpl/search/save/SaveModalLayoutView_tmpl", "utils/Utils", "modules/Modal", "utils/UrlLinks", "platform", "models/VSearch", "collection/VSearchList", "utils/CommonViewFunction", "utils/Messages" ], function(require, Backbone, SaveModalLayoutViewTmpl, Utils, Modal, UrlLinks, platform, VSearch, VSearchList, CommonViewFunction, Messages) {
  2. var SaveModalLayoutView = Backbone.Marionette.LayoutView.extend({
  3. _viewName: "SaveModalLayoutView",
  4. template: SaveModalLayoutViewTmpl,
  5. regions: {},
  6. ui: {
  7. saveAsName: "[data-id='saveAsName']"
  8. },
  9. templateHelpers: function() {
  10. return {
  11. selectedModel: this.selectedModel ? this.selectedModel.toJSON() : null,
  12. rename: this.rename
  13. };
  14. },
  15. events: function() {
  16. var events = {};
  17. return events;
  18. },
  19. initialize: function(options) {
  20. function getModelName(model) {
  21. if (model.get("name")) return model.get("name").toLowerCase();
  22. }
  23. var that = this;
  24. _.extend(this, _.pick(options, "rename", "selectedModel", "collection", "getValue", "isBasic", "saveObj")),
  25. this.model = new VSearch(), this.saveSearchCollection = new VSearchList(), this.saveSearchCollection.url = UrlLinks.saveSearchApiUrl(),
  26. this.saveSearchCollection.fullCollection.comparator = function(model) {
  27. return getModelName(model);
  28. }, this.saveObj ? this.onCreateButton() : (this.modal = modal = new Modal({
  29. titleHtml: !0,
  30. title: "<span>" + (this.selectedModel && this.rename ? "Rename" : "Save") + (this.isBasic ? " Basic" : " Advanced") + " Custom Filter</span>",
  31. content: this,
  32. cancelText: "Cancel",
  33. okCloses: !1,
  34. okText: this.selectedModel ? "Update" : "Save",
  35. allowCancel: !0
  36. }), this.modal.open(), modal.$el.find("button.ok").attr("disabled", "true"), modal.on("ok", function() {
  37. modal.$el.find("button.ok").attr("disabled", "true"), that.onCreateButton();
  38. }), modal.on("closeModal", function() {
  39. modal.trigger("cancel");
  40. }));
  41. },
  42. hideLoader: function() {
  43. this.$el.find("form").removeClass("hide"), this.$el.find(".fontLoader").removeClass("show");
  44. },
  45. onRender: function() {
  46. if (1 == this.rename) this.hideLoader(); else {
  47. var that = this;
  48. this.saveSearchCollection.fetch({
  49. success: function(collection, data) {
  50. that.saveSearchCollection.fullCollection.reset(_.where(data, {
  51. searchType: that.isBasic ? "BASIC" : "ADVANCED"
  52. }));
  53. var options = "";
  54. that.saveSearchCollection.fullCollection.each(function(model) {
  55. options += '<option value="' + model.get("name") + '">' + model.get("name") + "</option>";
  56. }), that.ui.saveAsName.append(options), that.ui.saveAsName.val(""), that.ui.saveAsName.select2({
  57. placeholder: "Enter filter name ",
  58. allowClear: !1,
  59. tags: !0,
  60. multiple: !1,
  61. templateResult: function(state) {
  62. return state.id ? state.element ? $("<span><span class='option-title-light'>Update:</span> <strong>" + _.escape(state.text) + "</strong></span>") : $("<span><span class='option-title-light'>New:</span> <strong>" + _.escape(state.text) + "</strong></span>") : state.text;
  63. }
  64. }).on("change", function() {
  65. var val = that.ui.saveAsName.val();
  66. val.length ? (that.selectedModel = that.saveSearchCollection.fullCollection.find({
  67. name: val
  68. }), that.selectedModel ? that.modal.$el.find("button.ok").text("Save As") : that.modal.$el.find("button.ok").text("Save"),
  69. that.modal.$el.find("button.ok").removeAttr("disabled")) : (that.modal.$el.find("button.ok").attr("disabled", "true"),
  70. that.selectedModel = null);
  71. });
  72. },
  73. silent: !0
  74. }), this.hideLoader();
  75. }
  76. },
  77. onCreateButton: function() {
  78. var that = this, obj = {
  79. name: this.ui.saveAsName.val() || null,
  80. value: this.getValue()
  81. };
  82. this.saveObj && _.extend(obj, this.saveObj);
  83. var saveObj = CommonViewFunction.generateObjectForSaveSearchApi(obj);
  84. if (this.selectedModel) {
  85. var selectedModel = this.selectedModel.toJSON();
  86. this.rename !== !0 && _.extend(selectedModel.searchParameters, saveObj.searchParameters),
  87. selectedModel.name = obj.name, saveObj = selectedModel;
  88. } else this.isBasic ? saveObj.searchType = "BASIC" : saveObj.searchType = "ADVANCED";
  89. this.model.urlRoot = UrlLinks.saveSearchApiUrl(), this.model.save(saveObj, {
  90. type: saveObj.guid ? "PUT" : "POST",
  91. success: function(model, data) {
  92. if (that.collection) if (saveObj.guid) {
  93. var collectionRef = that.collection.find({
  94. guid: data.guid
  95. });
  96. collectionRef && collectionRef.set(data), Utils.notifySuccess({
  97. content: obj.name + Messages.getAbbreviationMsg(!1, "editSuccessMessage")
  98. });
  99. } else that.collection.add(data), Utils.notifySuccess({
  100. content: obj.name + Messages.getAbbreviationMsg(!1, "addSuccessMessage")
  101. });
  102. that.callback && that.callback();
  103. }
  104. }), this.modal && this.modal.trigger("cancel");
  105. }
  106. });
  107. return SaveModalLayoutView;
  108. });