Helpers.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. module INSPECTOR {
  2. export class Helpers {
  3. /**
  4. * Returns the type of the given object. First
  5. * uses getClassName. If nothing is returned, used the type of the constructor
  6. */
  7. public static GET_TYPE(obj:any) : string{
  8. if (obj != null && obj != undefined) {
  9. let classname = BABYLON.Tools.getClassName(obj);
  10. if (!classname || classname === 'object'){
  11. classname = obj.constructor.name;
  12. // classname is undefined in IE11
  13. if (!classname) {
  14. classname = this._GetFnName(obj.constructor);
  15. }
  16. }
  17. // If the class name has no matching properties, check every type
  18. if (!this._CheckIfTypeExists(classname)) {
  19. return this._GetTypeFor(obj);
  20. }
  21. return classname;
  22. } else {
  23. return 'type_not_defined';
  24. }
  25. }
  26. /**
  27. * Check if some properties are defined for the given type.
  28. */
  29. private static _CheckIfTypeExists(type:string) {
  30. let properties = PROPERTIES[type];
  31. if (properties) {
  32. return true;
  33. }
  34. return false;
  35. }
  36. /**
  37. * Returns true if the user browser is edge.
  38. */
  39. public static IsBrowserEdge() : boolean {
  40. //Detect if we are running on a faulty buggy OS.
  41. var regexp = /Edge/
  42. return regexp.test(navigator.userAgent);
  43. }
  44. /**
  45. * Returns the name of the type of the given object, where the name
  46. * is in PROPERTIES constant.
  47. * Returns 'Undefined' if no type exists for this object
  48. */
  49. private static _GetTypeFor(obj:any) {
  50. for (let type in PROPERTIES) {
  51. let typeBlock = PROPERTIES[type];
  52. if (typeBlock.type) {
  53. if (obj instanceof typeBlock.type) {
  54. return type;
  55. }
  56. }
  57. }
  58. return 'type_not_defined';
  59. }
  60. /**
  61. * Returns the name of a function (workaround to get object type for IE11)
  62. */
  63. private static _GetFnName(fn) {
  64. var f = typeof fn == 'function';
  65. var s = f && ((fn.name && ['', fn.name]) || fn.toString().match(/function ([^\(]+)/));
  66. return (!f && 'not a function') || (s && s[1] || 'anonymous');
  67. }
  68. /** Send the event which name is given in parameter to the window */
  69. public static SEND_EVENT(eventName:string){
  70. let event;
  71. if (Inspector.DOCUMENT.createEvent) {
  72. event = Inspector.DOCUMENT.createEvent('HTMLEvents');
  73. event.initEvent(eventName, true, true);
  74. } else {
  75. event = new Event(eventName);
  76. }
  77. window.dispatchEvent(event);
  78. }
  79. /** Returns the given number with 2 decimal number max if a decimal part exists */
  80. public static Trunc(nb) :number {
  81. if(Math.round(nb) !== nb) {
  82. return nb.toFixed(2);
  83. }
  84. return nb;
  85. };
  86. /**
  87. * Useful function used to create a div
  88. */
  89. public static CreateDiv(className?:string, parent?: HTMLElement) : HTMLElement{
  90. return Helpers.CreateElement('div', className, parent);
  91. }
  92. public static CreateElement(element:string, className?:string, parent?: HTMLElement) : HTMLElement{
  93. let elem = Inspector.DOCUMENT.createElement(element);
  94. if (className) {
  95. elem.className = className;
  96. }
  97. if (parent) {
  98. parent.appendChild(elem);
  99. }
  100. return elem;
  101. }
  102. /**
  103. * Removes all children of the given div.
  104. */
  105. public static CleanDiv(div:HTMLElement) {
  106. while ( div.firstChild ) {
  107. div.removeChild(div.firstChild);
  108. }
  109. }
  110. /**
  111. * Returns the true value of the given CSS Attribute from the given element (in percentage or in pixel, as it was specified in the css)
  112. */
  113. public static Css(elem:HTMLElement, cssAttribute:string) : string{
  114. let clone = elem.cloneNode(true) as HTMLElement;
  115. let div = Helpers.CreateDiv('', Inspector.DOCUMENT.body);
  116. div.style.display = 'none';
  117. div.appendChild(clone);
  118. let value = Inspector.WINDOW.getComputedStyle(clone)[cssAttribute];
  119. div.parentNode.removeChild(div);
  120. return value;
  121. }
  122. public static LoadScript() {
  123. BABYLON.Tools.LoadFile("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/highlight.min.js", (elem) => {
  124. let script = Helpers.CreateElement('script', '', Inspector.DOCUMENT.body);
  125. script.textContent = elem;
  126. // Load glsl detection
  127. BABYLON.Tools.LoadFile("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/languages/glsl.min.js", (elem) => {
  128. let script = Helpers.CreateElement('script', '', Inspector.DOCUMENT.body);
  129. script.textContent = elem;
  130. // Load css style
  131. BABYLON.Tools.LoadFile("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/styles/zenburn.min.css", (elem) => {
  132. let style = Helpers.CreateElement('style', '', Inspector.DOCUMENT.body);
  133. style.textContent = elem;
  134. });
  135. }, null, null, null, () => {
  136. console.log("erreur");
  137. });
  138. }, null, null, null, () => {
  139. console.log("erreur");
  140. });
  141. }
  142. public static IsSystemName(name: string): boolean {
  143. if (name==null) {
  144. return false;
  145. }
  146. return name.indexOf("###")===0 && name.lastIndexOf("###")===(name.length-3);
  147. }
  148. }
  149. }