Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/online_check/check.js
blob: 69019564bcd6494804dade4240f2f3daa82858e2 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 # check.js - simple application to check numbers
 #
 # Copyright (C) 2017-2020 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>',
        $('<div/>').text(result.number).html(),
        ': <b>',
        $('<div/>').text(result.name).html(),
        '</b>',
        '<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').html(h.join(''))
  }

  function checkfield(field) {
    var value = field.val()
    // only trigger update if value changed from previous validation
    if (value !== field.data('oldvalue')) {
      field.data('oldvalue', value)
      $('#' + $(field).attr('id') + '_results').slideUp(200, function () {
        $.get('.', {number: value}, function (data) {
          window.history.pushState({value: value, data: data}, $(document).find('title').text(), '?number=' + encodeURIComponent(value))
          updateresults(field, data)
        })
        $(this).slideDown(300)
      })
    }
  }

  // update results based on history navigation
  window.onpopstate = function (e) {
    var field = $('.stdnum_check')
    if (e.state) {
      var value = e.state.value
      var data = e.state.data
      field.val(value)
      field.data('oldvalue', value)
      updateresults(field, data)
    } else {
      field.val('')
      field.data('oldvalue', '')
      updateresults(field, [])
    }
  }

  // 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()

  // save current state
  var value = $('.stdnum_check').val()
  $('.stdnum_check').data('oldvalue', value)
  $.get('', {number: value}, function (data) {
    window.history.replaceState({value: value, data: data}, $(document).find('title').text(), '?number=' + encodeURIComponent(value))
  })
})