Class: Cuba::Tools::Widget::Base
- Defined in:
- lib/cuba/tools/widget/base.rb
Constant Summary collapse
- JS_ESCAPE =
{ '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }
- PARTIAL_REGEX =
Regexp.new '([a-zA-Z_]+)$'
- VIEW_TYPES =
%w(slim erb haml)
Class Attribute Summary collapse
-
.available_helper_methods ⇒ Object
Returns the value of attribute available_helper_methods.
-
.events ⇒ Object
Returns the value of attribute events.
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#events ⇒ Object
Returns the value of attribute events.
-
#name ⇒ Object
Returns the value of attribute name.
-
#req ⇒ Object
Returns the value of attribute req.
-
#res ⇒ Object
Returns the value of attribute res.
-
#widget_state ⇒ Object
Returns the value of attribute widget_state.
Class Method Summary collapse
Instance Method Summary collapse
- #current_user ⇒ Object
- #escape(js) ⇒ Object
- #id_for(state) ⇒ Object
-
#initialize(app, res, req, name, events) ⇒ Base
constructor
A new instance of Base.
- #partial(template, locals = {}) ⇒ Object
- #render(*args) ⇒ Object
- #render_state(options = {}) ⇒ Object
- #replace(state, opts = {}) ⇒ Object
- #reset_state ⇒ Object
- #set_state(state) ⇒ Object
- #trigger(widget_event, data = {}) ⇒ Object
- #trigger_event(widget_name, widget_event, data = {}) ⇒ Object
- #view_path ⇒ Object
Constructor Details
#initialize(app, res, req, name, events) ⇒ Base
Returns a new instance of Base.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/cuba/tools/widget/base.rb', line 10 def initialize app, res, req, name, events @app = app @res = res @req = req @name = name.to_s @events = events @widget_state = false events.add_observer self, :trigger_event end |
Class Attribute Details
.available_helper_methods ⇒ Object
Returns the value of attribute available_helper_methods.
158 159 160 |
# File 'lib/cuba/tools/widget/base.rb', line 158 def available_helper_methods @available_helper_methods end |
.events ⇒ Object
Returns the value of attribute events.
158 159 160 |
# File 'lib/cuba/tools/widget/base.rb', line 158 def events @events end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
8 9 10 |
# File 'lib/cuba/tools/widget/base.rb', line 8 def app @app end |
#events ⇒ Object
Returns the value of attribute events.
8 9 10 |
# File 'lib/cuba/tools/widget/base.rb', line 8 def events @events end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/cuba/tools/widget/base.rb', line 8 def name @name end |
#req ⇒ Object
Returns the value of attribute req.
8 9 10 |
# File 'lib/cuba/tools/widget/base.rb', line 8 def req @req end |
#res ⇒ Object
Returns the value of attribute res.
8 9 10 |
# File 'lib/cuba/tools/widget/base.rb', line 8 def res @res end |
#widget_state ⇒ Object
Returns the value of attribute widget_state.
8 9 10 |
# File 'lib/cuba/tools/widget/base.rb', line 8 def @widget_state end |
Class Method Details
.respond_to(event, opts = {}) ⇒ Object
160 161 162 163 |
# File 'lib/cuba/tools/widget/base.rb', line 160 def respond_to event, opts = {} @events ||= [] @events << [event.to_s, opts] end |
.responds_to(*events) ⇒ Object
165 166 167 168 169 170 |
# File 'lib/cuba/tools/widget/base.rb', line 165 def responds_to *events @events ||= [] events.each do |event| @events << [event, {}] end end |
Instance Method Details
#current_user ⇒ Object
116 117 118 |
# File 'lib/cuba/tools/widget/base.rb', line 116 def current_user app.current_user end |
#escape(js) ⇒ Object
153 154 155 |
# File 'lib/cuba/tools/widget/base.rb', line 153 def escape js js.to_s.gsub(/(\\|<\/|\r\n|\\3342\\2200\\2250|[\n\r"'])/) {|match| JS_ESCAPE[match] } end |
#id_for(state) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/cuba/tools/widget/base.rb', line 146 def id_for state w_name = req.env[:widget_name].to_s.gsub(/_/, '-') w_state = state.to_s.gsub(/_/, '-') "#{w_name}-#{w_state}" end |
#partial(template, locals = {}) ⇒ Object
111 112 113 114 |
# File 'lib/cuba/tools/widget/base.rb', line 111 def partial template, locals = {} locals[:partial] = template render locals end |
#render(*args) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/cuba/tools/widget/base.rb', line 71 def render *args if args.first.kind_of? Hash locals = args.first # if it's a partial we add an underscore infront of it state = view = locals[:state] || "#{locals[:partial]}".gsub(PARTIAL_REGEX, '_\1') else state = view = args.first locals = args.length > 1 ? args.last : {} end # set the state and view if it's blank if view.blank? state = view = caller[0][/`.*'/][1..-2] # override state if widget_state set elsif !locals[:state] && state = end req.env[:widget_name] = name req.env[:widget_state] = state view_folder = self.class.to_s.gsub( /\w+::Widgets::/, '' ).split('::').map(&:underscore).join('/') # check for the extension file = false VIEW_TYPES.each do |type| file = "#{view_path}/#{view_folder}/#{view}.#{type}" if File.file? file break else file = false end end app.render file, locals end |
#render_state(options = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/cuba/tools/widget/base.rb', line 29 def render_state = {} state = || [:state] if method(state).parameters.length > 0 public_send(state, .to_deep_ostruct) else public_send(state) end end |
#replace(state, opts = {}) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/cuba/tools/widget/base.rb', line 124 def replace state, opts = {} if !state.is_a? String opts[:state] = state content = render_state opts selector = '#' + id_for(state) else if !opts.key?(:content) and !opts.key?(:with) opts[:state] = caller[0][/`.*'/][1..-2] content = render_state opts else content = opts[:content] || opts[:with] end selector = state end res.write '$("' + selector + '").replaceWith("' + escape(content) + '");' # scroll to the top of the page just as if we went to the url directly # if opts[:scroll_to_top] # res.write 'window.scrollTo(0, 0);' # end end |
#reset_state ⇒ Object
25 26 27 |
# File 'lib/cuba/tools/widget/base.rb', line 25 def reset_state @widget_state = false end |
#set_state(state) ⇒ Object
21 22 23 |
# File 'lib/cuba/tools/widget/base.rb', line 21 def set_state state @widget_state = state end |
#trigger(widget_event, data = {}) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/cuba/tools/widget/base.rb', line 39 def trigger , data = {} = data.delete(:for) req.env[:loaded_widgets].each do |n, w| w.trigger_event ( || req.params['widget_name']), , data.to_deep_ostruct end end |
#trigger_event(widget_name, widget_event, data = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/cuba/tools/widget/base.rb', line 48 def trigger_event , , data = {} if class_events = self.class.events class_events.each do |class_event, opts| if class_event.to_s == .to_s && ( .to_s == name or opts[:for].to_s == .to_s ) if not opts[:with] e = else e = opts[:with] end if method(e) and method(e).parameters.length > 0 send(e, data) else send(e) end end end end end |