mutations.js 4.76 KB
Newer Older
wanli's avatar
wanli committed
1 2 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
export default {
  /**
   * Set selected disk
   * @param state
   * @param disk
   */
  setDisk(state, disk) {
    state.selectedDisk = disk;
  },

  /**
   * Set directories and files in selected directory
   * @param state
   * @param data
   */
  setDirectoryContent(state, data) {
    state.directories = data.directories;
    state.files = data.files;
  },

  /**
   * Set selected directory
   * @param state
   * @param directory
   */
  setSelectedDirectory(state, directory) {
    state.selectedDirectory = directory;
  },

  /**
   * Set selected items
   * @param state
   * @param type (directories, files)
   * @param path
   */
  setSelected(state, { type, path }) {
    state.selected[type].push(path);
  },

  /**
   * Remove item from array
   * @param state
   * @param arrayIndex
   */
  removeSelected(state, { type, path }) {
    const itemIndex = state.selected[type].indexOf(path);
    if (itemIndex !== -1) state.selected[type].splice(itemIndex, 1);
  },

  /**
   * Change selected item
   * @param state
   * @param type
   * @param path
   */
  changeSelected(state, { type, path }) {
    state.selected.directories = [];
    state.selected.files = [];
    state.selected[type].push(path);
  },

  /**
   * Reset selected items array
   * @param state
   */
  resetSelected(state) {
    state.selected.directories = [];
    state.selected.files = [];
  },

  /**
   * Add new file
   * @param state
   * @param newFile
   */
  addNewFile(state, newFile) {
    state.files.push(newFile);
  },

  /**
   * Update file
   * @param state
   * @param file
   */
  updateFile(state, file) {
86 87
    const itemIndex = state.files.findIndex((el) => file && el.basename === file.basename);

wanli's avatar
wanli committed
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    if (itemIndex !== -1) state.files[itemIndex] = file;
  },

  /**
   * Add new directory
   * @param state
   * @param newDirectory
   */
  addNewDirectory(state, newDirectory) {
    state.directories.push(newDirectory);
  },

  /**
   * Change history pointer (back)
   * @param state
   */
  pointerBack(state) {
    state.historyPointer -= 1;
  },

  /**
   * Change history pointer (forward)
   * @param state
   */
  pointerForward(state) {
    state.historyPointer += 1;
  },

  /**
   * Add to history
   * @param state
   * @param path
   */
  addToHistory(state, path) {
    if (state.historyPointer < state.history.length - 1) {
      // erase next elements in the history
      state.history.splice(state.historyPointer + 1, Number.MAX_VALUE);
    }
    // add new path
    state.history.push(path);
    // change history pointer
    state.historyPointer += 1;
  },

  /**
   * Reset history
   * @param state
   */
  resetHistory(state) {
    state.history = [null];
    state.historyPointer = 0;
  },

  /**
   * Set view type
   * Grid or Table
   * @param state
   * @param type
   */
  setView(state, type) {
    state.viewType = type;
  },

  /**
   * Set sort settings - field name
   * @param state
   * @param field
   */
  setSortField(state, field) {
    state.sort.field = field;
  },

  /**
   * Set sort settings - direction
   * @param state
   * @param direction
   */
  setSortDirection(state, direction) {
    state.sort.direction = direction;
  },

  /**
   * Reset sort settings
   * @param state
   */
  resetSortSettings(state) {
    state.sort.field = 'name';
    state.sort.direction = 'up';
  },

  /**
   * Sort table by name field
   * @param state
   */
  sortByName(state) {
    if (state.sort.direction === 'up') {
      state.directories.sort((a, b) => a.basename.localeCompare(b.basename));
      state.files.sort((a, b) => a.basename.localeCompare(b.basename));
    } else {
      state.directories.sort((a, b) => b.basename.localeCompare(a.basename));
      state.files.sort((a, b) => b.basename.localeCompare(a.basename));
    }
  },

  /**
   * Sort by file size
   * @param state
   */
  sortBySize(state) {
    state.directories.sort((a, b) => a.basename.localeCompare(b.basename));

    if (state.sort.direction === 'up') {
      state.files.sort((a, b) => a.size - b.size);
    } else {
      state.files.sort((a, b) => b.size - a.size);
    }
  },

  /**
   * Sort by file extension
   * @param state
   */
  sortByType(state) {
    state.directories.sort((a, b) => a.basename.localeCompare(b.basename));

    if (state.sort.direction === 'up') {
      state.files.sort((a, b) => a.extension.localeCompare(b.extension));
    } else {
      state.files.sort((a, b) => b.extension.localeCompare(a.extension));
    }
  },

  /**
   * Sort by date
   * @param state
   */
  sortByDate(state) {
    if (state.sort.direction === 'up') {
      state.directories.sort((a, b) => a.timestamp - b.timestamp);
      state.files.sort((a, b) => a.timestamp - b.timestamp);
    } else {
      state.directories.sort((a, b) => b.timestamp - a.timestamp);
      state.files.sort((a, b) => b.timestamp - a.timestamp);
    }
  },
};