all files / src/ options.cjsx

81.25% Statements 13/16
50% Branches 3/6
100% Functions 3/3
86.67% Lines 13/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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                                                                                                                                                                       89× 89×      
_ = require('underscore')
 
# use ReactDatum.Options.set and .get to access please
__options = {
  # use global ReactBootstrap (if it exists)
  ReactBootstrap: window?.ReactBootstrap || null
  errorIconClass: null
}
 
###
  These are global options used to control various aspects
  of ReactDatum rendering and functionality.
 
  Currently supported configurable options:
 
    ReactBootstrap: Defaults to global 'ReactBootstrap' if it exists.
      If set this option will use ReactBootstrap for popovers such as when
      a Datum is ellipsized and for validation errors. 
      If not set, ReactDatum will use the HTML5 title tooltips for popovers
      
    RbOverlayProps: 
      You can change the placement, trigger, etc used for popovers when using
      ReactBootstrap.
      
    errorIconClass: default: null.  Icon css class to use for indicating 
      validation errors. If not set, a red unicode exclamation point is used.  
  
 
###
module.exports = class Options 
  
  ###
    These are defaulted onto whatever is provided via ReactDatum.Options.set().  
    
  ###
  @_defaults:
    errorIconClass: null
    ReactBootstrap: null
    RbOverlayProps: 
      trigger: ['hover','focus']
      placement: 'right'  
    LazyPhoto:
      notFoundUrl: "http://zulily.github.io/react-datum/img/petals.png"
      loadingUrl: "http://zulily.github.io/react-datum/img/blank.jpg"
        
        
      
  
  @_options: _.extend {}, @_defaults
  
  
  ###
    Use to set a ReactDatum option.  Arguments can be either `(key, value)` or `({key: value, key: value})`
      
    Example:
    ```
      ReactDatum = require('react-datum')
      
      // use the version of react bootstrap I got somewhere 
      ReactDatum.Options.set('ReactBootstrap', loadedBootstrapLib)
      
      // use the fontawesome 4.5 exclamation sign icon for errors
      ReactDatum.Options.set('errorIconClass', 'fa fa-exclamation-circle')
    
      // change the placement of the popover (if using ReactBootstrap)
      ReactDatum.Options.set({RbOverlayProps: {placement: 'bottom'}})
    ```
  ###
  @set: (option, value) ->
    
    _options = Options._options
    
    extension = {}
    Eif _.isObject(option)
      extension = option
    else
      extension[option] = value
  
    for key, value of extension
      # this allows the user to specify a single attribute within the options[key], 
      # like say RbOverlayProps.placement
      Eif @_options[key]? && _.isObject(@_options[key]) && _.isObject(value) 
        _.extend @_options[key], value
      else
        @_options[key] = value
      
    return @_options
    
 
  ###
    Get a previously set option or it's default if not set.  Returns full set of options if no option arg 
    is provided.
  ###
  @get: (option=null) ->
    Ireturn _.extend({}, @_options) unless option?
    return @_options[option]