edit cell wip

This commit is contained in:
Faris Ansari 2017-10-01 00:22:59 +05:30
parent 150db0fef7
commit f2177f96ed
5 changed files with 216 additions and 82 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -3,6 +3,7 @@ import {
getBodyHTML, getBodyHTML,
getRowHTML, getRowHTML,
getColumnHTML, getColumnHTML,
getEditCellHTML,
prepareRowHeader, prepareRowHeader,
prepareRows, prepareRows,
getDefault, getDefault,
@ -256,46 +257,57 @@ export default class ReGrid {
this.setBodyWidth(); this.setBodyWidth();
this.setStyle( this.setStyle('.data-table .body-scrollable', {
'.data-table .body-scrollable', 'margin-top': (this.header.height() + 1) + 'px'
`margin-top: ${this.header.height() + 1}px;` });
);
// hide edit cells by default
this.setStyle('.data-table .body-scrollable .edit-cell', {
display: 'none'
});
this.bodyScrollable.find('.table').css('margin', 0); this.bodyScrollable.find('.table').css('margin', 0);
} }
bindEditCell() { bindEditCell() {
const { events } = this; const self = this;
const $editPopup = this.wrapper.find('.edit-popup'); const $editPopup = this.wrapper.find('.edit-popup');
$editPopup.hide(); $editPopup.hide();
if (!events.on_cell_doubleclick) return; // if (!self.events.onCellEdit) return;
this.bodyScrollable.on('dblclick', '.data-table-col', function () { this.bodyScrollable.on('dblclick', '.data-table-col', function () {
const $cell = $(this); const $cell = $(this);
const rowIndex = $cell.attr('data-row-index'); const rowIndex = $cell.attr('data-row-index');
const colIndex = $cell.attr('data-col-index'); const colIndex = $cell.attr('data-col-index');
const $editCell = $cell.find('.edit-cell');
$editPopup.empty(); const cell = self.getCell(rowIndex, colIndex);
const { top, left } = $cell.position();
$editPopup.css({ $editCell.find('input').val(cell.content);
top: top - 12, // $editPopup.html(getEditCellHTML(cellValue.content));
left: left - 12
}); const width = $cell.find('.content').width() + 1;
const height = $cell.find('.content').height() + 1;
const selector = `.data-table .body-scrollable [data-row-index="${rowIndex}"][data-row-index="${rowIndex}"] .edit-cell`;
self.setStyle(selector, { width, height });
$editCell.show();
$editCell.find('input').select();
// showing the popup is the responsibility of event handler // showing the popup is the responsibility of event handler
events.on_cell_doubleclick( // self.events.onCellEdit(
$cell.get(0), // $cell.get(0),
$editPopup, // $editPopup,
rowIndex, // rowIndex,
colIndex // colIndex
); // );
}); });
$(document.body).on('click', e => { $(document.body).on('click', e => {
if ($(e.target).is('.edit-popup, .edit-popup *')) return; if ($(e.target).is('.edit-cell, .edit-cell *')) return;
$editPopup.hide(); self.bodyScrollable.find('.edit-cell').hide();
}); });
} }
@ -404,17 +416,15 @@ export default class ReGrid {
} }
setColumnWidth(colIndex, width) { setColumnWidth(colIndex, width) {
this.setStyle( this.setStyle(`[data-col-index="${colIndex}"] .content`, {
`[data-col-index="${colIndex}"] .content`, width: width + 'px'
`width: ${width}px;` });
);
} }
setRowHeight(rowIndex, height) { setRowHeight(rowIndex, height) {
self.setStyle( this.setStyle(`[data-row-index="${rowIndex}"] .content`, {
`[data-row-index="${rowIndex}"] .content`, height: height + 'px'
`width: ${height}px;` });
);
} }
setColumnHeaderWidth(colIndex, width) { setColumnHeaderWidth(colIndex, width) {
@ -463,38 +473,60 @@ export default class ReGrid {
); );
} }
setStyle(rule, style) { setStyle(rule, styleMap) {
this.getStyleEl(); this.getStyleEl();
const self = this;
let styles = this.$style.text(); let styles = this.$style.text();
const rulePatternStr = `${escapeRegExp(rule)} {([^}]*)}`; const rulePatternStr = `${escapeRegExp(rule)} {([^}]*)}`;
const rulePattern = new RegExp(rulePatternStr, 'g'); const rulePattern = new RegExp(rulePatternStr, 'g');
if (styles.match(rulePattern)) { if (styles.match(rulePattern)) {
// rules exists, append/replace properties // rules exists, append/replace properties
const property = style.split(':')[0];
const propPattern = new RegExp(`${escapeRegExp(property)}[^;]*;`);
styles = styles.replace(rulePattern, function (match, propertyStr) { for (const property in styleMap) {
if (propertyStr.match(propPattern)) { const value = styleMap[property];
// property exists, replace with empty string
propertyStr = propertyStr.replace(propPattern, '');
}
propertyStr = propertyStr.trim();
const replacer = const propPattern = new RegExp(`${escapeRegExp(property)}[^;]*;`);
`${rule} {${propertyStr}${style}}`;
styles = styles.replace(rulePattern, function (match, propertyStr) {
if (propertyStr.match(propPattern)) {
// property exists, replace with empty string
propertyStr = propertyStr.replace(propPattern, '');
}
propertyStr = propertyStr.trim();
const _styleMap = {};
_styleMap[property] = value;
const replacer =
`${rule} {${propertyStr}${self.getCSSString(_styleMap)}}`;
return replacer;
});
}
return replacer;
});
} else { } else {
// rules doesn't exists, add rule with properties // rules doesn't exists, add rule with properties
styles += `${rule} {${style}}`; styles += `${rule} {${styleMap}}`;
} }
this.$style.html(styles); this.$style.html(styles);
} }
getCSSString(styleMap) {
let style = '';
for (const prop in styleMap) {
if (styleMap.hasOwnProperty(prop)) {
style += `${prop}: ${styleMap[prop]};`;
}
}
console.log(style);
return style;
}
getStyleEl() { getStyleEl() {
if (!this.$style) { if (!this.$style) {
this.$style = $('<style data-id="regrid"></style>') this.$style = $('<style data-id="regrid"></style>')
@ -514,6 +546,12 @@ export default class ReGrid {
return this.data.rows.find(row => row[0].rowIndex === rowIndex); return this.data.rows.find(row => row[0].rowIndex === rowIndex);
} }
getCell(rowIndex, colIndex) {
rowIndex = +rowIndex;
colIndex = +colIndex;
return this.data.rows[rowIndex][colIndex];
}
getColumnHeaderElement(colIndex) { getColumnHeaderElement(colIndex) {
colIndex = +colIndex; colIndex = +colIndex;
if (colIndex < 0) return null; if (colIndex < 0) return null;

View File

@ -63,12 +63,37 @@
.edit-popup { .edit-popup {
position: absolute; position: absolute;
border: 1px solid gray;
background: white; background: white;
border-radius: 4px;
box-shadow: 0 6px 12px rgba(0, 0, 0, .175); box-shadow: 0 6px 12px rgba(0, 0, 0, .175);
padding: 12px; input {
outline: none;
padding: 8px;
font-size: inherit;
font-family: inherit;
width: inherit;
height: inherit;
border: 2px solid theme-color('primary');
}
}
.edit-cell {
position: absolute;
top: -1px;
left: -1px;
background: white;
box-shadow: 0 6px 12px rgba(0, 0, 0, .175);
z-index: 1;
input {
outline: none;
padding: 8px;
font-size: inherit;
font-family: inherit;
width: inherit;
height: inherit;
border: 2px solid theme-color('primary');
}
} }
.noselect { .noselect {

View File

@ -17,6 +17,14 @@ function makeDataAttributeString(props) {
.trim(); .trim();
} }
function getEditCellHTML() {
return `
<div class="edit-cell">
<input type="text" />
</div>
`;
}
function getColumnHTML(column) { function getColumnHTML(column) {
const { rowIndex, colIndex, isHeader } = column; const { rowIndex, colIndex, isHeader } = column;
const dataAttr = makeDataAttributeString({ const dataAttr = makeDataAttributeString({
@ -25,12 +33,15 @@ function getColumnHTML(column) {
isHeader isHeader
}); });
const editCellHTML = isHeader ? '' : getEditCellHTML();
return ` return `
<td class="data-table-col noselect" ${dataAttr}> <td class="data-table-col noselect" ${dataAttr}>
<div class="content ellipsis"> <div class="content ellipsis">
${column.format ? column.format(column.content) : column.content} ${column.format ? column.format(column.content) : column.content}
<span class="sort-indicator"></span> <span class="sort-indicator"></span>
</div> </div>
${editCellHTML}
</td> </td>
`; `;
} }
@ -121,6 +132,7 @@ export default {
getBodyHTML, getBodyHTML,
getRowHTML, getRowHTML,
getColumnHTML, getColumnHTML,
getEditCellHTML,
prepareRowHeader, prepareRowHeader,
prepareRows, prepareRows,
makeDataAttributeString, makeDataAttributeString,