orbit-basic/lib/orbit_app/module/widget.rb

173 lines
4.6 KiB
Ruby

module OrbitApp
module Module
module WidgetUtility
Version = "0.1"
module ClassMethods
@@widgets = []
def add(var)
@@widgets << var
end
def all
return @@widgets
end
end
extend ClassMethods
def self.included( other )
other.extend( ClassMethods )
end
class WidgetSet
attr_reader :widgets,:categories_query,:tags_query
def initialize(&block)
@widgets = []
@default_widget = nil
@categories_query = ''
@tags_query = ''
block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
end
%w{categories_query tags_query}.each do |field|
define_method(field){|var| instance_variable_set( "@" + field, var)}
end
def find_by_path(path)
@widgets.each do |widget|
return widget if widget.name == path
end
end
def get_categories
eval(@categories_query) rescue nil
end
def get_tags
eval(@tags_query) rescue nil
end
def default_widget(&block)
@default_widget = DefaultWidget.new(&block)
end
def customize_widget(name,&block)
@widgets << Widget.new(name,&block)
end
# def customize_widget(name,label,options ={})
# @widgets << Widget.new(name,label,options)
# end
# def customize_widget_options_fields_i18n(settings)
# @customize_widget_options_fields_i18n = settings
# end
def to_module_app_format
hash = {}
@widgets.collect{|t| hash[t.name] = t.to_hash}
hash["default_widget"]= DefaultWidget.get_interface_args unless @default_widget.blank?
hash
end
def get_fields
get_default_widget.get_fields
end
def get_default_widget
return @default_widget
end
end
class DefaultWidget
STYLE = ["typeA","typeB_style2","typeB_style3","typeB_style4","typeC"]
def initialize(&block)
@query = nil
@image = nil
@more_link = {}
@fields = []
block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
end
def more_link_to(label_i18n,path_method)
@more_link[:label_i18n] = label_i18n
@more_link[:path_method] = path_method
end
def self.get_interface_args
#will need to work with design
{:name=>"default_widget",:field=>nil,:label=>'label',:style=>STYLE,:i18n=>'default_widget.default_widget'}
end
def to_module_app_format
{:query=>@query,:image=>@image} rescue nil
end
def link_field(field_name,setting)
@fields << [field_name,setting,:link]
end
def field(field_name)
@fields << [field_name,nil,:field]
end
def get_fields
@fields
end
def query(var)
@query = var
end
def image(var)
@image = var
end
end
class Widget
attr_reader :name,:default_template,:fields
def initialize(name,&block)
@name = name
@options = {}
block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
# @fields = options[:fields]
# @label = label
# @style= options[:style]
end
def widget_i18n(str) # "gallery.widget.widget1"
@widget_i18n = str
end
def style(ary)# []
@style = ary
end
def get_options
@options
end
def options(opt_name,*options) # "vertical",
option = options[0]
@options[opt_name] = {:label_i18n=>option[:i18n] , :opts=>option[:options_item]}
# options => :i18n => "gallery.widget_option.vertical",:options_item=>[1, 2]
# :i18n =>"gallery.widget_option.album",:options_item => {"query"=>"GalleryAlbum.all", "value"=>:id, "label"=>:name}
end
def to_hash
{:name => @name,:fields=>@fields,:label=>@label,:style=>@style,:i18n=>@widget_i18n}
end
protected
end# of widget
end # of WidgetRegisition
end # of Module
end # of OrbitApp