Orbit/lib/orbit_app/module/widget.rb

206 lines
5.7 KiB
Ruby
Raw Permalink Normal View History

module OrbitApp
module Module
module WidgetUtility
2013-03-22 06:19:10 +00:00
Version = "0.2"
module ClassMethods
2013-03-22 06:19:10 +00:00
@@widgets = {}
2013-03-22 06:19:10 +00:00
def add(name,default_widget,customize_widget)
@@widgets[name] = {:default_widget=>default_widget,:customize_widget=>customize_widget}
Hash[@@widgets.sort]
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
2013-03-22 06:19:10 +00:00
def initialize(name,key,&block)
@widgets = []
@default_widget = nil
@categories_query = ''
@tags_query = ''
block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
2013-03-22 06:19:10 +00:00
OrbitApp::Module::WidgetUtility.add(key,@default_widget,@widgets)
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)
2013-03-22 06:19:10 +00:00
# OrbitApp::Module::FrontendUtility.add()
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
2013-03-22 06:19:10 +00:00
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
2013-09-02 09:24:28 +00:00
@sorting_query = 'desc(:created_at)'
@more_link = {}
@fields = []
@enabled_styles = STYLE
block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
end
2013-09-02 08:33:50 +00:00
def sorting(query)
@sorting_query = query
end
def enable(args)
@enabled_styles = args
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
{:name=>"default_widget",:field=>@fields,:style=>@enabled_styles,:i18n=>'default_widget.default_widget'}
2012-12-03 10:52:36 +00:00
end
def to_module_app_format
2013-09-02 08:33:50 +00:00
{"query"=>@query,"image"=>@image,"more_link"=>@more_link,"enabled_styles"=>@enabled_styles,:sorting_query=>@sorting_query} 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.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