-
-
Notifications
You must be signed in to change notification settings - Fork 335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#1044 implement user column type #1045
Open
florentinap
wants to merge
1
commit into
gristlabs:main
Choose a base branch
from
florentinap:user-column-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {NTextBox} from "app/client/widgets/NTextBox"; | ||
import {ViewFieldRec} from "app/client/models/entities/ViewFieldRec"; | ||
import {DataRowModel} from "app/client/models/DataRowModel"; | ||
import {icon} from "app/client/ui2018/icons"; | ||
import {theme} from "app/client/ui2018/cssVars"; | ||
import {dom, styled} from "grainjs"; | ||
|
||
/** | ||
* User - The widget for displaying users from team. | ||
*/ | ||
export class User extends NTextBox { | ||
constructor(field: ViewFieldRec) { | ||
super(field); | ||
} | ||
|
||
public buildDom(row: DataRowModel) { | ||
const value = row.cells[this.field.colId()]; | ||
|
||
return cssUser( | ||
cssUserIcon('FieldUser'), | ||
dom.domComputed((use) => { return String(use(value)); }) | ||
); | ||
} | ||
} | ||
|
||
const cssUser = styled('div.field_clip', ``); | ||
|
||
const cssUserIcon = styled(icon, ` | ||
float: left; | ||
--icon-color: ${theme.lightText}; | ||
margin: -1px 2px 2px 0; | ||
`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import { | ||
ACIndexImpl, | ||
ACItem, | ||
ACResults, | ||
buildHighlightedDom, | ||
HighlightFunc, | ||
normalizeText | ||
} from 'app/client/lib/ACIndex'; | ||
import {Autocomplete} from 'app/client/lib/autocomplete'; | ||
import {theme, vars} from 'app/client/ui2018/cssVars'; | ||
import {menuCssClass} from 'app/client/ui2018/menus'; | ||
import {icon} from "app/client/ui2018/icons"; | ||
import {FieldOptions} from 'app/client/widgets/NewBaseEditor'; | ||
import {NTextEditor} from 'app/client/widgets/NTextEditor'; | ||
import {makeT} from 'app/client/lib/localization'; | ||
import {IToken} from 'app/client/lib/TokenField'; | ||
import {AppModel} from "app/client/models/AppModel"; | ||
import {UserAccessData} from "app/common/UserAPI"; | ||
import {undef} from 'app/common/gutil'; | ||
import {dom, Observable, styled} from 'grainjs'; | ||
|
||
const t = makeT('UserEditor'); | ||
|
||
export class UserItem implements ACItem, IToken { | ||
public cleanText: string = normalizeText(this.label); | ||
constructor( | ||
public label: string, | ||
public id: number | ||
) {} | ||
} | ||
|
||
/** | ||
* A UserEditor offers an autocomplete of choices from the users of team. | ||
*/ | ||
export class UserEditor extends NTextEditor { | ||
private _appModel: AppModel; | ||
private _autocomplete?: Autocomplete<UserItem>; | ||
private _dropdownConditionError = Observable.create<string | null>(this, null); | ||
private _userList: UserItem[] = []; | ||
|
||
constructor(options: FieldOptions) { | ||
super(options); | ||
|
||
this._appModel = options.gristDoc.appModel; | ||
|
||
// Decorate the editor to look like a user column value (with a "user" icon). | ||
// But not on readonly mode - here we will reuse default decoration | ||
if (!options.readonly) { | ||
this.cellEditorDiv.classList.add(cssUserEditor.className); | ||
this.cellEditorDiv.appendChild(cssUserEditIcon('FieldUser')); | ||
} | ||
|
||
this.textInput.value = undef(options.state, options.editValue, this._idToText()); | ||
|
||
if (this._autocomplete) { | ||
if (options.editValue === undefined) { | ||
this._autocomplete.search((items) => items.findIndex((item) => item.label === options.cellValue)); | ||
} else { | ||
this._autocomplete.search(); | ||
} | ||
} | ||
} | ||
|
||
public async attach(cellElem: Element) { | ||
super.attach(cellElem); | ||
// don't create autocomplete for readonly mode | ||
if (this.options.readonly) { | ||
return; | ||
} | ||
|
||
try { | ||
this._userList = (await this._appModel.api.getUsers()).map((user: UserAccessData) => ({ | ||
label: user.name || user.email, | ||
cleanText: normalizeText(user.name || user.email), | ||
id: user.id | ||
})); | ||
} catch (e) { | ||
this._dropdownConditionError?.set(e); | ||
} | ||
|
||
this._autocomplete = this.autoDispose(new Autocomplete<UserItem>(this.textInput, { | ||
menuCssClass: `${menuCssClass} ${cssUserList.className} test-autocomplete`, | ||
buildNoItemsMessage: () => { | ||
return dom.domComputed(use => { | ||
const error = use(this._dropdownConditionError); | ||
if (error) { | ||
return t('Error in dropdown condition'); | ||
} | ||
|
||
return t('No choices matching condition'); | ||
}); | ||
}, | ||
search: this._doSearch.bind(this), | ||
renderItem: this._renderItem.bind(this), | ||
getItemText: (item) => item.label, | ||
onClick: () => this.options.commands.fieldEditSave(), | ||
})); | ||
} | ||
|
||
public getCellValue() { | ||
const selectedItem = this._autocomplete && this._autocomplete.getSelectedItem(); | ||
|
||
if (selectedItem) { | ||
// Selected from the autocomplete dropdown. | ||
return selectedItem.label; | ||
} else if (normalizeText(this.textInput.value) === this._idToText()) { | ||
// Unchanged from what's already in the cell. | ||
return this.options.cellValue; | ||
} | ||
|
||
return super.getCellValue(); | ||
} | ||
|
||
private _idToText() { | ||
const value = this.options.cellValue; | ||
|
||
if (typeof value === 'number') { | ||
return this._userList.find((user) => value === user.id)?.label || ''; | ||
} | ||
|
||
return String(value || ''); | ||
} | ||
|
||
private async _doSearch(text: string): Promise<ACResults<UserItem>> { | ||
const items = new ACIndexImpl(this._userList); | ||
|
||
return items.search(text); | ||
} | ||
|
||
private _renderItem(item: UserItem, highlightFunc: HighlightFunc) { | ||
return cssUserItem( | ||
buildHighlightedDom(item.label, highlightFunc, cssMatchText) | ||
); | ||
} | ||
} | ||
|
||
const cssUserEditor = styled('div', ` | ||
& > .celleditor_text_editor, & > .celleditor_content_measure { | ||
padding-left: 18px; | ||
} | ||
`); | ||
|
||
const cssUserEditIcon = styled(icon, ` | ||
background-color: ${theme.lightText}; | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
margin: 3px 3px 0 3px; | ||
`); | ||
|
||
// Set z-index to be higher than the 1000 set for .cell_editor. | ||
const cssUserList = styled('div', ` | ||
z-index: 1001; | ||
overflow-y: auto; | ||
padding: 8px 0 0 0; | ||
--weaseljs-menu-item-padding: 8px 16px; | ||
`); | ||
|
||
const cssUserItem = styled('li', ` | ||
display: block; | ||
font-family: ${vars.fontFamily}; | ||
white-space: pre; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
outline: none; | ||
padding: var(--weaseljs-menu-item-padding, 8px 24px); | ||
cursor: pointer; | ||
color: ${theme.menuItemFg}; | ||
|
||
&.selected { | ||
background-color: ${theme.menuItemSelectedBg}; | ||
color: ${theme.menuItemSelectedFg}; | ||
} | ||
`); | ||
|
||
const cssMatchText = styled('span', ` | ||
color: ${theme.autocompleteMatchText}; | ||
.selected > & { | ||
color: ${theme.autocompleteSelectedMatchText}; | ||
} | ||
`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if that would not be a security issue, considering that it would expose the list of every users of an instance (their name and their email), no matter what access to their information the requester have.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my use case, I control all users in the instance, so there is no problem. You know more use cases, so you know if there is one that listing all users might be a security issue. Do you think it would be better to list only users who have access to the current site?