PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/public/friendlycode/js/fc/ui/error-help.js

https://github.com/mrsonord/thimble.webmaker.org
JavaScript | 93 lines | 72 code | 9 blank | 12 comment | 5 complexity | 35016222f2b9007cfba36043c66ab469 MD5 | raw file
Possible License(s): Apache-2.0
  1. // Provides helpful Slowparse-based error suggestions for a
  2. // ParsingCodeMirror.
  3. define(["jquery-slowparse", "./mark-tracker"], function($, markTracker) {
  4. "use strict";
  5. // Display an animated arrow pointing at a particular position in a
  6. // codeMirror instance. It disappears after a short delay.
  7. function pointAtPosition(codeMirror, pos) {
  8. var MOVE_TIME = 500;
  9. var PAUSE_TIME = 1000;
  10. var pointer = $('<div class="cursor-pointer"></div>');
  11. codeMirror.addWidget(pos, pointer[0], true);
  12. pointer.css({
  13. opacity: 0,
  14. paddingTop: '50px'
  15. });
  16. pointer.animate({
  17. opacity: 1,
  18. paddingTop: 0
  19. }, MOVE_TIME).delay(PAUSE_TIME).fadeOut(function() {
  20. pointer.remove();
  21. });
  22. }
  23. return function ErrorHelp(options) {
  24. var self = {};
  25. var codeMirror = options.codeMirror;
  26. var template = options.template;
  27. var errorArea = options.errorArea;
  28. var relocator = options.relocator;
  29. var timeout = null;
  30. var ERROR_DISPLAY_DELAY = 250;
  31. // remove the error help from view
  32. function clearError() {
  33. clearTimeout(timeout);
  34. errorHelpMarks.clear();
  35. errorArea.hide();
  36. relocator.cleanup();
  37. }
  38. // The escape key should close error help
  39. $(document).keyup(function(event) {
  40. if (event.keyCode == 27)
  41. clearError();
  42. });
  43. // Keep track of error highlighting.
  44. var errorHelpMarks = markTracker(codeMirror, relocator);
  45. // Report the given Slowparse error.
  46. function reportError(error) {
  47. var startMark = null,
  48. endMark = null,
  49. errorHTML = $("<div></div>").fillError(error);
  50. errorArea.html(template({error: errorHTML.html()})).show();
  51. errorArea.eachErrorHighlight(function(start, end, i) {
  52. // Point the error message's arrow at the first occurrence of
  53. // the word "here" in the error message.
  54. if (startMark === null)
  55. startMark = start;
  56. errorHelpMarks.mark(start, end, "highlight-" + (i+1), this);
  57. $(this).click(function() {
  58. var pos = codeMirror.posFromIndex(start);
  59. codeMirror.setCursor(pos);
  60. pointAtPosition(codeMirror, pos);
  61. });
  62. endMark = end;
  63. });
  64. relocator.relocate(errorArea, startMark, endMark, "error");
  65. // clicking the dismiss link should also close error help
  66. var dismiss = errorArea.find(".dismiss");
  67. if(dismiss) {
  68. dismiss.click(clearError);
  69. }
  70. errorArea.hide();
  71. }
  72. codeMirror.on("change", clearError);
  73. codeMirror.on("reparse", function(event) {
  74. clearError();
  75. // same as context-sensitive-help.js, "cursor-activity" handling
  76. if (event.error) {
  77. timeout = setTimeout(function() {
  78. reportError(event.error);
  79. }, ERROR_DISPLAY_DELAY);
  80. }
  81. });
  82. return self;
  83. };
  84. });