| | 1 | /* |
|---|
| | 2 | ConfigObject : |
|---|
| | 3 | { 'simple value key' : 'simple value' |
|---|
| | 4 | , 'just child values key' : ConfigObject |
|---|
| | 5 | , 'value + child values key' : [ 'simple value', ConfigObject ] |
|---|
| | 6 | } |
|---|
| | 7 | */ |
|---|
| | 8 | |
|---|
| | 9 | // main call for turning configNodes into form objects |
|---|
| | 10 | function formDisplayResponse(container, response) { |
|---|
| | 11 | container.innerHTML = ""; |
|---|
| | 12 | var configObject = Ext.util.JSON.decode(response.responseText); |
|---|
| | 13 | var configObjectDom = configObjectToDom(configObject); |
|---|
| | 14 | container.appendChild(configObjectDom); |
|---|
| | 15 | } |
|---|
| | 16 | |
|---|
| | 17 | // top call, recursable call for turning configObjects |
|---|
| | 18 | // into form fields |
|---|
| | 19 | function configObjectToDom(configObject, depth) { |
|---|
| | 20 | |
|---|
| | 21 | // contain fields in a section |
|---|
| | 22 | var div = document.createElement("div"); |
|---|
| | 23 | div.className = "section"; |
|---|
| | 24 | |
|---|
| | 25 | // first collect keys by type... simple and complex |
|---|
| | 26 | var simpleKeys = []; |
|---|
| | 27 | var complexKeys = []; |
|---|
| | 28 | for(var entry in configObject) { |
|---|
| | 29 | var value = configObject[entry]; |
|---|
| | 30 | if(typeof(value) == "string") { |
|---|
| | 31 | simpleKeys[simpleKeys.length] = entry; |
|---|
| | 32 | } else { |
|---|
| | 33 | complexKeys[complexKeys.length] = entry; |
|---|
| | 34 | } |
|---|
| | 35 | } |
|---|
| | 36 | |
|---|
| | 37 | // first display simple key value pairs, sorted alphabetically |
|---|
| | 38 | simpleKeys.sort(); |
|---|
| | 39 | for(var i = 0; i < simpleKeys.length; i++) { |
|---|
| | 40 | var key = simpleKeys[i]; |
|---|
| | 41 | var node = toFormField(key, configObject[key]); |
|---|
| | 42 | div.appendChild(node); |
|---|
| | 43 | } |
|---|
| | 44 | |
|---|
| | 45 | // now display move complex information sections, sorted alphabetically |
|---|
| | 46 | if(!depth) depth = 1; |
|---|
| | 47 | complexKeys.sort(); |
|---|
| | 48 | for(var i = 0; i < complexKeys.length; i++) { |
|---|
| | 49 | var key = complexKeys[i]; |
|---|
| | 50 | |
|---|
| | 51 | // toSection will recurse back this function when child information is complex |
|---|
| | 52 | var node = toSection(key, configObject[key], depth); |
|---|
| | 53 | div.appendChild(node); |
|---|
| | 54 | } |
|---|
| | 55 | |
|---|
| | 56 | return div; |
|---|
| | 57 | } |
|---|
| | 58 | |
|---|
| | 59 | // given key and value, return a div with a label and an imput box |
|---|
| | 60 | // TODO: add a name to assocate with it |
|---|
| | 61 | function toFormField(key, value) { |
|---|
| | 62 | var div = document.createElement("div"); |
|---|
| | 63 | div.className = "entry"; |
|---|
| | 64 | var label = document.createElement("label"); |
|---|
| | 65 | label.appendChild(document.createTextNode(key + ": ")); |
|---|
| | 66 | var input = toInput(value); |
|---|
| | 67 | label.appendChild(input); |
|---|
| | 68 | div.appendChild(label); |
|---|
| | 69 | return div; |
|---|
| | 70 | } |
|---|
| | 71 | |
|---|
| | 72 | // given a value, return an imput object |
|---|
| | 73 | // length adjusted based on current value |
|---|
| | 74 | // TODO: add a name to assocate with it |
|---|
| | 75 | function toInput(value) { |
|---|
| | 76 | var input = document.createElement("input"); |
|---|
| | 77 | input.value = value; |
|---|
| | 78 | input.size = value.length; |
|---|
| | 79 | return input; |
|---|
| | 80 | } |
|---|
| | 81 | |
|---|
| | 82 | // give a key, complexConfigObject, and depth |
|---|
| | 83 | // return a header |
|---|
| | 84 | // followed (ala configObjectToDom) by key/value inputs, then more complex sections |
|---|
| | 85 | function toSection(key, complexConfigObject, depth) { |
|---|
| | 86 | var div = document.createElement("div"); |
|---|
| | 87 | var hNumber = Math.min(depth, 6); // limit h tags to h6 |
|---|
| | 88 | var h = document.createElement("h" + hNumber); |
|---|
| | 89 | h.appendChild(document.createTextNode(key)); |
|---|
| | 90 | var kids = complexConfigObject; |
|---|
| | 91 | if(complexConfigObject.length) { |
|---|
| | 92 | // this is an array... [value, kids] |
|---|
| | 93 | value = complexConfigObject[0]; |
|---|
| | 94 | var input = toInput(value); |
|---|
| | 95 | h.appendChild(document.createTextNode(" ")); |
|---|
| | 96 | h.appendChild(input); |
|---|
| | 97 | kids = complexConfigObject[1]; |
|---|
| | 98 | } |
|---|
| | 99 | var innerSection = configObjectToDom(kids, depth + 1); |
|---|
| | 100 | |
|---|
| | 101 | div.appendChild(h); |
|---|
| | 102 | div.appendChild(innerSection); |
|---|
| | 103 | return div; |
|---|
| | 104 | } |
|---|
| | 105 | |
|---|
| | 106 | // just show us the JSON |
|---|
| | 107 | function simpleDisplayResponse(container, response) { |
|---|
| | 108 | container.innerHTML = response.responseText; |
|---|
| | 109 | } |
|---|
| | 110 | |
|---|
| | 111 | // swith between JSON display and form field display |
|---|
| | 112 | //var processResponse = simpleDisplayResponse; |
|---|
| | 113 | var processResponse = formDisplayResponse; |
|---|
| | 114 | |
|---|