forked from mbk-lab/rui_orig
2
0
Fork 0

Added "disabled-items" property to DropDownList

This commit is contained in:
Alexei Anoshenko 2022-05-04 22:35:54 +03:00
parent 54a867591c
commit 19bb1694de
4 changed files with 186 additions and 3 deletions

View File

@ -3001,6 +3001,21 @@ float32, float64, int, int8…int64, uint, uint8…uint64.
func GetDropDownItems(view View, subviewID string) []string func GetDropDownItems(view View, subviewID string) []string
Можно запретить выбор отдельных пунктов. Для этого используется свойство "disabled-items" (константа DisabledItems).
Данному свойству присваивается массив индексов запрещенных пунктов. Индекс может задаваться или числом или в виде текста
или как константа. Поэтому свойству "disabled-items" могут присваиваться следующие типы данных:
* []int
* int
* []string
* string может содержать несколько индексов разделенных запятыми
* []interface{} содержащий в качестве элементов только: string, int, int8…int64, uint, uint8…uint64.
Все эти типы данных преопразуются в []interface{} и присваиваются свойству "disabled-items".
Прочитать значение свойства "disabled-items" можно с помощью функции
func GetDropDownDisabledItems(view View, subviewID string) []int
Выбранное значение определяется int свойством "current" (константа Current). Значение по умолчанию 0. Выбранное значение определяется int свойством "current" (константа Current). Значение по умолчанию 0.
Прочитать значение данного свойства можно с помощью функции Прочитать значение данного свойства можно с помощью функции

View File

@ -2972,6 +2972,20 @@ You can read the value of the "items" property using the function
func GetDropDownItems(view View, subviewID string) []string func GetDropDownItems(view View, subviewID string) []string
You can disable the selection of individual items. For this, the "disabled-items" property (constant DisabledItems) is used.
This property is assigned an array of disabled item indices. The index can be specified either as a number, as text, or as a constant. Therefore, the following data types can be assigned to the "disabled-items" property:
* []int
* int
* []string
* string - can contain multiple indexes separated by commas
* []interface{} - containing as elements only: string, int, int8…int64, uint, uint8…uint64.
All of these data types are converted to []interface{} and assigned to the "disabled-items" property.
You can read the value of the "disabled-items" property using the function
func GetDropDownDisabledItems(view View, subviewID string) []int
The selected value is determined by the int property "current" (Current constant). The default is 0. The selected value is determined by the int property "current" (Current constant). The default is 0.
You can read the value of this property using the function You can read the value of this property using the function

View File

