From 03f5bb8b59bf7be63b83e1d00b735b08e1615df0 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Sun, 29 Oct 2017 16:34:54 +0530 Subject: [PATCH] Move sorting to datamanager --- lib/frappe-datatable.js | 582 +++++++++++++++++++----------------- lib/frappe-datatable.js.map | 2 +- src/datamanager.js | 50 +++- src/datatable.js | 55 ++-- 4 files changed, 380 insertions(+), 309 deletions(-) diff --git a/lib/frappe-datatable.js b/lib/frappe-datatable.js index 39a5722..356e79b 100644 --- a/lib/frappe-datatable.js +++ b/lib/frappe-datatable.js @@ -88,7 +88,7 @@ var _datatable = __webpack_require__(1); var _datatable2 = _interopRequireDefault(_datatable); -var _package = __webpack_require__(8); +var _package = __webpack_require__(9); var _package2 = _interopRequireDefault(_package); @@ -115,11 +115,11 @@ var _createClass = function () { function defineProperties(target, props) { for var _utils = __webpack_require__(2); -var _datamanager = __webpack_require__(9); +var _datamanager = __webpack_require__(3); var _datamanager2 = _interopRequireDefault(_datamanager); -__webpack_require__(3); +__webpack_require__(4); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } @@ -148,7 +148,10 @@ var DataTable = function () { } this.options = Object.assign({}, DEFAULT_OPTIONS, options); + // custom user events this.events = this.options.events; + // map of checked rows + this.checkMap = []; this.datamanager = new _datamanager2.default(this.options); @@ -393,7 +396,7 @@ var DataTable = function () { var _self$getCellAttr2 = self.getCellAttr($cell), colIndex = _self$getCellAttr2.colIndex; - if (self.options.addCheckbox && colIndex === 0) { + if (self.options.addCheckboxColumn && colIndex === 0) { return; } @@ -584,7 +587,7 @@ var DataTable = function () { this.header.on('click', '.data-table-col .content span', function () { var $cell = $(this).closest('.data-table-col'); - var sortAction = (0, _utils.getDefault)($cell.attr('data-sort-action'), 'none'); + var sortOrder = (0, _utils.getDefault)($cell.attr('data-sort-order'), 'none'); var colIndex = $cell.attr('data-col-index'); var col = self.getColumn(colIndex); @@ -594,47 +597,41 @@ var DataTable = function () { // reset sort indicator self.header.find('.sort-indicator').text(''); - self.header.find('.data-table-col').attr('data-sort-action', 'none'); + self.header.find('.data-table-col').attr('data-sort-order', 'none'); - if (sortAction === 'none') { - $cell.attr('data-sort-action', 'asc'); + if (sortOrder === 'none') { + $cell.attr('data-sort-order', 'asc'); $cell.find('.sort-indicator').text('▲'); - } else if (sortAction === 'asc') { - $cell.attr('data-sort-action', 'desc'); + } else if (sortOrder === 'asc') { + $cell.attr('data-sort-order', 'desc'); $cell.find('.sort-indicator').text('▼'); - } else if (sortAction === 'desc') { - $cell.attr('data-sort-action', 'none'); + } else if (sortOrder === 'desc') { + $cell.attr('data-sort-order', 'none'); $cell.find('.sort-indicator').text(''); } // sortWith this action - var sortWith = $cell.attr('data-sort-action'); + sortOrder = $cell.attr('data-sort-order'); if (self.events && self.events.onSort) { - self.events.onSort(colIndex, sortWith); + self.events.onSort(colIndex, sortOrder); } else { - self.sortRows(colIndex, sortWith); + self.sortRows(colIndex, sortOrder); self.refreshRows(); } }); } }, { key: 'sortRows', - value: function sortRows(colIndex) { - var sortAction = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'none'; - - colIndex = +colIndex; - this.datamanager.sortRows(colIndex, sortAction); + value: function sortRows(colIndex, sortOrder) { + this.datamanager.sortRows(colIndex, sortOrder); } }, { key: 'bindCheckbox', value: function bindCheckbox() { - if (!this.options.addCheckbox) return; + if (!this.options.addCheckboxColumn) return; var self = this; - // map of checked rows - this.checkMap = []; - this.wrapper.on('click', '.data-table-col[data-col-index="0"] [type="checkbox"]', function () { var $checkbox = $(this); var $cell = $checkbox.closest('.data-table-col'); @@ -655,7 +652,6 @@ var DataTable = function () { }, { key: 'getCheckedRows', value: function getCheckedRows() { - this.checkMap = this.checkMap || []; return this.checkMap.map(function (c, rowIndex) { if (c) { @@ -771,9 +767,10 @@ var DataTable = function () { return; } - var deltaWidth = (availableWidth - headerWidth) / this.data.columns.length; + var columns = this.datamanager.getColumns(); + var deltaWidth = (availableWidth - headerWidth) / this.datamanager.getColumnCount(); - this.data.columns.map(function (col) { + columns.map(function (col) { var width = _this5.getColumnHeaderElement(col.colIndex).width(); var finalWidth = width + deltaWidth - 16; @@ -807,27 +804,17 @@ var DataTable = function () { }, { key: 'getColumn', value: function getColumn(colIndex) { - colIndex = +colIndex; - return this.data.columns.find(function (col) { - return col.colIndex === colIndex; - }); + return this.datamanager.getColumn(colIndex); } }, { key: 'getRow', value: function getRow(rowIndex) { - rowIndex = +rowIndex; - return this.data.rows.find(function (row) { - return row[0].rowIndex === rowIndex; - }); + return this.datamanager.getRow(rowIndex); } }, { key: 'getCell', value: function getCell(rowIndex, colIndex) { - rowIndex = +rowIndex; - colIndex = +colIndex; - return this.data.rows.find(function (row) { - return row[0].rowIndex === rowIndex; - })[colIndex]; + return this.datamanager.getCell(rowIndex, colIndex); } }, { key: 'getColumnHeaderElement', @@ -850,7 +837,7 @@ var DataTable = function () { }, { key: 'getTotalRows', value: function getTotalRows() { - return this.data.rows.length; + return this.datamanager.getRowCount(); } }, { key: 'log', @@ -1073,10 +1060,282 @@ module.exports = exports['default']; /* 3 */ /***/ (function(module, exports, __webpack_require__) { +"use strict"; + + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +var DataManager = function () { + function DataManager(options) { + _classCallCheck(this, DataManager); + + this.options = options; + this.rowCount = 0; + this.currentSort = { + sortBy: -1, // colIndex + sortOrder: 'none' // asc, desc, none + }; + } + + _createClass(DataManager, [{ + key: 'init', + value: function init(data) { + var columns = data.columns, + rows = data.rows; + + + this.columns = this.prepareColumns(columns); + this.rows = this.prepareRows(rows); + } + }, { + key: 'prepareColumns', + value: function prepareColumns(columns) { + if (!Array.isArray(columns)) { + throw new TypeError('`columns` must be an array'); + } + + if (this.options.addSerialNoColumn && !this._serialNoColumnAdded) { + var val = { + content: 'Sr. No', + resizable: false + }; + + columns = [val].concat(columns); + this._serialNoColumnAdded = true; + } + + if (this.options.addCheckboxColumn && !this._checkboxColumnAdded) { + var _val = { + content: '', + resizable: false + }; + + columns = [_val].concat(columns); + this._checkboxColumnAdded = true; + } + + // wrap the title in span + columns = columns.map(function (column) { + if (typeof column === 'string') { + column = '' + column + ''; + } else if ((typeof column === 'undefined' ? 'undefined' : _typeof(column)) === 'object') { + column.content = '' + column.content + ''; + } + + return column; + }); + + return _prepareColumns(columns, { + isHeader: 1 + }); + } + }, { + key: 'prepareRows', + value: function prepareRows(rows) { + var _this = this; + + if (!Array.isArray(rows) || !Array.isArray(rows[0])) { + throw new TypeError('`rows` must be an array of arrays'); + } + + rows = rows.map(function (row, i) { + var index = _this._getNextRowCount(); + + if (row.length < _this.columns.length) { + if (_this._serialNoColumnAdded) { + var val = index + 1 + ''; + + row = [val].concat(row); + } + + if (_this._checkboxColumnAdded) { + var _val2 = ''; + + row = [_val2].concat(row); + } + } + + return prepareRow(row, index); + }); + + return rows; + } + }, { + key: 'appendRows', + value: function appendRows(rows) { + if (Array.isArray(rows) && !Array.isArray(rows[0])) { + rows = [rows]; + } + var _rows = this.prepareRows(rows); + + this.rows = this.rows.concat(_rows); + } + }, { + key: 'sortRows', + value: function sortRows(colIndex) { + var sortOrder = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'none'; + + colIndex = +colIndex; + + if (this.currentSort.colIndex === colIndex) { + // reverse the array if only sortOrder changed + if (this.currentSort.sortOrder === 'asc' && sortOrder === 'desc' || this.currentSort.sortOrder === 'desc' && sortOrder === 'asc') { + this.reverseArray(this.rows); + this.currentSort.sortOrder = sortOrder; + return; + } + } + + this.rows.sort(function (a, b) { + var _aIndex = a[0].rowIndex; + var _bIndex = b[0].rowIndex; + var _a = a[colIndex].content; + var _b = b[colIndex].content; + + if (sortOrder === 'none') { + return _aIndex - _bIndex; + } else if (sortOrder === 'asc') { + if (_a < _b) return -1; + if (_a > _b) return 1; + if (_a === _b) return 0; + } else if (sortOrder === 'desc') { + if (_a < _b) return 1; + if (_a > _b) return -1; + if (_a === _b) return 0; + } + return 0; + }); + + this.currentSort.colIndex = colIndex; + this.currentSort.sortOrder = sortOrder; + } + }, { + key: 'reverseArray', + value: function reverseArray(array) { + var left = null; + var right = null; + var length = array.length; + + for (left = 0, right = length - 1; left < right; left += 1, right -= 1) { + var temporary = array[left]; + + array[left] = array[right]; + array[right] = temporary; + } + } + }, { + key: 'getRowCount', + value: function getRowCount() { + return this.rowCount; + } + }, { + key: '_getNextRowCount', + value: function _getNextRowCount() { + var val = this.rowCount; + + this.rowCount++; + return val; + } + }, { + key: 'getRows', + value: function getRows(start, end) { + return this.rows.slice(start, end); + } + }, { + key: 'getColumns', + value: function getColumns() { + return this.columns; + } + }, { + key: 'getColumnCount', + value: function getColumnCount() { + return this.columns.length; + } + }, { + key: 'getColumn', + value: function getColumn(colIndex) { + colIndex = +colIndex; + return this.columns.find(function (col) { + return col.colIndex === colIndex; + }); + } + }, { + key: 'getRow', + value: function getRow(rowIndex) { + rowIndex = +rowIndex; + return this.rows.find(function (row) { + return row[0].rowIndex === rowIndex; + }); + } + }, { + key: 'getCell', + value: function getCell(rowIndex, colIndex) { + rowIndex = +rowIndex; + colIndex = +colIndex; + return this.rows.find(function (row) { + return row[0].rowIndex === rowIndex; + })[colIndex]; + } + }, { + key: 'get', + value: function get() { + return { + columns: this.columns, + rows: this.rows + }; + } + }]); + + return DataManager; +}(); + +exports.default = DataManager; + + +function _prepareColumns(columns) { + var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; + + var _columns = columns.map(prepareCell); + + return _columns.map(function (col) { + return Object.assign({}, col, props); + }); +} + +function prepareRow(row, i) { + return _prepareColumns(row, { + rowIndex: i + }); +} + +function prepareCell(col, i) { + if (typeof col === 'string') { + col = { + content: col + }; + } + return Object.assign(col, { + colIndex: i + }); +} +module.exports = exports['default']; + +/***/ }), +/* 4 */ +/***/ (function(module, exports, __webpack_require__) { + // style-loader: Adds some css to the DOM by adding a ')\n .prependTo(this.wrapper);\n }\n\n getColumn(colIndex) {\n colIndex = +colIndex;\n return this.data.columns.find(col => col.colIndex === colIndex);\n }\n\n getRow(rowIndex) {\n rowIndex = +rowIndex;\n return this.data.rows.find(row => row[0].rowIndex === rowIndex);\n }\n\n getCell(rowIndex, colIndex) {\n rowIndex = +rowIndex;\n colIndex = +colIndex;\n return this.data.rows.find(row => row[0].rowIndex === rowIndex)[colIndex];\n }\n\n getColumnHeaderElement(colIndex) {\n colIndex = +colIndex;\n if (colIndex < 0) return null;\n return this.wrapper.find(\n `.data-table-col[data-is-header][data-col-index=\"${colIndex}\"]`\n );\n }\n\n getColumnMinWidth(colIndex) {\n colIndex = +colIndex;\n return this.minWidthMap && this.minWidthMap[colIndex];\n }\n\n getCellAttr($cell) {\n return $cell.data();\n }\n\n getTotalRows() {\n return this.data.rows.length;\n }\n\n log() {\n if (this.options.enableLogs) {\n console.log.apply(console, arguments);\n }\n }\n}\n\n\n\n\n// WEBPACK FOOTER //\n// ./src/datatable.js","function camelCaseToDash(str) {\n return str.replace(/([A-Z])/g, (g) => `-${g[0].toLowerCase()}`);\n}\n\nfunction makeDataAttributeString(props) {\n const keys = Object.keys(props);\n\n return keys\n .map((key) => {\n const _key = camelCaseToDash(key);\n const val = props[key];\n\n if (val === undefined) return '';\n return `data-${_key}=\"${val}\" `;\n })\n .join('')\n .trim();\n}\n\nfunction getEditCellHTML() {\n return `\n
\n `;\n}\n\nfunction getColumnHTML(column) {\n const { rowIndex, colIndex, isHeader } = column;\n const dataAttr = makeDataAttributeString({\n rowIndex,\n colIndex,\n isHeader\n });\n\n const editCellHTML = isHeader ? '' : getEditCellHTML();\n\n return `\n \n
\n ${column.format ? column.format(column.content) : column.content}\n \n
\n ${editCellHTML}\n \n `;\n}\n\nfunction getRowHTML(columns, props) {\n const dataAttr = makeDataAttributeString(props);\n\n return `\n \n ${columns.map(getColumnHTML).join('')}\n \n `;\n}\n\nfunction getHeaderHTML(columns) {\n const $header = `\n \n ${getRowHTML(columns, { isHeader: 1, rowIndex: -1 })}\n \n `;\n\n // columns.map(col => {\n // if (!col.width) return;\n // const $cellContent = $header.find(\n // `.data-table-col[data-col-index=\"${col.colIndex}\"] .content`\n // );\n\n // $cellContent.width(col.width);\n // });\n\n return $header;\n}\n\nfunction getBodyHTML(rows) {\n return `\n \n ${rows.map(row => getRowHTML(row, { rowIndex: row[0].rowIndex })).join('')}\n \n `;\n}\n\nfunction prepareColumn(col, i) {\n if (typeof col === 'string') {\n col = {\n content: col\n };\n }\n return Object.assign(col, {\n colIndex: i\n });\n}\n\nfunction prepareColumns(columns, props = {}) {\n const _columns = columns.map(prepareColumn);\n\n return _columns.map(col => Object.assign({}, col, props));\n}\n\nfunction prepareRowHeader(columns) {\n return prepareColumns(columns, {\n rowIndex: -1,\n isHeader: 1,\n format: (content) => `${content}`\n });\n}\n\nfunction prepareRow(row, i) {\n return prepareColumns(row, {\n rowIndex: i\n });\n}\n\nfunction prepareRows(rows) {\n return rows.map(prepareRow);\n}\n\nfunction getDefault(a, b) {\n return a !== undefined ? a : b;\n}\n\nfunction escapeRegExp(str) {\n // https://stackoverflow.com/a/6969486\n return str.replace(/[\\-\\[\\]\\/\\{\\}\\(\\)\\*\\+\\?\\.\\\\\\^\\$\\|]/g, '\\\\$&');\n}\n\nfunction getCSSString(styleMap) {\n let style = '';\n\n for (const prop in styleMap) {\n if (styleMap.hasOwnProperty(prop)) {\n style += `${prop}: ${styleMap[prop]}; `;\n }\n }\n\n return style.trim();\n}\n\nfunction getCSSRuleBlock(rule, styleMap) {\n return `${rule} { ${getCSSString(styleMap)} }`;\n}\n\nfunction namespaceSelector(selector) {\n return '.data-table ' + selector;\n}\n\nfunction buildCSSRule(rule, styleMap, cssRulesString = '') {\n // build css rules efficiently,\n // append new rule if doesnt exist,\n // update existing ones\n\n const rulePatternStr = `${escapeRegExp(rule)} {([^}]*)}`;\n const rulePattern = new RegExp(rulePatternStr, 'g');\n\n if (cssRulesString && cssRulesString.match(rulePattern)) {\n for (const property in styleMap) {\n const value = styleMap[property];\n const propPattern = new RegExp(`${escapeRegExp(property)}:([^;]*);`);\n\n cssRulesString = cssRulesString.replace(rulePattern, function (match, propertyStr) {\n if (propertyStr.match(propPattern)) {\n // property exists, replace value with new value\n propertyStr = propertyStr.replace(propPattern, (match, valueStr) => {\n return `${property}: ${value};`;\n });\n }\n propertyStr = propertyStr.trim();\n\n const replacer =\n `${rule} { ${propertyStr} }`;\n\n return replacer;\n });\n }\n\n return cssRulesString;\n }\n // no match, append new rule block\n return `${cssRulesString}${getCSSRuleBlock(rule, styleMap)}`;\n}\n\nexport default {\n getHeaderHTML,\n getBodyHTML,\n getRowHTML,\n getColumnHTML,\n getEditCellHTML,\n prepareRowHeader,\n prepareRows,\n namespaceSelector,\n getCSSString,\n buildCSSRule,\n makeDataAttributeString,\n getDefault,\n escapeRegExp\n};\n\n\n\n// WEBPACK FOOTER //\n// ./src/utils.js","// style-loader: Adds some css to the DOM by adding a ')\n .prependTo(this.wrapper);\n }\n\n getColumn(colIndex) {\n return this.datamanager.getColumn(colIndex);\n }\n\n getRow(rowIndex) {\n return this.datamanager.getRow(rowIndex);\n }\n\n getCell(rowIndex, colIndex) {\n return this.datamanager.getCell(rowIndex, colIndex);\n }\n\n getColumnHeaderElement(colIndex) {\n colIndex = +colIndex;\n if (colIndex < 0) return null;\n return this.wrapper.find(\n `.data-table-col[data-is-header][data-col-index=\"${colIndex}\"]`\n );\n }\n\n getColumnMinWidth(colIndex) {\n colIndex = +colIndex;\n return this.minWidthMap && this.minWidthMap[colIndex];\n }\n\n getCellAttr($cell) {\n return $cell.data();\n }\n\n getTotalRows() {\n return this.datamanager.getRowCount();\n }\n\n log() {\n if (this.options.enableLogs) {\n console.log.apply(console, arguments);\n }\n }\n}\n\n\n\n\n// WEBPACK FOOTER //\n// ./src/datatable.js","function camelCaseToDash(str) {\n return str.replace(/([A-Z])/g, (g) => `-${g[0].toLowerCase()}`);\n}\n\nfunction makeDataAttributeString(props) {\n const keys = Object.keys(props);\n\n return keys\n .map((key) => {\n const _key = camelCaseToDash(key);\n const val = props[key];\n\n if (val === undefined) return '';\n return `data-${_key}=\"${val}\" `;\n })\n .join('')\n .trim();\n}\n\nfunction getEditCellHTML() {\n return `\n
\n `;\n}\n\nfunction getColumnHTML(column) {\n const { rowIndex, colIndex, isHeader } = column;\n const dataAttr = makeDataAttributeString({\n rowIndex,\n colIndex,\n isHeader\n });\n\n const editCellHTML = isHeader ? '' : getEditCellHTML();\n\n return `\n \n
\n ${column.format ? column.format(column.content) : column.content}\n \n
\n ${editCellHTML}\n \n `;\n}\n\nfunction getRowHTML(columns, props) {\n const dataAttr = makeDataAttributeString(props);\n\n return `\n \n ${columns.map(getColumnHTML).join('')}\n \n `;\n}\n\nfunction getHeaderHTML(columns) {\n const $header = `\n \n ${getRowHTML(columns, { isHeader: 1, rowIndex: -1 })}\n \n `;\n\n // columns.map(col => {\n // if (!col.width) return;\n // const $cellContent = $header.find(\n // `.data-table-col[data-col-index=\"${col.colIndex}\"] .content`\n // );\n\n // $cellContent.width(col.width);\n // });\n\n return $header;\n}\n\nfunction getBodyHTML(rows) {\n return `\n \n ${rows.map(row => getRowHTML(row, { rowIndex: row[0].rowIndex })).join('')}\n \n `;\n}\n\nfunction prepareColumn(col, i) {\n if (typeof col === 'string') {\n col = {\n content: col\n };\n }\n return Object.assign(col, {\n colIndex: i\n });\n}\n\nfunction prepareColumns(columns, props = {}) {\n const _columns = columns.map(prepareColumn);\n\n return _columns.map(col => Object.assign({}, col, props));\n}\n\nfunction prepareRowHeader(columns) {\n return prepareColumns(columns, {\n rowIndex: -1,\n isHeader: 1,\n format: (content) => `${content}`\n });\n}\n\nfunction prepareRow(row, i) {\n return prepareColumns(row, {\n rowIndex: i\n });\n}\n\nfunction prepareRows(rows) {\n return rows.map(prepareRow);\n}\n\nfunction getDefault(a, b) {\n return a !== undefined ? a : b;\n}\n\nfunction escapeRegExp(str) {\n // https://stackoverflow.com/a/6969486\n return str.replace(/[\\-\\[\\]\\/\\{\\}\\(\\)\\*\\+\\?\\.\\\\\\^\\$\\|]/g, '\\\\$&');\n}\n\nfunction getCSSString(styleMap) {\n let style = '';\n\n for (const prop in styleMap) {\n if (styleMap.hasOwnProperty(prop)) {\n style += `${prop}: ${styleMap[prop]}; `;\n }\n }\n\n return style.trim();\n}\n\nfunction getCSSRuleBlock(rule, styleMap) {\n return `${rule} { ${getCSSString(styleMap)} }`;\n}\n\nfunction namespaceSelector(selector) {\n return '.data-table ' + selector;\n}\n\nfunction buildCSSRule(rule, styleMap, cssRulesString = '') {\n // build css rules efficiently,\n // append new rule if doesnt exist,\n // update existing ones\n\n const rulePatternStr = `${escapeRegExp(rule)} {([^}]*)}`;\n const rulePattern = new RegExp(rulePatternStr, 'g');\n\n if (cssRulesString && cssRulesString.match(rulePattern)) {\n for (const property in styleMap) {\n const value = styleMap[property];\n const propPattern = new RegExp(`${escapeRegExp(property)}:([^;]*);`);\n\n cssRulesString = cssRulesString.replace(rulePattern, function (match, propertyStr) {\n if (propertyStr.match(propPattern)) {\n // property exists, replace value with new value\n propertyStr = propertyStr.replace(propPattern, (match, valueStr) => {\n return `${property}: ${value};`;\n });\n }\n propertyStr = propertyStr.trim();\n\n const replacer =\n `${rule} { ${propertyStr} }`;\n\n return replacer;\n });\n }\n\n return cssRulesString;\n }\n // no match, append new rule block\n return `${cssRulesString}${getCSSRuleBlock(rule, styleMap)}`;\n}\n\nexport default {\n getHeaderHTML,\n getBodyHTML,\n getRowHTML,\n getColumnHTML,\n getEditCellHTML,\n prepareRowHeader,\n prepareRows,\n namespaceSelector,\n getCSSString,\n buildCSSRule,\n makeDataAttributeString,\n getDefault,\n escapeRegExp\n};\n\n\n\n// WEBPACK FOOTER //\n// ./src/utils.js","\nexport default class DataManager {\n constructor(options) {\n this.options = options;\n this.rowCount = 0;\n this.currentSort = {\n sortBy: -1, // colIndex\n sortOrder: 'none' // asc, desc, none\n };\n }\n\n init(data) {\n let { columns, rows } = data;\n\n this.columns = this.prepareColumns(columns);\n this.rows = this.prepareRows(rows);\n }\n\n prepareColumns(columns) {\n if (!Array.isArray(columns)) {\n throw new TypeError('`columns` must be an array');\n }\n\n if (this.options.addSerialNoColumn && !this._serialNoColumnAdded) {\n const val = {\n content: 'Sr. No',\n resizable: false\n };\n\n columns = [val].concat(columns);\n this._serialNoColumnAdded = true;\n }\n\n if (this.options.addCheckboxColumn && !this._checkboxColumnAdded) {\n const val = {\n content: '',\n resizable: false\n };\n\n columns = [val].concat(columns);\n this._checkboxColumnAdded = true;\n }\n\n // wrap the title in span\n columns = columns.map(column => {\n if (typeof column === 'string') {\n column = `${column}`;\n } else if (typeof column === 'object') {\n column.content = `${column.content}`;\n }\n\n return column;\n });\n\n return prepareColumns(columns, {\n isHeader: 1\n });\n }\n\n prepareRows(rows) {\n if (!Array.isArray(rows) || !Array.isArray(rows[0])) {\n throw new TypeError('`rows` must be an array of arrays');\n }\n\n rows = rows.map((row, i) => {\n const index = this._getNextRowCount();\n\n if (row.length < this.columns.length) {\n if (this._serialNoColumnAdded) {\n const val = (index + 1) + '';\n\n row = [val].concat(row);\n }\n\n if (this._checkboxColumnAdded) {\n const val = '';\n\n row = [val].concat(row);\n }\n }\n\n return prepareRow(row, index);\n });\n\n return rows;\n }\n\n appendRows(rows) {\n if (Array.isArray(rows) && !Array.isArray(rows[0])) {\n rows = [rows];\n }\n const _rows = this.prepareRows(rows);\n\n this.rows = this.rows.concat(_rows);\n }\n\n sortRows(colIndex, sortOrder = 'none') {\n colIndex = +colIndex;\n\n if (this.currentSort.colIndex === colIndex) {\n // reverse the array if only sortOrder changed\n if (\n (this.currentSort.sortOrder === 'asc' && sortOrder === 'desc') ||\n (this.currentSort.sortOrder === 'desc' && sortOrder === 'asc')\n ) {\n this.reverseArray(this.rows);\n this.currentSort.sortOrder = sortOrder;\n return;\n }\n }\n\n this.rows.sort((a, b) => {\n const _aIndex = a[0].rowIndex;\n const _bIndex = b[0].rowIndex;\n const _a = a[colIndex].content;\n const _b = b[colIndex].content;\n\n if (sortOrder === 'none') {\n return _aIndex - _bIndex;\n } else if (sortOrder === 'asc') {\n if (_a < _b) return -1;\n if (_a > _b) return 1;\n if (_a === _b) return 0;\n } else if (sortOrder === 'desc') {\n if (_a < _b) return 1;\n if (_a > _b) return -1;\n if (_a === _b) return 0;\n }\n return 0;\n });\n\n this.currentSort.colIndex = colIndex;\n this.currentSort.sortOrder = sortOrder;\n }\n\n reverseArray(array) {\n let left = null;\n let right = null;\n let length = array.length;\n\n for (left = 0, right = length - 1; left < right; left += 1, right -= 1) {\n const temporary = array[left];\n\n array[left] = array[right];\n array[right] = temporary;\n }\n }\n\n getRowCount() {\n return this.rowCount;\n }\n\n _getNextRowCount() {\n const val = this.rowCount;\n\n this.rowCount++;\n return val;\n }\n\n getRows(start, end) {\n return this.rows.slice(start, end);\n }\n\n getColumns() {\n return this.columns;\n }\n\n getColumnCount() {\n return this.columns.length;\n }\n\n getColumn(colIndex) {\n colIndex = +colIndex;\n return this.columns.find(col => col.colIndex === colIndex);\n }\n\n getRow(rowIndex) {\n rowIndex = +rowIndex;\n return this.rows.find(row => row[0].rowIndex === rowIndex);\n }\n\n getCell(rowIndex, colIndex) {\n rowIndex = +rowIndex;\n colIndex = +colIndex;\n return this.rows.find(row => row[0].rowIndex === rowIndex)[colIndex];\n }\n\n get() {\n return {\n columns: this.columns,\n rows: this.rows\n };\n }\n}\n\nfunction prepareColumns(columns, props = {}) {\n const _columns = columns.map(prepareCell);\n\n return _columns.map(col => Object.assign({}, col, props));\n}\n\nfunction prepareRow(row, i) {\n return prepareColumns(row, {\n rowIndex: i\n });\n}\n\nfunction prepareCell(col, i) {\n if (typeof col === 'string') {\n col = {\n content: col\n };\n }\n return Object.assign(col, {\n colIndex: i\n });\n}\n\n\n\n// WEBPACK FOOTER //\n// ./src/datamanager.js","// style-loader: Adds some css to the DOM by adding a