Module: RightRails::Helpers

Defined in:
lib/right_rails/helpers.rb

Overview

There is the namespace for the helpers and some private methods

Defined Under Namespace

Modules: Basic, Forms, Misc, Rails

Constant Summary collapse

XHR_OPTION_KEYS =

Varios RightJS unit keys

%w{
  method
  encoding
  async
  evalScripts
  evalResponse
  evalJSON
  secureJSON
  urlEncoded
  spinner
  spinnerFx
  params
}
CALENDAR_OPTION_KEYS =
%w{
  format
  showTime
  showButtons
  minDate
  maxDate
  twentyFourHour
  timePeriod
  listYears
  firstDay
  numberOfMonths
  fxName
  fxDuration
  hideOnPick
  update
  trigger
}
AUTOCOMPLETER_OPTION_KEYS =
%w{
  url
  param
  method
  minLength
  threshold
  cache
  local
  fxName
  fxDuration
  spinner
}
SLIDER_OPTION_KEYS =
%w{
  min
  max
  snap
  value
  direction
  update
  round
}
RATER_OPTION_KEYS =
%w{
  html
  size
  value
  update
  disabled
  disableOnVote
  url
  param
  Xhr
}
COLORPICKER_OPTION_KEYS =
%w{
  format
  update
  updateBg
  trigger
  fxName
  fxDuration
}
TABS_OPTION_KEYS =
%w{
  idPrefix
  tabsElement
  resizeFx
  resizeDuration
  scrollTabs
  scrollDuration
  selected
  disabled
  closable
  loop
  loopPause
  url
  cache
  Xhr
  Cookie
}
TAGS_OPTION_KEYS =
%w{
  tags
  vertical
  allowNew
  nocase
  autocomplete
  separator
}
%w{
  group
  endOpacity
  fxDuration
  hideOnEsc
  hideOnOutClick
  showCloseButton
  blockContent
  mediaWidth
  mediaHeight
}
RESIZABLE_OPTION_KEYS =
%w{
  minWidth
  maxWidth
  minHeight
  maxHeight
}
RTE_OPTION_KEYS =
%w{
  toolbar
  autoresize
  showToolbar
  showStatus
  videoSize
}
SORTABLE_OPTION_KEYS =
%w{
  url
  direction
  tags
  method
  Xhr
  idParam
  posParam
  parseId
  dragClass
  accept
  minLength
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_unit_options(options, unit) ⇒ Object

Adds the unit options to the options list



306
307
308
309
310
311
312
313
314
315
316
# File 'lib/right_rails/helpers.rb', line 306

def add_unit_options(options, unit)
  options_string = unit_options(options, unit)

  if RightRails::Config.rightjs_version > 1
    options["data-#{unit}"] = options_string
  elsif options_string != '{}'
    options["data-#{unit}-options"] = options_string
  end

  options
end

.build_xhr_request(options) ⇒ Object

Builds a RightJS based Xhr request call



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/right_rails/helpers.rb', line 334

def build_xhr_request(options)
  xhr = options[:submit] ?
    "new #{RightRails::Helpers.prefix}Xhr(" :
    "#{RightRails::Helpers.prefix}Xhr.load("

  xhr << "'#{options[:url]}'"

  # building the options
  xhr_options = { :onSuccess => '',  :onFailure => '', :onComplete => '' }

  # grabbing the standard XHR options
  options.each do |key, value|
    if XHR_OPTION_KEYS.include?(key.to_s)
      xhr_options[key] = case value.class.name.to_sym
        when :NilClass then 'null'
        when :String   then "'#{value}'"
        when :Symbol   then key.to_s == 'method' ? "'#{value}'" : "#{value}"
        else           value.inspect
      end
    end
  end

  # checking the parameters options
  xhr_options[:params] = options[:with]        if options[:with]

  # checking the callbacks
  xhr_options[:onSuccess]  = "#{options[:success]};"  if options[:success]
  xhr_options[:onFailure]  = "#{options[:failure]};"  if options[:failure]
  xhr_options[:onComplete] = "#{options[:complete]};" if options[:complete]

  # checking the update option
  if options[:update]
    template = options[:position] ?
      "#{prefix}$('%s').insert(this.text,'%s')" :
      "#{prefix}$('%s').update(this.text)%s"

    if options[:update].is_a?(Hash)
      xhr_options[:onSuccess]  << template % [options[:update][:success], options[:position]] if options[:update][:success]
      xhr_options[:onFailure]  << template % [options[:update][:failure], options[:position]] if options[:update][:failure]
    else
      xhr_options[:onComplete] << template % [options[:update], options[:position]]
    end
  end

  # converting the callbacks
  [:onSuccess, :onFailure, :onComplete].each do |key|
    if xhr_options[key] == '' then xhr_options.delete key
    else xhr_options[key] = "function(request){#{xhr_options[key]}}"
    end
  end

  # ebbedding the xhr options
  pairs = xhr_options.collect do |key, value|
    if value == '' then nil
    else
      "#{key}:#{value}"
    end
  end.compact.sort

  xhr << ",{#{pairs.join(',')}}" unless pairs.empty?

  xhr << ')'

  # forms sending adjustements
  xhr << ".send(#{options[:submit]})" if options[:submit]
  xhr.gsub! /^.+?(,|(\)))/, prefix + '$(this).send(\2'  if options[:form]

  xhr
