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