diff --git a/bower.json b/bower.json index 1e2c914..29dafb2 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "sensei-grid", - "version": "0.1.0", + "version": "0.1.2", "homepage": "https://github.com/datazenit/sensei-grid", "authors": [ "Lauris " diff --git a/dist/sensei-grid.css b/dist/sensei-grid.css index d9070f4..46aae7f 100644 --- a/dist/sensei-grid.css +++ b/dist/sensei-grid.css @@ -1,5 +1,5 @@ /** - * sensei-grid v0.1.0 + * sensei-grid v0.1.2 * Copyright (c) 2014 Lauris Dzilums * Licensed under MIT */ @@ -52,7 +52,7 @@ table { } .sensei-grid, .sensei-grid-editor input, -.sensei-grid-editor input { +.sensei-grid-editor select { font: 14px Helvetica, Arial, sans-serif; line-height: 20px; } diff --git a/dist/sensei-grid.js b/dist/sensei-grid.js index 78c588e..1a67f41 100644 --- a/dist/sensei-grid.js +++ b/dist/sensei-grid.js @@ -1,5 +1,5 @@ /** - * sensei-grid v0.1.0 + * sensei-grid v0.1.2 * Copyright (c) 2014 Lauris Dzilums * Licensed under MIT */ @@ -494,17 +494,17 @@ // specific keyCodes that won't be hijacked from the editor var editorCodes = [8, 37, 38, 39, 40]; - if ((plugin.getActiveCell().length === 0 && !plugin.isEditing) || !_.contains(codes, e.keyCode)) { + if ((plugin.getActiveCell().length === 0 && !plugin.isEditing) || !_.contains(codes, e.which)) { return; } - if (plugin.isEditing && _.contains(editorCodes, e.keyCode)) { + if (plugin.isEditing && _.contains(editorCodes, e.which)) { return; } else { e.preventDefault(); } - switch (e.keyCode) { + switch (e.which) { case 37: // left plugin.move("left"); break; diff --git a/dist/sensei-grid.min.css b/dist/sensei-grid.min.css index 9881db8..e643fc8 100644 --- a/dist/sensei-grid.min.css +++ b/dist/sensei-grid.min.css @@ -1 +1 @@ -a,abbr,acronym,address,applet,article,aside,audio,b,big,blockquote,body,canvas,caption,center,cite,code,dd,del,details,dfn,div,dl,dt,em,embed,fieldset,figcaption,figure,footer,form,h1,h2,h3,h4,h5,h6,header,hgroup,html,i,iframe,img,ins,kbd,label,legend,li,mark,menu,nav,object,ol,output,p,pre,q,ruby,s,samp,section,small,span,strike,strong,sub,summary,sup,table,tbody,td,tfoot,th,thead,time,tr,tt,u,ul,var,video{margin:0;padding:0;border:0;font:inherit;vertical-align:baseline}article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:after,blockquote:before,q:after,q:before{content:'';content:none}table{border-collapse:collapse;border-spacing:0}.sensei-grid{outline:0}.sensei-grid,.sensei-grid-editor input{font:14px Helvetica,Arial,sans-serif;line-height:20px}.sensei-grid *{box-sizing:border-box}.sensei-grid table{border-spacing:0;border-collapse:collapse;width:100%;table-layout:fixed;empty-cells:show;border:1px solid #ddd}.sensei-grid table td,.sensei-grid table th{overflow:hidden;white-space:nowrap;border:1px solid #ddd;text-align:left;padding:5px;vertical-align:top}.sensei-grid thead{background:#F5F5F5}.sensei-grid thead>tr>th{vertical-align:bottom;border-bottom-width:1px}.sensei-grid .activeCell{background:#d0e9f8;border:1px double #39f}.sensei-grid .activeRow{background:#eff7fc}.sensei-grid-editor{position:absolute;background:#fff;border:2px solid #39f;box-shadow:2px 2px 2px rgba(0,0,0,.1)}.sensei-grid-editor input,.sensei-grid-editor select{border:none;width:100%;height:100%;box-shadow:none;outline:0;padding:5px 4px;background:#fff}.sensei-grid-sortable tr>th{cursor:pointer;user-select:none;-webkit-user-select:none} \ No newline at end of file +a,abbr,acronym,address,applet,article,aside,audio,b,big,blockquote,body,canvas,caption,center,cite,code,dd,del,details,dfn,div,dl,dt,em,embed,fieldset,figcaption,figure,footer,form,h1,h2,h3,h4,h5,h6,header,hgroup,html,i,iframe,img,ins,kbd,label,legend,li,mark,menu,nav,object,ol,output,p,pre,q,ruby,s,samp,section,small,span,strike,strong,sub,summary,sup,table,tbody,td,tfoot,th,thead,time,tr,tt,u,ul,var,video{margin:0;padding:0;border:0;font:inherit;vertical-align:baseline}article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:after,blockquote:before,q:after,q:before{content:'';content:none}table{border-collapse:collapse;border-spacing:0}.sensei-grid{outline:0}.sensei-grid,.sensei-grid-editor input,.sensei-grid-editor select{font:14px Helvetica,Arial,sans-serif;line-height:20px}.sensei-grid *{box-sizing:border-box}.sensei-grid table{border-spacing:0;border-collapse:collapse;width:100%;table-layout:fixed;empty-cells:show;border:1px solid #ddd}.sensei-grid table td,.sensei-grid table th{overflow:hidden;white-space:nowrap;border:1px solid #ddd;text-align:left;padding:5px;vertical-align:top}.sensei-grid thead{background:#F5F5F5}.sensei-grid thead>tr>th{vertical-align:bottom;border-bottom-width:1px}.sensei-grid .activeCell{background:#d0e9f8;border:1px double #39f}.sensei-grid .activeRow{background:#eff7fc}.sensei-grid-editor{position:absolute;background:#fff;border:2px solid #39f;box-shadow:2px 2px 2px rgba(0,0,0,.1)}.sensei-grid-editor input,.sensei-grid-editor select{border:none;width:100%;height:100%;box-shadow:none;outline:0;padding:5px 4px;background:#fff}.sensei-grid-sortable tr>th{cursor:pointer;user-select:none;-webkit-user-select:none} \ No newline at end of file diff --git a/dist/sensei-grid.min.js b/dist/sensei-grid.min.js index 4ef84cd..c4e7eb6 100644 --- a/dist/sensei-grid.min.js +++ b/dist/sensei-grid.min.js @@ -1,6 +1,6 @@ /** - * sensei-grid v0.1.0 + * sensei-grid v0.1.2 * Copyright (c) 2014 Lauris Dzilums * Licensed under MIT */ -!function(a){a.fn.grid=function(b,c,d){var e=this,f={sortable:!0,tableClass:""};return e.isEditing=!1,a.fn.isOnScreen=function(){var b=a(window),c={top:b.scrollTop(),left:b.scrollLeft()};c.right=c.left+b.width(),c.bottom=c.top+b.height();var d=this.offset();return d.right=d.left+this.outerWidth(),d.bottom=d.top+this.outerHeight(),!(c.rightd.right||c.bottomd.bottom)},a.fn.setActiveCell=function(){a("tr",e.$el).removeClass("activeRow"),a("tr>.activeCell",e.$el).removeClass("activeCell"),a(this).addClass("activeCell"),a(this).parent("tr").addClass("activeRow"),e.events.trigger("cell:select",a(this))},a.fn.cellPosition=function(){var b=a("div",this).position();console.log("cell pos",b),b.left=Math.round(b.left),b.top=Math.round(b.top);var c=a(this).outerWidth()-a(this).width(),d=a(this).outerHeight()-a(this).height();return b.top-=Math.round(d/2),b.left-=Math.round(c/2),b},e.events={_events:{}},e.events.on=function(a,b,c){_.has(this._events,a)||(this._events[a]=[]),this._events[a].push({callback:b,context:c})},e.events.trigger=function(a){var b=Array.prototype.slice.call(arguments,1);if(_.has(this._events,a)){var c=this._events[a];_.each(c,function(c){console.log("trigger event",a,c);var d=_.bind(c.callback,c.context);d.apply(this,b)})}},e.events.off=function(a){_.has(this._events,a)&&delete this._events[a]},e.registerEditor=function(a){var b=new a(e);e.editors[b.name]=b},e.render=function(){console.log("render"),e.renderBaseTable(),e.renderColumns(),e.renderData(),_.each(e.editors,function(a){a.initialize(),a.render(),a.getElement().hide()}),e.bindEvents()},e.destroy=function(){e.unbindEvents(),e.$el.remove()},e.bindEvents=function(){e.$el.on("click","tr>td",e.clickCell),e.$el.on("dblclick","tr>td",e.dblClickCell),e.$el.on("blur",e.blur),e.$el.on("keydown",e.keydown),a(document).on("click",e.editorBlur)},e.unbindEvents=function(){e.$el.off("click","tr>td"),e.$el.off("dblclick","tr>td"),e.$el.off("blur"),e.$el.off("keydown"),a(document).off("click")},e.editorBlur=function(b){0===e.$el.has(a(b.target)).length&&(console.log("editorBlur -> is grid event:",e.$el.has(a(b.target)).length),e.exitEditor(),e.deactivateCell())},e.hideEditors=function(){a(".sensei-grid-editor",e.$el).hide()},e.blur=function(a){console.log("blur",a.relatedTarget),e.isEditing?console.log("focus moved to editor"):(console.log("grid blur, focus not on editor"),e.exitEditor(),e.isEditing=!1,e.deactivateCell())},e.parsers={},e.parsers.string=function(a){return a.toString()},e.parsers["int"]=function(a){return parseInt(a)},e.parsers["float"]=function(a){return parseFloat(a)},e.getCellData=function(a){var b=a.text(),c=e.getCellType(a);return _.has(e.parsers,c)&&(b=e.parsers[c](b)),b},e.getCellColumn=function(a){return a.data("column")},e.getCellType=function(a){return a.data("type")},e.getCellDataByIndex=function(a,b){var c=e.getRowByIndex(a),d=e.getCellFromRowByIndex(c,b);return e.getCellData(d)},e.getCellDataByKey=function(a,b){var c=e.getRowByIndex(a),d=e.getCellFromRowByKey(c,b);return e.getCellData(d)},e.getCellFromRowByIndex=function(a,b){var c=a.find("td").eq(b);if(0===c.length)throw new Error("Cell does not exist");return c},e.getCellFromRowByKey=function(b,c){var d=b.find("td").filter(function(){return a(this).data("column")===c});if(0===d.length)throw new Error("Cell does not exist");return d},e.getRowCellsByIndex=function(a){return e.getRowByIndex(a).find("td")},e.getRowCells=function(a){return a.find("td")},e.getRowByIndex=function(a){var b=e.$el.find("tbody>tr").eq(a);if(0===b.length)throw new Error("Row does not exist");return b},e.getRowDataByIndex=function(a){var b=e.getRowByIndex(a);return e.getRowData(b)},e.getRowData=function(b){var c=e.getRowCells(b),d={};return c.each(function(){d[e.getCellColumn(a(this))]=e.getCellData(a(this))}),d},e.getRows=function(){return e.$el.find("tbody>tr")},e.getGridData=function(){var b=e.getRows();return b.map(function(){return e.getRowData(a(this))}).get()},e.getActiveCell=function(){return e.isEditing&&e.activeEditor&&e.activeEditor.activeCell?e.activeEditor.activeCell:a("td.activeCell",e.$el)},e.deactivateCell=function(){console.log("deactivateCell");var a=e.getActiveCell();a.removeClass("activeCell"),a.parent("tr").removeClass("activeRow")},e.clearActiveCell=function(){var b=e.getActiveCell();a(">div",b).empty()},e.moveRight=function(){var b=e.getActiveCell();if(b.next().length>0)b.next().setActiveCell();else{var c=b.parent("tr").next();c.length>0&&a("td:first",c).setActiveCell()}},e.moveUp=function(){var b=e.getActiveCell(),c=b.parent("tr").prev();if(c.length>0){var d=b.index(),f=a("td",c).eq(d);f.length>0?f.setActiveCell():a("td:last",c).setActiveCell()}},e.moveLeft=function(){var b=e.getActiveCell();if(console.log("left"),b.prev().length>0)b.prev().setActiveCell();else{var c=b.parent("tr").prev();c.length>0&&a("td:last",c).setActiveCell()}},e.moveDown=function(){var b=e.getActiveCell(),c=b.parent("tr").next();if(c.length>0){var d=b.index(),f=a("td",c).eq(d);f.length>0?f.setActiveCell():a("td:first",c).setActiveCell()}},e.move=function(a){var b="move"+a.charAt(0).toUpperCase()+a.substr(1);_.has(e,b)?(e[b](),e.isEditing&&e.saveEditor(),e.isEditing&&(console.log("move -> edit cell",e.getActiveCell()),e.editCell()),e.getActiveCell().find(">div").isOnScreen()||e.getActiveCell().get(0).scrollIntoView(_.contains(["up","left"],a)?!0:!1)):console.warn("move method not found",b)},e.editCell=function(){console.log("editCell"),e.showEditor()},e.getEditor=function(){return e.activeEditor},e.getEditorInstance=function(){var a=e.getActiveCell(),b=a.data("editor");return b&&_.has(e.editors,b)?(console.log("getEditor",b),e.editors[b]):(console.warn("Editor not found:",b),null)},e.saveEditor=function(){if(console.log("save editor"),e.isEditing){var b=e.getActiveCell(),c=e.activeEditor.getValue();b.html(a("
").text(c));var d={};d[b.data("column")]=c,e.events.trigger("editor:save",d,b)}e.getEditor().hide()},e.exitEditor=function(a){console.log("exit editor");var b=e.getActiveCell();e.isEditing&&e.activeEditor&&(a?e.getEditor().hide():e.saveEditor()),console.log("need to regain focus on sensei-grid"),b.setActiveCell(),e.$el.focus(),e.isEditing=!1},e.moveEditor=function(){e.isEditing&&(e.showEditor(),console.log("editor is visible, move along"),e.editCell())},e.showEditor=function(){console.log("show editor"),e.activeEditor=e.getEditorInstance();var a=e.activeEditor.getElement(),b=e.getActiveCell();e.activeEditor.activeCell=b,e.isEditing=!0,a.show(),a.css(b.cellPosition()),a.css({width:b.outerWidth()+1,height:b.outerHeight()+1});var c=b.data("column"),d=b.text();e.activeEditor.setValue(d);var f={};return f[c]=d,e.events.trigger("editor:load",f,b),a},e.keydown=function(a){var b=!0,c=[8,9,13,27,37,38,39,40],d=[8,37,38,39,40];if(!(0===e.getActiveCell().length&&!e.isEditing||!_.contains(c,a.keyCode)||e.isEditing&&_.contains(d,a.keyCode))){switch(a.preventDefault(),a.keyCode){case 37:e.move("left");break;case 38:e.move("up");break;case 39:e.move("right");break;case 40:e.move("down");break;case 13:e.isEditing?a.ctrlKey&&a.shiftKey?e.move("up"):a.ctrlKey&&!a.shiftKey?e.move("down"):e.exitEditor():e.editCell();break;case 27:e.isEditing?e.exitEditor(!0):e.$el.blur();break;case 9:e.move(a.shiftKey?"left":"right");break;case 8:e.clearActiveCell()}b&&a.preventDefault()}},e.clickCell=function(b){b.preventDefault(),console.log("clicked cell"),e.isEditing&&e.exitEditor(),a(this).setActiveCell()},e.dblClickCell=function(b){b.preventDefault(),console.log("double clicked cell"),a(this).setActiveCell(),e.editCell()},e.renderColumns=function(){console.log("renderColumns");var b=a("thead",e.$el),c=document.createElement("tr");_.each(e.columns,function(b){var d=document.createElement("th"),e=document.createElement("div");a(e).text(b.name),d.appendChild(e),a(d).data("type",b.type||"string"),a(d).data("editor",b.editor||"BasicEditor"),c.appendChild(d)}),b.append(c)},e.renderData=function(){console.log("renderData");var b=a("tbody",e.$el);_.each(e.data,function(c){var d=document.createElement("tr");_.each(e.columns,function(b){var e=document.createElement("td"),f=document.createElement("div");_.has(c,b.name)&&a(f).text(c[b.name]),a(e).data("column",b.name),a(e).data("type",b.type||"string"),a(e).data("editor",b.editor||"BasicEditor"),e.appendChild(f),d.appendChild(e)}),b.append(d)})},e.renderBaseTable=function(){console.log("renderBaseTable");var a=document.createElement("table"),b=document.createElement("thead"),c=document.createElement("tbody");a.appendChild(b),a.appendChild(c),a.className=e.config.tableClass,e.$el.html(a),e.$el.attr("tabindex",-1)},e.init=function(b,c,d){return console.log("sensei grid init"),e.config=a.extend({},f,d),e.data=b,e.columns=c,e.$el=a(this),e.editors={},e},e.init(b,c,d)}}(jQuery),function(a){var b=this,c=function(a){this.grid=a};c.extend=function(a){var b,c=this;b=function(){return c.apply(this,arguments)};var d=function(){this.constructor=b};return d.prototype=c.prototype,b.prototype=new d,a&&_.extend(b.prototype,a),b.__super__=c.prototype,b},c.prototype.getElement=function(){return a(this.editor)},c.prototype.initialize=function(){},c.prototype.render=function(){},c.prototype.show=function(){this.getElement().show()},c.prototype.hide=function(){console.log("Editor hide"),this.getElement().hide(),this.grid.activeEditor.activeCell=null,this.grid.activeEditor=null},c.prototype.getValue=function(){console.warn("Editor.getValue not implemented")},c.prototype.setValue=function(){console.warn("Editor.setValue not implemented")},b.Editor=c,b.BasicEditor=c.extend({types:[],name:"BasicEditor",render:function(){if(console.log("BasicEditor.render"),!this.editor){this.editor=document.createElement("div"),this.editor.className="sensei-grid-editor sensei-grid-basic-editor";var a=document.createElement("input");this.editor.appendChild(a),this.grid.$el.append(this.editor)}},getValue:function(){return a("input",this.editor).val()},setValue:function(b){a("input",this.editor).val(b).focus()}})}(jQuery); \ No newline at end of file +!function(a){a.fn.grid=function(b,c,d){var e=this,f={sortable:!0,tableClass:""};return e.isEditing=!1,a.fn.isOnScreen=function(){var b=a(window),c={top:b.scrollTop(),left:b.scrollLeft()};c.right=c.left+b.width(),c.bottom=c.top+b.height();var d=this.offset();return d.right=d.left+this.outerWidth(),d.bottom=d.top+this.outerHeight(),!(c.rightd.right||c.bottomd.bottom)},a.fn.setActiveCell=function(){a("tr",e.$el).removeClass("activeRow"),a("tr>.activeCell",e.$el).removeClass("activeCell"),a(this).addClass("activeCell"),a(this).parent("tr").addClass("activeRow"),e.events.trigger("cell:select",a(this))},a.fn.cellPosition=function(){var b=a("div",this).position();console.log("cell pos",b),b.left=Math.round(b.left),b.top=Math.round(b.top);var c=a(this).outerWidth()-a(this).width(),d=a(this).outerHeight()-a(this).height();return b.top-=Math.round(d/2),b.left-=Math.round(c/2),b},e.events={_events:{}},e.events.on=function(a,b,c){_.has(this._events,a)||(this._events[a]=[]),this._events[a].push({callback:b,context:c})},e.events.trigger=function(a){var b=Array.prototype.slice.call(arguments,1);if(_.has(this._events,a)){var c=this._events[a];_.each(c,function(c){console.log("trigger event",a,c);var d=_.bind(c.callback,c.context);d.apply(this,b)})}},e.events.off=function(a){_.has(this._events,a)&&delete this._events[a]},e.registerEditor=function(a){var b=new a(e);e.editors[b.name]=b},e.render=function(){console.log("render"),e.renderBaseTable(),e.renderColumns(),e.renderData(),_.each(e.editors,function(a){a.initialize(),a.render(),a.getElement().hide()}),e.bindEvents()},e.destroy=function(){e.unbindEvents(),e.$el.remove()},e.bindEvents=function(){e.$el.on("click","tr>td",e.clickCell),e.$el.on("dblclick","tr>td",e.dblClickCell),e.$el.on("blur",e.blur),e.$el.on("keydown",e.keydown),a(document).on("click",e.editorBlur)},e.unbindEvents=function(){e.$el.off("click","tr>td"),e.$el.off("dblclick","tr>td"),e.$el.off("blur"),e.$el.off("keydown"),a(document).off("click")},e.editorBlur=function(b){0===e.$el.has(a(b.target)).length&&(console.log("editorBlur -> is grid event:",e.$el.has(a(b.target)).length),e.exitEditor(),e.deactivateCell())},e.hideEditors=function(){a(".sensei-grid-editor",e.$el).hide()},e.blur=function(a){console.log("blur",a.relatedTarget),e.isEditing?console.log("focus moved to editor"):(console.log("grid blur, focus not on editor"),e.exitEditor(),e.isEditing=!1,e.deactivateCell())},e.parsers={},e.parsers.string=function(a){return a.toString()},e.parsers["int"]=function(a){return parseInt(a)},e.parsers["float"]=function(a){return parseFloat(a)},e.getCellData=function(a){var b=a.text(),c=e.getCellType(a);return _.has(e.parsers,c)&&(b=e.parsers[c](b)),b},e.getCellColumn=function(a){return a.data("column")},e.getCellType=function(a){return a.data("type")},e.getCellDataByIndex=function(a,b){var c=e.getRowByIndex(a),d=e.getCellFromRowByIndex(c,b);return e.getCellData(d)},e.getCellDataByKey=function(a,b){var c=e.getRowByIndex(a),d=e.getCellFromRowByKey(c,b);return e.getCellData(d)},e.getCellFromRowByIndex=function(a,b){var c=a.find("td").eq(b);if(0===c.length)throw new Error("Cell does not exist");return c},e.getCellFromRowByKey=function(b,c){var d=b.find("td").filter(function(){return a(this).data("column")===c});if(0===d.length)throw new Error("Cell does not exist");return d},e.getRowCellsByIndex=function(a){return e.getRowByIndex(a).find("td")},e.getRowCells=function(a){return a.find("td")},e.getRowByIndex=function(a){var b=e.$el.find("tbody>tr").eq(a);if(0===b.length)throw new Error("Row does not exist");return b},e.getRowDataByIndex=function(a){var b=e.getRowByIndex(a);return e.getRowData(b)},e.getRowData=function(b){var c=e.getRowCells(b),d={};return c.each(function(){d[e.getCellColumn(a(this))]=e.getCellData(a(this))}),d},e.getRows=function(){return e.$el.find("tbody>tr")},e.getGridData=function(){var b=e.getRows();return b.map(function(){return e.getRowData(a(this))}).get()},e.getActiveCell=function(){return e.isEditing&&e.activeEditor&&e.activeEditor.activeCell?e.activeEditor.activeCell:a("td.activeCell",e.$el)},e.deactivateCell=function(){console.log("deactivateCell");var a=e.getActiveCell();a.removeClass("activeCell"),a.parent("tr").removeClass("activeRow")},e.clearActiveCell=function(){var b=e.getActiveCell();a(">div",b).empty()},e.moveRight=function(){var b=e.getActiveCell();if(b.next().length>0)b.next().setActiveCell();else{var c=b.parent("tr").next();c.length>0&&a("td:first",c).setActiveCell()}},e.moveUp=function(){var b=e.getActiveCell(),c=b.parent("tr").prev();if(c.length>0){var d=b.index(),f=a("td",c).eq(d);f.length>0?f.setActiveCell():a("td:last",c).setActiveCell()}},e.moveLeft=function(){var b=e.getActiveCell();if(console.log("left"),b.prev().length>0)b.prev().setActiveCell();else{var c=b.parent("tr").prev();c.length>0&&a("td:last",c).setActiveCell()}},e.moveDown=function(){var b=e.getActiveCell(),c=b.parent("tr").next();if(c.length>0){var d=b.index(),f=a("td",c).eq(d);f.length>0?f.setActiveCell():a("td:first",c).setActiveCell()}},e.move=function(a){var b="move"+a.charAt(0).toUpperCase()+a.substr(1);_.has(e,b)?(e[b](),e.isEditing&&e.saveEditor(),e.isEditing&&(console.log("move -> edit cell",e.getActiveCell()),e.editCell()),e.getActiveCell().find(">div").isOnScreen()||e.getActiveCell().get(0).scrollIntoView(_.contains(["up","left"],a)?!0:!1)):console.warn("move method not found",b)},e.editCell=function(){console.log("editCell"),e.showEditor()},e.getEditor=function(){return e.activeEditor},e.getEditorInstance=function(){var a=e.getActiveCell(),b=a.data("editor");return b&&_.has(e.editors,b)?(console.log("getEditor",b),e.editors[b]):(console.warn("Editor not found:",b),null)},e.saveEditor=function(){if(console.log("save editor"),e.isEditing){var b=e.getActiveCell(),c=e.activeEditor.getValue();b.html(a("
").text(c));var d={};d[b.data("column")]=c,e.events.trigger("editor:save",d,b)}e.getEditor().hide()},e.exitEditor=function(a){console.log("exit editor");var b=e.getActiveCell();e.isEditing&&e.activeEditor&&(a?e.getEditor().hide():e.saveEditor()),console.log("need to regain focus on sensei-grid"),b.setActiveCell(),e.$el.focus(),e.isEditing=!1},e.moveEditor=function(){e.isEditing&&(e.showEditor(),console.log("editor is visible, move along"),e.editCell())},e.showEditor=function(){console.log("show editor"),e.activeEditor=e.getEditorInstance();var a=e.activeEditor.getElement(),b=e.getActiveCell();e.activeEditor.activeCell=b,e.isEditing=!0,a.show(),a.css(b.cellPosition()),a.css({width:b.outerWidth()+1,height:b.outerHeight()+1});var c=b.data("column"),d=b.text();e.activeEditor.setValue(d);var f={};return f[c]=d,e.events.trigger("editor:load",f,b),a},e.keydown=function(a){var b=!0,c=[8,9,13,27,37,38,39,40],d=[8,37,38,39,40];if(!(0===e.getActiveCell().length&&!e.isEditing||!_.contains(c,a.which)||e.isEditing&&_.contains(d,a.which))){switch(a.preventDefault(),a.which){case 37:e.move("left");break;case 38:e.move("up");break;case 39:e.move("right");break;case 40:e.move("down");break;case 13:e.isEditing?a.ctrlKey&&a.shiftKey?e.move("up"):a.ctrlKey&&!a.shiftKey?e.move("down"):e.exitEditor():e.editCell();break;case 27:e.isEditing?e.exitEditor(!0):e.$el.blur();break;case 9:e.move(a.shiftKey?"left":"right");break;case 8:e.clearActiveCell()}b&&a.preventDefault()}},e.clickCell=function(b){b.preventDefault(),console.log("clicked cell"),e.isEditing&&e.exitEditor(),a(this).setActiveCell()},e.dblClickCell=function(b){b.preventDefault(),console.log("double clicked cell"),a(this).setActiveCell(),e.editCell()},e.renderColumns=function(){console.log("renderColumns");var b=a("thead",e.$el),c=document.createElement("tr");_.each(e.columns,function(b){var d=document.createElement("th"),e=document.createElement("div");a(e).text(b.name),d.appendChild(e),a(d).data("type",b.type||"string"),a(d).data("editor",b.editor||"BasicEditor"),c.appendChild(d)}),b.append(c)},e.renderData=function(){console.log("renderData");var b=a("tbody",e.$el);_.each(e.data,function(c){var d=document.createElement("tr");_.each(e.columns,function(b){var e=document.createElement("td"),f=document.createElement("div");_.has(c,b.name)&&a(f).text(c[b.name]),a(e).data("column",b.name),a(e).data("type",b.type||"string"),a(e).data("editor",b.editor||"BasicEditor"),e.appendChild(f),d.appendChild(e)}),b.append(d)})},e.renderBaseTable=function(){console.log("renderBaseTable");var a=document.createElement("table"),b=document.createElement("thead"),c=document.createElement("tbody");a.appendChild(b),a.appendChild(c),a.className=e.config.tableClass,e.$el.html(a),e.$el.attr("tabindex",-1)},e.init=function(b,c,d){return console.log("sensei grid init"),e.config=a.extend({},f,d),e.data=b,e.columns=c,e.$el=a(this),e.editors={},e},e.init(b,c,d)}}(jQuery),function(a){var b=this,c=function(a){this.grid=a};c.extend=function(a){var b,c=this;b=function(){return c.apply(this,arguments)};var d=function(){this.constructor=b};return d.prototype=c.prototype,b.prototype=new d,a&&_.extend(b.prototype,a),b.__super__=c.prototype,b},c.prototype.getElement=function(){return a(this.editor)},c.prototype.initialize=function(){},c.prototype.render=function(){},c.prototype.show=function(){this.getElement().show()},c.prototype.hide=function(){console.log("Editor hide"),this.getElement().hide(),this.grid.activeEditor.activeCell=null,this.grid.activeEditor=null},c.prototype.getValue=function(){console.warn("Editor.getValue not implemented")},c.prototype.setValue=function(){console.warn("Editor.setValue not implemented")},b.Editor=c,b.BasicEditor=c.extend({types:[],name:"BasicEditor",render:function(){if(console.log("BasicEditor.render"),!this.editor){this.editor=document.createElement("div"),this.editor.className="sensei-grid-editor sensei-grid-basic-editor";var a=document.createElement("input");this.editor.appendChild(a),this.grid.$el.append(this.editor)}},getValue:function(){return a("input",this.editor).val()},setValue:function(b){a("input",this.editor).val(b).focus()}})}(jQuery); \ No newline at end of file diff --git a/package.json b/package.json index 944fe67..348ad57 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sensei-grid", - "version": "0.1.0", + "version": "0.1.2", "description": "Simple data grid in JavaScript/HTML.", "main": "index.js", "directories": {