Class: Conjoin::Widgets::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/conjoin/widgets.rb

Constant Summary collapse

JS_ESCAPE =
{ '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" }

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options
  @app          = app
  @res          = res
  @req          = req
  @settings     = settings
  @event        = event
  @folder       = folder
  @options      = 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_methodsObject

Returns the value of attribute available_helper_methods.



319
320
321
# File 'lib/conjoin/widgets.rb', line 319

def available_helper_methods
  @available_helper_methods
end

.eventsObject

Returns the value of attribute events.



319
320
321
# File 'lib/conjoin/widgets.rb', line 319

def events
  @events
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



135
136
137
# File 'lib/conjoin/widgets.rb', line 135

def app
  @app
end

#eventObject

Returns the value of attribute event.



135
136
137
# File 'lib/conjoin/widgets.rb', line 135

def event
  @event
end

#folderObject

Returns the value of attribute folder.



135
136
137
# File 'lib/conjoin/widgets.rb', line 135

def folder
  @folder
end

#optionsObject

Returns the value of attribute options.



135
136
137
# File 'lib/conjoin/widgets.rb', line 135

def options
  @options
end

#reqObject

Returns the value of attribute req.



135
136
137
# File 'lib/conjoin/widgets.rb', line 135

def req
  @req
end

#resObject

Returns the value of attribute res.



135
136
137
# File 'lib/conjoin/widgets.rb', line 135

def res
  @res
end

#settingsObject

Returns the value of attribute settings.



135
136
137
# File 'lib/conjoin/widgets.rb', line 135

def settings
  @settings
end

#widget_stateObject

Returns the value of attribute widget_state.



135
136
137
# File 'lib/conjoin/widgets.rb', line 135

def widget_state
  @widget_state
end

Class Method Details

.helper_method(method) ⇒ Object



333
334
335
336
# File 'lib/conjoin/widgets.rb', line 333

def helper_method method
  @available_helper_methods ||= []
  @available_helper_methods << method
end

.helper_methods(*methods) ⇒ Object



338
339
340
341
342
# File 'lib/conjoin/widgets.rb', line 338

def helper_methods *methods
  methods.each do |method|
    helper_method method
  end
end

.respond_to(event, opts = {}) ⇒ Object



321
322
323
324
# File 'lib/conjoin/widgets.rb', line 321

def respond_to event, opts = {}
  @events ||= []
  @events << [event, opts]
end

.responds_to(*events) ⇒ Object



326
327
328
329
330
331
# File 'lib/conjoin/widgets.rb', line 326

def responds_to *events
  @events ||= []
  events.each do |event|
    @events << [event, {}]
  end
end

Instance Method Details

#add_after(selector, opts = {}) ⇒ Object



201
202
203
204
# File 'lib/conjoin/widgets.rb', line 201

def add_after selector, opts = {}
  content = render opts
  res.write '$("' + selector + '").after("'+ escape(content) +'");'
end

#append(selector, opts = {}) ⇒ Object



196
197
198
199
# File 'lib/conjoin/widgets.rb', line 196

def append selector, opts = {}
  content = render opts
  res.write '$("' + selector + '").append("'+ escape(content) +'");'
end

#attrs_for(selector, opts = {}) ⇒ Object



206
207
208
# File 'lib/conjoin/widgets.rb', line 206

def attrs_for selector, opts = {}
  res.write '$("' + selector + '").attr('+ (opts.to_json) +');'
end

#current_userObject



156
157
158
# File 'lib/conjoin/widgets.rb', line 156

def current_user
  app.current_user
end

#escape(js) ⇒ Object



210
211
212
# File 'lib/conjoin/widgets.rb', line 210

def escape js
  js.to_s.gsub(/(\\|<\/|\r\n|\\3342\\2200\\2250|[\n\r"'])/) {|match| JS_ESCAPE[match] }
end

#hide(selector) ⇒ Object



192
193
194
# File 'lib/conjoin/widgets.rb', line 192

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_changeObject



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

#partial(template, locals = {}) ⇒ Object



256
257
258
259
# File 'lib/conjoin/widgets.rb', line 256

def partial template, locals = {}
  locals[:partial] = template
  render locals
end

#render(*args) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/conjoin/widgets.rb', line 261

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 widget_state

  unless view.present?
    state = view = caller[0][/`.*'/][1..-2]

    if (options.key?(:from_event) and !options.key?(:replace))
      @options[:cache] = false
    end
  end

  if locals.key?(:state) and state and state.to_s == view.to_s
    if method(state).parameters.length > 0
      return send(state, locals.to_ostruct)
    else
      return send(state)
    end
  end

  tmpl_engine = settings[:render][:template_engine]

  if (req_helper_methods = req.env[:widgets][folder][:req_helper_methods]) \
  and (!options.key?(:cache))
    locals.reverse_merge! req_helper_methods
  else
    req.env[:widgets][folder][:req_helper_methods] = {}

    helper_methods.each do |method|
      unless locals.key? method
        req.env[:widgets][folder][:req_helper_methods][method] = locals[method] = self.send method
      end
    end
  end

  req.env[:widget_name]  = folder
  req.env[:widget_state] = state

  locals[:w] = locals[:widget] = self

  view_folder = self.class.to_s.gsub(/\w+::Widgets::/, '').split('::').map(&:underscore).join('/')
  app.render "#{app.widgets_root}/#{view_folder}/#{view}.#{tmpl_engine}", locals
end

#render_state(options = {}) ⇒ Object



248
249
250
251
252
253
254
# File 'lib/conjoin/widgets.rb', line 248

def render_state options = {}
  if method(state).parameters.length > 0
    send(state, options.to_ostruct)
  else
    send(state)
  end
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
189
190
# 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
  # if opts[:scroll_to_top]
  #   res.write 'window.scrollTo(0, 0);'
  # end
end

#set_state(state) ⇒ Object



240
241
242
# File 'lib/conjoin/widgets.rb', line 240

def set_state state
  @widget_state = state
end

#stateObject



244
245
246
# File 'lib/conjoin/widgets.rb', line 244

def state
  @widget_state
end

#trigger(t_event, data = {}) ⇒ Object



214
215
216
217
218
# File 'lib/conjoin/widgets.rb', line 214

def trigger t_event, data = {}
  req.env[:loaded_widgets].each do |n, w|
    w.trigger_event t_event, req.params['widget_name'], data.to_ostruct
  end
end

#trigger_event(t_event, widget_name, data = {}) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/conjoin/widgets.rb', line 220

def trigger_event t_event, widget_name, data = {}
  if events = self.class.events
    events.each do |class_event, opts|
      if class_event == t_event && (widget_name == folder.to_s or opts[:for].to_s == widget_name)
        if not opts[:with]
          e = t_event
        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