All files index.js

95.74% Statements 45/47
88.24% Branches 15/17
85.71% Functions 6/7
95.65% Lines 44/46
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            1x       2x       4x       22x 22x 22x 22x 22x       1x 1x 1x 1x     64x   4x 60x 15x 45x 7x 38x 5x 5x 9x   5x   33x         17x 17x   17x 42x     17x 17x         1x 1x 1x 1x 1x 1x       1x     1x 1x 1x   1x     1x   1x     1x                                
import * as forms from './forms'
import * as actions from './actions'
import * as conditions from './conditions'
 
import debug from 'debug'
 
var log = debug('facond')
 
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('facond.actions', actions)
window.jsRegistry.register('facond.conditions', conditions)
window.jsRegistry.register('facond.forms', forms)
 
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 facond-hide class exists
  Eif (!window.document.getElementById('facond-style')) {
    let style = window.document.createElement('style')
    style.type = 'text/css'
    style.id = 'facond-style'
    style.innerHTML = '.facond-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/facond-configuration"]')) {
    setup(script)
  }
})
 
export {
  convert,
  instanciate,
  jsRegistry,
  JsDictClsRegistry,
  actions,
  conditions,
  forms,
  setup
}