TopicUploader.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: admin
  5. * Copyright IBM Corp. 2015, 2017
  6. * US Government Users Restricted Rights -Use, duplication or disclosure
  7. * restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['underscore', 'bi/admin/common/Uploader', 'bi/admin/nls/StringResource'], function (_, Uploader, StringResource) {
  10. 'use strict'; //NOSONAR: meant to be strict
  11. var TopicUploader = Uploader.extend({
  12. /**
  13. *@options.glassContext - glassContext
  14. *@options.$el - Container
  15. *@options.ajax() - ajax function to upload data
  16. *@options.ajaxOptions
  17. */
  18. init: function init(options) {
  19. TopicUploader.inherited('init', this, arguments);
  20. _.extend(this, options);
  21. this.includeFileName = true;
  22. },
  23. _getInput: function _getInput() {
  24. var inputOptions = this._getBaseInputOptions();
  25. if (this.fileType && this.fileType === 'json') {
  26. inputOptions.accept = '.json';
  27. } else {
  28. inputOptions.accept = '.txt';
  29. }
  30. return $('<input>', inputOptions);
  31. },
  32. _isValidFileName: function _isValidFileName(fileName) {
  33. if (this.fileType === 'json') {
  34. return /^[a-z0-9_ \-]{1,200}\.json$/i.test(fileName);
  35. } else {
  36. return /^[a-z0-9_ \-]{1,200}\.txt$/i.test(fileName);
  37. }
  38. },
  39. // NB - the message returned should match regexp above
  40. _getInvalidFileNameMessage: function _getInvalidFileNameMessage() {
  41. return StringResource.get('msgFileNameTopicUpload');
  42. },
  43. _getFileName: function _getFileName(fileName, e) {
  44. var topicName;
  45. try {
  46. var fileStr = String.fromCharCode.apply(null, new Uint8Array(e.target.result));
  47. topicName = JSON.parse(fileStr).topicName;
  48. if (_.isUndefined(topicName) || _.isEmpty(topicName)) {
  49. topicName = fileName.substr(0, fileName.lastIndexOf('.'));
  50. }
  51. } catch (error) {
  52. return fileName.substr(0, fileName.lastIndexOf('.'));
  53. }
  54. return topicName;
  55. }
  56. });
  57. return TopicUploader;
  58. });