nested-sort
Advanced tools
Comparing version 2.1.0 to 2.2.0
'use strict'; | ||
/** | ||
* @typedef {object} dataEngineDataItem | ||
* @property {number|string} id | ||
* @property {string} text | ||
* @property {number|string} [parent] | ||
*/ | ||
class DataEngine { | ||
/** | ||
* @constructor | ||
* @param {array.<dataEngineDataItem>} data | ||
* @param {object[]} data | ||
* @param {object} [propertyMap={}] | ||
*/ | ||
constructor({ data }) { | ||
constructor({ data, propertyMap = {} }) { | ||
this.data = data; | ||
this.sortedData = []; | ||
this.sortedDataDomArray = []; | ||
this.propertyMap = propertyMap; | ||
this.maybeTransformData(); | ||
} | ||
maybeTransformData() { | ||
if (!Object.keys(this.propertyMap).length) return; | ||
const getItemPropProxyName = this.getItemPropProxyName.bind(this); | ||
this.data = this.data.map(item => { | ||
return new Proxy(item, { | ||
get(target, prop, receiver) { | ||
return Reflect.get(target, getItemPropProxyName(prop), receiver) | ||
} | ||
}) | ||
}); | ||
} | ||
/** | ||
* @returns {array.<dataEngineDataItem>} | ||
* @param {PropertyKey} prop | ||
* @returns {PropertyKey} | ||
*/ | ||
getItemPropProxyName(prop) { | ||
if (this.propertyMap.hasOwnProperty(prop)) { | ||
return this.propertyMap[prop] | ||
} | ||
return prop | ||
} | ||
/** | ||
* @returns {object[]} | ||
*/ | ||
sortListItems() { | ||
@@ -34,3 +56,3 @@ this.sortedData = [...this.data].sort((item1, item2) => { | ||
/** | ||
* @param {dataEngineDataItem} item | ||
* @param {object[]} item | ||
* @param {string} nodeName | ||
@@ -144,3 +166,3 @@ * @returns {HTMLElement} | ||
* @param {HTMLUListElement} ul | ||
* @returns {{parent, id: string}[]} | ||
* @returns {object[]} | ||
*/ | ||
@@ -153,4 +175,4 @@ convertDomToData(ul) { | ||
return { | ||
id: li.dataset.id, | ||
parent | ||
[this.getItemPropProxyName('id')]: li.dataset.id, | ||
[this.getItemPropProxyName('parent')]: parent, | ||
} | ||
@@ -179,2 +201,3 @@ }) | ||
* @param {array|string} listClassNames | ||
* @param {object} [propertyMap={}] | ||
*/ | ||
@@ -186,3 +209,4 @@ constructor({ | ||
el, | ||
listClassNames | ||
listClassNames, | ||
propertyMap = {} | ||
} = {}) { | ||
@@ -197,2 +221,3 @@ this.data = data; | ||
this.listClassNames = this.createListClassNamesArray(listClassNames); | ||
this.propertyMap = propertyMap; | ||
this.actions = { | ||
@@ -240,3 +265,3 @@ onDrop | ||
} | ||
this.dataEngine = new DataEngine({data: this.data}); | ||
this.dataEngine = new DataEngine({data: this.data, propertyMap: this.propertyMap}); | ||
return this.dataEngine | ||
@@ -243,0 +268,0 @@ } |
@@ -1,22 +0,44 @@ | ||
/** | ||
* @typedef {object} dataEngineDataItem | ||
* @property {number|string} id | ||
* @property {string} text | ||
* @property {number|string} [parent] | ||
*/ | ||
class DataEngine { | ||
/** | ||
* @constructor | ||
* @param {array.<dataEngineDataItem>} data | ||
* @param {object[]} data | ||
* @param {object} [propertyMap={}] | ||
*/ | ||
constructor({ data }) { | ||
constructor({ data, propertyMap = {} }) { | ||
this.data = data; | ||
this.sortedData = []; | ||
this.sortedDataDomArray = []; | ||
this.propertyMap = propertyMap; | ||
this.maybeTransformData(); | ||
} | ||
maybeTransformData() { | ||
if (!Object.keys(this.propertyMap).length) return; | ||
const getItemPropProxyName = this.getItemPropProxyName.bind(this); | ||
this.data = this.data.map(item => { | ||
return new Proxy(item, { | ||
get(target, prop, receiver) { | ||
return Reflect.get(target, getItemPropProxyName(prop), receiver) | ||
} | ||
}) | ||
}); | ||
} | ||
/** | ||
* @returns {array.<dataEngineDataItem>} | ||
* @param {PropertyKey} prop | ||
* @returns {PropertyKey} | ||
*/ | ||
getItemPropProxyName(prop) { | ||
if (this.propertyMap.hasOwnProperty(prop)) { | ||
return this.propertyMap[prop] | ||
} | ||
return prop | ||
} | ||
/** | ||
* @returns {object[]} | ||
*/ | ||
sortListItems() { | ||
@@ -32,3 +54,3 @@ this.sortedData = [...this.data].sort((item1, item2) => { | ||
/** | ||
* @param {dataEngineDataItem} item | ||
* @param {object[]} item | ||
* @param {string} nodeName | ||
@@ -142,3 +164,3 @@ * @returns {HTMLElement} | ||
* @param {HTMLUListElement} ul | ||
* @returns {{parent, id: string}[]} | ||
* @returns {object[]} | ||
*/ | ||
@@ -151,4 +173,4 @@ convertDomToData(ul) { | ||
return { | ||
id: li.dataset.id, | ||
parent | ||
[this.getItemPropProxyName('id')]: li.dataset.id, | ||
[this.getItemPropProxyName('parent')]: parent, | ||
} | ||
@@ -177,2 +199,3 @@ }) | ||
* @param {array|string} listClassNames | ||
* @param {object} [propertyMap={}] | ||
*/ | ||
@@ -184,3 +207,4 @@ constructor({ | ||
el, | ||
listClassNames | ||
listClassNames, | ||
propertyMap = {} | ||
} = {}) { | ||
@@ -195,2 +219,3 @@ this.data = data; | ||
this.listClassNames = this.createListClassNamesArray(listClassNames); | ||
this.propertyMap = propertyMap; | ||
this.actions = { | ||
@@ -238,3 +263,3 @@ onDrop | ||
} | ||
this.dataEngine = new DataEngine({data: this.data}); | ||
this.dataEngine = new DataEngine({data: this.data, propertyMap: this.propertyMap}); | ||
return this.dataEngine | ||
@@ -241,0 +266,0 @@ } |
@@ -29,2 +29,17 @@ (function (global, factory) { | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _toConsumableArray(arr) { | ||
@@ -63,15 +78,12 @@ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); | ||
/** | ||
* @typedef {object} dataEngineDataItem | ||
* @property {number|string} id | ||
* @property {string} text | ||
* @property {number|string} [parent] | ||
*/ | ||
var DataEngine = /*#__PURE__*/function () { | ||
/** | ||
* @constructor | ||
* @param {array.<dataEngineDataItem>} data | ||
* @param {object[]} data | ||
* @param {object} [propertyMap={}] | ||
*/ | ||
function DataEngine(_ref) { | ||
var data = _ref.data; | ||
var data = _ref.data, | ||
_ref$propertyMap = _ref.propertyMap, | ||
propertyMap = _ref$propertyMap === void 0 ? {} : _ref$propertyMap; | ||
@@ -83,9 +95,38 @@ _classCallCheck(this, DataEngine); | ||
this.sortedDataDomArray = []; | ||
this.propertyMap = propertyMap; | ||
this.maybeTransformData(); | ||
} | ||
/** | ||
* @returns {array.<dataEngineDataItem>} | ||
*/ | ||
_createClass(DataEngine, [{ | ||
key: "maybeTransformData", | ||
value: function maybeTransformData() { | ||
if (!Object.keys(this.propertyMap).length) return; | ||
var getItemPropProxyName = this.getItemPropProxyName.bind(this); | ||
this.data = this.data.map(function (item) { | ||
return new Proxy(item, { | ||
get: function get(target, prop, receiver) { | ||
return Reflect.get(target, getItemPropProxyName(prop), receiver); | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* @param {PropertyKey} prop | ||
* @returns {PropertyKey} | ||
*/ | ||
_createClass(DataEngine, [{ | ||
}, { | ||
key: "getItemPropProxyName", | ||
value: function getItemPropProxyName(prop) { | ||
if (this.propertyMap.hasOwnProperty(prop)) { | ||
return this.propertyMap[prop]; | ||
} | ||
return prop; | ||
} | ||
/** | ||
* @returns {object[]} | ||
*/ | ||
}, { | ||
key: "sortListItems", | ||
@@ -100,3 +141,3 @@ value: function sortListItems() { | ||
/** | ||
* @param {dataEngineDataItem} item | ||
* @param {object[]} item | ||
* @param {string} nodeName | ||
@@ -229,3 +270,3 @@ * @returns {HTMLElement} | ||
* @param {HTMLUListElement} ul | ||
* @returns {{parent, id: string}[]} | ||
* @returns {object[]} | ||
*/ | ||
@@ -236,9 +277,10 @@ | ||
value: function convertDomToData(ul) { | ||
var _this3 = this; | ||
return Array.from(ul.querySelectorAll('li')).map(function (li) { | ||
var _ref2; | ||
var parentListItem = li.parentNode; | ||
var parent = parentListItem.dataset.id; | ||
return { | ||
id: li.dataset.id, | ||
parent: parent | ||
}; | ||
return _ref2 = {}, _defineProperty(_ref2, _this3.getItemPropProxyName('id'), li.dataset.id), _defineProperty(_ref2, _this3.getItemPropProxyName('parent'), parent), _ref2; | ||
}); | ||
@@ -272,2 +314,3 @@ } | ||
* @param {array|string} listClassNames | ||
* @param {object} [propertyMap={}] | ||
*/ | ||
@@ -284,3 +327,5 @@ function nestedSort() { | ||
el = _ref.el, | ||
listClassNames = _ref.listClassNames; | ||
listClassNames = _ref.listClassNames, | ||
_ref$propertyMap = _ref.propertyMap, | ||
propertyMap = _ref$propertyMap === void 0 ? {} : _ref$propertyMap; | ||
@@ -297,2 +342,3 @@ _classCallCheck(this, nestedSort); | ||
this.listClassNames = this.createListClassNamesArray(listClassNames); | ||
this.propertyMap = propertyMap; | ||
this.actions = { | ||
@@ -338,3 +384,4 @@ onDrop: onDrop | ||
this.dataEngine = new DataEngine({ | ||
data: this.data | ||
data: this.data, | ||
propertyMap: this.propertyMap | ||
}); | ||
@@ -341,0 +388,0 @@ return this.dataEngine; |
{ | ||
"name": "nested-sort", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"author": "Hesam Bahrami (Genzo)", | ||
@@ -5,0 +5,0 @@ "description": "A JavaScript library to create a nested list of elements", |
52714
1477