%3 ::xowiki::formfield::CompoundField ::xowiki::formfield::CompoundField add_component add_statistics check=compound convert_to_external convert_to_internal create_components exists_named_sub_component generate_fieldnames get_component get_compound_value get_named_sub_component get_named_sub_component_value has_instance_variable leaf_components make_correct named_sub_components object pretty_value render_input reset_on_validation_error same_value set_compound_value set_disabled set_is_repeat_template specs_unmodified validate value ::xowiki::formfield::FormField ::xowiki::formfield::FormField → fc_decode → fc_encode → get_from_name → get_single_spec → interprete_condition CSSclass_list_add add_statistics answer_check=AND answer_check=answer_words answer_check=btwn answer_check=contains answer_check=contains-not answer_check=eq answer_check=ge answer_check=gt answer_check=in answer_check=le answer_check=lt answer_check=match answer_is_correct asWidgetSpec behavior booleanAttributes config_from_spec convert_to_external convert_to_internal describe dict_to_fc dict_to_spec dict_value escape_message_keys field_value handle_transmit_always has_instance_variable init initialize interprete_single_spec is_disabled is_repeat_template_p leaf_components localize make_correct pretty_image pretty_value process_correct_when_modifier remove_omit render render_answer_statistics render_collapsed render_disabled_as_div render_error_msg render_form_widget render_help_text render_input render_item render_localizer render_modal render_result_statistics render_word_statistics repeat repeat_add_label resetBooleanAttributes reset_on_validation_error reset_parameter same_value set_disabled set_feedback set_is_repeat_template stats_record_count validate validation_check value_if_nothing_is_returned_from_form word_statistics ::xowiki::formfield::CompoundField->::xowiki::formfield::FormField ::xo::tdom::Object ::xo::tdom::Object ::xowiki::formfield::FormField->::xo::tdom::Object ::xowiki::formfield::repeatContainer ::xowiki::formfield::repeatContainer check_nr_components component_item_spec convert_to_internal count_values initialize item_spec pretty_value render_input require_component set_compound_value trim_values validate ::xowiki::formfield::repeatContainer->::xowiki::formfield::CompoundField

Class ::xowiki::formfield::repeatContainer

::xowiki::formfield::repeatContainer[i] create ... \
           [ -max (default "5") ] \
           [ -min (default "1") ] \
           [ -repeat_add_label (default "#xowiki.form-repeatable-add#") ] \
           [ -repeat_remove_label (default "#xowiki.delete#") ]

Class Relations

  • class: ::xotcl::Class[i]
  • superclass: ::xowiki::formfield::CompoundField[i]
::xotcl::Class create ::xowiki::formfield::repeatContainer \
     -superclass ::xowiki::formfield::CompoundField