end

.css_prefixObject

Switches between the css class-names prefixes depending on current RightJS version



172
173
174
# File 'lib/right_rails/helpers.rb', line 172

def css_prefix
  RightRails::Config.rightjs_version < 2 ? 'right' : 'rui'
end

.get_keys_for(unit) ⇒ Object

returns a list of keys for the unit



327
328
329
# File 'lib/right_rails/helpers.rb', line 327

def get_keys_for(unit)
  const_get("#{unit.to_s.upcase}_OPTION_KEYS") || []
end

.modules_registry_for(context) ⇒ Object

Returns the rightjs-modules registry for the context



265
266
267
268
269
# File 'lib/right_rails/helpers.rb', line 265

def modules_registry_for(context)
  context.instance_eval do
    return @___rightjs_modules_registry ||= []
  end
end

.prefixObject

Adds the ‘RightJS.` prefix in safe-mode



164
165
166
# File 'lib/right_rails/helpers.rb', line 164

def prefix
  RightRails::Config.safe_mode? ? 'RightJS.' : ''
end

.remove_unit_options(options, unit) ⇒ Object

Removes the unit option keys out of the given options



321
322
323
324
# File 'lib/right_rails/helpers.rb', line 321

def remove_unit_options(options, unit)
  unit_keys = get_keys_for(unit)
  options.reject{ |k, v| unit_keys.include?(k.to_s) }
end

.require_js_module(context, *list) ⇒ Object

Tells the script that the user needs this module

Generally just a private modules handling interface



195
196
197
198
199
200
201
202
203
# File 'lib/right_rails/helpers.rb', line 195

def require_js_module(context, *list)
  registry = modules_registry_for(context)

  list.each do |name|
    unless registry.include?(name.to_s)
      registry << name.to_s
    end
  end
end

.required_js_files(context) ⇒ Object

Returns a list of required JavaScript files for RightJS



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
# File 'lib/right_rails/helpers.rb', line 208

def required_js_files(context)
  scripts = ['right']
  config  = RightRails::Config


  if config.include_scripts_automatically?
    # hooking up the 'rails' javascript module if required
    scripts << 'right/rails' if config.include_rails_module?

    # adding the modules if needed
    scripts += modules_registry_for(context).collect do |package|
      "right/#{package}"
    end

    # swapping to the sources in the development mode
    if config.swap_builds_and_sources? && config.dev_env?
      scripts = scripts.collect do |package|
        "#{package}-src"
      end
    end

    # loading up the locales if available
    if defined?(I18n)
      locale_file = "#{config.locales_path}/#{I18n.locale.to_s.downcase}"

      if File.exists? "#{locale_file}.js"
        scripts << locale_file.slice(config.public_path.size + "/javascripts/".size, locale_file.size)
      end
    end
  end

  # switching to CDN server if asked
  if !config.dev_env? && config.use_cdn_in_production?
    scripts.map! do |script|
      header  = File.read("#{config.public_path}/javascripts/#{script}.js", 100)

      if version = header[/\d+\.\d+\.\d+/]
        script += "-#{version}"
      end

      if script.slice(0, 6) == 'right/' # plugins and modules
        script.gsub! 'right/', (
          header.include?('/ui/') ? 'ui/' : 'plugins/'
        )
        script.gsub! 'plugins/', '' if script.include?('/i18n/')
      end

      "#{config.cdn_url}/#{script}.js"
    end
  end

  scripts
end

.unit_options(options, unit) ⇒ Object

Collects the RightJS unit options out of the given list of options

NOTE: will nuke matching keys out of the original options object

Parameters:

  • user's

    options

  • unit

    name



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/right_rails/helpers.rb', line 279

def unit_options(options, unit)
  unit_options = []
  unit_keys    = get_keys_for(unit)

  options.dup.each do |key, value|
    c_key = key.to_s.camelize.gsub!(/^[A-Z]/){ |m| m.downcase }

    if unit_keys.include?(c_key)
      value = options.delete key

      value = case value.class.name.to_sym
        when :NilClass then 'null'
        when :Symbol   then c_key == 'method' ? "'#{value}'" : "#{value}"
        when :String   then "'#{value}'"
        else                value.inspect
      end

      unit_options << "#{c_key}:#{value}"
    end
  end

  "{#{unit_options.sort.join(',')}}"
end

Instance Method Details

#html_safe(string) ⇒ Object



181
182
183
# File 'lib/right_rails/helpers.rb', line 181

def html_safe(string)
  string.html_safe if string
end