Class: CML::Tags::Taxonomy

Inherits:
CML::Tag show all
Defined in:
lib/cml/tags/taxonomy.rb

Constant Summary collapse

SIMPLE_OPTIONS =
["root-select", "log-search", "multi-select", "select-all"]
URL_OPTIONS =
["src", "search-src"]
ARRAY_OPTIONS =
["top-level", "selectable", "nonselectable"]
BareTemplate =
<<-HTML.freeze
  {{inputs}}
  {{search_log}}
  <div class="taxonomy_widget_container" {{taxonomy_options}}></div>
HTML
MultiInputTemplate =
<<-HTML.freeze
  <div class="taxonomy_choices">
    {{inputs}} 
  </div>
HTML
InputTemplate =
<<-HTML.freeze
  <div class="cml_row taxonomy_choice">
    <input name="{{name}}" type="{{input_type}}" class="{{classes}}" value="{{value}}" />
    <input name="{{id_name}}" type="{{input_type}}" class="taxonomy_id no_validate" value="{{id_value}}" />
  </div>
HTML
SearchLogTemplate =

The actual search logging is done by the taxonomy widget by adding input array fields to this div

<<-HTML.freeze
  <div class="cml_row taxonomy_search" data-name="{{search_log_name}}"></div>
HTML
Template =
<<-HTML.freeze
  {{label}}
  #{BareTemplate}
HTML

Constants included from CML::TagLogic

CML::TagLogic::And, CML::TagLogic::AndPhraseExp, CML::TagLogic::CombinatorDefault, CML::TagLogic::CombinatorDict, CML::TagLogic::CombinatorExp, CML::TagLogic::GroupExp, CML::TagLogic::Or, CML::TagLogic::OrCombinatorExp, CML::TagLogic::PrecedenceRegexp, CML::TagLogic::TokenExp, CML::TagLogic::TokenRegexp

Instance Attribute Summary

Attributes inherited from CML::Tag

#attrs, #cml, #opts, #tag

Attributes included from CML::TagLogic

#errors, #has_grouped_logic, #has_liquid_logic, #logic_tree

Instance Method Summary collapse

Methods inherited from CML::Tag

#cacheable?, #children, #classes, #convert, #ensure_resolved, #gold=, #gold?, #gold_class, #gold_reason, #initialize, #instructions, #iterating?, #label, #legend, memoize, #name, #parent_multiples, #prefix, #preloaded_data, #raw_label, #to_liquid, #to_s, #validate?, #validations, #validators, #wrapper, #wrapper_classes

Methods included from CML::TagLogic

#dependencies_on_fields, #dependencies_through_cml_group, #depends_on_fields, #describe_logic_token, #detect_grouped_logic, #detect_liquid_logic, #each_logic_token_in, #expand_logic, #expand_parsed_expression, #keep_merge!, #only_if, parse_expression, resolve_combinator

Constructor Details

This class inherits a constructor from CML::Tag

Instance Method Details

#dataObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/cml/tags/taxonomy.rb', line 91

def data
  super.merge({
    "taxonomy_options" => taxonomy_options,
    "value" => value,
    "id_value" => id_value,
    "id_name" => prefix(id_name),
    "search_log_name" => search_log_name, 
    "input_type" => "text"
  })
end

#finite_value?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/cml/tags/taxonomy.rb', line 49

def finite_value?
  true
end

#id_nameObject



83
84
85
# File 'lib/cml/tags/taxonomy.rb', line 83

def id_name
  name(false, "id")
end

#id_valueObject



79
80
81
# File 'lib/cml/tags/taxonomy.rb', line 79

def id_value
 ( (@opts[:data] || {})[id_name] || "" ).to_s 
end

#in_logic_graph?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/cml/tags/taxonomy.rb', line 45

def in_logic_graph?
  is_field?
end

#is_field?Boolean

allow use of the taxonomy tool for browsing without a form input

Returns:

  • (Boolean)


41
42
43
# File 'lib/cml/tags/taxonomy.rb', line 41

def is_field?
  !(@attrs["field"] && @attrs["field"].to_s == "false")
end

#multi_typeObject

Overriden to get the right name prefix/suffix



71
72
73
# File 'lib/cml/tags/taxonomy.rb', line 71

def multi_type
  @attrs["multi-select"] ? :list : nil
end

#search_log_nameObject



87
88
89
# File 'lib/cml/tags/taxonomy.rb', line 87

def search_log_name
  name(true, "search_log")
end

#taxonomy_optionsObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cml/tags/taxonomy.rb', line 53

def taxonomy_options
  cw_data = {}

  (SIMPLE_OPTIONS + ARRAY_OPTIONS).each do |opt|
    attr_opt = @attrs[opt]
    cw_data[opt] = attr_opt.to_s if attr_opt
  end

  URL_OPTIONS.each do |opt|
    attr_opt = @attrs[opt]
    cw_data[opt] = URI.escape(attr_opt.to_s) if attr_opt
  end
  
  cw_data.map{|opt_name, opt_value| "data-#{opt_name}=\"#{opt_value}\""}.join(" ")
end

#to_htmlObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cml/tags/taxonomy.rb', line 103

def to_html
  if is_field?
    if @opts[:normalize]
      data_opt = opts[:data] || {}
      seeds = Array(data_opt[@opts[:parser].golds[name(false)]] || data_opt[name(false)])
      # Zip in the id values for taxonomy nodes
      seeds = seeds.zip(data_opt[id_name] || [])
      seeds << nil if seeds.length == 0
    
      inputs = seeds.map do |input, p_name|
        Liquid::Template.parse(InputTemplate).render(data.merge({
          "value" => input.to_s, 
          "id_value" => p_name.to_s,
          "classes" => classes+(input ? " cml_gold_loaded" : ""),
          "input_type" => "text"
        }))
      end.join("\n")
      search_log = ""
    else
      search_src = @attrs["search-src"].to_s
      log_search = @attrs["log-search"].to_s
      multi_select = @attrs["multi-select"].to_s

      inputs = Liquid::Template.parse(InputTemplate).render(data)
      inputs = Liquid::Template.parse(MultiInputTemplate).render({"inputs" => inputs}) if !multi_select.empty?
      
      if !search_src.empty? && !log_search.empty?
        search_log = Liquid::Template.parse(SearchLogTemplate).render(data)
      else
        search_log = ""
      end
    end
  else
    inputs = ""
    search_log = ""
  end
  
  wrapper(Liquid::Template.parse(Template).render(data.merge({
    "inputs" => inputs,
    "search_log" => search_log
  }), [LiquidFilters]))
end

#valueObject



75
76
77
# File 'lib/cml/tags/taxonomy.rb', line 75

def value
  (@attrs["value"] || (@opts[:data] || {})[name] || "").to_s
end