actions.js 3.34 KB
Newer Older
wanli's avatar
wanli committed
1
/* eslint-disable object-curly-newline */
wanli's avatar
wanli committed
2
import GET from '@/utils/get';
wanli's avatar
wanli committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

export default {
  /**
   * Load files and folders for the selected directory
   * @param state
   * @param commit
   * @param dispatch
   * @param rootState
   * @param path
   * @param history
   * @returns {Promise}
   */
  selectDirectory({ state, commit, dispatch, rootState }, { path, history }) {
    // reset content
    commit('setDirectoryContent', { directories: [], files: [] });

    // get content for the selected directory
    return GET.content(state.selectedDisk, path).then((response) => {
      if (response.data.result.status === 'success') {
        commit('resetSelected');
        commit('resetSortSettings');
        commit('setDirectoryContent', response.data);
        commit('setSelectedDirectory', path);

        if (history) commit('addToHistory', path);

        // if directories tree is shown, not main directory and directory have subdirectories
        if (
          rootState.fm.settings.windowsConfig === 2
          && path
          && response.data.directories.length
        ) {
          dispatch('fm/tree/showSubdirectories', path, { root: true });
        }
      }
    });
  },

  /**
   * Refresh content in the selected directory
   * @param state
   * @param commit
   * @param dispatch
   */
  refreshDirectory({ state, commit, dispatch }) {
    GET.content(state.selectedDisk, state.selectedDirectory).then((response) => {
      commit('resetSelected');
      commit('resetSortSettings');
      commit('resetHistory');

      // add to history selected directory
      if (state.selectedDirectory) commit('addToHistory', state.selectedDirectory);

      if (response.data.result.status === 'success') {
        commit('setDirectoryContent', response.data);
      } else if (response.data.result.status === 'danger') {
        // If directory not found try to load main directory
        commit('setSelectedDirectory', null);
        dispatch('refreshDirectory');
      }
    });
  },

  /**
   * History Back
   * @param state
   * @param commit
   * @param dispatch
   */
  historyBack({ state, commit, dispatch }) {
    dispatch('selectDirectory', {
      path: state.history[state.historyPointer - 1],
      history: false,
    });
    commit('pointerBack');
  },

  /**
   * History Forward
   * @param state
   * @param commit
   * @param dispatch
   */
  historyForward({ state, commit, dispatch }) {
    dispatch('selectDirectory', {
      path: state.history[state.historyPointer + 1],
      history: false,
    });
    commit('pointerForward');
  },

  /**
   * Sort data by field
   * @param context
   * @param field
   * @param direction
   */
  sortBy({ state, commit }, { field, direction }) {
    if (state.sort.field === field && !direction) {
      commit('setSortDirection', state.sort.direction === 'up' ? 'down' : 'up');
    } else if (direction) {
      commit('setSortDirection', direction);
      commit('setSortField', field);
    } else {
      commit('setSortDirection', 'up');
      commit('setSortField', field);
    }
    // sort by field type
    switch (field) {
      case 'name':
        commit('sortByName');
        break;
      case 'size':
        commit('sortBySize');
        break;
      case 'type':
        commit('sortByType');
        break;
      case 'date':
        commit('sortByDate');
        break;
      default:
        break;
    }
  },
};