diff --git a/dist/sensei-grid.js b/dist/sensei-grid.js index b96f768..9228517 100644 --- a/dist/sensei-grid.js +++ b/dist/sensei-grid.js @@ -142,14 +142,38 @@ } }; + // core parsers for cell values + plugin.parsers = {}; + plugin.parsers["string"] = function (val) { + return val.toString(); + }; + plugin.parsers["int"] = function (val) { + return parseInt(val); + }; + plugin.parsers["float"] = function (val) { + return parseFloat(val); + }; + plugin.getCellData = function ($cell) { - return $cell.text(); + var value = $cell.text(); + var type = plugin.getCellType($cell); + + // parse value according to defined cell type + if (_.has(plugin.parsers, type)) { + value = plugin.parsers[type](value); + } + + return value; }; plugin.getCellColumn = function ($cell) { return $cell.data("column"); }; + plugin.getCellType = function ($cell) { + return $cell.data("type"); + }; + plugin.getCellDataByIndex = function (row, cell) { var $row = plugin.getRowByIndex(row); var $cell = plugin.getCellFromRowByIndex($row, cell); @@ -165,7 +189,7 @@ plugin.getCellFromRowByIndex = function ($row, index) { var $cell = $row.find("td").eq(index); if ($cell.length === 0) { - throw new Error("Cell does not exist: ", $cell); + throw new Error("Cell does not exist"); } return $cell; @@ -177,7 +201,7 @@ return $(this).data("column") === key; }); if ($cell.length === 0) { - throw new Error("Cell does not exist: ", $cell); + throw new Error("Cell does not exist"); } return $cell; @@ -194,7 +218,7 @@ plugin.getRowByIndex = function (index) { var $row = plugin.$el.find("tbody>tr").eq(index); if ($row.length === 0) { - throw new Error("Row does not exist: ", $row); + throw new Error("Row does not exist"); } return $row; diff --git a/dist/sensei-grid.min.js b/dist/sensei-grid.min.js index 6208de1..d82730a 100644 --- a/dist/sensei-grid.min.js +++ b/dist/sensei-grid.min.js @@ -3,4 +3,4 @@ * 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.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.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.getCellData=function(a){return a.text()},e.getCellColumn=function(a){return a.data("column")},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: ",c);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: ",d);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: ",b);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.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.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