Methods (to be applied on instances)

  • check_nr_components (scripted)

    if {$neededComponents > $availableComponents} {
      lassign [:item_spec] isRequired itemSpec
      for {set i $availableComponents} {$i < $neededComponents} {incr i} {
        :require_component $i
      }
    }
  • component_item_spec (scripted)

    #
    # Return a single itemspec suited for the nth component, derived
    # from the repeatable formfield spec.
    #
    if {$i <= [:min] && $isRequired} {
      set componentItemSpec [list $i $itemSpec,required,label=$i]
    } else {
      set componentItemSpec [list $i $itemSpec,label=$i]
    }
    return $componentItemSpec
  • convert_to_internal (scripted)

    set values [:value]
    :trim_values
    set r [next]
    #:msg name=${:name},value=[:get_compound_value]
    
    #
    # remove "unneeded" entries from instance attributes
    #
    ${:object} instvar instance_attributes
    foreach {name value} $values {
      if {[dict exists $instance_attributes $name]} {
        dict unset instance_attributes $name
      }
    }
    return $r
  • count_values (scripted)

    set count 1
    set highestCount 1
    if {![:required]} {set highestCount [:min]}
    # The first pair is the default from the template field (.0)
    set default [lindex $values 1]
    foreach f [lrange ${:components} 1 end] {name value} [lrange $values 2 end] {
      if {[$f required] || ($value ne "" && ![$f same_value $value $default])} {set highestCount $count}
      incr count
    }
    return $highestCount
  • initialize (scripted)

    ::xo::Page requireJS "/resources/xowiki/repeat.js"
    ::xo::Page requireJS urn:ad:js:jquery
    
    if {[info exists :__initialized_repeat]} {return}
    next
    
    set :__initialized_repeat 1
    #
    # Derive the spec of the contained items from the spec of the
    # container.
    #
    lassign [:item_spec] isRequired itemSpec
    
    #
    # Use item .0 as template for other items in .js (e.g. blank an
    # item with the template, when it is deleted. By using a
    # potentially compound item as template, we are able to preserve
    # default values for subfields without knowing the detailed
    # structure).
    #
    set componentItemSpecs [list [list 0 $itemSpec]]
    
    #
    # We use dynamic repeat fields. The number of fields generated on
    # the server side is the minimum, while the rest will be created
    # on demand via javascript.
    #
    set max [:min]
    
    for {set i 1} {$i <= $max} {incr i} {
      set componentItemSpec [:component_item_spec $i $itemSpec $isRequired]
      #ns_log notice "dynamic repeat componentItemSpec $componentItemSpec"
      lappend componentItemSpecs $componentItemSpec
    }
    :create_components $componentItemSpecs
    
    #
    # Deactivate template item
    #
    set componentList ${:components}
    if {[llength $componentList] > 0} {
      [lindex $componentList 0] set_disabled true
      [lindex $componentList 0] set_is_repeat_template true
    }
  • item_spec (scripted)

    #
    # Return the spec of a contained item, which is a subset of the
    # container spec.
    #
    set result {}
    set is_required false
    foreach s [split [:spec] ,] {
      # don't propagate "repeat" and "label" properties
      if { [string match "repeat=*" $s] || [string match "label=*" $s] } continue
      if { "required" eq $s} {set is_required true; continue}
      if { "disabled" eq $s} {:set_disabled true}
      lappend result $s
    }
    return [list $is_required [join $result ,]]
  • max (setter)

  • min (setter)

  • pretty_value (scripted)

    #
    # Simple renderer for repeated values
    #
    set ff [dict create {*}$v]
    set html "<ol class='repeatContainer'>\n"
    
    :set_compound_value $v
    foreach c [lrange ${:components} 1 [:count_values $v]] {
      if {[dict exists $ff [$c set name]]} {
        append html "<li>[$c pretty_value [dict get $ff [$c set name]]]</li>\n"
      }
    }
    append html "</ol>\n"
    return $html
  • render_input (scripted)

    #
    # Render content of the container within in a fieldset,
    # without labels for the contained items.
    #
    html::fieldset [:get_attributes id {CSSclass class}] {
      set i 0
      set clientData [subst {{"min":${:min},"max":${:max}"name":"${:name}"}}]
      set CSSclass   "[:form_widget_CSSclass] repeatable"
    
      set providedValues [:count_values [:value]]
      if {${:min} > $providedValues} {
        set nrItems ${:min}
      } else {
        set nrItems $providedValues
      }
      incr nrItems
    
      set containerIsDisabled [:is_disabled]
      set containerIsPrototype [string match "*.0*" ${:name}]
      set isPrototypeElement 0
      foreach c ${:components} {
        set atts [list class $CSSclass]
        lappend atts data-repeat $clientData
        if {$i == 0 || $i >= $nrItems} {
          lappend atts style "display: none;"
        }
        ::html::div $atts {
          #
          # Compound fields - link not shown if we are not rendering
          # for the template and copy the template afterwards.
          #
          if {!$containerIsDisabled || $containerIsPrototype} {
            set del_id "repeat-del-link-[$c set id]"
            ::html::a -href "#"  -id $del_id  -title ${:repeat_remove_label}  -class "delete-item-button repeat-del-link" {
                  html::t ""
                }
            template::add_event_listener  -id $del_id  -script [subst {xowiki.repeat.delItem(this,'$clientData');}]
          }
          $c render_input
        }
        incr i
      }
      #ns_log notice "repeat container $c [$c name] isDisabled $containerIsDisabled containerIsPrototype $containerIsPrototype"
      if {!$containerIsDisabled || $containerIsPrototype } {
        set hidden [expr {[:count_values [:value]] == ${:max} ? "display: none;" : ""}]
        set add_id "repeat-add-link-[:id]"
        #ns_log notice "... add another for ${:name}"
        html::a -href "#"  -id $add_id  -style $hidden  -class "repeat-add-link" {
              html::t ${:repeat_add_label}
            }
        template::add_event_listener  -id $add_id  -script [subst {xowiki.repeat.newItem(this,'$clientData');}]
      }
    }
  • repeat_add_label (setter)

  • repeat_remove_label (setter)

  • require_component (scripted)

    #
    # Require the nth component of a repeat field
    #
    lassign [:item_spec] isRequired itemSpec
    set componentItemSpec [:component_item_spec $i $itemSpec $isRequired]
    #ns_log notice "dynamic repeat field: add component on the fly: $componentItemSpec"
    :add_component $componentItemSpec
  • set_compound_value (scripted)

    #
    # Before setting compound values, check if we have the repeat
    # structure already set.
    #
    set neededComponents [expr {[llength $value] / 2}]
    set availableComponents [llength ${:components}]
    #:log "repeatContainer set_compound_value <$value> have $availableComponents needed $neededComponents"
    :check_nr_components $neededComponents $availableComponents
    next
  • trim_values (scripted)

    # Trim trailing values identical to default.
    # Trimming the components list seems sufficient.
    set count [:count_values [:value]]
    set :components [lrange ${:components} 0 $count]
  • validate (scripted)

    foreach c [lrange ${:components} 1 [:count_values [:value]]] {
      set result [$c validate $obj]
      if {$result ne ""} {
        return $result
      }
    }
    return ""