rename-modal.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. import Ember from 'ember';
  19. import OperationModal from '../mixins/operation-modal';
  20. export default Ember.Component.extend(OperationModal, {
  21. closeOnEscape: true,
  22. hasError: false,
  23. errorMessage: '',
  24. isUpdating: false,
  25. renameService: Ember.inject.service('file-rename'),
  26. fileSelectionService: Ember.inject.service('files-selection'),
  27. selectedFiles: Ember.computed.alias('fileSelectionService.files'),
  28. selected: Ember.computed('selectedFiles', function() {
  29. return this.get('selectedFiles').objectAt(0);
  30. }),
  31. selectionName: Ember.computed.oneWay('selected.name'),
  32. hasErrorReset: Ember.observer('selectionName', 'selected.name', function() {
  33. if (this.get('hasError') && (this.get('selectionName') !== this.get('selected.name'))) {
  34. this.set('hasError', false);
  35. }
  36. }),
  37. actions: {
  38. didOpenModal: function() {
  39. this.set('selectionName', this.get('selected.name'));
  40. // This was required as the DOM may not be visible due to animation in bootstrap modal
  41. Ember.run.later(() => {
  42. this.$('input').focus();
  43. }, 500);
  44. },
  45. rename: function() {
  46. if(Ember.isBlank(this.get('selectionName'))) {
  47. return false;
  48. }
  49. if(this.get('selected.name') === this.get('selectionName')) {
  50. this.set('hasError', true);
  51. this.set('errorMessage', 'Name should be different');
  52. return false;
  53. }
  54. this.set('isUpdating', true);
  55. this.get('renameService').rename(this.get('selected.path'), this.get('selectionName'))
  56. .then((response) => {
  57. this.set('isUpdating', false);
  58. this.send('close');
  59. this.sendAction('refreshAction');
  60. }, (error) => {
  61. this.set('isUpdating', false);
  62. if(error.retry) {
  63. this.set('hasError', true);
  64. this.set('errorMessage', error.message);
  65. } else {
  66. this.send('close');
  67. }
  68. });
  69. }
  70. }
  71. });