Skip to content

Commit

Permalink
Merge branch 'feature/mergedCustomHeaderRendererCellFunctionsGetters'…
Browse files Browse the repository at this point in the history
… into develop
  • Loading branch information
warpech committed Nov 18, 2013
2 parents 4107d88 + ea94ac2 commit 3151685
Show file tree
Hide file tree
Showing 16 changed files with 850 additions and 174 deletions.
492 changes: 492 additions & 0 deletions demo/js/jquery.ui.position.js

Large diffs are not rendered by default.

168 changes: 102 additions & 66 deletions demo/renderers_html.html
Expand Up @@ -12,6 +12,7 @@
<link data-jsfiddle="common" rel="stylesheet" media="screen" href="../dist/jquery.handsontable.full.css">
<!-- the below is only needed for DateCell (uses jQuery UI Datepicker) -->
<script data-jsfiddle="common" src="../lib/jquery-ui/js/jquery-ui.custom.min.js"></script>
<script data-jsfiddle="common" src="js/jquery.ui.position.js"></script>
<link data-jsfiddle="common" rel="stylesheet" media="screen"
href="../lib/jquery-ui/css/ui-bootstrap/jquery-ui.custom.css">

Expand Down Expand Up @@ -274,12 +275,13 @@ <h2>Changing cell type from a dropdown menu in cell header</h2>
.changeType {
border: 1px solid #bbb;
color: #bbb;
background: #eee;
border-radius: 2px;
padding: 2px;
font-size: 9px;

float: right;
line-height: 12px;
line-height: 9px;
margin: 3px 3px 0 0;
}

Expand All @@ -292,95 +294,99 @@ <h2>Changing cell type from a dropdown menu in cell header</h2>
.changeType.pressed {
background-color: #999;
}
</style>

<script data-jsfiddle="example3">
//TODO fix the colHeader menu demo. I commented it out because it caused exceptions since 0.10.0-beta3
/*
var HOT
, changeCol
, columns = [
{type: 'numeric'},
{type: 'numeric'},
{type: 'numeric'},
{type: 'numeric'},
{type: 'numeric'},
{type: 'numeric'}
];
function contextMenuItem(name, value) {
return {name: name, type: "radio", radio: "typeRadio", value: value, events: {
change: function () {
$(this).trigger('contextmenu:hide');
}
}};
.changeTypeMenu {
position: absolute;
border: 1px solid #ccc;
margin-top: 18px;
box-shadow: 0 1px 3px -1px #323232;
background: white;
padding: 0;
display: none;
z-index: 10;
}

$.contextMenu({
selector: '.changeType',
items: {
"text": contextMenuItem("Text", "text"),
"numeric": contextMenuItem("Numeric", "numeric"),
"date": contextMenuItem("Date", "date")
},
events: {
show: function (opt) {
$.contextMenu.setInputValues(opt, {typeRadio: getColumnType(changeCol)});
},
hide: function (opt) {
var values = $.contextMenu.getInputValues(opt);
setColumnType(changeCol, values.typeRadio);
}
}
});
.changeTypeMenu li{
text-align: left;
list-style: none;
padding: 2px 20px;
cursor: pointer;
}

function getColumnType(i) {
return columns[i].type;
.changeTypeMenu li.active:before {
content: "\2714";
margin-left: -15px;
margin-right: 3px;
}

function setColumnType(i, type) {
columns[i].type = type;
HOT.updateSettings({columns: columns});
HOT.render();
.changeTypeMenu li:hover {
background: #eee;
}


</style>

<script data-jsfiddle="example3">
var data = [
["", "Maserati", "Mazda", "Mercedes", "Mini", "Mitsubishi"],
["2009", 0, 2941, 4303, 354, 5814],
["2010", "fff", 2905, 2867, 412, 5284],
["2010", 3, 2905, 2867, 412, 5284],
["2011", 4, 2517, 4822, 552, 6127],
["2012", 2, 2422, 5399, 776, 4151]
];

var columns = [
{type: 'numeric'},
{type: 'numeric'},
{type: 'numeric'},
{type: 'numeric'},
{type: 'numeric'},
{type: 'numeric'}
];


$('#example3').handsontable({
data: data,
minRows: 5,
minCols: 6,
minSpareRows: 1,
autoWrapRow: true,
colHeaders: true,
minSpareRows: 1,
type: 'numeric',
columns: columns,
contextMenu: true,
allowInvalid: true,
afterGetColHeader: function (col, TH) {
var BUTTON = document.createElement('DIV');
BUTTON.className = "changeType";
BUTTON.appendChild(document.createTextNode('\u25BC')); //adds BLACK DOWN-POINTING TRIANGLE icon
var instance = this;
var menu = buildMenu(columns[col].type);

var $button = $(BUTTON);
$button.click(function () {
changeCol = col;
var $button = buildButton();
$button.click(function (e) {

var pos = $button.offset();
$button.contextMenu({x: pos.left, y: pos.top + $button.outerHeight() });
BUTTON.className = "changeType pressed";
e.preventDefault();
e.stopImmediatePropagation();

$(document.body).one("contextmenu:hide", function () {
BUTTON.className = "changeType";
$('.changeTypeMenu').hide();

menu.show();

menu.position({
my: 'left top',
at: 'left bottom',
of: $button,
within: instance.rootElement
});

$(document).off('click.changeTypeMenu.hide');

$(document).one('click.changeTypeMenu.hide', function () {
menu.hide();
});


});

TH.firstChild.appendChild(BUTTON);
menu.on('click', 'li', function () {
setColumnType(col, $(this).data('colType'), instance);
});

TH.firstChild.appendChild($button[0]);
TH.appendChild(menu[0]);
},
cells: function (row, col, prop) {
if (row === 0) {
Expand All @@ -392,8 +398,38 @@ <h2>Changing cell type from a dropdown menu in cell header</h2>
}
});

HOT = $('#example3').handsontable('getInstance');
*/
function buildMenu(activeCellType){
var menu = $('<ul></ul>').addClass('changeTypeMenu');

$.each(['text', 'numeric', 'date'], function (i, type) {
var item = $('<li></li>').data('colType', type).text(type);

if(activeCellType == type){
item.addClass('active');
}

menu.append(item);

});


return menu;

}

