summaryrefslogtreecommitdiff
path: root/js/looking-glass.js
blob: 3020e08c31ee3e4d1b6493f93cc3918c3841779b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function request_doc(query) {
  $.ajax({
    type: 'post',
    url: 'execute.php',
    data: { doc: query, dontlook: '' }
  }).done(function(response) {
    var response = $.parseJSON(response);

    $('#command-reminder').text(response.command);
    $('#description-help').html(response.description);
    $('#parameter-help').html(response.parameter);
  }).fail(function(xhr) {
    $('#help-content').text('Cannot load documentation...');
  });
}

$(document).ready(function() {
  // hide the optional parameters field
  $('.result').hide();
  $('.loading').hide();
  $('.alert').hide();

  // close the alert bar
  $('.close').click(function() {
    $('.alert').slideUp();
  });

  // clear the form and page
  $('#clear').click(function(e) {
    $('.alert').slideUp();

    e.preventDefault();

    // reset the parameter field if it was marked as error
    $('#input-param').parent().removeClass('has-error');

    // reset the form and update the doc modal
    $(this).closest('form').get(0).reset();
    request_doc($('#query').val());
  });

  // reset the view to the default one
  $('#backhome').click(function() {
    $('.content').slideDown();
    $('.result').slideUp();
  });

  // initialize the help modal
  request_doc($('#query').val());

  // update help when a command is selected
  $('#query').on('change', function(e) {
    e.preventDefault();
    request_doc($('#query').val());
  });

  // if the field has been completed, turn it back to normal
  $('#input-param').change(function() {
    $('#input-param').parent().removeClass('has-error');
  });

  // send an ajax request that will get the info on the router
  $('form').on('submit', function(e) {
    e.preventDefault();

    $.ajax({
      type: 'post',
      url: 'execute.php',
      data: $('form').serialize(),
      beforeSend: function() {
        // show loading bar
        $('#command_properties').attr('disabled', '');
        $('.alert').hide();
        $('.loading').show();
      },
      complete: function() {
        // hide loading bar
        $('#command_properties').removeAttr('disabled');
        $('.loading').hide();
      }
    }).done(function(response) {
      if (!response || (response.length === 0)) {
        // no parameter given
        $('#error-text').text('No parameter given.');
        $('#input-param').focus().parent().addClass('has-error');
        $('.alert').slideDown();
      } else {
        var response = $.parseJSON(response);

        if (response.error) {
          $('#error-text').text(response.error);
          $('.alert').slideDown();
        } else {
          $('#output').html(response.result);
          $('.content').slideUp();
          $('.result').slideDown();
        }
      }
    }).fail(function(xhr) {
      $('#error-text').text(xhr.responseText);
      $('.alert').slideDown();
    });
  });
});