utils.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * This JS file contains utils functions
  3. */
  4. class Utils {
  5. constructor(parent) {
  6. this.parent = parent;
  7. this.multipleSize = [1280, 1024, 'Mobile'];
  8. }
  9. /**
  10. * When something is written in the editor, it reset the safe mode
  11. */
  12. markDirty() {
  13. if (this.parent.monacoCreator.BlockEditorChange) return;
  14. this.setToMultipleID("safemodeToggle", "addClass", "checked");!
  15. this.setToMultipleID('safemodeToggle', 'innerHTML', 'Safe mode <i class="fa fa-check-square" aria-hidden="true"></i>');
  16. };
  17. toLocationError(errorMessage, errorEvent) {
  18. // Do we have any location info?
  19. if (errorEvent.hasOwnProperty('lineNumber') && errorEvent.hasOwnProperty('columnNumber'))
  20. return errorEvent;
  21. // Else try to parse the stack to retrieve location...
  22. var regEx = /\(.+:(\d+):(\d+)\)\n/g;
  23. var match = regEx.exec(errorEvent.stack);
  24. if (match) {
  25. var error = new EvalError(errorMessage);
  26. error.lineNumber = match[1];
  27. error.columnNumber = match[2];
  28. return error;
  29. }
  30. // Not an error with proper location
  31. return null;
  32. }
  33. /**
  34. * Used to show error messages
  35. * @param {String} errorMessage
  36. * @param {String} errorEvent
  37. */
  38. showError(errorMessage, errorEvent) {
  39. let errorContent = '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button>';
  40. const locationError = this.toLocationError(errorMessage, errorEvent);
  41. if (locationError == null) {
  42. // use a regular message
  43. errorContent += `${errorMessage}</div>`;
  44. } else {
  45. // we have location information
  46. errorContent += `<span id="gotoLocation">Line ${locationError.lineNumber} : ${locationError.columnNumber} - ${errorMessage}</span></div>`;
  47. }
  48. document.getElementById("errorZone").style.display = 'block';
  49. document.getElementById("errorZone").innerHTML = errorContent;
  50. // Close button error
  51. document.getElementById("errorZone").querySelector('.close').addEventListener('click', function () {
  52. document.getElementById("errorZone").style.display = 'none';
  53. });
  54. // Go To Location
  55. const gotoLocation = document.getElementById("gotoLocation");
  56. const jsEditor = this.parent.monacoCreator.jsEditor;
  57. if (gotoLocation) {
  58. gotoLocation.addEventListener('click', function () {
  59. const position = {
  60. lineNumber: Number(locationError.lineNumber),
  61. column: Number(locationError.columnNumber)
  62. };
  63. jsEditor.revealPositionInCenter(position, monaco.editor.ScrollType.Smooth);
  64. jsEditor.setPosition(position);
  65. });
  66. }
  67. };
  68. /**
  69. * Apply things to the differents menu sizes
  70. */
  71. setToMultipleID(id, thingToDo, param) {
  72. this.multipleSize.forEach(function (size) {
  73. if (thingToDo == "innerHTML") {
  74. document.getElementById(id + size).innerHTML = param
  75. }
  76. else if (thingToDo == "click") {
  77. if (param.length > 1) {
  78. for (var i = 0; i < param.length; i++) {
  79. document.getElementById(id + size).addEventListener("click", param[i]);
  80. }
  81. }
  82. else
  83. document.getElementById(id + size).addEventListener("click", param);
  84. }
  85. else if (thingToDo == "addClass") {
  86. document.getElementById(id + size).classList.add(param);
  87. }
  88. else if (thingToDo == "removeClass") {
  89. document.getElementById(id + size).classList.remove(param);
  90. }
  91. else if (thingToDo == "display") {
  92. document.getElementById(id + size).style.display = param;
  93. }
  94. });
  95. };
  96. /**
  97. * Function to get the current screen size
  98. */
  99. getCurrentSize() {
  100. for(var i = 0; i < this.multipleSize.length; i++) {
  101. if(document.getElementById("menuButton" + this.multipleSize[i]).offsetHeight > 0) return this.multipleSize[i];
  102. }
  103. };
  104. }