Module: Zena::Use::HtmlTags::FormTags

Included in:
ViewMethods
Defined in:
lib/zena/use/html_tags.rb

Instance Method Summary collapse

Instance Method Details

#check_exists(opts) ⇒ Object

TODO: select_id should use ‘check_exists’



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/zena/use/html_tags.rb', line 74

def check_exists(opts)
  watch  = opts[:watch] || 'node_title'
  name_ref = unique_id
  params = []

  # Filtering key
  key = 'name'
  params << "#{key}=' + this.value + '"

  # Attribute to display
  attribute = opts[:show] || 'short_path'
  params << "attr=#{attribute}"

  # Scoping
  if kpath  = opts[:kpath]
    params << "kpath=#{kpath}"
  end

  function_name = "check#{unique_id}"
  js_data << "#{function_name} = function(event) {
    new Ajax.Updater('#{name_ref}', '/nodes/#{(opts[:node] || @node).zip}/attribute?#{params.join('&')}', {method:'get', asynchronous:true, evalScripts:true});
  };"

  js_data << "$('#{watch}').check_exists = #{function_name};"
  js_data << "Event.observe('#{watch}', 'change', #{function_name});"
  js_data << "Event.observe('#{watch}', 'keyup', #{function_name});"

  "<span class='select_id_name' id='#{name_ref}'>#{opts[:current]}</span>"
end

#form_groupsObject

TODO: test Return the list of groups from the visitor for forms



10
11
12
# File 'lib/zena/use/html_tags.rb', line 10

def form_groups
  @form_groups ||= Group.find(:all, :select=>'id, name', :conditions=>"id IN (#{visitor.group_ids.join(',')})", :order=>"name ASC").collect {|p| [p.name, p.id]}
end

#form_skinsObject

TODO: test Return the list of possible templates



16
17
18
# File 'lib/zena/use/html_tags.rb', line 16

def form_skins
  @form_skins ||= Node.sfind('skins').map {|r| [r.title, r.zip]}
end

#readers_for(obj = @node) ⇒ Object

Display the list of readers (group names).



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/zena/use/html_tags.rb', line 110

def readers_for(obj=@node)
  readers = if obj.public?
    _('img_public')
  else
    names = []
    names << obj.rgroup.name if obj.rgroup
    names << obj.wgroup.name if obj.wgroup
    names.uniq.join(', ')
  end
  custom = obj.inherit != 1 ? "<span class='custom'>#{_('img_custom_inherit')}</span>" : ''
  "#{custom} #{readers}"
end

#select_id(obj, sym, opt = {}) ⇒ Object

Display an input field to select an id. The user can enter an id or a name in the field and the node’s path is shown next to the input field. If the :class option is specified and the elements in this class are not too many, a select menu is shown instead (nodes in the menu are found using secure_write scope). ‘Sym’ is the field to select the id for (parent_id, …).



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/zena/use/html_tags.rb', line 24

def select_id(obj, sym, opt={})
  unless kpath = opt[:kpath]
    klass = opt[:class].kind_of?(Class) ? opt[:class] : Node.get_class(opt[:class] || 'Node')
    kpath = klass.kpath
  end

  if obj == 'link'
    if link = instance_variable_get("@#{obj}")
      node        = link.this
      current_obj = link.other
    end
  else
    unless id = opt[:id]
      node = instance_variable_get("@#{obj}")
      if node
        id = node.send(sym.to_sym)
      else
        id = nil
      end
      if !node || node.new_record?
        node = current_site.root_node
      end
    end

    if !id.blank?
      current_obj = secure!(Node) { Node.find(id) } rescue nil
    end
  end


  name_ref = unique_id
  attribute = opt[:show] || 'short_path'
  if current_obj
    zip = current_obj[:zip]
    current = current_obj.send(attribute.to_sym)
    if current.kind_of?(Array)
      current = current.join('/ ')
    end
  else
    zip = ''
    current = ''
  end
  input_id = opt[:input_id] ? " id='#{params[:input_id]}'" : ''
  # we use both 'onChange' and 'onKeyup' for old javascript compatibility
  update = "new Ajax.Updater('#{name_ref}', '/nodes/#{(node || @node).zip}/attribute?pseudo_id=' + this.value + '&attr=#{attribute}', {method:'get', asynchronous:true, evalScripts:true});"
  "<div class='select_id'><input type='text' size='8'#{input_id} name='#{obj}[#{sym}]' value='#{zip}' onChange=\"#{update}\" onKeyup=\"#{update}\"/>"+
  "<span class='select_id_name' id='#{name_ref}'>#{current}</span></div>"
end

#unique_idObject



104
105
106
107
# File 'lib/zena/use/html_tags.rb', line 104

def unique_id
  @counter ||= 0
  "h#{Time.now.to_i}_#{@counter += 1}"
end