mappers.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { Tools } from 'babylonjs';
  2. import { ViewerConfiguration } from './configuration';
  3. import { kebabToCamel } from '../helper';
  4. /**
  5. * This is the mapper's interface. Implement this function to create your own mapper and register it at the mapper manager
  6. */
  7. export interface IMapper {
  8. map(rawSource: any): ViewerConfiguration;
  9. }
  10. /**
  11. * This is a simple HTML mapper.
  12. * This mapper parses a single HTML element and returns the configuration from its attributes.
  13. * it parses numbers and boolean values to the corresponding variable types.
  14. * The following HTML element:
  15. * <div test="1" random-flag="true" a.string.object="test"> will result in the following configuration:
  16. *
  17. * {
  18. * test: 1, //a number!
  19. * randomFlag: boolean, //camelCase and boolean
  20. * a: {
  21. * string: {
  22. * object: "test" //dot-separated object levels
  23. * }
  24. * }
  25. * }
  26. */
  27. class HTMLMapper implements IMapper {
  28. /**
  29. * Map a specific element and get configuration from it
  30. * @param element the HTML element to analyze.
  31. */
  32. map(element: HTMLElement): ViewerConfiguration {
  33. let config = {};
  34. for (let attrIdx = 0; attrIdx < element.attributes.length; ++attrIdx) {
  35. let attr = element.attributes.item(attrIdx);
  36. // map "object.property" to the right configuration place.
  37. let split = attr.nodeName.split('.');
  38. split.reduce((currentConfig, key, idx) => {
  39. //convert html-style to json-style
  40. let camelKey = kebabToCamel(key);
  41. if (idx === split.length - 1) {
  42. let val: any = attr.nodeValue; // firefox warns nodeValue is deprecated, but I found no sign of it anywhere.
  43. if (val === "true") {
  44. val = true;
  45. } else if (val === "false") {
  46. val = false;
  47. } else {
  48. var isnum = /^\d+$/.test(val);
  49. if (isnum) {
  50. let number = parseFloat(val);
  51. if (!isNaN(number)) {
  52. val = number;
  53. }
  54. }
  55. }
  56. currentConfig[camelKey] = val;
  57. } else {
  58. currentConfig[camelKey] = currentConfig[camelKey] || {};
  59. }
  60. return currentConfig[camelKey];
  61. }, config);
  62. }
  63. return config;
  64. }
  65. }
  66. /**
  67. * A simple string-to-JSON mapper.
  68. * This is the main mapper, used to analyze downloaded JSON-Configuration or JSON payload
  69. */
  70. class JSONMapper implements IMapper {
  71. map(rawSource: string) {
  72. return JSON.parse(rawSource);
  73. }
  74. }
  75. /**
  76. * The DOM Mapper will traverse an entire DOM Tree and will load the configuration from the
  77. * DOM elements and attributes.
  78. */
  79. class DOMMapper implements IMapper {
  80. /**
  81. * The mapping function that will convert HTML data to a viewer configuration object
  82. * @param baseElement the baseElement from which to start traversing
  83. * @returns a ViewerCOnfiguration object from the provided HTML Element
  84. */
  85. map(baseElement: HTMLElement): ViewerConfiguration {
  86. let htmlMapper = new HTMLMapper();
  87. let config = htmlMapper.map(baseElement);
  88. let traverseChildren = function (element: HTMLElement, partConfig) {
  89. let children = element.children;
  90. if (children.length) {
  91. for (let i = 0; i < children.length; ++i) {
  92. let item = <HTMLElement>children.item(i);
  93. // use the HTML Mapper to read configuration from a single element
  94. let configMapped = htmlMapper.map(item);
  95. let key = kebabToCamel(item.nodeName.toLowerCase());
  96. if (item.attributes.getNamedItem('array') && item.attributes.getNamedItem('array').nodeValue === 'true') {
  97. partConfig[key] = [];
  98. } else {
  99. if (element.attributes.getNamedItem('array') && element.attributes.getNamedItem('array').nodeValue === 'true') {
  100. partConfig.push(configMapped)
  101. } else if (partConfig[key]) {
  102. //exists already! probably an array
  103. element.setAttribute('array', 'true');
  104. let oldItem = partConfig[key];
  105. partConfig = [oldItem, configMapped]
  106. } else {
  107. partConfig[key] = configMapped;
  108. }
  109. }
  110. traverseChildren(item, partConfig[key] || configMapped);
  111. }
  112. }
  113. return partConfig;
  114. }
  115. traverseChildren(baseElement, config);
  116. return config;
  117. }
  118. }
  119. /**
  120. * The MapperManager manages the different implemented mappers.
  121. * It allows the user to register new mappers as well and use them to parse their own configuration data
  122. */
  123. export class MapperManager {
  124. private _mappers: { [key: string]: IMapper };
  125. /**
  126. * The default mapper is the JSON mapper.
  127. */
  128. public static DefaultMapper = 'json';
  129. constructor() {
  130. this._mappers = {
  131. "html": new HTMLMapper(),
  132. "json": new JSONMapper(),
  133. "dom": new DOMMapper()
  134. }
  135. }
  136. /**
  137. * Get a specific configuration mapper.
  138. *
  139. * @param type the name of the mapper to load
  140. */
  141. public getMapper(type: string) {
  142. if (!this._mappers[type]) {
  143. Tools.Error("No mapper defined for " + type);
  144. }
  145. return this._mappers[type] || this._mappers[MapperManager.DefaultMapper];
  146. }
  147. /**
  148. * Use this functio to register your own configuration mapper.
  149. * After a mapper is registered, it can be used to parse the specific type fo configuration to the standard ViewerConfiguration.
  150. * @param type the name of the mapper. This will be used to define the configuration type and/or to get the mapper
  151. * @param mapper The implemented mapper
  152. */
  153. public registerMapper(type: string, mapper: IMapper) {
  154. this._mappers[type] = mapper;
  155. }
  156. /**
  157. * Dispose the mapper manager and all of its mappers.
  158. */
  159. public dispose() {
  160. this._mappers = {};
  161. }
  162. }
  163. /**
  164. * mapperManager is a singleton of the type MapperManager.
  165. * The mapperManager can be disposed directly with calling mapperManager.dispose()
  166. * or indirectly with using BabylonViewer.disposeAll()
  167. */
  168. export let mapperManager = new MapperManager();