{I" class:ETI"BundledAsset;FI"logical_path;TI"mercury/table_editor.js;FI" pathname;TI"Œ/home/edik/.rvm/gems/ruby-2.0.0-p247@very_simple_cms/bundler/gems/mercury-3e0245e33195/app/assets/javascripts/mercury/table_editor.js.coffee;FI"content_type;TI"application/javascript;TI" mtime;Tl+½¡9RI" length;Ti¨4I" digest;TI"%6d5431ad8bc2dccb4e8cc7633e2fa2d5;FI" source;TI"¨4(function() { this.Mercury.tableEditor = function(table, cell, cellContent) { Mercury.tableEditor.load(table, cell, cellContent); return Mercury.tableEditor; }; jQuery.extend(Mercury.tableEditor, { load: function(table, cell, cellContent) { this.table = table; this.cell = cell; this.cellContent = cellContent != null ? cellContent : ''; this.row = this.cell.parent('tr'); this.columnCount = this.getColumnCount(); return this.rowCount = this.getRowCount(); }, addColumnBefore: function() { return this.addColumn('before'); }, addColumnAfter: function() { return this.addColumn('after'); }, addColumn: function(position) { var i, intersecting, matchOptions, matching, newCell, row, rowSpan, sig, _i, _len, _ref, _results; if (position == null) { position = 'after'; } sig = this.cellSignatureFor(this.cell); _ref = this.table.find('tr'); _results = []; for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) { row = _ref[i]; rowSpan = 1; matchOptions = position === 'after' ? { right: sig.right } : { left: sig.left }; if (matching = this.findCellByOptionsFor(row, matchOptions)) { newCell = jQuery("<" + (matching.cell.get(0).tagName) + ">").html(this.cellContent); this.setRowspanFor(newCell, matching.height); if (position === 'before') { matching.cell.before(newCell); } else { matching.cell.after(newCell); } _results.push(i += matching.height - 1); } else if (intersecting = this.findCellByIntersectionFor(row, sig)) { _results.push(this.setColspanFor(intersecting.cell, intersecting.width + 1)); } else { _results.push(void 0); } } return _results; }, removeColumn: function() { var adjusting, cell, i, intersecting, matching, removing, row, sig, _i, _j, _k, _len, _len1, _len2, _ref, _results; sig = this.cellSignatureFor(this.cell); if (sig.width > 1) { return; } removing = []; adjusting = []; _ref = this.table.find('tr'); for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) { row = _ref[i]; if (matching = this.findCellByOptionsFor(row, { left: sig.left, width: sig.width })) { removing.push(matching.cell); i += matching.height - 1; } else if (intersecting = this.findCellByIntersectionFor(row, sig)) { adjusting.push(intersecting.cell); } } for (_j = 0, _len1 = removing.length; _j < _len1; _j++) { cell = removing[_j]; jQuery(cell).remove(); } _results = []; for (_k = 0, _len2 = adjusting.length; _k < _len2; _k++) { cell = adjusting[_k]; _results.push(this.setColspanFor(cell, this.colspanFor(cell) - 1)); } return _results; }, addRowBefore: function() { return this.addRow('before'); }, addRowAfter: function() { return this.addRow('after'); }, addRow: function(position) { var cell, cellCount, colspan, newCell, newRow, previousRow, rowCount, rowspan, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2; if (position == null) { position = 'after'; } newRow = jQuery(''); if ((rowspan = this.rowspanFor(this.cell)) > 1 && position === 'after') { this.row = jQuery(this.row.nextAll('tr')[rowspan - 2]); } cellCount = 0; _ref = this.row.find('th, td'); for (_i = 0, _len = _ref.length; _i < _len; _i++) { cell = _ref[_i]; colspan = this.colspanFor(cell); newCell = jQuery("<" + cell.tagName + ">").html(this.cellContent); this.setColspanFor(newCell, colspan); cellCount += colspan; if ((rowspan = this.rowspanFor(cell)) > 1 && position === 'after') { this.setRowspanFor(cell, rowspan + 1); continue; } newRow.append(newCell); } if (cellCount < this.columnCount) { rowCount = 0; _ref1 = this.row.prevAll('tr'); for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { previousRow = _ref1[_j]; rowCount += 1; _ref2 = jQuery(previousRow).find('td[rowspan], th[rowspan]'); for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) { cell = _ref2[_k]; rowspan = this.rowspanFor(cell); if (rowspan - 1 >= rowCount && position === 'before') { this.setRowspanFor(cell, rowspan + 1); } else if (rowspan - 1 >= rowCount && position === 'after') { if (rowspan - 1 === rowCount) { newCell = jQuery("<" + cell.tagName + ">").html(this.cellContent); this.setColspanFor(newCell, this.colspanFor(cell)); newRow.append(newCell); } else { this.setRowspanFor(cell, rowspan + 1); } } } } } if (position === 'before') { return this.row.before(newRow); } else { return this.row.after(newRow); } }, removeRow: function() { var aboveRow, cell, i, match, minRowspan, prevRowspan, rowsAbove, rowspan, rowspansMatch, sig, _i, _j, _k, _l, _len, _len1, _len2, _len3, _m, _ref, _ref1, _ref2, _ref3, _ref4; rowspansMatch = true; prevRowspan = 0; minRowspan = 0; _ref = this.row.find('td, th'); for (_i = 0, _len = _ref.length; _i < _len; _i++) { cell = _ref[_i]; rowspan = this.rowspanFor(cell); if (prevRowspan && rowspan !== prevRowspan) { rowspansMatch = false; } if (rowspan < minRowspan || !minRowspan) { minRowspan = rowspan; } prevRowspan = rowspan; } if (!rowspansMatch && this.rowspanFor(this.cell) > minRowspan) { return; } if (minRowspan > 1) { for (i = _j = 0, _ref1 = minRowspan - 2; 0 <= _ref1 ? _j <= _ref1 : _j >= _ref1; i = 0 <= _ref1 ? ++_j : --_j) { jQuery(this.row.nextAll('tr')[i]).remove(); } } _ref2 = this.row.find('td[rowspan], th[rowspan]'); for (_k = 0, _len1 = _ref2.length; _k < _len1; _k++) { cell = _ref2[_k]; sig = this.cellSignatureFor(cell); if (sig.height === minRowspan) { continue; } if (match = this.findCellByOptionsFor(this.row.nextAll('tr')[minRowspan - 1], { left: sig.left, forceAdjacent: true })) { this.setRowspanFor(cell, this.rowspanFor(cell) - this.rowspanFor(this.cell)); if (match.direction === 'before') { match.cell.before(jQuery(cell).clone()); } else { match.cell.after(jQuery(cell).clone()); } } } if (this.columnsFor(this.row.find('td, th')) < this.columnCount) { rowsAbove = 0; _ref3 = this.row.prevAll('tr'); for (_l = 0, _len2 = _ref3.length; _l < _len2; _l++) { aboveRow = _ref3[_l]; rowsAbove += 1; _ref4 = jQuery(aboveRow).find('td[rowspan], th[rowspan]'); for (_m = 0, _len3 = _ref4.length; _m < _len3; _m++) { cell = _ref4[_m]; rowspan = this.rowspanFor(cell); if (rowspan > rowsAbove) { this.setRowspanFor(cell, rowspan - this.rowspanFor(this.cell)); } } } } return this.row.remove(); }, increaseColspan: function() { var cell; cell = this.cell.next('td, th'); if (!cell.length) { return; } if (this.rowspanFor(cell) !== this.rowspanFor(this.cell)) { return; } if (this.cellIndexFor(cell) > this.cellIndexFor(this.cell) + this.colspanFor(this.cell)) { return; } this.setColspanFor(this.cell, this.colspanFor(this.cell) + this.colspanFor(cell)); return cell.remove(); }, decreaseColspan: function() { var newCell; if (this.colspanFor(this.cell) === 1) { return; } this.setColspanFor(this.cell, this.colspanFor(this.cell) - 1); newCell = jQuery("<" + (this.cell.get(0).tagName) + ">").html(this.cellContent); this.setRowspanFor(newCell, this.rowspanFor(this.cell)); return this.cell.after(newCell); }, increaseRowspan: function() { var match, nextRow, sig; sig = this.cellSignatureFor(this.cell); nextRow = this.row.nextAll('tr')[sig.height - 1]; if (nextRow && (match = this.findCellByOptionsFor(nextRow, { left: sig.left, width: sig.width }))) { this.setRowspanFor(this.cell, sig.height + match.height); return match.cell.remove(); } }, decreaseRowspan: function() { var match, newCell, nextRow, sig; sig = this.cellSignatureFor(this.cell); if (sig.height === 1) { return; } nextRow = this.row.nextAll('tr')[sig.height - 2]; if (match = this.findCellByOptionsFor(nextRow, { left: sig.left, forceAdjacent: true })) { newCell = jQuery("<" + (this.cell.get(0).tagName) + ">").html(this.cellContent); this.setColspanFor(newCell, this.colspanFor(this.cell)); this.setRowspanFor(this.cell, sig.height - 1); if (match.direction === 'before') { return match.cell.before(newCell); } else { return match.cell.after(newCell); } } }, getColumnCount: function() { return this.columnsFor(this.table.find('thead tr:first-child, tbody tr:first-child, tfoot tr:first-child').first().find('td, th')); }, getRowCount: function() { return this.table.find('tr').length; }, cellIndexFor: function(cell) { var aboveCell, aboveRow, columns, index, row, rowsAbove, _i, _j, _len, _len1, _ref, _ref1; cell = jQuery(cell); row = cell.parent('tr'); columns = this.columnsFor(row.find('td, th')); index = this.columnsFor(cell.prevAll('td, th')); if (columns < this.columnCount) { rowsAbove = 0; _ref = row.prevAll('tr'); for (_i = 0, _len = _ref.length; _i < _len; _i++) { aboveRow = _ref[_i]; rowsAbove += 1; _ref1 = jQuery(aboveRow).find('td[rowspan], th[rowspan]'); for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { aboveCell = _ref1[_j]; if (this.rowspanFor(aboveCell) > rowsAbove && this.cellIndexFor(aboveCell) <= index) { index += this.colspanFor(aboveCell); } } } } return index; }, cellSignatureFor: function(cell) { var sig; sig = { cell: jQuery(cell) }; sig.left = this.cellIndexFor(cell); sig.width = this.colspanFor(cell); sig.height = this.rowspanFor(cell); sig.right = sig.left + sig.width; return sig; }, findCellByOptionsFor: function(row, options) { var cell, prev, sig, _i, _len, _ref; _ref = jQuery(row).find('td, th'); for (_i = 0, _len = _ref.length; _i < _len; _i++) { cell = _ref[_i]; sig = this.cellSignatureFor(cell); if (typeof options.right !== 'undefined') { if (sig.right === options.right) { return sig; } } if (typeof options.left !== 'undefined') { if (options.width) { if (sig.left === options.left && sig.width === options.width) { return sig; } } else if (!options.forceAdjacent) { if (sig.left === options.left) { return sig; } } else if (options.forceAdjacent) { if (sig.left > options.left) { prev = jQuery(cell).prev('td, th'); if (prev.length) { sig = this.cellSignatureFor(prev); sig.direction = 'after'; } else { sig.direction = 'before'; } return sig; } } } } if (options.forceAdjacent) { sig.direction = 'after'; return sig; } return null; }, findCellByIntersectionFor: function(row, signature) { var cell, sig, _i, _len, _ref; _ref = jQuery(row).find('td, th'); for (_i = 0, _len = _ref.length; _i < _len; _i++) { cell = _ref[_i]; sig = this.cellSignatureFor(cell); if (sig.right - signature.left >= 0 && sig.right > signature.left) { return sig; } } return null; }, columnsFor: function(cells) { var cell, count, _i, _len; count = 0; for (_i = 0, _len = cells.length; _i < _len; _i++) { cell = cells[_i]; count += this.colspanFor(cell); } return count; }, colspanFor: function(cell) { return parseInt(jQuery(cell).attr('colspan')) || 1; }, rowspanFor: function(cell) { return parseInt(jQuery(cell).attr('rowspan')) || 1; }, setColspanFor: function(cell, value) { return jQuery(cell).attr('colspan', value > 1 ? value : null); }, setRowspanFor: function(cell, value) { return jQuery(cell).attr('rowspan', value > 1 ? value : null); } }); }).call(this); ;TI"required_assets_digest;TI"%a1b4cfd7a9925153020a6be85db55d62;FI" _version;TI"%2cf3622cbba861b85d33ffabfa420c13;F