Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/online_check/check.js
blob: 28528f6a3282c5d402d8cd52d3bf75a331429bf9 (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
105
106
/*
 # check.js - simple application to check numbers
 #
 # Copyright (C) 2017 Arthur de Jong.
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
 # License as published by the Free Software Foundation; either
 # version 2.1 of the License, or (at your option) any later version.
 #
 # This library is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 # Lesser General Public License for more details.
 #
 # You should have received a copy of the GNU Lesser General Public
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301 USA
 */

$( document ).ready(function() {

  function format(value) {
    return $("<div/>").text(value).html().replace(
      /\n\n/g, "<br/>\n"
    ).replace(
      /^[*] (.*)$/gm, "<ul><li>$1</li></ul>"
    ).replace(
      /(\b(https?|ftp):\/\/[^\s<]*[-\w+&@#/%=~_|])/ig,
      "<a href='$1'>$1</a>"
    )
  }

  function updateresults(field, results) {
    // build HTML to present
    var h = ["<ul>"];
    $.each(results, function(index, result) {
      h.push(
        "<li><b>",
        $("<div/>").text(result["name"]).html(),
        "</b><br/>",
        $("<div/>").text(result["number"]).html(),
        "<p>",
        format(result["description"]),
        $.map(result["conversions"], function(value, key){
          return [
            "<br/><b><i>",
            $("<div/>").text(key).html(),
            "</i></b>: ",
            $("<div/>").text(value).html()].join('')
        }).join(''),
        "</p></li>")
    });
    h.push("</ul>");
    // replace the results div
    $("#" + $(field).attr("id") + "_results").slideUp("quick", function() {
      $(this).html(h.join(""));
      $(this).slideDown("quick");
    });
  }

  function checkfield(field) {
    var value = field.val();
    // only trigger update if value changed from previous validation
    if (value != $(this).data("oldvalue")) {
      $(this).data("oldvalue", value);
      $.get('', {number: value}, function(data) {
        updateresults(field, data);
      });
    }
  }

  // trigger a check when user stopped typing
  $(".stdnum_check").on("input propertychange", function (event) {
      if (window.event && event.type == "propertychange" && event.propertyName != "value")
        return;
      var field = $(this);
      window.clearTimeout($(this).data("timeout"));
      $(this).data("timeout", setTimeout(function () {
        checkfield(field);
    }, 2000));
  });

  // trigger a check when losing focus
  $(".stdnum_check").on("blur", function() {
    window.clearTimeout($(this).data("timeout"));
    checkfield($(this));
  });

  // prevent enter from submitting the form
  $(".stdnum_check").keydown(function(event) {
    if(event.keyCode == 13) {
      event.preventDefault();
      checkfield($(this));
      return false;
    }
  });

  // hide the submit button
  $(".stdnum_hide").hide();

  // focus the text field
  $(".stdnum_check").focus();

});