aboutsummaryrefslogtreecommitdiffstats
path: root/sdv/pdf/site/scripts/writeToHTML.js
blob: 8eb8c29c2ac09e159a0bd76f20bea89e282d229c (plain)
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
109
110
111
112
113
114
115
116
117
/**
* Processes target HTML element and fill with values passed in
* javascript object
*
* @param {element} element to update with values
* @param {obj} Object from which values will be read
*/
  function writeToHTML(element, obj){

    var el = element.childNodes;
    for(var i in el)
    {

        if(el[i] instanceof HTMLInputElement && el[i].hasAttribute('name')){

            if(el[i].type == 'text')
              el[i].value = getValue(obj, el[i].name);

        }
        if(el[i] instanceof HTMLSelectElement && el[i].hasAttribute('name')){

          var option = getValue(obj, el[i].name);
          var defaultValue = el[i].value;
          el[i].value = option;
          if ( el[i].value != option){
              el[i].focus();
              alert(option + " is an invalid value! Setting to default");
              el[i].value = defaultValue;
          }

        }
        if(el[i] instanceof HTMLDivElement){

            if(el[i].hasAttribute('name'))
            {              
                if(el[i].classList.contains('arr')){

                    name = el[i].getAttribute('name');
                    values = getValue(obj, name);

                    // Sync number of Arr div with name inside element with number of values
                    syncArr(element, name, values.length);

                    // Update value inside all divs
                    var i = 0;
                    for(var div of element.getElementsByClassName('arr'))
                      if(div.getAttribute("name") == name)
                        writeToHTML(div , values[i++]);

                } //else-if single div
                else
                    writeToHTML(el[i], getValue(obj, el[i].getAttribute('name')));

            }//else-if blank div without attribute name, then simply pass values to next child
            else
                writeToHTML(el[i], obj);
        }

    }
  }

  // Reads value from obj with string 'key1.key2.key3' convention
  function getValue(obj, str) {
    str = str.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
    str = str.replace(/^\./, '');           // strip a leading dot
    var a = str.split('.');
    for (var i = 0; i < a.length; ++i) {
        var key = a[i];
        if (key in obj) {
            obj = obj[key];
        } else {
            alert('Invalid PDF file! Key '+key+ ' not Found');
            return "";
        }
    }
    return obj;
  }

function syncArr(el, name, length){

  // count number of arr-div with name present inside el
  var count = 0;
  var cp = null;
  for(var div of el.getElementsByClassName('arr')){
    if(div.getAttribute("name") == name){
      count++;
      cp = div;
    }
  }

  // balance loop
  while(count - length){
    if((count - length) < 0)
    { // add more div
      newdiv = cp.cloneNode(true);
      if (!newdiv.lastElementChild.classList.contains('del-button')){
        del ='<div class="del-button" onclick="remove(this)"></div>'
        newdiv.innerHTML += del;
      }
      cp.after(newdiv);
      count++;
    }
    else
    { // remove div
      for(var div of el.getElementsByClassName('arr'))
        if(div.getAttribute("name") == name)
          if (div.lastElementChild.classList.contains('del-button')){
            div.parentNode.removeChild(div);
            count--;
            if((count - length)==0)
              break;
          }
    }
  }

}