Helpers.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 (typeof obj === 'boolean') {
  9. return 'boolean';
  10. }
  11. if (obj != null && obj != undefined) {
  12. let classname = BABYLON.Tools.GetClassName(obj);
  13. if (!classname || classname === 'object') {
  14. classname = obj.constructor.name;
  15. // classname is undefined in IE11
  16. if (!classname) {
  17. classname = this._GetFnName(obj.constructor);
  18. }
  19. }
  20. // If the class name has no matching properties, check every type
  21. if (!this._CheckIfTypeExists(classname)) {
  22. return this._GetTypeFor(obj);
  23. }
  24. return classname;
  25. } else {
  26. return 'type_not_defined';
  27. }
  28. }
  29. /**
  30. * Check if some properties are defined for the given type.
  31. */
  32. private static _CheckIfTypeExists(type: string) {
  33. let properties = (<any>PROPERTIES)[type];
  34. if (properties) {
  35. return true;
  36. }
  37. return false;
  38. }
  39. /**
  40. * Returns true if the user browser is edge.
  41. */
  42. public static IsBrowserEdge(): boolean {
  43. var regexp = /Edge/
  44. return regexp.test(navigator.userAgent);
  45. }
  46. /**
  47. * Returns true if the user browser is IE.
  48. */
  49. public static IsBrowserIE(): boolean {
  50. var regexp = /Trident.*rv\:11\./
  51. return regexp.test(navigator.userAgent);
  52. }
  53. /**
  54. * Returns the name of the type of the given object, where the name
  55. * is in PROPERTIES constant.
  56. * Returns 'Undefined' if no type exists for this object
  57. */
  58. private static _GetTypeFor(obj: any) {
  59. for (let type in PROPERTIES) {
  60. let typeBlock = (<any>PROPERTIES)[type];
  61. if (typeBlock.type) {
  62. if (obj instanceof typeBlock.type) {
  63. return type;
  64. }
  65. }
  66. }
  67. return 'type_not_defined';
  68. }
  69. /**
  70. * Returns the name of a function (workaround to get object type for IE11)
  71. */
  72. private static _GetFnName(fn: any) {
  73. var f = typeof fn == 'function';
  74. var s = f && ((fn.name && ['', fn.name]) || fn.toString().match(/function ([^\(]+)/));
  75. return (!f && 'not a function') || (s && s[1] || 'anonymous');
  76. }
  77. /** Send the event which name is given in parameter to the window */
  78. public static SEND_EVENT(eventName: string) {
  79. let event;
  80. if (Inspector.DOCUMENT.createEvent) {
  81. event = Inspector.DOCUMENT.createEvent('HTMLEvents');
  82. event.initEvent(eventName, true, true);
  83. } else {
  84. event = new Event(eventName);
  85. }
  86. window.dispatchEvent(event);
  87. }
  88. /** Returns the given number with 2 decimal number max if a decimal part exists */
  89. public static Trunc(nb: number): number {
  90. if (typeof nb !== 'number') {
  91. return 0;
  92. }
  93. if (Math.round(nb) !== nb) {
  94. return (<any>nb.toFixed(2));
  95. }
  96. return nb;
  97. };
  98. /**
  99. * Useful function used to create a div
  100. */
  101. public static CreateDiv(className: BABYLON.Nullable<string> = null, parent?: HTMLElement): HTMLElement {
  102. return Helpers.CreateElement('div', className, parent);
  103. }
  104. /**
  105. * Useful function used to create a input
  106. */
  107. public static CreateInput(className?: string, parent?: HTMLElement): HTMLInputElement {
  108. return <HTMLInputElement>Helpers.CreateElement('input', className, parent);
  109. }
  110. public static CreateElement(element: string, className: BABYLON.Nullable<string> = null, parent?: HTMLElement): HTMLElement {
  111. let elem = Inspector.DOCUMENT.createElement(element);
  112. if (className) {
  113. elem.className = className;
  114. }
  115. if (parent) {
  116. parent.appendChild(elem);
  117. }
  118. return elem;
  119. }
  120. /**
  121. * Removes all children of the given div.
  122. */
  123. public static CleanDiv(div: HTMLElement) {
  124. while (div.firstChild) {
  125. div.removeChild(div.firstChild);
  126. }
  127. }
  128. /**
  129. * 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)
  130. */
  131. public static Css(elem: HTMLElement, cssAttribute: string): string {
  132. let clone = elem.cloneNode(true) as HTMLElement;
  133. let div = Helpers.CreateDiv('', Inspector.DOCUMENT.body);
  134. div.style.display = 'none';
  135. div.appendChild(clone);
  136. let value = (<any>Inspector.WINDOW.getComputedStyle(clone))[cssAttribute];
  137. if (div.parentNode) {
  138. div.parentNode.removeChild(div);
  139. }
  140. return value;
  141. }
  142. public static LoadScript() {
  143. BABYLON.Tools.LoadFile("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/highlight.min.js", (elem) => {
  144. let script = Helpers.CreateElement('script', '', Inspector.DOCUMENT.body);
  145. script.textContent = elem as string;
  146. // Load glsl detection
  147. BABYLON.Tools.LoadFile("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/languages/glsl.min.js", (elem) => {
  148. let script = Helpers.CreateElement('script', '', Inspector.DOCUMENT.body);
  149. script.textContent = elem as string;
  150. // Load css style
  151. BABYLON.Tools.LoadFile("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/styles/zenburn.min.css", (elem) => {
  152. let style = Helpers.CreateElement('style', '', Inspector.DOCUMENT.body);
  153. style.textContent = elem as string;
  154. });
  155. }, undefined, undefined, undefined, () => {
  156. console.log('Error : LoadFile "glsl.min.js"');
  157. });
  158. }, undefined, undefined, undefined, () => {
  159. console.log('Error : LoadFile "highlight.min.js"')
  160. });
  161. }
  162. public static IsSystemName(name: string): boolean {
  163. if (name == null) {
  164. return false;
  165. }
  166. return name.indexOf("###") === 0 && name.lastIndexOf("###") === (name.length - 3);
  167. }
  168. /**
  169. * Return an array of PropertyLine for an obj
  170. * @param obj
  171. */
  172. public static GetAllLinesProperties(obj: any): Array<PropertyLine> {
  173. let propertiesLines: Array<PropertyLine> = [];
  174. let props = Helpers.GetAllLinesPropertiesAsString(obj);
  175. for (let prop of props) {
  176. let infos = new Property(prop, obj);
  177. propertiesLines.push(new PropertyLine(infos));
  178. }
  179. return propertiesLines;
  180. }
  181. /**
  182. * Returns an array of string corresponding to tjhe list of properties of the object to be displayed
  183. * @param obj
  184. */
  185. public static GetAllLinesPropertiesAsString(obj: any, dontTakeThis: Array<string> = []): Array<string> {
  186. let props: Array<string> = [];
  187. for (let prop in obj) {
  188. //No private and no function
  189. if (dontTakeThis.indexOf(prop) === -1 && prop.substring(0, 1) !== '_' && typeof obj[prop] !== 'function') {
  190. props.push(prop);
  191. }
  192. }
  193. return props;
  194. }
  195. public static Capitalize(str: string): string {
  196. return str.charAt(0).toUpperCase() + str.slice(1);
  197. }
  198. }
  199. }