module JQueryOnRails module Helpers module JQueryUiHelper unless const_defined? :FX_OPTIONS FX_OPTIONS = { 'Up' => {:mode=>:hide,:direction=>:vertical}, 'Down' => {:mode=>:show,:direction=>:vertical}, 'Left' => {:mode=>:hide,:direction=>:horizontal}, 'Right' => {:mode=>:show,:direction=>:horizontal}, 'Out' => {:mode=>:hide,:direction=>nil}, 'In' => {:mode=>:show,:direction=>nil}, :fadeIn => {:mode=>:primitive,:direction=>nil}, :fadeOut => {:mode=>:primitive,:direction=>nil}, :animate => {:mode=>:primitive,:direction=>nil}, :toggleSlide => {:direction=>:up}, :puff => {:direction=>nil}, :size => {:direction=>nil}, :toggleAppear => {:direction=>nil,:mode=>"return element[name+(element.is(':hidden')?'In':'Out')](options);"} }.with_indifferent_access FX_NAMES = HashWithIndifferentAccess.new{|h,k| k} FX_NAMES.update :toggleAppear=>'fade', :appear=>'fadeIn', :fade=>'fadeOut', :morph=>'animate', :shrink=>'sizeOut', :grow=>'sizeIn' end # Generates jQuery UI effects. This expands upon the core jQuery 1.4 effects # that are generated by JQueryHelper#visual_effect def visual_effect(name, element_id=false, js_options={}) element = element_id ? ActiveSupport::JSON.encode("##{element_id}") : "element" fx_opt, name = FX_OPTIONS[name = name.to_s.camelize(:lower)] || {}, FX_NAMES[name] fx_opt ||= FX_NAMES[name] fx_opt[:mode], name = :toggle, FX_NAMES[name[6,1].downcase+name[7..-1]] if name.start_with? 'toggle' fx_opt = FX_OPTIONS[$1].merge fx_opt if ! FX_OPTIONS.include? name and name.sub! /(Up|Down|Left|Right|In|Out)$/, '' fx_opt = FX_OPTIONS[name].merge fx_opt if FX_OPTIONS.include? name fx_opt.merge! js_options if ! fx_opt.include? :direction then fx_opt[:direction] = "'vertical'" elsif fx_opt[:direction].nil? then fx_opt.delete :direction else fx_opt[:direction] = "'#{fx_opt[:direction]}'" end # [:endcolor, :direction, :startcolor, :scaleMode, :restorecolor] fx = "jQuery(#{element})" fx = "#{fx}.css('background-color','#{fx_opt.delete :startcolor}')" if fx_opt[:startcolor] if name=='animate' then fx_opt[:backgroundColor] ||= fx_opt.delete :endcolor elsif fx_opt[:endcolor] then fx = "#{fx}.animate('background-color','#{fx_opt.delete :endcolor}')" end method, fx_opt = fx_opt.delete(:mode)||:effect, (options_for_javascript fx_opt unless fx_opt.empty?) case method; when :primitive; "#{fx}.#{name}(#{fx_opt});" when Symbol; "#{fx}.#{method}('#{name}'#{','+fx_opt if fx_opt.present?});" else "(function(element,name,options){ #{method} })(#{fx},'#{name}'#{','+fx_opt if fx_opt.present?});" end end end end end