Class: Lipsiadmin::Ext::Component
- Inherits:
-
Object
- Object
- Lipsiadmin::Ext::Component
- Defined in:
- lib/view/helpers/ext/component.rb
Overview
This is the base class of ext components
You can generate your custom ExtJs objects like:
# Generates:
# var groupingView = Ext.grid.GroupingView({
# forceFit: true
# });
Component.new("Ext.grid.GroupingView", { :forceFit => true });
If you want to override our default templates you can do easly with:
Lipsiadmin::Ext::Component.template_paths.unshift("/path/to/my/js/templates")
Direct Known Subclasses
Constant Summary collapse
- @@template_paths =
["#{File.dirname(__FILE__)}/templates", "#{Rails.root}/app/views/backend"]
Instance Method Summary collapse
-
#add(klass, options = {}, &block) ⇒ Object
Generates and add new Component for generate on the fly ExtJs Objects.
-
#after ⇒ Object
Returns an array of javascripts to add afters component is rendered.
-
#before ⇒ Object
Returns an array of javascripts to add before component is rendered.
-
#config ⇒ Object
Return the configuration hash.
-
#config=(options = {}) ⇒ Object
Write the the configuration of object from an hash.
-
#get_var ⇒ Object
Get the var used by the component defaults is the id of the component.
-
#id(new_id) ⇒ Object
The id of the component.
-
#initialize(klass, options = {}, &block) ⇒ Component
constructor
:nodoc:.
-
#method_missing(method, arguments = nil, &block) ⇒ Object
:nodoc:.
-
#on(event, function = nil, scope = nil, &block) ⇒ Object
Generates a new handler for the given component.
-
#prefix=(value) ⇒ Object
Set the prefix for the var of the component.
-
#raise_error(error) ⇒ Object
:nodoc:.
-
#title(title, global = true) ⇒ Object
Define the title of the component.
-
#to_s(options = {}) ⇒ Object
Returns the javascript for current component.
-
#un(event) ⇒ Object
Remove a listener.
-
#var(var) ⇒ Object
Set var used by the component.
-
#with_output_buffer(buf = '') ⇒ Object
Used by ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.
Constructor Details
#initialize(klass, options = {}, &block) ⇒ Component
:nodoc:
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/view/helpers/ext/component.rb', line 25 def initialize(klass, ={}, &block)#:nodoc: @klass = klass @prefix = .delete(:prefix) @var = .delete(:var) @config = Configuration.new() @before, @after = [], [] @items, @un = {}, {} if self.class == Component && block_given? yield self end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, arguments = nil, &block) ⇒ Object
:nodoc:
85 86 87 88 89 90 91 |
# File 'lib/view/helpers/ext/component.rb', line 85 def method_missing(method, arguments=nil, &block)#:nodoc: if method.to_s =~ /^get_/ @config[method.to_s.gsub(/^get_/, "").to_sym] else add_object(method, arguments) end end |
Instance Method Details
#add(klass, options = {}, &block) ⇒ Object
Generates and add new Component for generate on the fly ExtJs Objects
Examples:
# Generates:
# var panel = new Ext.Panel({
# id: 'testPanel',
# region: 'center',
# ...
# })
# mycmp.add(panel)
#
mycmp.add "Ext.Panel" do |panel|
panel.id "testPanel",
panel.region :center
...
end
170 171 172 |
# File 'lib/view/helpers/ext/component.rb', line 170 def add(klass, ={}, &block) add_object(Component.new(klass, .merge(:prefix => get_var), &block)) end |
#after ⇒ Object
Returns an array of javascripts to add afters component is rendered.
109 110 111 |
# File 'lib/view/helpers/ext/component.rb', line 109 def after @after end |
#before ⇒ Object
Returns an array of javascripts to add before component is rendered.
103 104 105 |
# File 'lib/view/helpers/ext/component.rb', line 103 def before @before end |
#config ⇒ Object
Return the configuration hash
81 82 83 |
# File 'lib/view/helpers/ext/component.rb', line 81 def config @config end |
#config=(options = {}) ⇒ Object
Write the the configuration of object from an hash
75 76 77 |
# File 'lib/view/helpers/ext/component.rb', line 75 def config=(={}) @config = Configuration.new() end |
#get_var ⇒ Object
Get the var used by the component defaults is the id of the component
54 55 56 57 58 59 60 61 62 |
# File 'lib/view/helpers/ext/component.rb', line 54 def get_var # I will nillify obj if they are blank @var = nil if @var.blank? @config.delete(:var) if @config[:var].blank? # Return a correct var current_var = (@var || @config[:var] || build_var) @prefix.to_s + current_var.to_s end |
#id(new_id) ⇒ Object
The id of the component
39 40 41 |
# File 'lib/view/helpers/ext/component.rb', line 39 def id(new_id) @config[:id] = new_id end |
#on(event, function = nil, scope = nil, &block) ⇒ Object
Generates a new handler for the given component
Examples:
# Generates:
# grid.on("dblclick", function() {
# edit();
# new();
# Ext.Msg.alert("Hello", "world");
# });
grid.on :dblclick do |p|
p.call "edit"
p.call "new"
p.ext_alert "Hello", "world"
end
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/view/helpers/ext/component.rb', line 129 def on(event, function=nil, scope=nil, &block) # Remove old handlers un(event) @un[event.to_sym] = false # we need to reset it scope = ", #{scope.to_l}" unless scope.blank? if function after << "#{get_var}.on(#{event.to_json}, #{function}#{scope});" else generator = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(self, &block) after << "#{get_var}.on(#{event.to_json}, function() { \n #{generator.to_s.gsub("\n", "\n ")}\n}#{scope});" end end |
#prefix=(value) ⇒ Object
Set the prefix for the var of the component. This is usefull when for example we are using two grids for solve conflict problems.
97 98 99 |
# File 'lib/view/helpers/ext/component.rb', line 97 def prefix=(value) @prefix = value end |
#raise_error(error) ⇒ Object
:nodoc:
193 194 195 |
# File 'lib/view/helpers/ext/component.rb', line 193 def raise_error(error)#:nodoc: raise ComponentError, error end |
#title(title, global = true) ⇒ Object
Define the title of the component.
Every component can have a title, because in Lipsiadmin we use it as a “pagetitle”, but if you need it as a config you can provide global = false
69 70 71 |
# File 'lib/view/helpers/ext/component.rb', line 69 def title(title, global=true) global ? (before << "Backend.app.setTitle(#{title.to_json});") : config[:title] = title end |
#to_s(options = {}) ⇒ Object
184 185 186 187 188 189 190 191 |
# File 'lib/view/helpers/ext/component.rb', line 184 def to_s(={}) script = [].tap do |script| script << @before.uniq.compact.join("\n\n") script << "var #{get_var} = new #{@klass}(#{config.to_s});" script << @after.uniq.compact.join("\n\n") end script.delete_if { |s| s.blank? }.join("\n\n") end |
#un(event) ⇒ Object
Remove a listener
Example: grid.un(:dblclick)
146 147 148 149 150 |
# File 'lib/view/helpers/ext/component.rb', line 146 def un(event) @un[event.to_sym] = true found = @after.delete_if { |s| s.start_with?("#{get_var}.on(#{event.to_json}") if s.is_a?(String) } after << "#{get_var}.un(#{event.to_json})" unless found end |
#var(var) ⇒ Object
Set var used by the component
Generates: var myVar = new Ext.Grid({...});
store.var "myVar"
48 49 50 |
# File 'lib/view/helpers/ext/component.rb', line 48 def var(var) @var = var end |
#with_output_buffer(buf = '') ⇒ Object
Used by ActionView::Helpers::PrototypeHelper::JavaScriptGenerator
175 176 177 |
# File 'lib/view/helpers/ext/component.rb', line 175 def with_output_buffer(buf = '')#:nodoc: yield end |