Orbit/lib/orbit_app/module/widget.rb

195 lines
5.2 KiB
Ruby
Raw Normal View History

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
2013-02-23 10:29:50 +00:00
# def data_count(var)
# @data_count = var
# end
def find_by_path(path)
@widgets.each do |widget|
return widget if widget.name == path
end
end
def get_categories
2012-12-03 10:52:36 +00:00
eval(@categories_query) rescue nil
end
2013-02-23 10:29:50 +00:00
def get_tags
2012-12-03 10:52:36 +00:00
eval(@tags_query) rescue nil
end
2013-02-23 10:29:50 +00:00
def default_widget_setting
@default_widget
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}
2012-12-03 10:52:36 +00:00
hash["default_widget"]= DefaultWidget.get_interface_args unless @default_widget.blank?
hash
end
def get_fields
get_default_widget.get_fields
end
2013-02-18 09:03:18 +00:00
def get_link_methods
get_default_widget.get_link_methods
end
def get_default_widget
return @default_widget
end
end
class DefaultWidget
2013-02-23 10:29:50 +00:00
include DefaultWidgetTagHelper
2012-12-03 10:52:36 +00:00
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
2013-02-23 10:29:50 +00:00
def link_to_more(*args)
@more_link[:label_i18n] = (args[1] ? args[1][:title_i18n] : 'default_widget.default_link_to_more' )
@more_link[:path_method] = args[0]
end
2012-12-03 10:52:36 +00:00
def self.get_interface_args
#will need to work with design
2013-02-18 07:13:31 +00:00
{:name=>"default_widget",:field=>@fields,:style=>STYLE,:i18n=>'default_widget.default_widget'}
2012-12-03 10:52:36 +00:00
end
def to_module_app_format
2013-02-23 10:29:50 +00:00
{"query"=>@query,"image"=>@image,"more_link"=>@more_link} 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
2013-02-18 09:03:18 +00:00
def get_link_methods
return flatten_hashes(@fields.select{|t| t[2]==:link}.map{|t| Hash[t[0].to_sym,t[1]] })
end
def query(var)
@query = var
end
def image(var)
@image = var
end
2013-02-18 09:03:18 +00:00
def flatten_hashes(ary)
Hash[*ary.map(&:to_a).flatten]
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
2013-02-18 07:13:31 +00:00
{:name => @name,:fields=>@fields,:style=>@style,:i18n=>@widget_i18n}
end
protected
end# of widget
end # of WidgetRegisition
end # of Module
end # of OrbitApp