function buildButton() {
return $('<button></button>').addClass('changeType').html('\u25BC');
}

function setColumnType(i, type, instance) {
columns[i].type = type;
instance.updateSettings({columns: columns});
instance.validateCells(function(){
instance.render();
});
}


</script>
</div>
</div>
Expand Down
43 changes: 29 additions & 14 deletions src/core.js
Expand Up @@ -14,12 +14,9 @@ Handsontable.Core = function (rootElement, userSettings) {
, editorManager
, autofill
, instance = this
, GridSettings = function () {
};
, GridSettings = function () {};

Handsontable.helper.inherit(GridSettings, DefaultSettings); //create grid settings as a copy of default settings
Handsontable.helper.extend(GridSettings.prototype, Handsontable.TextCell); //overwrite defaults with default cell
expandType(userSettings);
Handsontable.helper.extend(GridSettings.prototype, userSettings); //overwrite defaults with user settings

this.rootElement = rootElement;
Expand Down Expand Up @@ -1317,17 +1314,18 @@ Handsontable.Core = function (rootElement, userSettings) {
changes.splice(i, 1);
}
else {
var row = changes[i][0];
var col = datamap.propToCol(changes[i][1]);
var logicalCol = instance.runHooksAndReturn('modifyCol', col); //column order may have changes, so we need to translate physical col index (stored in datasource) to logical (displayed to user)
var cellProperties = instance.getCellMeta(changes[i][0], logicalCol);
var cellProperties = instance.getCellMeta(row, logicalCol);

if (cellProperties.dataType === 'number' && typeof changes[i][3] === 'string') {
if (cellProperties.type === 'numeric' && typeof changes[i][3] === 'string') {
if (changes[i][3].length > 0 && /^-?[\d\s]*\.?\d*$/.test(changes[i][3])) {
changes[i][3] = numeral().unformat(changes[i][3] || '0'); //numeral cannot unformat empty string
}
}

if (cellProperties.validator) {
if (instance.getCellValidator(cellProperties)) {
waitingForValidator.addValidatorToQueue();
instance.validateCell(changes[i][3], cellProperties, (function (i, cellProperties) {
return function (result) {
Expand Down Expand Up @@ -1407,7 +1405,7 @@ Handsontable.Core = function (rootElement, userSettings) {
}

this.validateCell = function (value, cellProperties, callback, source) {
var validator = cellProperties.validator;
var validator = instance.getCellValidator(cellProperties);

if (Object.prototype.toString.call(validator) === '[object RegExp]') {
validator = (function (validator) {
Expand Down Expand Up @@ -1826,7 +1824,7 @@ Handsontable.Core = function (rootElement, userSettings) {
// Use settings provided by user
if (GridSettings.prototype.columns) {
column = GridSettings.prototype.columns[i];
expandType(column);
// expandType(column);
Handsontable.helper.extend(proto, column);
}
}
Expand Down Expand Up @@ -2051,13 +2049,13 @@ Handsontable.Core = function (rootElement, userSettings) {
cellProperties.instance = instance;

instance.PluginHooks.run('beforeGetCellMeta', row, col, cellProperties);
expandType(cellProperties); //for `type` added in beforeGetCellMeta
// expandType(cellProperties); //for `type` added in beforeGetCellMeta

if (cellProperties.cells) {
var settings = cellProperties.cells.call(cellProperties, row, col, prop);

if (settings) {
expandType(settings); //for `type` added in cells
// expandType(settings); //for `type` added in cells
Handsontable.helper.extend(cellProperties, settings);
}
}
Expand Down Expand Up @@ -2091,6 +2089,22 @@ Handsontable.Core = function (rootElement, userSettings) {
}
};

this.getCellRenderer = function (row, col) {
var renderer = Handsontable.helper.cellMethodLookupFactory('renderer').call(this, row, col);

if(typeof renderer == 'string'){
renderer = Handsontable.cellLookup.renderer[renderer];
}

return renderer

};

this.getCellEditor = Handsontable.helper.cellMethodLookupFactory('editor');

this.getCellValidator = Handsontable.helper.cellMethodLookupFactory('validator');


/**
* Validates all cells using their validator functions and calls callback when finished. Does not render the view
* @param callback
Expand Down Expand Up @@ -2559,8 +2573,8 @@ Handsontable.Core = function (rootElement, userSettings) {
this.version = '@@version'; //inserted by grunt from package.json
};

var DefaultSettings = function () {
};
var DefaultSettings = function () {};

DefaultSettings.prototype = {
data: void 0,
width: void 0,
Expand Down Expand Up @@ -2596,7 +2610,8 @@ DefaultSettings.prototype = {
invalidCellClassName: 'htInvalid',
fragmentSelection: false,
readOnly: false,
nativeScrollbars: false
nativeScrollbars: false,
type: 'text'
};
Handsontable.DefaultSettings = DefaultSettings;

Expand Down
2 changes: 1 addition & 1 deletion src/editorManager.js
Expand Up @@ -358,7 +358,7 @@
var originalValue = instance.getDataAtCell(row, col);
var cellProperties = instance.getCellMeta(row, col);

var editorClass = cellProperties.editor;
var editorClass = instance.getCellEditor(cellProperties);
activeEditor = Handsontable.editors.getEditor(editorClass, instance);

activeEditor.prepare(row, col, prop, td, originalValue, cellProperties);
Expand Down
18 changes: 13 additions & 5 deletions src/editors/baseEditor.js
Expand Up @@ -23,9 +23,7 @@
this._closeCallback(result);
}

BaseEditor.prototype.init = function(){
throw Error('Editor init() method unimplemented');
};
BaseEditor.prototype.init = function(){};

BaseEditor.prototype.val = function(newValue){
throw Error('Editor val() method unimplemented');
Expand Down Expand Up @@ -56,7 +54,17 @@
baseClass.apply(this, arguments);
}

return Handsontable.helper.inherit(Editor, baseClass);
function inherit(Child, Parent){
function Bridge() {
}

Bridge.prototype = Parent.prototype;
Child.prototype = new Bridge();
Child.prototype.constructor = Child;
return Child;
}

return inherit(Editor, baseClass);
};

BaseEditor.prototype.saveValue = function (val, ctrlDown) {
Expand Down Expand Up @@ -135,7 +143,7 @@

this.saveValue(val, ctrlDown);

if(this.cellProperties.validator){
if(this.instance.getCellValidator(this.cellProperties)){
var that = this;
this.instance.addHookOnce('afterValidate', function (result) {
that.state = Handsontable.EditorState.FINISHED;
Expand Down

0 comments on commit 3151685

Please sign in to comment.