Skip to content

Commit

Permalink
feat: add 'joinLastRow' option
Browse files Browse the repository at this point in the history
nearly everything can now also be added to the row before instead of 
creating a new one
  • Loading branch information
EdJoPaTo committed Sep 9, 2018
1 parent 66d6761 commit 3494e5c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Methods often have these parameters:
Will be set as the button text.
- `setFunc`
Will be called when the user selects an option from the menu
- `joinLastRow` (optional)
When set to true the button will try to join the row before
- `hide` (optional)
Hides the button in the menu when the Function returns false

Expand All @@ -68,15 +70,15 @@ This can be a `string` or `function(ctx)`.
`backButtonText` and `mainMenuButtonText` will be used for the back and top buttons.
Submenus will use these attibutes of parents.

### `menu.manual(actionCode, text, {hide})`
### `menu.manual(actionCode, text, {hide, joinLastRow})`

Add a Button for a manual (or legacy) bot.action

`actionCode` has to be unique in this menu.
`text` can be a `string` or a `function(ctx)` that will be set as the Button text.
`hide(ctx)` (optional) can hide the button when return is true.

### `menu.submenu(text, menu, {hide})`
### `menu.submenu(text, menu, {hide, joinLastRow})`

Creates a Button in the menu to a submenu

Expand All @@ -87,7 +89,7 @@ It uses the actionCode of the provided `menu`.
`menu` is another TelegrafInlineMenu with an actionCode below the one of the current menu.
`hide(ctx)` (optional) can hide the button that opens the submenu.

### `menu.toggle(actionCode, text, setFunc, {isSetFunc, hide})`
### `menu.toggle(actionCode, text, setFunc, {isSetFunc, hide, joinLastRow})`

Creates a button that toggles a setting

Expand All @@ -99,7 +101,7 @@ Creates a button that toggles a setting
This will show an emoji to the user on the button as text prefix.
`hide(ctx)` (optional) can hide the button when return is true.

### `menu.select(actionCode, options, setFunc, {isSetFunc, prefixFunc, hide, columns})`
### `menu.select(actionCode, options, setFunc, {isSetFunc, prefixFunc, hide, joinLastRow, columns})`

Creates multiple buttons for each provided option.

Expand All @@ -125,7 +127,7 @@ Can only be used when `isSetFunc` is not used.
This is an alias for `menu.select`
The wording makes more sense with list that are not exclusive selections.

### `menu.question(actionCode, buttonText, setFunc, {questionText, hide})`
### `menu.question(actionCode, buttonText, setFunc, {questionText, hide, joinLastRow})`

When the user presses the button, he will be asked a question.
The answer he gives will be given via `setFunc(ctx, answer)`
Expand Down
1 change: 1 addition & 0 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ addMenu.question('filter', filterText,
addMenu.toggle('clearfilter', 'Filter aufheben', ctx => {
ctx.session.eventfilter = '.+'
}, {
joinLastRow: true,
hide: ctx => ctx.session.eventfilter === '.+'
})

Expand Down
35 changes: 23 additions & 12 deletions telegraf-inline-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,27 @@ class TelegrafInlineMenu {
return Composer.branch(hide, Composer.safePassThru(), Composer.compose(fns))
}

manual(action, text, {hide} = {}) {
addButton(button, ownRow = true) {
if (ownRow) {
this.buttons.push([
button
])
} else {
const lastRow = this.buttons[this.buttons.length - 1]
lastRow.push(button)
}
}

manual(action, text, {hide, joinLastRow} = {}) {
const actionCode = this.prefix + ':' + action
this.buttons.push([{
this.addButton({
text,
actionCode,
hide
}])
}, !joinLastRow)
}

submenu(text, submenu, {hide} = {}) {
submenu(text, submenu, {hide, joinLastRow} = {}) {
if (!hide) {
hide = () => false
}
Expand All @@ -117,15 +128,15 @@ class TelegrafInlineMenu {
submenu.parent = this

const actionCode = submenu.prefix
this.buttons.push([{
this.addButton({
text,
actionCode,
hide
}])
}, !joinLastRow)
this.bot.use(this.hideMiddleware(hide, submenu.bot))
}

toggle(action, text, setFunc, {isSetFunc, hide} = {}) {
toggle(action, text, setFunc, {isSetFunc, hide, joinLastRow} = {}) {
if (!hide) {
hide = () => false
}
Expand All @@ -138,12 +149,12 @@ class TelegrafInlineMenu {

const textPrefix = isSetFunc ? async ctx => enabledEmoji(await isSetFunc(ctx)) : undefined

this.buttons.push([{
this.addButton({
text,
textPrefix,
actionCode,
hide
}])
}, !joinLastRow)
}

list(action, options, setFunc, optionalArgs = {}) {
Expand Down Expand Up @@ -179,7 +190,7 @@ class TelegrafInlineMenu {
}
}

question(action, buttonText, setFunc, {hide, questionText} = {}) {
question(action, buttonText, setFunc, {hide, questionText, joinLastRow} = {}) {
if (!questionText) {
questionText = buttonText
}
Expand All @@ -200,11 +211,11 @@ class TelegrafInlineMenu {
])
}))

this.buttons.push([{
this.addButton({
text: buttonText,
actionCode,
hide
}])
}, !joinLastRow)
}
}

Expand Down

0 comments on commit 3494e5c

Please sign in to comment.