Skip to content

Commit

Permalink
fix(files): fix favorites legacy to vue handling and sorting
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed May 4, 2023
1 parent 3951c7b commit 11b19d7
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 30 deletions.
12 changes: 12 additions & 0 deletions apps/files/js/tagsplugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,25 @@
tags = tags.split('|');
tags = _.without(tags, '');
var isFavorite = tags.indexOf(OC.TAG_FAVORITE) >= 0;

// Fake Node object for vue compatibility
const node = {
type: 'folder',
path: (dir + '/' + fileName).replace(/\/\/+/g, '/'),
root: '/files/' + OC.getCurrentUser().uid
}

if (isFavorite) {
// remove tag from list
tags = _.without(tags, OC.TAG_FAVORITE);
removeFavoriteFromList(dir + '/' + fileName);
// vue compatibility
window._nc_event_bus.emit('files:favorites:removed', node)
} else {
tags.push(OC.TAG_FAVORITE);
addFavoriteToList(dir + '/' + fileName);
// vue compatibility
window._nc_event_bus.emit('files:favorites:added', node)
}

// pre-toggle the star
Expand Down
97 changes: 67 additions & 30 deletions apps/files/src/views/favorites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
import type NavigationService from '../services/Navigation.ts'
import type { Navigation } from '../services/Navigation.ts'
import { translate as t } from '@nextcloud/l10n'
import { getLanguage, translate as t } from '@nextcloud/l10n'
import StarSvg from '@mdi/svg/svg/star.svg?raw'
import FolderSvg from '@mdi/svg/svg/folder.svg?raw'

Expand All @@ -33,7 +33,32 @@ import { subscribe } from '@nextcloud/event-bus'
import { Node, FileType } from '@nextcloud/files'
import logger from '../logger'

const favoriteFolders = loadState('files', 'favoriteFolders', [])
const generateFolderView = function(folder: string, index = 0): Navigation {
return {
id: generateIdFromPath(folder),
name: basename(folder),

icon: FolderSvg,
order: index,
params: {
dir: folder,
view: 'favorites',
},

parent: 'favorites',

columns: [],

getContents,
} as Navigation
}

const generateIdFromPath = function(path: string): string {
return `favorite-${hashCode(path)}`
}

const favoriteFolders = loadState('files', 'favoriteFolders', []) as string[]
const favoriteFoldersViews = favoriteFolders.map((folder, index) => generateFolderView(folder, index))

export default () => {
const Navigation = window.OCP.Files.Navigation as NavigationService
Expand All @@ -50,9 +75,7 @@ export default () => {
getContents,
} as Navigation)

favoriteFolders.forEach((folder) => {
Navigation.register(generateFolderView(folder))
})
favoriteFoldersViews.forEach(view => Navigation.register(view))

/**
* Update favourites navigation when a new folder is added
Expand All @@ -68,7 +91,7 @@ export default () => {
return
}

Navigation.register(generateFolderView(node.path))
addPathToFavorites(node.path)
})

/**
Expand All @@ -85,30 +108,44 @@ export default () => {
return
}

Navigation.remove(generateIdFromPath(node.path))
removePathFromFavorites(node.path)
})
}

const generateFolderView = function(folder: string): Navigation {
return {
id: generateIdFromPath(folder),
name: basename(folder),

icon: FolderSvg,
order: -100, // always first
params: {
dir: folder,
view: 'favorites',
},

parent: 'favorites',

columns: [],

getContents,
} as Navigation
}

const generateIdFromPath = function(path: string): string {
return `favorite-${hashCode(path)}`
/**
* Sort the favorites paths array and
* update the order property of the existing views
*/
const updateAndSortViews = function() {
favoriteFolders.sort((a, b) => a.localeCompare(b, getLanguage(), { ignorePunctuation: true }))
favoriteFolders.forEach((folder, index) => {
const view = favoriteFoldersViews.find(view => view.id === generateIdFromPath(folder))
if (view) {
view.order = index
}
})
}

// Add a folder to the favorites paths array and update the views
const addPathToFavorites = function(path: string) {
const view = generateFolderView(path)
// Update arrays
favoriteFolders.push(path)
favoriteFoldersViews.push(view)
// Update and sort views
updateAndSortViews()
Navigation.register(view)
console.debug(favoriteFolders, favoriteFoldersViews)
}

// Remove a folder from the favorites paths array and update the views
const removePathFromFavorites = function(path: string) {
const id = generateIdFromPath(path)
const index = favoriteFolders.findIndex(f => f === path)
// Update arrays
favoriteFolders.splice(index, 1)
favoriteFoldersViews.splice(index, 1)
Navigation.remove(id)
updateAndSortViews()
console.debug(favoriteFolders, favoriteFoldersViews)
}
}

0 comments on commit 11b19d7

Please sign in to comment.