All files index.js

78.79% Statements 26/33
76.92% Branches 10/13
60% Functions 3/5
78.79% Lines 26/33
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                1x     9x   9x 9x 9x     9x 9x 9x 9x   9x       9x       4x 4x 4x   4x 11x   11x   11x 3x 8x     8x       4x 4x       1x           1x         1x                      
import * as form from './form'
import * as action from './action'
import * as rule from './rule'
import * as condition from './condition'
 
import $ from 'jquery'
import debug from 'debug'
 
var log = debug('ddf')
 
function getClass(name) {
  log('getClass(%s)', name)
 
  let parts = name.split('.')
  let pkg = parts[0]
  let map = {
    ddf: './'
  }
  let path = map['ddf'] + parts[1]
  let classname = parts[2]
  var tmp = require(path)
  let cls = tmp[classname]
 
  Iif (cls === undefined) {
    throw 'Could not load ' + name
  }
 
  return cls
}
 
function instanciate(attrs) {
  let cls = getClass(attrs.cls)
  log(cls)
  let obj = new cls()
 
  for (let key in attrs) {
    let value = attrs[key]
 
    Iif (attrs[key] === undefined || attrs[key] === null) {
      obj[key] = value
    } else if (value instanceof Array && value.length && value[0].cls !== undefined) {
      obj[key] = value.map(instanciate)
    } else Iif (attrs[key].cls !== undefined) {
      obj[key] = instanciate(attrs)
    } else {
      obj[key] = value
    }
  }
 
  log('Instanciated', obj)
  return obj
}
 
function setup() {
  $.fn.ddf = function(configuration) {
    let form = ddf.instanciate(configuration)
    form.bind($(this))
    form.update()
  }
 
  $('script[type="text/ddf-configuration"]').each(function() {
    $(this).parents('form').ddf(JSON.parse($(this).text()))
  })
}
 
$(document).ready(setup)
 
export {
  instanciate,
  getClass,
  action,
  condition,
  form,
  rule,
  setup
}