All files index.js

95.83% Statements 46/48
88.24% Branches 15/17
85.71% Functions 6/7
95.74% Lines 45/47
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 100 101 102 103 104 105 106 107 108              1x       2x       5x       25x 25x 25x 25x 25x       1x 1x 1x 1x 1x     70x   4x 66x 17x 49x 9x 40x 5x 5x 9x   5x   35x         19x 19x   19x 46x     19x 19x         1x 1x 1x 1x 1x 1x       1x     1x 1x 1x   1x     1x   1x     1x                                  
import * as form from './form'
import * as action from './action'
import * as rule from './rule'
import * as condition from './condition'
 
import debug from 'debug'
 
var log = debug('ddf')
 
class JsDictClsRegistry {
  constructor() {
    this.modules = {}
  }
 
  register(name, module) {
    this.modules[name] = module
  }
 
  get(name) {
    let parts = name.split('.')
    let className = parts.slice(-1)[0]
    let moduleName = parts.slice(0, -1).join('.')
    let module = this.modules[moduleName]
    return module[className]
  }
}
 
var jsRegistry = window.jsRegistry = new JsDictClsRegistry()
window.jsRegistry.register('ddf.action', action)
window.jsRegistry.register('ddf.condition', condition)
window.jsRegistry.register('ddf.form', form)
window.jsRegistry.register('ddf.rule', rule)
 
function convert(value) {
  if (value === undefined || value === null) {
    // inhibit further type and attribute checks
    return value
  } else if (value.cls !== undefined) {
    return instanciate(value)
  } else if (Array.isArray(value)) {
    return value.map(convert)
  } else if (value && typeof value === 'object' && value.constructor === Object) {
    let newvalue = {}
    for (let key in value) {
      newvalue[key] = convert(value[key])
    }
    return newvalue
  } else {
    return value
  }
}
 
function instanciate(attrs) {
  let cls = jsRegistry.get(attrs.cls)
  let obj = new cls()
 
  for (let key in attrs) {
    obj[key] = convert(attrs[key])
  }
 
  log('Instanciated', obj)
  return obj
}
 
function setup(script) {
  // ensure a ddf-hide class exists
  Eif (!window.document.getElementById('ddf-style')) {
    let style = window.document.createElement('style')
    style.type = 'text/css'
    style.id = 'ddf-style'
    style.innerHTML = '.ddf-hide { display: none; }'
    window.document.getElementsByTagName('head')[0].appendChild(style)
  }
 
  // instanciate a form with the configuration in the script tag
  let form = instanciate(JSON.parse(script.textContent))
 
  // bind configured Form instance to form element
  let htmlElement = script
  while (htmlElement = htmlElement.parentElement)
    Eif (htmlElement.matches('form')) break
 
  form.bind(htmlElement)
 
  // trigger initial form setup
  form.update()
 
  return form
}
 
document.addEventListener('DOMContentLoaded', function() {
  for (let script of document.querySelectorAll('script[type="text/ddf-configuration"]')) {
    setup(script)
  }
})
 
export {
  convert,
  instanciate,
  jsRegistry,
  JsDictClsRegistry,
  action,
  condition,
  form,
  rule,
  setup
}