@ -17,6 +17,7 @@ type DropDownList interface {
type dropDownListData struct { type dropDownListData struct {
viewData viewData
items []string items []string
disabledItems []interface{}
dropDownListener []func(DropDownList, int) dropDownListener []func(DropDownList, int)
} }
@ -36,6 +37,7 @@ func (list *dropDownListData) Init(session Session) {
list.viewData.Init(session) list.viewData.Init(session)
list.tag = "DropDownList" list.tag = "DropDownList"
list.items = []string{} list.items = []string{}
list.disabledItems = []interface{}{}
list.dropDownListener = []func(DropDownList, int){} list.dropDownListener = []func(DropDownList, int){}
} }
@ -58,6 +60,15 @@ func (list *dropDownListData) remove(tag string) {
list.propertyChangedEvent(tag) list.propertyChangedEvent(tag)
} }
case DisabledItems:
if len(list.disabledItems) > 0 {
list.disabledItems = []interface{}{}
if list.created {
updateInnerHTML(list.htmlID(), list.session)
}
list.propertyChangedEvent(tag)
}
case DropDownEvent: case DropDownEvent:
if len(list.dropDownListener) > 0 { if len(list.dropDownListener) > 0 {
list.dropDownListener = []func(DropDownList, int){} list.dropDownListener = []func(DropDownList, int){}
@ -85,10 +96,18 @@ func (list *dropDownListData) Set(tag string, value interface{}) bool {
} }
func (list *dropDownListData) set(tag string, value interface{}) bool { func (list *dropDownListData) set(tag string, value interface{}) bool {
if value == nil {
list.remove(tag)
return true
}
switch tag { switch tag {
case Items: case Items:
return list.setItems(value) return list.setItems(value)
case DisabledItems:
return list.setDisabledItems(value)
case DropDownEvent: case DropDownEvent:
return list.setDropDownListener(value) return list.setDropDownListener(value)
@ -119,7 +138,7 @@ func (list *dropDownListData) setItems(value interface{}) bool {
list.items = value list.items = value
case []DataValue: case []DataValue:
list.items = []string{} list.items = make([]string, 0, len(value))
for _, val := range value { for _, val := range value {
if !val.IsObject() { if !val.IsObject() {
list.items = append(list.items, val.Value()) list.items = append(list.items, val.Value())
@ -133,7 +152,7 @@ func (list *dropDownListData) setItems(value interface{}) bool {
} }
case []interface{}: case []interface{}:
items := []string{} items := make([]string, 0, len(value))
for _, v := range value { for _, v := range value {
switch val := v.(type) { switch val := v.(type) {
case string: case string:
@ -183,6 +202,91 @@ func (list *dropDownListData) setItems(value interface{}) bool {
return true return true
} }
func (list *dropDownListData) setDisabledItems(value interface{}) bool {
switch value := value.(type) {
case []int:
list.disabledItems = make([]interface{}, len(value))
for i, n := range value {
list.disabledItems[i] = n
}
case []interface{}:
disabledItems := make([]interface{}, len(value))
for i, val := range value {
if val == nil {
notCompatibleType(DisabledItems, value)
return false
}
switch val := val.(type) {
case string:
if isConstantName(val) {
disabledItems[i] = val
} else {
n, err := strconv.Atoi(val)
if err != nil {
notCompatibleType(DisabledItems, value)
return false
}
disabledItems[i] = n
}
default:
if n, ok := isInt(val); ok {
disabledItems[i] = n
} else {
notCompatibleType(DisabledItems, value)
return false
}
}
}
list.disabledItems = disabledItems
case string:
values := strings.Split(value, ",")
disabledItems := make([]interface{}, len(values))
for i, str := range values {
str = strings.Trim(str, " ")
if str == "" {
notCompatibleType(DisabledItems, value)
return false
}
if isConstantName(str) {
disabledItems[i] = str
} else {
n, err := strconv.Atoi(str)
if err != nil {
notCompatibleType(DisabledItems, value)
return false
}
disabledItems[i] = n
}
}
list.disabledItems = disabledItems
case []DataValue:
disabledItems := make([]string, 0, len(value))
for _, val := range value {
if !val.IsObject() {
disabledItems = append(disabledItems, val.Value())
}
}
return list.setDisabledItems(disabledItems)
default:
notCompatibleType(DisabledItems, value)
return false
}
if list.created {
updateInnerHTML(list.htmlID(), list.session)
}
list.propertyChangedEvent(Items)
return true
}
func (list *dropDownListData) setDropDownListener(value interface{}) bool { func (list *dropDownListData) setDropDownListener(value interface{}) bool {
switch value := value.(type) { switch value := value.(type) {
case func(DropDownList, int): case func(DropDownList, int):
@ -250,6 +354,9 @@ func (list *dropDownListData) get(tag string) interface{} {
case Items: case Items:
return list.items return list.items
case DisabledItems:
return list.disabledItems
case Current: case Current:
result, _ := intProperty(list, Current, list.session, 0) result, _ := intProperty(list, Current, list.session, 0)
return result return result
@ -273,8 +380,19 @@ func (list *dropDownListData) htmlSubviews(self View, buffer *strings.Builder) {
if list.items != nil { if list.items != nil {
current := GetCurrent(list, "") current := GetCurrent(list, "")
notTranslate := GetNotTranslate(list, "") notTranslate := GetNotTranslate(list, "")
disabledItems := GetDropDownDisabledItems(list, "")
for i, item := range list.items { for i, item := range list.items {
if i == current { disabled := false
for _, index := range disabledItems {
if i == index {
disabled = true
break
}
}
if disabled {
buffer.WriteString("<option disabled>")
} else if i == current {
buffer.WriteString("<option selected>") buffer.WriteString("<option selected>")
} else { } else {
buffer.WriteString("<option>") buffer.WriteString("<option>")
@ -349,3 +467,37 @@ func GetDropDownItems(view View, subviewID string) []string {
} }
return []string{} return []string{}
} }
// func GetDropDownDisabledItems return the list of disabled item indexes
func GetDropDownDisabledItems(view View, subviewID string) []int {
if subviewID != "" {
view = ViewByID(view, subviewID)
}
if view != nil {
if value := view.Get(DisabledItems); value != nil {
if values, ok := value.([]interface{}); ok {
count := len(values)
if count > 0 {
result := make([]int, 0, count)
for _, value := range values {
switch value := value.(type) {
case int:
result = append(result, value)
case string:
if value != "" && value[0] == '@' {
if val, ok := view.Session().Constant(value[1:]); ok {
if n, err := strconv.Atoi(val); err == nil {
result = append(result, n)
}
}
}
}
}
return result
}
}
}
}
return []int{}
}

View File

@ -328,6 +328,8 @@ const (
Content = "content" Content = "content"
// Items is the constant for the "items" property tag. // Items is the constant for the "items" property tag.
Items = "items" Items = "items"
// DisabledItems is the constant for the "disabled-items" property tag.
DisabledItems = "disabled-items"
// Current is the constant for the "current" property tag. // Current is the constant for the "current" property tag.
Current = "current" Current = "current"
// Type is the constant for the "type" property tag. // Type is the constant for the "type" property tag.