Skip to content

Commit

Permalink
Grid Editing: Add cursor cell navigation to grid. Also includes some …
Browse files Browse the repository at this point in the history
…cleanup and bugfix in grid and datasource-local
  • Loading branch information
jzaefferer committed Jun 22, 2011
1 parent 4f09739 commit 258c960
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 12 deletions.
5 changes: 5 additions & 0 deletions grid-editing/editor.js
Expand Up @@ -42,7 +42,11 @@ $.widget( "ui.editor", {
that.submit( event );
}, 100 );
},
keydown: function( event ) {
event.stopPropagation();
},
keyup: function( event ) {
event.stopPropagation();
if ( event.keyCode === $.ui.keyCode.ENTER || event.keyCode === $.ui.keyCode.NUMPAD_ENTER ) {
this.submit( event );
} else if ( event.keyCode === $.ui.keyCode.ESCAPE ) {
Expand All @@ -58,6 +62,7 @@ $.widget( "ui.editor", {
this._trigger("start", event );
},
_hide: function( event ) {
this.input.blur();
this.inputWrapper.hide();
this.inner.show();
},
Expand Down
6 changes: 2 additions & 4 deletions grid-editing/grid-editor.js
@@ -1,11 +1,9 @@
/*
* Inline Editor
* Grid Inline Editor
*
* Depends on:
* widget
*
* Optional:
* spinner
* editor
*/
(function( $ ) {

Expand Down
14 changes: 14 additions & 0 deletions grid-editing/grid.html
Expand Up @@ -20,9 +20,19 @@
<script src="../grid-spf/pager.js"></script>
<script src="editor.js"></script>
<script src="grid-editor.js"></script>
<script src="navigator.js"></script>
<script src="localstore.js"></script>
<script src="helpers.js"></script>
<script>
if ( !window.console ) {
window.console = {
log: function() {
var message = Array.prototype.slice.call( arguments, 1 ).join( ", " );
$("<div>").text( message ).appendTo("body");
}
}
}

var store = $.demos.localstore( {
key: "grid-editing-developers",
initial: "../grid-spf/developers.json"
Expand Down Expand Up @@ -74,8 +84,11 @@
developers._trigger("objectchange", event, {
object: object
});
// for the navigator
grid.element.focus();
}
});
grid.element.navigator();

// TODO find a better way to add markup to the generated row template
grid.options.rowTemplate = grid.options.rowTemplate.substring( 0, grid.options.rowTemplate.length - 35 )
Expand Down Expand Up @@ -141,6 +154,7 @@
});
</script>
<style>
.navigator-active { border: 2px solid blue; }
</style>
</head>
<body>
Expand Down
1 change: 0 additions & 1 deletion grid-editing/helpers.js
Expand Up @@ -51,7 +51,6 @@ function meta( input ) {
}
output.push(field);
}
console.log(input, output)
return output;
}

Expand Down
95 changes: 95 additions & 0 deletions grid-editing/navigator.js
@@ -0,0 +1,95 @@
/*
* Grid Navigator
*
* Depends on:
* widget
*/
(function( $ ) {

$.widget( "ui.navigator", {
_create: function() {
this.active = $();
this.element.attr( "tabIndex", 0 );
this._bind({
focusin: "activate",
focusout: "deactivate",
keydown: "move",
keyup: "enter",
click: "select"
});
},
select: function( event ) {
var target = $( event.target ).closest( "td" );
if (target.length) {
this.deactivate();
this.active = target;
this.activate();
}
},
activate: function() {
if ( !this.active.length || !this.active.parents("body").length ) {
this.active = this.element.find("td:first");
}
this.active.addClass("navigator-active");
},
deactivate: function() {
this.active.removeClass("navigator-active");
},
move: function( event ) {
switch ( event.keyCode ) {
case $.ui.keyCode.RIGHT:
this.right(); break;
case $.ui.keyCode.LEFT:
this.left(); break;
case $.ui.keyCode.UP:
this.up(); break;
case $.ui.keyCode.DOWN:
this.down(); break;
}
},
enter: function( event ) {
switch ( event.keyCode ) {
case $.ui.keyCode.ENTER:
this.click(); break;
}
},
right: function() {
var next = this.active.next();
if (next.length) {
this.deactivate();
this.active = next;
this.activate();
}
},
left: function() {
var next = this.active.prev();
if (next.length) {
this.deactivate();
this.active = next;
this.activate();
}
},
up: function() {
var index = this.active[ 0 ].cellIndex;
var next = this.active.parent().prev().children( "td" ).eq( index );
if (next.length) {
this.deactivate();
this.active = next;
this.activate();
}
},
down: function() {
var index = this.active[ 0 ].cellIndex;
var next = this.active.parent().next().children( "td" ).eq( index );
if (next.length) {
this.deactivate();
this.active = next;
this.activate();
}
},
click: function() {
this.active.trigger("click");
}
});

})( jQuery );
3 changes: 1 addition & 2 deletions grid-spf/datasource-local.js
Expand Up @@ -10,12 +10,11 @@ $.widget( "ui.localDatasource", $.ui.datasource, {
var sortedItems = that._sort( that._filter( that.options.input ) );
response( that._page( sortedItems ), sortedItems.length );
}
this.refresh();
},
_filter: function( items ) {
if ( this.options.filter ) {
var that = this;
return $.grep( items, function ( item ) {
return $.grep( items, function ( item ) {
var property,
filter,
match = true;
Expand Down
9 changes: 4 additions & 5 deletions grid-spf/grid.js
@@ -1,10 +1,10 @@
/*
* Grid
*
*
* Depends on:
* tmpl
* datastore
*
*
* Optional:
* extractingDatasource
*/
Expand All @@ -17,7 +17,6 @@ $.widget( "ui.grid", {
},
_create: function() {
var that = this;

this._columns();
this._rowTemplate();
this.element.addClass( "ui-widget" );
Expand All @@ -27,7 +26,7 @@ $.widget( "ui.grid", {
// TODO add item
});
});
$(this.options.source).bind("datasourceresponse", function() {
$(this.options.source.element).bind("datasourceresponse", function() {
that.refresh();
});
},
Expand All @@ -44,7 +43,7 @@ $.widget( "ui.grid", {
tbody.find( "td" ).addClass( "ui-widget-content" );
this._trigger("refresh");
},

_columns: function() {
if ( this.options.columns ) {
// TODO this code assumes any present th is a column header, but it may be a row header
Expand Down

0 comments on commit 258c960

Please sign in to comment.