class Tofu::Tofu

Attributes

session[R]

Public Class Methods

add_erb(method_name, fname, dir=nil) click to toggle source
# File lib/tofu.rb, line 209
def self.add_erb(method_name, fname, dir=nil)
  erb = ERBMethod.new(method_name, fname, dir)
  @erb_method.push(erb)
end
new(session) click to toggle source
# File lib/tofu.rb, line 230
def initialize(session)
  @session = session
  @session.entry(self)
  @tofu_seq = nil
end
reload_erb() click to toggle source
# File lib/tofu.rb, line 224
def self.reload_erb
  @erb_method.each do |erb|
    reload_erb1(erb)
  end
end
reload_erb1(erb) click to toggle source
# File lib/tofu.rb, line 219
def self.reload_erb1(erb)
  erb.reload(self)
rescue SyntaxError
end
set_erb(fname, dir=nil) click to toggle source
# File lib/tofu.rb, line 214
def self.set_erb(fname, dir=nil)
  @erb_method = [ERBMethod.new('to_html(context=nil)', fname, dir)]
  reload_erb
end

Public Instance Methods

a_and_update(method_name, add_param, context, target=nil) click to toggle source
# File lib/tofu.rb, line 533
def a_and_update(method_name, add_param, context, target=nil)
  target ||= self
  param = {
    'tofu_inner_id' => target.tofu_id
  }
  param.update(add_param)

  param = make_param(method_name, param)
  ary = param.collect do |k, v|
          "#{u(k)}=#{u(v)}"
  end
  path = URI.parse(context.req_absolute_path)
  url = path + %Q!#{action(context)}?#{ary.join('&')}!
  %Q!tofu_x_update("#{target.tofu_id}", #{url.to_s.dump});!
end
action(context) click to toggle source
# File lib/tofu.rb, line 293
def action(context)
  context.req_script_name.to_s + context.req_path_info.to_s
end
do_else(context, params) click to toggle source
# File lib/tofu.rb, line 290
def do_else(context, params)
end
on_update_script(ary_or_script) click to toggle source
# File lib/tofu.rb, line 549
def on_update_script(ary_or_script)
  ary = if String === ary_or_script
          [ary_or_script]
        else
          ary_or_script
        end
  str = %Q!<form name="#{tofu_id}tofu_x_eval">!
  ary.each do |script|
    str << %Q!<input type='hidden' name='tofu_x_eval' value="#{script.gsub('"', '&quot;')}" />!
  end
  str << '</form>'
  str
end
send_request(context, params) click to toggle source
# File lib/tofu.rb, line 269
def send_request(context, params)
  cmd, = params['tofu_cmd']
  msg = 'do_' + cmd.to_s

  if @tofu_seq
    seq, = params['tofu_seq']
    unless @tofu_seq.to_s == seq
      p [seq, @tofu_seq.to_s] if $DEBUG
      return
    end
  end

  if respond_to?(msg)
    send(msg, context, params)
  else
    do_else(context, params)
  end
ensure
  @tofu_seq = @tofu_seq.succ if @tofu_seq
end
to_div(context) click to toggle source
# File lib/tofu.rb, line 245
def to_div(context)
  to_elem('div', context)
end
to_elem(element, context) click to toggle source
# File lib/tofu.rb, line 253
def to_elem(element, context)
  elem(element, {'class'=>tofu_class, 'id'=>tofu_id}) {
    begin
      to_html(context)
    rescue
      "<p>error! #{h($!)}</p>"
    end
  }
end
to_html(context) click to toggle source
# File lib/tofu.rb, line 263
def to_html(context); ''; end
to_inner_html(context) click to toggle source
# File lib/tofu.rb, line 265
def to_inner_html(context)
  to_html(context)
end
to_span(context) click to toggle source
# File lib/tofu.rb, line 249
def to_span(context)
  to_elem('span', context)
end
tofu_class() click to toggle source
# File lib/tofu.rb, line 237
def tofu_class
  self.class.to_s
end
tofu_id() click to toggle source
# File lib/tofu.rb, line 241
def tofu_id
  self.__id__.to_s
end
update_after(msec, context) click to toggle source
# File lib/tofu.rb, line 567
def update_after(msec, context)
  callback = update_me(context)
  script = %Q!setTimeout(#{callback.dump}, #{msec})!
  on_update_script(script)
end
update_js() click to toggle source
# File lib/tofu.rb, line 485
    def update_js
      <<-"EOS"
      function tofu_x_eval(tofu_id) {
        var ary = document.getElementsByName(tofu_id + "tofu_x_eval");
        for (var j = 0; j < ary.length; j++) {
          var tofu_arg = ary[j];
          for (var i = 0; i < tofu_arg.childNodes.length; i++) {
            var node = tofu_arg.childNodes[i];
            if (node.attributes.getNamedItem('name').nodeValue == 'tofu_x_eval') {
              var script = node.attributes.getNamedItem('value').nodeValue;
              try {
                 eval(script);
              } catch(e) {
              }
            }
          }
        }
      }

      function tofu_x_update(tofu_id, url) {
        var x;
        try {
          x = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
          try {
            x = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e) {
            x = null;
          }
        }
        if (!x && typeof XMLHttpRequest != "undefined") {
           x = new XMLHttpRequest();
        }
        if (x) {
          x.onreadystatechange = function() {
            if (x.readyState == 4 && x.status == 200) {
              var tofu = document.getElementById(tofu_id);
              tofu.innerHTML = x.responseText;
              tofu_x_eval(tofu_id);
            }
          }
          x.open("GET", url);
          x.send(null);
        }
      }
EOS
    end
update_me(context) click to toggle source
# File lib/tofu.rb, line 563
def update_me(context)
  a_and_update('else', {}, context)
end

Private Instance Methods

a(method, param, context) click to toggle source
# File lib/tofu.rb, line 361
def a(method, param, context)
  make_anchor(method, param, context)
end
attr(opt) click to toggle source
# File lib/tofu.rb, line 298
def attr(opt)
  ary = opt.collect do |k, v|
    if v 
      %Q!#{k}="#{h(v)}"!
    else
      nil
    end
  end.compact
  return nil if ary.size == 0 
  ary.join(' ')
end
elem(name, opt={}) click to toggle source
# File lib/tofu.rb, line 310
def elem(name, opt={})
  head = ["#{name}", attr(opt)].compact.join(" ")
  if block_given?
    %Q!<#{head}>\n#{yield}\n</#{name}>!
  else
    %Q!<#{head} />!
  end  
end
form(method_name, context_or_param, context_or_empty=nil) click to toggle source
# File lib/tofu.rb, line 329
def form(method_name, context_or_param, context_or_empty=nil)
  if context_or_empty.nil? 
    context = context_or_param
    add_param = {}
  else
    context = context_or_empty
    add_param = context_or_param
  end
  param = make_param(method_name, add_param)
  hidden = input_hidden(param)
  %Q!<form action="#{action(context)}" method="post" enctype="multipart/form-data">\n! + hidden
end
href(method_name, add_param, context) click to toggle source
# File lib/tofu.rb, line 342
def href(method_name, add_param, context)
  param = make_param(method_name, add_param)
  ary = param.collect do |k, v|
    "#{u(k)}=#{u(v)}"
  end
  %Q!href="#{action(context)}?#{ary.join('&')}"!
end
input_hidden(param) click to toggle source
# File lib/tofu.rb, line 350
def input_hidden(param)
  ary = param.collect do |k, v|
          %Q!<input type="hidden" name="#{h(k)}" value="#{h(v)}" />\n!
  end
  ary.join('')
end
make_anchor(method, param, context) click to toggle source
# File lib/tofu.rb, line 357
def make_anchor(method, param, context)
  "<a #{href(method, param, context)}>"
end
make_param(method_name, add_param={}) click to toggle source
# File lib/tofu.rb, line 319
def make_param(method_name, add_param={})
  param = {
    'tofu_id' => tofu_id,
    'tofu_cmd' => method_name
  }
  param['tofu_seq'] = @tofu_seq if @tofu_seq
  param.update(add_param)
  return param
end