ContentFilePickerBody.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: Cognos Analytics
  5. * Copyright IBM Corp. 2016, 2017
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['underscore', 'bi/commons/ui/View', 'bi/admin/nls/StringResource', 'bacontentnav/ui/widgets/FileSelector', 'bi/commons/utils/BidiUtil', 'bacontentnav/utils/ContentStoreObject'], function (_, View, StringResource, FileSelector, BidiUtil, ContentStoreObject) {
  9. 'use strict'; //NOSONAR
  10. var ContentFilePickerBody = View.extend({
  11. init: function init(options) {
  12. ContentFilePickerBody.inherited('init', this, arguments);
  13. _.extend(this, options);
  14. this._placeholder = StringResource.get('selectFolder');
  15. },
  16. render: function render() {
  17. var objectInfo;
  18. var $container = $('<div></div>');
  19. this.$el.append($container);
  20. this._fileSelector = new FileSelector({
  21. $el: $container,
  22. placeholder: this._placeholder,
  23. title: this._placeholder,
  24. openDialogOptions: {
  25. 'glassContext': this.glassContext,
  26. 'typesToOpen': ['folder'],
  27. 'multiSelect': false,
  28. 'onOpenCallback': this._setContentPath.bind(this),
  29. 'dialogTitle': this._placeholder,
  30. 'primaryBtnText': StringResource.get('select'),
  31. 'teamContentOnly': this.teamContentOnly
  32. }
  33. });
  34. return this.glassContext.getSvc('.Content').then(function (contentSvc) {
  35. return new Promise(function (resolve) {
  36. if (this.controller._isCurrentValueDefined()) {
  37. var requestEndpoint = this.controller._getRequestEndpoint(contentSvc);
  38. var options = {
  39. dataType: 'json',
  40. data: {
  41. fields: 'defaultName,ancestors'
  42. }
  43. };
  44. this.glassContext.services.ajax.get(requestEndpoint, options).then(function (result) {
  45. objectInfo = result.data[0];
  46. resolve();
  47. }.bind(this), function () {
  48. resolve();
  49. });
  50. } else {
  51. resolve();
  52. }
  53. }.bind(this));
  54. }.bind(this)).then(function () {
  55. return this._fileSelector.render();
  56. }.bind(this)).then(function () {
  57. this._setFileSelectorLabel(objectInfo, true);
  58. return this.$el;
  59. }.bind(this));
  60. },
  61. _setFileSelectorLabel: function _setFileSelectorLabel(objectInfo, appendDefaultName) {
  62. if (objectInfo) {
  63. var path = ContentStoreObject.getLocation(objectInfo, appendDefaultName);
  64. this._fileSelector.setValue(BidiUtil.userPreferredTextDir === '' ? path : BidiUtil.enforceTextDirectionForLocation(path));
  65. } else if (this.controller._isCurrentValueDefined()) {
  66. this._fileSelector.setValue(StringResource.get('unavailable'));
  67. }
  68. },
  69. _setContentPath: function _setContentPath(selectedObject) {
  70. this.controller._setContentPath(selectedObject);
  71. this._setFileSelectorLabel(selectedObject[0], false);
  72. if (_.isFunction(this.onChange)) {
  73. this.onChange(this.isValueChanged());
  74. }
  75. },
  76. reset: function reset() {
  77. this.controller.setDefaultValue();
  78. this._fileSelector.reset();
  79. },
  80. getCurrentValue: function getCurrentValue() {
  81. return this.controller.currentValue;
  82. },
  83. isValueChanged: function isValueChanged() {
  84. return this.controller.isValueChanged();
  85. }
  86. });
  87. return ContentFilePickerBody;
  88. });