From 5b9efa42914516b54dc82e825a0a7fbec69584f4 Mon Sep 17 00:00:00 2001 From: fateme Date: Thu, 31 Dec 2020 14:58:43 +0330 Subject: [PATCH] add filterByLevel --- resources/js/Global/mixins/commingSoon.js | 3 +- .../js/Global/plugins/globalComponent.js | 2 + resources/js/Global/utils/common/Object.js | 42 +++++++++++++++++++ .../Global/utils/common/ProcessTreeArray.js | 22 +++++++++- 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 resources/js/Global/utils/common/Object.js diff --git a/resources/js/Global/mixins/commingSoon.js b/resources/js/Global/mixins/commingSoon.js index e91a81b..ea810f9 100644 --- a/resources/js/Global/mixins/commingSoon.js +++ b/resources/js/Global/mixins/commingSoon.js @@ -10,7 +10,8 @@ var CommingSoonArray = [ "blog-post", "blog-setting", "client_export", - "blog-videos" + "blog-videos", + "service-specialInfo" ]; const commingSoon = { methods: { diff --git a/resources/js/Global/plugins/globalComponent.js b/resources/js/Global/plugins/globalComponent.js index 898fc05..f97c087 100644 --- a/resources/js/Global/plugins/globalComponent.js +++ b/resources/js/Global/plugins/globalComponent.js @@ -1,5 +1,7 @@ import Vue from 'vue'; +import '@Global/utils/common/Object'; + // global Styles import "@riophae/vue-treeselect/dist/vue-treeselect.css";//for tree select diff --git a/resources/js/Global/utils/common/Object.js b/resources/js/Global/utils/common/Object.js new file mode 100644 index 0000000..73d94b5 --- /dev/null +++ b/resources/js/Global/utils/common/Object.js @@ -0,0 +1,42 @@ +// Warn if overriding existing method +if(Array.prototype.equals) + console.warn("Overriding existing Array.prototype.equals. Possible causes: New API defines the method, there's a framework conflict or you've got double inclusions in your code."); +// attach the .equals method to Array's prototype to call it on any array +Array.prototype.equals = function (array) { + // if the other array is a falsy value, return + if (!array) + return false; + + // compare lengths - can save a lot of time + if (this.length != array.length) + return false; + + for (var i = 0, l=this.length; i < l; i++) { + // Check if we have nested arrays + if (this[i] instanceof Array && array[i] instanceof Array) { + // recurse into the nested arrays + if (!this[i].equals(array[i])) + return false; + } + else if (this[i] != array[i]) { + // Warning - two different object instances will never be equal: {x:20} != {x:20} + return false; + } + } + return true; +} +// Hide method from for-in loops +Object.defineProperty(Array.prototype, "equals", {enumerable: false}); + + + + +Array.prototype.matchItems = function (array) { + // if the other array is a falsy value, return + if (!array) + return []; + + return this.filter(item => array.includes(item)); +} +// Hide method from for-in loops +Object.defineProperty(Array.prototype, "matchItems", {enumerable: false}); diff --git a/resources/js/Global/utils/common/ProcessTreeArray.js b/resources/js/Global/utils/common/ProcessTreeArray.js index 428d60e..d17728e 100644 --- a/resources/js/Global/utils/common/ProcessTreeArray.js +++ b/resources/js/Global/utils/common/ProcessTreeArray.js @@ -287,6 +287,25 @@ const insertTreeArray = ( // }; +const filterByLevel = (list, level1) => { + + let filteredList = []; + let level = parseInt(level1); + checkLevel(list); + + function checkLevel(list, parentId = null, levelCounter = 1) { + list.map(item => { + if(item.parent_id == parentId) { + if(levelCounter == level) { + filteredList.push(item); + } else { + checkLevel(list, item.id, levelCounter + 1); + } + } + }) + } + return filteredList; +} export { convertTreeToList, insertTreeArray, @@ -295,5 +314,6 @@ export { listSearchSelect, convertListToTree, addIndexTreeToList, - addHierarchyToList + addHierarchyToList, + filterByLevel };