import { RuleContext } from '@alfresco/adf-extensions' ; import * as navigation from './navigation.rules' ; import * as repository from './repository.rules' ; import { isAdmin } from './user.rules' ; export interface AcaRuleContext extends RuleContext { withCredentials : boolean ; } param context export function canCopyNode ( context ) { return [ hasSelection ( context ) , navigation . isNotTrashcan ( context ) , navigation . isNotLibraries ( context ) ] . every ( Boolean ) ; } /**
 * Checks if user can mark selected nodes as **Favorite**.
 * JSON ref: `app.selection.canAddFavorite`
 */ export function canAddFavorite ( context ) { if ( ! context . selection . isEmpty ) { if ( navigation . isFavorites ( context ) || navigation . isLibraries ( context ) || navigation . isTrashcan ( context ) ) { return false ; } return context . selection . nodes . some ( ( node ) => ! node . entry . isFavorite ) ; } return false ; } /**
 * Checks if user can un-mark selected nodes as **Favorite**.
 * JSON ref: `app.selection.canRemoveFavorite`
 */ export function canRemoveFavorite ( context ) { if ( ! context . selection . isEmpty && ! navigation . isTrashcan ( context ) ) { if ( navigation . isFavorites ( context ) ) { return true ; } return context . selection . nodes . every ( ( node ) => node . entry . isFavorite ) ; } return false ; } /**
 * Checks if user can share selected file.
 * JSON ref: `app.selection.file.canShare`
 */ export function canShareFile ( context ) { return [ context . selection . file , navigation . isNotTrashcan ( context ) , repository . hasQuickShareEnabled ( context ) , ! isShared ( context ) ] . every ( Boolean ) ; } /**
 * Checks if user can perform "Join" or "Cancel Join Request" on a library.
 * JSON ref: `canToggleJoinLibrary`
 */ export function canToggleJoinLibrary ( context ) { return ( [ hasLibrarySelected ( context ) , ! isPrivateLibrary ( context ) , hasNoLibraryRole ( context ) ] . every ( Boolean ) || [ hasLibrarySelected ( context ) , isPrivateLibrary ( context ) , hasNoLibraryRole ( context ) , isAdmin ( context ) ] . every ( Boolean ) ) ; } param context export function canEditFolder ( context ) { return [ canUpdateSelectedFolder ( context ) , navigation . isNotTrashcan ( context ) ] . every ( Boolean ) ; } /**
 * Checks if the selected file is already shared.
 * JSON ref: `app.selection.file.isShared`
 */ export function isShared ( context ) { if ( navigation . isSharedFiles ( context ) && context . selection . file ) { return true ; } if ( navigation . isNotTrashcan ( context ) && ! context . selection . isEmpty && context . selection . file ) { return ! ! ( context . selection . file . entry && context . selection . file . entry . properties && context . selection . file . entry . properties [ 'qshare:sharedId' ] ) ; } return false ; } /**
 * Checks if user can delete selected nodes.
 * JSON ref: `app.selection.canDelete`
 */ export function canDeleteSelection ( context ) { if ( navigation . isNotTrashcan ( context ) && navigation . isNotLibraries ( context ) && navigation . isNotSearchResults ( context ) && ! context . selection . isEmpty ) { if ( hasLockedFiles ( context ) ) { return false ; } if ( navigation . isFavorites ( context ) ) { return true ; } if ( navigation . isPreview ( context ) ) { return context . permissions . check ( context . selection . nodes , [ 'delete' ] ) ; } if ( navigation . isSharedFiles ( context ) ) { return context . permissions . check ( context . selection . nodes , [ 'delete' ] , { target : 'allowableOperationsOnTarget' } ) ; } return context . permissions . check ( context . selection . nodes , [ 'delete' ] ) ; } return false ; } /**
 * Checks if user can un-share selected nodes.
 * JSON ref: `app.selection.canUnshare`
 */ export function canUnshareNodes ( context ) { if ( ! context . selection . isEmpty ) { return context . permissions . check ( context . selection . nodes , [ 'delete' ] , { target : 'allowableOperationsOnTarget' } ) ; } return false ; } /**
 * Checks if user selected anything.
 * JSON ref: `app.selection.notEmpty`
 */ export function hasSelection ( context ) { return ! context . selection . isEmpty ; } /**
 * Checks if user can create a new folder with current path.
 * JSON ref: `app.navigation.folder.canCreate`
 */ export function canCreateFolder ( context ) { const { currentFolder } = context . navigation ; if ( currentFolder ) { return context . permissions . check ( currentFolder , [ 'create' ] ) ; } return false ; } /**
 * Checks if user can upload content to current folder.
 * JSON ref: `app.navigation.folder.canUpload`
 */ export function canUpload ( context ) { const { currentFolder } = context . navigation ; if ( currentFolder ) { return context . permissions . check ( currentFolder , [ 'create' ] ) ; } return false ; } /**
 * Checks if user can download selected nodes (either files or folders).
 * JSON ref: `app.selection.canDownload`
 */ export function canDownloadSelection ( context ) { if ( ! context . selection . isEmpty && navigation . isNotTrashcan ( context ) ) { return context . selection . nodes . every ( ( node ) => { return node . entry && ( node . entry . isFile || node . entry . isFolder || ! ! node . entry . nodeId ) ; } ) ; } return false ; } /**
 * Checks if user has selected a folder.
 * JSON ref: `app.selection.folder`
 */ export function hasFolderSelected ( context ) { return ! ! context . selection . folder ; } /**
 * Checks if user has selected a library (site).
 * JSON ref: `app.selection.library`
 */ export function hasLibrarySelected ( context ) { return ! ! context . selection . library ; } /**
 * Checks if user has selected a **private** library (site)
 * JSON ref: `app.selection.isPrivateLibrary`
 */ export function isPrivateLibrary ( context ) { const library = context . selection . library ; return library ? ! ! ( library . entry && library . entry . visibility && library . entry . visibility === 'PRIVATE' ) : false ; } /**
 * Checks if the selected library has a **role** property defined.
 * JSON ref: `app.selection.hasLibraryRole`
 */ export function hasLibraryRole ( context ) { const library = context . selection . library ; return library ? ! ! ( library . entry && library . entry . role ) : false ; } /**
 * Checks if the selected library has no **role** property defined.
 * JSON ref: `app.selection.hasNoLibraryRole`
 */ export function hasNoLibraryRole ( context ) { return ! hasLibraryRole ( context ) ; } /**
 * Checks if user has selected a file.
 * JSON ref: `app.selection.file`
 */ export function hasFileSelected ( context ) { return ! ! ( context && context . selection && context . selection . file ) ; } /**
 * Checks if user can update the first selected node.
 * JSON ref: `app.selection.first.canUpdate`
 */ export function canUpdateSelectedNode ( context ) { if ( context . selection && ! context . selection . isEmpty ) { const node = context . selection . first ; if ( node . entry . isFile && hasLockedFiles ( context ) ) { return false ; } return context . permissions . check ( node , [ 'update' ] ) ; } return false ; } export function isMultiselection ( context ) { let isMultiNodeSelected = false ; if ( context . selection && ! context . selection . isEmpty ) { isMultiNodeSelected = context . selection . count > 1 ; } return isMultiNodeSelected ; } /**
 * Checks if user can update the first selected folder.
 * JSON ref: `app.selection.folder.canUpdate`
 */ export function canUpdateSelectedFolder ( context ) { const { folder } = context . selection ; if ( folder ) { return ( navigation . isFavorites ( context ) || context . permissions . check ( folder . entry , [ 'update' ] ) ) ; } return false ; } /**
 * Checks if user has selected a **locked** file node.
 * JSON ref: `app.selection.file.isLocked`
 */ export function hasLockedFiles ( context ) { if ( context && context . selection && context . selection . nodes ) { return context . selection . nodes . some ( ( node ) => { if ( ! node . entry . isFile ) { return false ; } return node . entry . isLocked || ( node . entry . properties && node . entry . properties [ 'cm:lockType' ] === 'READ_ONLY_LOCK' ) ; } ) ; } return false ; } /**
 * Checks if the selected file has **write** or **read-only** locks specified.
 * JSON ref: `app.selection.file.isLocked`
 */ export function isWriteLocked ( context ) { return ! ! ( context && context . selection && context . selection . file && context . selection . file . entry && context . selection . file . entry . properties && ( context . selection . file . entry . properties [ 'cm:lockType' ] === 'WRITE_LOCK' || context . selection . file . entry . properties [ 'cm:lockType' ] === 'READ_ONLY_LOCK' ) ) ; } /**
 * Checks if the selected file has **write** or **read-only** locks specified,
 * and that current user is the owner of the lock.
 * JSON ref: `app.selection.file.isLockOwner`
 */ export function isUserWriteLockOwner ( context ) { return ( isWriteLocked ( context ) && context . selection . file . entry . properties [ 'cm:lockOwner' ] && context . selection . file . entry . properties [ 'cm:lockOwner' ] . id === context . profile . id ) ; } /**
 * Checks if user can lock selected file.
 * JSON ref: `app.selection.file.canLock`
 */ export function canLockFile ( context ) { return ! isWriteLocked ( context ) && canUpdateSelectedNode ( context ) ; } /**
 * Checks if user can unlock selected file.
 * JSON ref: `app.selection.file.canUnlock`
 */ export function canUnlockFile ( context ) { const { file } = context . selection ; return isWriteLocked ( context ) && ( context . permissions . check ( file . entry , [ 'delete' ] ) || isUserWriteLockOwner ( context ) ) ; } /**
 * Checks if user can upload a new version of the file.
 * JSON ref: `app.selection.file.canUploadVersion`
 */ export function canUploadVersion ( context ) { if ( navigation . isFavorites ( context ) || navigation . isSharedFiles ( context ) ) { return hasFileSelected ( context ) ; } return [ hasFileSelected ( context ) , navigation . isNotTrashcan ( context ) , isWriteLocked ( context ) ? isUserWriteLockOwner ( context ) : canUpdateSelectedNode ( context ) ] . every ( Boolean ) ; } param context export function isTrashcanItemSelected ( context ) { return [ navigation . isTrashcan ( context ) , hasSelection ( context ) ] . every ( Boolean ) ; } param context export function canViewFile ( context ) { return [ hasFileSelected ( context ) , navigation . isNotTrashcan ( context ) ] . every ( Boolean ) ; } param context export function canLeaveLibrary ( context ) { return [ hasLibrarySelected ( context ) , hasLibraryRole ( context ) ] . every ( Boolean ) ; } param context export function canToggleSharedLink ( context ) { return [ hasFileSelected ( context ) , [ canShareFile ( context ) , isShared ( context ) ] . some ( Boolean ) ] . every ( Boolean ) ; } param context export function canShowInfoDrawer ( context ) { return [ hasSelection ( context ) , navigation . isNotLibraries ( context ) , navigation . isNotTrashcan ( context ) ] . every ( Boolean ) ; } param context export function canManageFileVersions ( context ) { return [ hasFileSelected ( context ) , navigation . isNotTrashcan ( context ) , ! hasLockedFiles ( context ) ] . every ( Boolean ) ; } param context export function canEditAspects ( context ) { return [ ! isMultiselection ( context ) , canUpdateSelectedNode ( context ) , ! isWriteLocked ( context ) , navigation . isNotTrashcan ( context ) , repository . isMajorVersionAvailable ( context , '7' ) ] . every ( Boolean ) ; } param context export function canManagePermissions ( context ) { return [ canUpdateSelectedNode ( context ) , navigation . isNotTrashcan ( context ) ] . every ( Boolean ) ; } param context export function canToggleEditOffline ( context ) { return [ hasFileSelected ( context ) , navigation . isNotTrashcan ( context ) , canLockFile ( context ) || canUnlockFile ( context ) ] . every ( Boolean ) ; } deprecated param context export function canToggleFavorite ( context ) { return [ [ canAddFavorite ( context ) , canRemoveFavorite ( context ) ] . some ( Boolean ) , [ navigation . isRecentFiles ( context ) , navigation . isSharedFiles ( context ) , navigation . isSearchResults ( context ) , navigation . isFavorites ( context ) ] . some ( Boolean ) ] . every ( Boolean ) ; } param context export function canShowLogout ( context ) { return ! context . withCredentials ; } param context export function isLibraryManager ( context ) { return hasLibrarySelected ( context ) && context . selection . library . entry && context . selection . library . entry . role === 'SiteManager' ; } param context export function canInfoPreview ( context ) { return navigation . isSearchResults ( context ) && ! isMultiselection ( context ) && ! hasFolderSelected ( context ) && ! navigation . isPreview ( context ) ; } export function showInfoSelectionButton ( context ) { return navigation . isSearchResults ( context ) && ! navigation . isPreview ( context ) ; }