From 096a118806382730df4cdbbff69b9f4068eafc47 Mon Sep 17 00:00:00 2001 From: Brian Miyaji Date: Wed, 19 Feb 2014 12:33:00 +1100 Subject: [PATCH] Add jQuery data table keyboard navigation --- admin/hooks/admin-enqueue-scripts.php | 1 + assets/js/admin.js | 38 +++++++++++++++++++++++++++ assets/js/jquery.caret.min.js | 10 +++++++ 3 files changed, 49 insertions(+) create mode 100644 assets/js/jquery.caret.min.js diff --git a/admin/hooks/admin-enqueue-scripts.php b/admin/hooks/admin-enqueue-scripts.php index 912c7d5c..d838d953 100644 --- a/admin/hooks/admin-enqueue-scripts.php +++ b/admin/hooks/admin-enqueue-scripts.php @@ -10,6 +10,7 @@ function sportspress_admin_enqueue_scripts( $hook ) { wp_enqueue_script( 'jquery' ); wp_enqueue_script( 'chosen', SPORTSPRESS_PLUGIN_URL .'assets/js/chosen.jquery.min.js', array( 'jquery' ), '1.1.0', true ); + wp_enqueue_script( 'caret', SPORTSPRESS_PLUGIN_URL .'assets/js/jquery.caret.min.js', array( 'jquery' ), '1.02', true ); if ( $hook == 'edit-tags.php' && isset( $_GET['taxonomy'] ) && $_GET['taxonomy'] == 'sp_venue' ): wp_enqueue_script( 'google-maps', 'http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places' ); diff --git a/assets/js/admin.js b/assets/js/admin.js index 88604c2d..9322d27a 100644 --- a/assets/js/admin.js +++ b/assets/js/admin.js @@ -104,6 +104,44 @@ jQuery(document).ready(function($){ } }); + // Data table keyboard navigation + $(".sp-data-table tbody tr td input").keydown(function(event) { + if([37,38,39,40].indexOf(event.keyCode) > -1){ + $el = $(this).closest("td"); + var col = $el.parent().children().index($el)+1; + var row = $el.parent().parent().children().index($el.parent())+1; + if(event.keyCode == 37){ + if ( $(this).caret().start != 0 ) + return true; + col -= 1; + } + if(event.keyCode == 38){ + row -= 1; + } + if(event.keyCode == 39){ + if ( $(this).caret().start != $(this).val().length ) + return true; + col += 1; + } + if(event.keyCode == 40){ + row += 1; + } + $el.closest("tbody").find("tr:nth-child("+row+") td:nth-child("+col+") input").focus(); + } + }); + + // Prevent data table from submitting form + $(".sp-data-table tbody tr td input").keypress(function(event) { + if(event.keyCode == 13){ + event.preventDefault(); + $el = $(this).closest("td"); + var col = $el.parent().children().index($el)+1; + var row = $el.parent().parent().children().index($el.parent())+2; + $el.closest("tbody").find("tr:nth-child("+row+") td:nth-child("+col+") input").focus(); + return false; + } + }); + // Total stats calculator $(".sp-data-table .sp-total input").on("updateTotal", function() { index = $(this).parent().index(); diff --git a/assets/js/jquery.caret.min.js b/assets/js/jquery.caret.min.js new file mode 100644 index 00000000..7a00ee55 --- /dev/null +++ b/assets/js/jquery.caret.min.js @@ -0,0 +1,10 @@ +/* + * + * Copyright (c) 2010 C. F., Wong (Cloudgen Examplet Store) + * Licensed under the MIT License: + * http://www.opensource.org/licenses/mit-license.php + * + */ +(function(k,e,i,j){k.fn.caret=function(b,l){var a,c,f=this[0],d=k.browser.msie;if(typeof b==="object"&&typeof b.start==="number"&&typeof b.end==="number"){a=b.start;c=b.end}else if(typeof b==="number"&&typeof l==="number"){a=b;c=l}else if(typeof b==="string")if((a=f.value.indexOf(b))>-1)c=a+b[e];else a=null;else if(Object.prototype.toString.call(b)==="[object RegExp]"){b=b.exec(f.value);if(b!=null){a=b.index;c=a+b[0][e]}}if(typeof a!="undefined"){if(d){d=this[0].createTextRange();d.collapse(true); +d.moveStart("character",a);d.moveEnd("character",c-a);d.select()}else{this[0].selectionStart=a;this[0].selectionEnd=c}this[0].focus();return this}else{if(d){c=document.selection;if(this[0].tagName.toLowerCase()!="textarea"){d=this.val();a=c[i]()[j]();a.moveEnd("character",d[e]);var g=a.text==""?d[e]:d.lastIndexOf(a.text);a=c[i]()[j]();a.moveStart("character",-d[e]);var h=a.text[e]}else{a=c[i]();c=a[j]();c.moveToElementText(this[0]);c.setEndPoint("EndToEnd",a);g=c.text[e]-a.text[e];h=g+a.text[e]}}else{g= +f.selectionStart;h=f.selectionEnd}a=f.value.substring(g,h);return{start:g,end:h,text:a,replace:function(m){return f.value.substring(0,g)+m+f.value.substring(h,f.value[e])}}}}})(jQuery,"length","createRange","duplicate"); \ No newline at end of file