Advertisement
devlargs

Untitled

Nov 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import JSONPretty from 'react-json-pretty';
  3. import guid from 'guid';
  4.  
  5. class Converter extends Component {
  6. constructor(props) {
  7. super(props);
  8.  
  9. this.state = {
  10. template: '',
  11. generated: null
  12. }
  13.  
  14. this.generateIds = this.generateIds.bind(this);
  15. this.createContentCell = this.createContentCell.bind(this);
  16. }
  17.  
  18. generateIds(json) {
  19. for (var key in json) {
  20. try {
  21. if (Object.keys(json[key]).length != 0) {
  22. if (typeof json[key] == 'object') {
  23. json['id'] = guid.raw();
  24. this.generateIds(json[key])
  25. } else {
  26. if (key.charAt(0) == '_') {
  27. json[key.substr(1, key.length)] = json[key]
  28. delete json[key];
  29. delete json['id'];
  30. json['id'] = guid.raw();
  31. }
  32. }
  33. }
  34. } catch (err) { }
  35. }
  36. }
  37.  
  38. createContentCell(json){
  39. var tempJson = Object.assign({}, json);
  40.  
  41. Object.keys(json).map(q => {
  42. if(typeof json[q] == 'object'){
  43. if(json[q].hasOwnProperty('class')){
  44. if(json[q]["class"] == 'row'){
  45. // console.log(json[q])
  46. json['cells'] = [
  47. json[q]
  48. ]
  49. delete json[q]['class']
  50. this.createContentCell(json[q])
  51. delete json[q]
  52.  
  53. }else{
  54. this.createContentCell(json[q])
  55. }
  56. }else{
  57. this.createContentCell(json[q]);
  58. }
  59.  
  60.  
  61.  
  62. }else{
  63. console.log(typeof json[q])
  64. }
  65. })
  66. }
  67.  
  68. variableValidation(q) {
  69. var types = ['text', 'link', 'image']
  70.  
  71. if (typeof q.type != 'undefined') {
  72. if (types.includes(q.type.toLowerCase())) {
  73. console.log(q.type);
  74. }
  75. }
  76. }
  77.  
  78.  
  79. convert() {
  80. let { template } = this.state;
  81. var x2js = new X2JS();
  82. let json = x2js.xml2js(template);
  83.  
  84. if (json) {
  85. this.generateIds(json);
  86. this.createContentCell(json);
  87.  
  88. return (
  89. <div style={{ marginTop: 10 }}>
  90. <JSONPretty id="json-pretty" json={json}></JSONPretty>
  91. </div>
  92. )
  93. } else {
  94. return <h3>Enter a valid template value.</h3>
  95. }
  96. }
  97.  
  98. render() {
  99. let { template } = this.state;
  100.  
  101. return (
  102. <div class="form-group">
  103. <hr />
  104.  
  105. <div>
  106. <textarea class="form-control" value={template} rows="7" onChange={(e) =>
  107. this.setState({
  108. template: e.target.value
  109. })
  110. }></textarea>
  111. </div>
  112.  
  113. {
  114. (template) ?
  115. this.convert() :
  116. <h3>Please enter template.</h3>
  117. }
  118.  
  119. </div>
  120. )
  121. }
  122. }
  123.  
  124. export default Converter;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement