Commit 553ce358 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Removed jQuery dependency

parent f88a38bb
#window.Nette.addError = (elem, message) -> mkelem = (elemtype, classname='', attrs={}) ->
# elem.focus() if elem.focus elem = document.createElement elemtype
# elem.className = classname
# if message elem[attr] = value for own attr, value of attrs
# elem.setCustomValidity(message) elem
# elem.valid = 0
# elem.checkValidity() # replace an input element with an addon group
create_addon = (input) ->
$ -> group = mkelem 'div', 'input-group'
$('form:not(.nofocus):first input:first').focus() input.parentNode.replaceChild group, input
group.appendChild input
create_addon = (input) -> addon = mkelem 'div', 'input-group-addon'
group = $('<div class="input-group">').insertBefore(input) group.appendChild addon
$(input).remove().appendTo(group) addon
$('<div class="input-group-addon">').appendTo(group)
# focus on first input field in first form
$('input[type=range]').each -> document.querySelector('form:not(.nofocus)')?.querySelector('input')?.focus()
create_addon(@)
$(@).on 'change input', -> # add an addon to range inputs displaying the currently selected value
$(@).next('.input-group-addon').text($(@).val()) for e in document.querySelectorAll 'input[type=range]'
$(@).change() create_addon e
$('input[type^=date]').each -> cb = -> @.nextElementSibling.innerHTML = @.value
create_addon(@) e.addEventListener 'change', cb
.html('<span class="glyphicon glyphicon-calendar"></span>') e.addEventListener 'input', cb
.on 'click', -> $(@).prev('input').focus()
cb.call e
$('input[type=number]').on('focus', -> $(@).select())
# add a calender addon to date inputs, and focus on input on addon click
$('input[type=radio][data-inline]').each -> for e in document.querySelectorAll 'input[type^=date]'
group = $('<label class="radio-inline">') addon = create_addon e
.append($(@).parent('label').html()) addon.innerHTML = '<span class="glyphicon glyphicon-calendar"></span>'
$(@).closest('.radio').replaceWith(group) addon.addEventListener 'click', -> @.previousElementSibling.focus()
if /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) # always select the whole number on a number type input to avoid backspacing
$('abbr').on 'click', -> for e in document.querySelectorAll 'input[type=number]'
alert($(@).attr('title')) e.addEventListener 'focus', -> @.select()
$('tr').on 'click', -> # inline radio labels
href = $(this).data('href') for e in document.querySelectorAll '.radio > label > input[type=radio][data-inline]'
document.location = href if href? group = mkelem 'label', 'radio-inline'
group.innerHTML = e.parentNode.innerHTML
$('select[multiple]').addClass('form-control') label = e.parentNode.parentNode
label.parentNode.replaceChild group, label
$('.tags-control').each ->
container = $(@) # make abbreviations clickable on mobile devices (since there is no hover)
tag_list = container.children('.tags') if /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test navigator.userAgent
text_input = container.find('input[type=text]') for e in document.getElementsByTagName 'abbr'
submit_btn = text_input.next().find('.btn') e.addEventListener 'click', -> alert @.getAttribute 'title'
# allow table rows to be links through the data-href attribute
for e in document.querySelectorAll '[data-href]'
e.addEventListener 'click', ->
document.location = @.getAttribute 'data-href'
# fix class names on select boxes with multiple="multiple"
for e in document.querySelectorAll 'select[multiple]'
e.classList.add 'form-control'
# tag list controls on score add/edit pages
if container = document.querySelector '.tags-control'
tag_list = container.querySelector '.tags'
text_input = container.querySelector 'input[type=text]'
submit_btn = text_input.nextElementSibling.querySelector '.btn'
generate_inputs = -> generate_inputs = ->
container.children('input[type=hidden]').remove() for e in container.querySelectorAll 'input[type=hidden]'
container.find('.tag .text').each -> e.parentNode.removeChild e
$('<input type="hidden" name="tags[]">')
.appendTo(container)
.val($(@).text())
submit_tag = -> for e in container.querySelectorAll '.tag .text'
tag = text_input.val().trim() console.log e, e.textContent
container.appendChild mkelem 'input', '',
type: 'hidden', name: 'tags[]', value: e.textContent
if tag == '' submit_tag = ->
return tag = text_input.value.trim()
return if tag == ''
text_input.value = ''
text_input.val('') link = tag_list.appendChild mkelem 'a', 'tag',
href: 'javascript:void(0)'
title: 'Click to remove tag'
link = $('<a href="javascript:void(0)" class="tag" label = link.appendChild mkelem 'span', 'label label-default'
title="Click to remove tag">').appendTo(tag_list) label.innerHTML =
$('<span class="label label-default">') '<span class="text"></span>
.append($('<span class="text">').text(tag)) <span class="glyphicon glyphicon-remove"></span>'
.append(' ') label.firstElementChild.textContent = tag
.append($('<span class="glyphicon glyphicon-remove">'))
.appendTo(link)
generate_inputs() generate_inputs()
text_input.focus() text_input.focus()
text_input.on 'keydown', (e) -> text_input.addEventListener 'keydown', (e) ->
if e.which == 13 if e.keyCode == 13
e.preventDefault() e.preventDefault()
submit_tag() submit_tag()
submit_btn.on 'click', submit_tag submit_btn.addEventListener 'click', submit_tag
container.on 'click', '.tag', -> container.addEventListener 'click', (e) ->
$(@).remove() if e.target.classList.contains 'glyphicon-remove'
label = e.target.parentNode
label.parentNode.removeChild label
generate_inputs() generate_inputs()
# add show/hide event to collapse targets (normally handled by Bootstrap)
for e in document.querySelectorAll '[data-toggle=collapse][data-target]'
((target) ->
if target
e.addEventListener 'click', (e) ->
e.preventDefault()
target.classList.toggle 'in'
)(document.querySelector e.getAttribute 'data-target')
num = (s) -> if s == '' or isNaN(parseInt(s)) then 0 else parseInt(s) if table = document.querySelector '.match-editable'
inputs = table.getElementsByTagName 'input'
$ -> rows = table.children[1].children
$('.match-editable').each -> match_total = table.querySelector '.match-total'
table = $(@) match_avg = table.querySelector '.match-avg'
match_total = table.find('.match-total')
match_avg = table.find('.match-avg') w = table.querySelector('tbody tr').getElementsByTagName('input').length
inputs = table.find('input') h = inputs.length / w
scores = []
sum_row = (row) ->
row_total = 0 sub = (row) -> rows[row].children[w + 1]
prev_total = 0 tot = (row) -> rows[row].children[w + 2]
incomplete = false num = (s) -> if s == '' or isNaN parseInt s then 0 else parseInt s
timer = 0
row.find('input').each ->
if $(@).val() == '' set_score = (col, row, score) ->
incomplete = true if score != scores[row * w + col]
return false scores[row * w + col] = score
clearTimeout timer
row_total += num($(@).val()) timer = setTimeout (-> render row), 50
true
if incomplete
return
if prev_row = row.prev('tr')
prev_total = num(prev_row.children('.total').text())
row.children('.row-total').text(row_total)
row.children('.total').text(prev_total + row_total)
row.children('.total').text(prev_total + row_total)
inputs.on 'change keyup', ->
sum = 0
count = 0
inputs.each ->
if $(@).val() != ''
sum += num($(@).val())
count++
sum_row($(@).closest('tr'))
match_total.text(sum)
match_avg.text(Math.round(sum / count * 10) / 10)
cell = $(@).closest('td')
cls = cell.attr('class').replace(/\s*val-\d+\s*/, '')
cell.attr('class', cls).addClass('val-' + $(@).val())
inputs.filter('[value=""]:first').focus()
$('textarea').on 'keyup', ->
$(@).height(0).height($(@).prop('scrollHeight'))
desired_scroll = $(@).offset().top - 60
if $(document).scrollTop() < desired_scroll
$(window).scrollTop(desired_scroll)
.keyup()
$('#toggle-colored').on 'click', ->
$(@).toggleClass('active').blur()
$('#match').toggleClass('colored')
if $('#match').hasClass('colored') and window.hasOwnProperty('localStorage')
localStorage.setItem('colored-scores', true)
console.log localStorage.getItem('colored-scores')
else else
localStorage.removeItem('colored-scores') false
if window.hasOwnProperty('localStorage') and localStorage.getItem('colored-scores') render = (dirty_row) ->
$('#match').addClass('colored') subtotal = 0
$('#toggle-colored').addClass('active') subtotal += scores[dirty_row * w + col] for col in [0...w]
sub(dirty_row).innerHTML = subtotal
total = if dirty_row then num tot(dirty_row - 1).innerHTML else 0
for row in [dirty_row...h]
total += num sub(row).innerHTML
tot(row).innerHTML = total
match_total.innerHTML = total
match_avg.innerHTML = Math.round(total / inputs.length * 10) / 10
for input, i in inputs
scores.push num input.value
((col, row) ->
cell = input.parentNode
cb = ->
value = num @.value
if set_score col, row, value
cell.className = cell.className.replace /val-\d+/, 'val-' + value
input.addEventListener 'change', cb
input.addEventListener 'keyup', cb
)(i % w, Math.floor i / w)
render 0
table.querySelector('input[value=""]')?.focus()
# notes area grows automatically with each added line
if notes = document.getElementById('notes')
cb = ->
@.style.height = 0
@.style.height = @.scrollHeight + 'px'
notes.addEventListener 'keyup', cb
cb.call notes
# color toggling + persistence in local storage
match = document.getElementById 'match'
toggle_color = document.getElementById('toggle-colored')
if 'localStorage' of window and localStorage.getItem 'colored-scores'
match.classList.add 'colored'
toggle_color.classList.add 'active'
toggle_color.addEventListener 'click', ->
@.classList.toggle 'active'
@.blur()
match.classList.toggle 'colored'
if 'localStorage' of window
if match.classList.contains 'colored'
localStorage.setItem 'colored-scores', true
else
localStorage.removeItem 'colored-scores'
...@@ -46,9 +46,6 @@ ...@@ -46,9 +46,6 @@
<div class="container"> <div class="container">
{include content} {include content}
</div> </div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
{*<script src="js/netteForms.js"></script>*}
<script src="js/forms.js"></script> <script src="js/forms.js"></script>
{ifset #scripts} {ifset #scripts}
{include scripts} {include scripts}
......
...@@ -19,8 +19,8 @@ ...@@ -19,8 +19,8 @@
<a href="match" class="btn btn-sm btn-default" title="{_'Add new match'}"> <a href="match" class="btn btn-sm btn-default" title="{_'Add new match'}">
<span class="glyphicon glyphicon-plus"></span> <span class="glyphicon glyphicon-plus"></span>
</a> </a>
<a data-toggle="collapse" href="#filter" title="{_'Filter'}" <a href="#filter" class="btn btn-sm btn-default" title="{_'Filter'}"
class="btn btn-sm btn-default"> data-toggle="collapse" data-target="#filter">
<span class="glyphicon glyphicon-filter"></span> <span class="glyphicon glyphicon-filter"></span>
</a> </a>
</div> </div>
......
...@@ -60,8 +60,8 @@ ...@@ -60,8 +60,8 @@
<h3 class="panel-title">{_'Notes'}</h3> <h3 class="panel-title">{_'Notes'}</h3>
</div> </div>
<div class="panel-body"> <div class="panel-body">
<textarea name="notes" wrap="hard" placeholder="{_'Click here to make a note'}" <textarea id="notes" name="notes" wrap="hard"
>{$match->notes}</textarea> placeholder="{_'Click here to make a note'}">{$match->notes}</textarea>
</div> </div>
</div> </div>
......
...@@ -10,11 +10,12 @@ ...@@ -10,11 +10,12 @@
{/block} {/block}
{block scripts} {block scripts}
<script type="text/javascript" src="jqplot/jquery.jqplot.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.highlighter.min.js"></script> <script src="jqplot/jquery.jqplot.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.dateAxisRenderer.min.js"></script> <script src="jqplot/plugins/jqplot.highlighter.min.js"></script>
<script type="text/javascript" src="jqplot/plugins/jqplot.canvasOverlay.min.js"></script> <script src="jqplot/plugins/jqplot.dateAxisRenderer.min.js"></script>
<script type="text/javascript"> <script src="jqplot/plugins/jqplot.canvasOverlay.min.js"></script>
<script>
window.data = [ window.data = [
{foreach $matches as $match} {foreach $matches as $match}
[{$match->created_at|date:'%Y-%m-%d %H:%M'}, {match_avg_score($match)}], [{$match->created_at|date:'%Y-%m-%d %H:%M'}, {match_avg_score($match)}],
...@@ -24,5 +25,5 @@ ...@@ -24,5 +25,5 @@
title: {_'Average arrow score over time'} title: {_'Average arrow score over time'}
}; };
</script> </script>
<script type="text/javascript" src="js/plot.js"></script> <script src="js/plot.js"></script>
{/block} {/block}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment