module OrbitApp module Module module WidgetUtility Version = "0.2" module ClassMethods @@widgets = {} 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 def initialize(name,key,&block) @widgets = [] @default_widget = nil @widget_title_enabled = true @column_title_enabled = false @categories_query = '' @tags_query = '' block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given? 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 # 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 eval(@categories_query) rescue nil end def get_tags eval(@tags_query) rescue nil end def default_widget_setting @default_widget end def default_widget(&block) @default_widget = DefaultWidget.new(&block) # 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 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_link_methods get_default_widget.get_link_methods end def get_default_widget return @default_widget end end class DefaultWidget include DefaultWidgetTagHelper STYLE = ["typeA","typeB_style2","typeB_style3","typeB_style4","typeC"] def initialize(&block) @query = nil @widget_title_enabled = true @column_title_enabled = false @image = nil @more_link = {} @fields = [] @enabled_styles = STYLE block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given? end def enable(args) @enabled_styles = args end 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 def self.get_interface_args {:name=>"default_widget",:field=>@fields,:style=>@enabled_styles,:i18n=>'default_widget.default_widget'} end def to_module_app_format {"query"=>@query,"image"=>@image,"more_link"=>@more_link,"enabled_styles"=>@enabled_styles,"widget_title_enabled"=>@widget_title_enabled, "column_title_enabled"=>@column_title_enabled} 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 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 widget_title_enabled(var) @widget_title_enabled = var end def column_title_enabled(var) @column_title_enabled = var end def image(var) @image = var end 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 {:name => @name,:fields=>@fields,:style=>@style,:i18n=>@widget_i18n} end protected end# of widget end # of WidgetRegisition end # of Module end # of OrbitApp