| Lines of Code | 73 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | import extend from 'extend' |
||
| 2 | |||
| 3 | export default class Form { |
||
| 4 | |||
| 5 | constructor() { |
||
| 6 | this._form = { |
||
| 7 | |||
| 8 | } |
||
| 9 | this._key = [] |
||
| 10 | } |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Get all input from a template |
||
| 14 | * @return {Array} array of input form |
||
| 15 | */ |
||
| 16 | get form(){ |
||
| 17 | return this._form |
||
| 18 | } |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Check if key is not is the form array |
||
| 22 | * @param {[type]} key [description] |
||
| 23 | * @return {[type]} [description] |
||
| 24 | */ |
||
| 25 | dontHaveKey(key){ |
||
| 26 | return typeof this._key[key] === 'undefined' || this._key[key] === null |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Add entry to abe engine form |
||
| 31 | * @param {String} type textarea | text | meta | link | image | ... |
||
|
|
|||
| 32 | * @param {String} key unique ID, no space allowed |
||
| 33 | * @param {String} desc input description |
||
| 34 | * @param {Int} max-length maximum characteres allowed inside input |
||
| 35 | * @param {String} tab tab name |
||
| 36 | * @param {String} jsonValue |
||
| 37 | * @return {Void} |
||
| 38 | */ |
||
| 39 | add(obj) { |
||
| 40 | var defaultValues = { |
||
| 41 | autocomplete: null, |
||
| 42 | block:'', |
||
| 43 | desc: '', |
||
| 44 | display: null, |
||
| 45 | editable: true, |
||
| 46 | key: '', |
||
| 47 | 'max-length': null, |
||
| 48 | order: 0, |
||
| 49 | placeholder: '', |
||
| 50 | prefill: false, |
||
| 51 | 'prefill-quantity': null, |
||
| 52 | reload: false, |
||
| 53 | required: false, |
||
| 54 | source: null, |
||
| 55 | tab: 'default', |
||
| 56 | type: 'text', |
||
| 57 | value: '', |
||
| 58 | visible: true |
||
| 59 | } |
||
| 60 | |||
| 61 | obj = extend(true, defaultValues, obj) |
||
| 62 | obj.key = obj.key.replace(/\./, '-') |
||
| 63 | |||
| 64 | if(obj.key.indexOf('[') < 0 && obj.key.indexOf('.') > -1) { |
||
| 65 | obj.block = obj.key.split('.')[0] |
||
| 66 | } |
||
| 67 | |||
| 68 | if(typeof this._form[obj.tab] === 'undefined' || this._form[obj.tab] === null) this._form[obj.tab] = {item:[]} |
||
| 69 | |||
| 70 | this._key[obj.key] = true // save key for dontHaveKey() |
||
| 71 | this._form[obj.tab].item.push(obj) |
||
| 72 | } |
||
| 73 | } |