PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/iwatchallit/towtruck
JavaScript | 83 lines | 66 code | 7 blank | 10 comment | 4 complexity | 15469f4ee48e7a48f94d46250b4cf053 MD5 | raw file
  1. "use strict";
  2. // Provides helpful Slowparse-based error suggestions for a
  3. // ParsingCodeMirror.
  4. define(["jquery-slowparse", "./mark-tracker"], function($, MarkTracker) {
  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. // The escape key should close error help
  32. $(document).keyup(function(event) {
  33. if (event.keyCode == 27)
  34. clearError();
  35. });
  36. // Keep track of error highlighting.
  37. var errorHelpMarks = MarkTracker(codeMirror, relocator);
  38. // Report the given Slowparse error.
  39. function reportError(error) {
  40. var startMark = null;
  41. var errorHTML = $("<div></div>").fillError(error);
  42. errorArea.html(template({error: errorHTML.html()})).show();
  43. errorArea.eachErrorHighlight(function(start, end, i) {
  44. // Point the error message's arrow at the first occurrence of
  45. // the word "here" in the error message.
  46. if (startMark === null)
  47. startMark = start;
  48. errorHelpMarks.mark(start, end, "highlight-" + (i+1), this);
  49. $(this).click(function() {
  50. var pos = codeMirror.posFromIndex(start);
  51. codeMirror.setCursor(pos);
  52. pointAtPosition(codeMirror, pos);
  53. });
  54. });
  55. relocator.relocate(errorArea, startMark, "error");
  56. errorArea.hide();
  57. }
  58. function clearError() {
  59. clearTimeout(timeout);
  60. errorHelpMarks.clear();
  61. errorArea.hide();
  62. relocator.cleanup();
  63. }
  64. codeMirror.on("change", clearError);
  65. codeMirror.on("reparse", function(event) {
  66. clearError();
  67. // same as context-sensitive-help.js, "cursor-activity" handling
  68. if (event.error) {
  69. timeout = setTimeout(function() {
  70. reportError(event.error);
  71. }, ERROR_DISPLAY_DELAY);
  72. }
  73. });
  74. return self;
  75. };
  76. });