ImportLayoutView.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. define([ "require", "backbone", "hbs!tmpl/import/ImportLayoutView_tmpl", "modules/Modal", "utils/CommonViewFunction", "utils/Utils", "utils/UrlLinks", "dropzone" ], function(require, Backbone, ImportLayoutViewTmpl, Modal, CommonViewFunction, Utils, UrlLinks, dropzone) {
  2. var ImportLayoutView = Backbone.Marionette.LayoutView.extend({
  3. _viewName: "ImportLayoutView",
  4. template: ImportLayoutViewTmpl,
  5. regions: {},
  6. ui: {
  7. errorContainer: "[data-id='errorContainer']",
  8. importGlossary: "[data-id='importGlossary']",
  9. errorDetails: "[data-id='errorDetails']"
  10. },
  11. events: function() {
  12. var events = {};
  13. return events;
  14. },
  15. initialize: function(options) {
  16. _.extend(this, _.pick(options, "callback", "isGlossary"));
  17. var that = this;
  18. this.modal = new Modal({
  19. title: this.isGlossary ? "导入术语" : "导入业务元数据",
  20. content: this,
  21. cancelText: "取消",
  22. okText: "上传",
  23. allowCancel: !0,
  24. okCloses: !1,
  25. mainClass: "dropzone-modal"
  26. }).open(), this.modal.$el.find("button.ok").attr("disabled", !0), this.modal.on("ok", function(e) {
  27. that.dropzone.processQueue();
  28. }), this.modal.on("closeModal", function() {
  29. that.modal.trigger("cancel");
  30. }), this.bindEvents();
  31. },
  32. bindEvents: function() {
  33. var that = this;
  34. $("body").on("click", ".importBackBtn", function() {
  35. var modalTitle = that.isGlossary ? "导入术语" : "导入业务元数据";
  36. that.toggleErrorAndDropZoneView({
  37. title: modalTitle,
  38. isErrorView: !1
  39. });
  40. });
  41. },
  42. onRender: function() {
  43. var that = this;
  44. this.dropzone = null;
  45. var updateButton = function(files) {
  46. var buttonEl = that.modal.$el.find("button.ok");
  47. 0 === files.length ? buttonEl.attr("disabled", !0) : buttonEl.attr("disabled", !1);
  48. }, headers = {};
  49. headers[CommonViewFunction.restCsrfCustomHeader] = CommonViewFunction.restCsrfValue || '""',
  50. this.ui.importGlossary.dropzone({
  51. url: that.isGlossary ? UrlLinks.glossaryImportUrl() : UrlLinks.businessMetadataImportUrl(),
  52. clickable: !0,
  53. acceptedFiles: ".csv,.xls,.xlsx",
  54. autoProcessQueue: !1,
  55. maxFiles: 1,
  56. addRemoveLinks: !0,
  57. timeout: 0,
  58. init: function() {
  59. that.dropzone = this, this.on("addedfile", function(file) {
  60. updateButton(this.files);
  61. }), this.on("removedfile", function(file) {
  62. updateButton(this.files);
  63. });
  64. },
  65. maxfilesexceeded: function(file) {
  66. this.removeAllFiles(), this.addFile(file);
  67. },
  68. success: function(file, response, responseObj) {
  69. var success = !0;
  70. if (response.failedImportInfoList && response.failedImportInfoList.length) {
  71. var errorStr = "";
  72. if (success = !1, that.ui.errorDetails.empty(), Utils.defaultErrorHandler(null, file.xhr, {
  73. defaultErrorMessage: response.failedImportInfoList[0].remarks
  74. }), response.failedImportInfoList.length > 1) {
  75. var modalTitle = '<div class="back-button importBackBtn" title="Back to import file"><i class="fa fa-angle-left "></i> </div> <div class="modal-name">Error Details</div>';
  76. _.each(response.failedImportInfoList, function(err_obj) {
  77. errorStr += "<li>" + _.escape(err_obj.remarks) + "</li>";
  78. }), that.ui.errorDetails.append(errorStr), that.toggleErrorAndDropZoneView({
  79. title: modalTitle,
  80. isErrorView: !0
  81. });
  82. }
  83. }
  84. success && (that.modal.trigger("cancel"), Utils.notifySuccess({
  85. content: "File: " + file.name + " imported successfully"
  86. })), that.callback && (response.successImportInfoList && response.successImportInfoList.length > 0 || success) && that.callback();
  87. },
  88. error: function(file, response, responseObj) {
  89. Utils.defaultErrorHandler(null, responseObj, {
  90. defaultErrorMessage: response.errorMessage || response
  91. }), that.modal.$el.find("button.ok").attr("disabled", !1);
  92. },
  93. dictDefaultMessage: "Drop files here or click to upload(.csv, .xls, .xlsx).",
  94. headers: headers
  95. });
  96. },
  97. toggleErrorAndDropZoneView: function(options) {
  98. var that = this;
  99. options && (that.modal.$el.find(".modal-title").html(options.title), options.isErrorView ? (that.ui.importGlossary.addClass("hide"),
  100. that.ui.errorContainer.removeClass("hide"), that.modal.$el.find("button.ok").addClass("hide")) : (that.ui.importGlossary.removeClass("hide"),
  101. that.ui.errorContainer.addClass("hide"), that.modal.$el.find("button.ok").removeClass("hide"),
  102. that.ui.errorDetails.empty()));
  103. }
  104. });
  105. return ImportLayoutView;
  106. });