mappers.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { Tools } from 'babylonjs';
  2. import { ViewerConfiguration } from './configuration';
  3. import { kebabToCamel } from '../helper';
  4. export interface IMapper {
  5. map(rawSource: any): ViewerConfiguration;
  6. }
  7. class HTMLMapper implements IMapper {
  8. map(element: HTMLElement): ViewerConfiguration {
  9. let config = {};
  10. for (let attrIdx = 0; attrIdx < element.attributes.length; ++attrIdx) {
  11. let attr = element.attributes.item(attrIdx);
  12. // map "object.property" to the right configuration place.
  13. let split = attr.nodeName.split('.');
  14. split.reduce((currentConfig, key, idx) => {
  15. //convert html-style to json-style
  16. let camelKey = kebabToCamel(key);
  17. if (idx === split.length - 1) {
  18. let val: any = attr.nodeValue; // firefox warns nodeValue is deprecated, but I found no sign of it anywhere.
  19. if (val === "true") {
  20. val = true;
  21. } else if (val === "false") {
  22. val = false;
  23. } else {
  24. let number = parseFloat(val);
  25. if (!isNaN(number)) {
  26. val = number;
  27. }
  28. }
  29. currentConfig[camelKey] = val;
  30. } else {
  31. currentConfig[camelKey] = currentConfig[camelKey] || {};
  32. }
  33. return currentConfig[camelKey];
  34. }, config);
  35. }
  36. return config;
  37. }
  38. }
  39. class JSONMapper implements IMapper {
  40. map(rawSource: any) {
  41. return JSON.parse(rawSource);
  42. }
  43. }
  44. // TODO - Dom configuration mapper.
  45. class DOMMapper implements IMapper {
  46. map(baseElement: HTMLElement): ViewerConfiguration {
  47. let htmlMapper = new HTMLMapper();
  48. let config = htmlMapper.map(baseElement);
  49. let traverseChildren = function (element: HTMLElement, partConfig) {
  50. let children = element.children;
  51. if (children.length) {
  52. for (let i = 0; i < children.length; ++i) {
  53. let item = <HTMLElement>children.item(i);
  54. let configMapped = htmlMapper.map(item);
  55. let key = kebabToCamel(item.nodeName.toLowerCase());
  56. if (item.attributes.getNamedItem('array') && item.attributes.getNamedItem('array').nodeValue === 'true') {
  57. partConfig[key] = [];
  58. } else {
  59. if (element.attributes.getNamedItem('array') && element.attributes.getNamedItem('array').nodeValue === 'true') {
  60. partConfig.push(configMapped)
  61. } else if (partConfig[key]) {
  62. //exists already! problem... probably an array
  63. element.setAttribute('array', 'true');
  64. let oldItem = partConfig[key];
  65. partConfig = [oldItem, configMapped]
  66. } else {
  67. partConfig[key] = configMapped;
  68. }
  69. }
  70. traverseChildren(item, partConfig[key] || configMapped);
  71. }
  72. }
  73. return partConfig;
  74. }
  75. traverseChildren(baseElement, config);
  76. return config;
  77. }
  78. }
  79. export class MapperManager {
  80. private mappers: { [key: string]: IMapper };
  81. public static DefaultMapper = 'json';
  82. constructor() {
  83. this.mappers = {
  84. "html": new HTMLMapper(),
  85. "json": new JSONMapper(),
  86. "dom": new DOMMapper()
  87. }
  88. }
  89. public getMapper(type: string) {
  90. if (!this.mappers[type]) {
  91. Tools.Error("No mapper defined for " + type);
  92. }
  93. return this.mappers[type] || this.mappers[MapperManager.DefaultMapper];
  94. }
  95. public registerMapper(type: string, mapper: IMapper) {
  96. this.mappers[type] = mapper;
  97. }
  98. }
  99. export let mapperManager = new MapperManager();