MultiChoice - guiled/LRE GitHub Wiki

Multichoices

Introduction

Multichoice in Let's Role are Choice that are check "exanded + multiple". They have some similar features to choices and can have some strange behaviors that LRE fixes to ease their use.

Specific methods

See the methods from Choice

Multichoice in LRE have some more methods to handle some specific cases.

setDefaultValue, clear, selectNone, unselectAll

multichoice.setDefaultValue() multichoice.clear() multichoice.selectNone() multichoice.unselectAll()

These methods do the same thing : remove all the selected choices and optionally set to the default value (see setChoices optional parameter or chocie.optional()).

selectAll

multichoice.selectAll()

The methods checks all choices. If it exceeds a previously set MultiChoice#maxChoiceNb this method does nothing except trigger the event limit.

invert

multichoice.invert()

This method invert the check state of each choice : every checked choice become unchecked, every unchecked choice become checked

checked, unchecked

multichoice.checked(): DataProvider multichoice.unchecked(): DataProvider

These two methods returns a DataProvider that dynamically provides the checked or unchecked values of the multichoice.

maxChoiceNb, minChoiceNb

multichoice.maxChoiceNb(nb?: undefined | number | Object, calculator?: Function) multichoice.minChoiceNb(nb?: undefined | number | Object, calculator?: Function)

These two methods allows you to limit the checked or unchecked choices in the multichoice.

The first parameter can be a number or an object with many key value. It can be also a dynamic setter (ie: a component, a function)

The second optional parameter is a function that returns the value to add for each checked choice.

The function to calculate is described as following:

function calculator(
    choiceLabel: string,
    choiceId: string | number,
    choiceData: Object,
    accumulatedValue: number | Object
): number | Object {}`

The returned value is added to the accumulatedValue for each checked choice and then compare to

Examples :

  • multichoice.maxChoiceNb(3) This will limit the checked choice at 3.

  • multichoice.maxChoiceNb(sheet.get("maxEncumbrance"), function (_lbl, _id, data) { return data.weight; }) This will limit the checked choice by adding the weight of every items and comparing it with the value of the component maxEncumbrance

Limit on many criteria

It is possible to limit the checked values with many criteria that must all passed multichoice.maxChoiceNb(object, dataSelector)

  • object : an object that contains max values or a function returning an object
  • dataSelector : a function that returns an object that will be summed.

Exemple : The following example will limit the multichoice to 4 selected items whose total size does not exceed 10 (like the bag size) total weight does not exceed 40 (the caracter capacity).

inventoryBag.maxChoiceNb({ nb: 4, capacity: 10, weight: 40}, function (label, value, data, calculation) {
  return {
    nb: 1,  // count one object
    capacity: data.size,
    weight: data.weight,
  };
});

Specific events

See the events from Choice

limit, limitmin, limitmax

multichoice.on("limit", function (component, type, minValue, maxValue) {}) multichoice.on("limitmin", function (component, type, minValue, maxValue) {}) multichoice.on("limitmax", function (component, type, minValue, maxValue) {})

These events are triggered as soon as the limit set with minChoiceNb or maxChoiceNb block an operation on the multichoice. Parameters :

  • type: a string containing "min" or "max"
  • minValue : the minimal value that triggered this event (when type = "min")
  • maxValue : the minimal value that triggered this event (when type = "max")