Class: Conjoin::Widgets::Base
- Inherits:
-
Object
- Object
- Conjoin::Widgets::Base
- Defined in:
- lib/conjoin/widgets.rb
Constant Summary collapse
- JS_ESCAPE =
{ '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }
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.
-
#event ⇒ Object
Returns the value of attribute event.
-
#folder ⇒ Object
Returns the value of attribute folder.
-
#options ⇒ Object
Returns the value of attribute options.
-
#req ⇒ Object
Returns the value of attribute req.
-
#res ⇒ Object
Returns the value of attribute res.
-
#settings ⇒ Object
Returns the value of attribute settings.
-
#widget_state ⇒ Object
Returns the value of attribute widget_state.
Class Method Summary collapse
- .helper_method(method) ⇒ Object
- .helper_methods(*methods) ⇒ Object
- .respond_to(event, opts = {}) ⇒ Object
- .responds_to(*events) ⇒ Object
Instance Method Summary collapse
- #add_after(selector, opts = {}) ⇒ Object
- #append(selector, opts = {}) ⇒ Object
- #attrs_for(selector, opts = {}) ⇒ Object
- #current_user ⇒ Object
- #escape(js) ⇒ Object
- #hide(selector) ⇒ Object
- #id_for(state) ⇒ Object
-
#initialize(app, res, req, settings, event, folder, options) ⇒ Base
constructor
A new instance of Base.
- #page_change ⇒ Object
- #render(*args) ⇒ Object
- #render_state ⇒ Object
- #replace(state, opts = {}) ⇒ Object
- #set_state(state) ⇒ Object
- #trigger_event(event, widget_name, data = {}) ⇒ Object
Constructor Details
#initialize(app, res, req, settings, event, folder, options) ⇒ Base
Returns a new instance of Base.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/conjoin/widgets.rb', line 137 def initialize app, res, req, settings, event, folder, @app = app @res = res @req = req @settings = settings @event = event @folder = folder @options = @widget_state = false # add the widget to the req widgets req.env[:widgets] ||= {} unless req.env[:widgets][folder] req.env[:widgets][folder] = {} end event.add_observer self, :trigger_event end |
Class Attribute Details
.available_helper_methods ⇒ Object
Returns the value of attribute available_helper_methods.
287 288 289 |
# File 'lib/conjoin/widgets.rb', line 287 def available_helper_methods @available_helper_methods end |
.events ⇒ Object
Returns the value of attribute events.
287 288 289 |
# File 'lib/conjoin/widgets.rb', line 287 def events @events end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
135 136 137 |
# File 'lib/conjoin/widgets.rb', line 135 def app @app end |
#event ⇒ Object
Returns the value of attribute event.
135 136 137 |
# File 'lib/conjoin/widgets.rb', line 135 def event @event end |
#folder ⇒ Object
Returns the value of attribute folder.
135 136 137 |
# File 'lib/conjoin/widgets.rb', line 135 def folder @folder end |
#options ⇒ Object
Returns the value of attribute options.
135 136 137 |
# File 'lib/conjoin/widgets.rb', line 135 def @options end |
#req ⇒ Object
Returns the value of attribute req.
135 136 137 |
# File 'lib/conjoin/widgets.rb', line 135 def req @req end |
#res ⇒ Object
Returns the value of attribute res.
135 136 137 |
# File 'lib/conjoin/widgets.rb', line 135 def res @res end |
#settings ⇒ Object
Returns the value of attribute settings.
135 136 137 |
# File 'lib/conjoin/widgets.rb', line 135 def settings @settings end |
#widget_state ⇒ Object
Returns the value of attribute widget_state.
135 136 137 |
# File 'lib/conjoin/widgets.rb', line 135 def @widget_state end |
Class Method Details
.helper_method(method) ⇒ Object
301 302 303 304 |
# File 'lib/conjoin/widgets.rb', line 301 def helper_method method @available_helper_methods ||= [] @available_helper_methods << method end |
.helper_methods(*methods) ⇒ Object
306 307 308 309 310 |
# File 'lib/conjoin/widgets.rb', line 306 def helper_methods *methods methods.each do |method| helper_method method end end |
.respond_to(event, opts = {}) ⇒ Object
289 290 291 292 |
# File 'lib/conjoin/widgets.rb', line 289 def respond_to event, opts = {} @events ||= [] @events << [event, opts] end |
.responds_to(*events) ⇒ Object
294 295 296 297 298 299 |
# File 'lib/conjoin/widgets.rb', line 294 def responds_to *events @events ||= [] events.each do |event| @events << [event, {}] end end |
Instance Method Details
#add_after(selector, opts = {}) ⇒ Object
199 200 201 202 |
# File 'lib/conjoin/widgets.rb', line 199 def add_after selector, opts = {} content = render opts res.write '$("' + selector + '").after("'+ escape(content) +'");' end |
#append(selector, opts = {}) ⇒ Object
194 195 196 197 |
# File 'lib/conjoin/widgets.rb', line 194 def append selector, opts = {} content = render opts res.write '$("' + selector + '").append("'+ escape(content) +'");' end |
#attrs_for(selector, opts = {}) ⇒ Object
204 205 206 |
# File 'lib/conjoin/widgets.rb', line 204 def attrs_for selector, opts = {} res.write '$("' + selector + '").attr('+ (opts.to_json) +');' end |
#current_user ⇒ Object
156 157 158 |
# File 'lib/conjoin/widgets.rb', line 156 def current_user app.current_user end |
#escape(js) ⇒ Object
208 209 210 |
# File 'lib/conjoin/widgets.rb', line 208 def escape js js.to_s.gsub(/(\\|<\/|\r\n|\\3342\\2200\\2250|[\n\r"'])/) {|match| JS_ESCAPE[match] } end |
#hide(selector) ⇒ Object
190 191 192 |
# File 'lib/conjoin/widgets.rb', line 190 def hide selector res.write '$("' + selector + '").hide();' end |
#id_for(state) ⇒ Object
160 161 162 |
# File 'lib/conjoin/widgets.rb', line 160 def id_for state "#{req.env[:widget_name]}_#{state}" end |
#page_change ⇒ Object
164 165 166 167 |
# File 'lib/conjoin/widgets.rb', line 164 def page_change res.headers["Content-Type"] = "text/javascript; charset=utf-8" res.write '$(document).trigger("page:change");' end |
#render(*args) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/conjoin/widgets.rb', line 236 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(/([a-zA-Z_]+)$/, '_\1') else state = view = args.first locals = args.length > 1 ? args.last : {} end state = @widget_state if unless view state = view = caller[0][/`.*'/][1..-2] if (.key?(:from_event) and !.key?(:replace)) @options[:cache] = false end end if locals.key?(:state) and state.to_s == view.to_s return send state end tmpl_engine = settings[:render][:template_engine] if (req_helper_methods = req.env[:widgets][folder][:req_helper_methods]) \ and (!.key?(:cache)) locals.merge! req_helper_methods else req.env[:widgets][folder][:req_helper_methods] = {} helper_methods.each do |method| req.env[:widgets][folder][:req_helper_methods][method] = locals[method] = self.send method end end req.env[:widget_name] = folder req.env[:widget_state] = state locals[:w] = locals[:widget] = self app.render "#{app.}/#{folder}/#{view}.#{tmpl_engine}", locals end |
#render_state ⇒ Object
232 233 234 |
# File 'lib/conjoin/widgets.rb', line 232 def render_state render state: end |
#replace(state, opts = {}) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/conjoin/widgets.rb', line 169 def replace state, opts = {} @options[:replace] = true 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) content = render 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 res.write 'window.scrollTo(0, 0);' end |
#set_state(state) ⇒ Object
228 229 230 |
# File 'lib/conjoin/widgets.rb', line 228 def set_state state @widget_state = state end |
#trigger_event(event, widget_name, data = {}) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/conjoin/widgets.rb', line 212 def trigger_event event, , data = {} if events = self.class.events events.each do |class_event, opts| if class_event == event && ( == folder.to_s or opts[:for].to_s == ) event = opts[:with] if opts[:with] if method(event).parameters.length > 0 send(event, data) else send(event) end end end end end |