Added array sortTree and flatten

This commit is contained in:
Ray 2023-12-24 03:30:54 +00:00
parent 3c0dd332a2
commit 9b74c1a90a
4 changed files with 48 additions and 50 deletions

View File

@ -1,3 +1,16 @@
Array.toFlatten = function (sourceArray, propName, destArray) {
for (let i=0; i<sourceArray.length; i++) {
destArray.push(sourceArray[i]);
if (typeof(sourceArray[i][propName]) != undefined) {
if (sourceArray[i][propName].length > 0) {
Array.toFlatten(sourceArray[i][propName], propName, destArray);
}
}
}
};
Array.prototype.addRange = function (array) {
if (array == null) {
return this;
@ -44,6 +57,14 @@ Array.prototype.countMany = function (...filters) {
return result;
};
Array.prototype.flatten = function (propName) {
let result = [];
Array.toFlatten(this, propName, result);
return result;
};
Array.prototype.index = function (propName, value) {
for (let i = 0; i < this.length; i++) {
if (typeof(this[i][propName]) == "undefined") {
@ -210,4 +231,16 @@ Array.prototype.selectMany = function (...filters) {
});
return result;
};
Array.prototype.sortTree = function (childPropName, sortPropName) {
this.orderBy(sortPropName);
for (let i=0; i<this.length; i++) {
if (this[i][childPropName].length <= 0) {
continue;
}
this[i][childPropName].orderBy(sortPropName);
}
};

View File

@ -7,14 +7,7 @@
<meta name="description" content="" />
<meta name="keyword" content="" />
<script src="ryz-js-ext/ryz-js-ext-array.js"></script>
<script src="ryz-js-ext/ryz-js-ext-boolean.js"></script>
<script src="ryz-js-ext/ryz-js-ext-date.js"></script>
<script src="ryz-js-ext/ryz-js-ext-document.js"></script>
<script src="ryz-js-ext/ryz-js-ext-math.js"></script>
<script src="ryz-js-ext/ryz-js-ext-string.js"></script>
<script src="ryz-js-ext/ryz-js-ext-window.js"></script>
<!-- <script src="ryzjsext.min.js"></script> -->
<script src="ryzjsext.min.js"></script>
<script src="ryz-gantts.js"></script>
<!-- <script src="bsdialog4.min.js"></script> -->

View File

@ -5,9 +5,11 @@ class RyzGantt {
a.Project = _options;
a.Tasks = [];
a.#initialiseComponents();
}
initialiseComponents() {
#initialiseComponents() {
const a = this;
}
@ -100,10 +102,7 @@ class RyzGantt {
ExportTasks() {
const a = this;
let result = [];
a.#exportTasks(result, a.Tasks.copy());
let result = a.Tasks.copy().flatten("Tasks");
for (var i=0; i<result.length; i++) {
result[i].Order = (i + 1);
@ -175,22 +174,21 @@ class RyzGantt {
const a = this;
let isSuccess = false;
if (!a.Recalc()) {
if (!a.Recalculate()) {
isSuccess = false;
}
return isSuccess;
}
Recalc() {
Recalculate() {
const a = this;
let isSuccess = false;
a.Sort();
// Get flat reference
let result = [];
a.#exportTasks(result, a.Tasks);
// Get flat references
let result = a.Tasks.flatten("Tasks");
// Reset calculated values
for (var i=0; i<result.length; i++) {
@ -311,7 +309,7 @@ class RyzGantt {
Sort() {
const a = this;
a.#sortTasks(a.Tasks);
a.Tasks.sortTree("Tasks", "StartDelay");
}
@ -337,18 +335,6 @@ class RyzGantt {
return new Date(node2FinishDate);
}
#exportTasks(outputArray, array) {
const a = this;
for (let i=0; i<array.length; i++) {
outputArray.push(array[i]);
if (array[i].Tasks.length > 0) {
a.#exportTasks(outputArray, array[i].Tasks);
}
}
}
#log(message) {
console.log(message);
}
@ -372,18 +358,4 @@ class RyzGantt {
return null;
}
#sortTasks(array) {
const a = this;
array.orderBy("StartDelay");
for (let i=0; i<array.length; i++) {
if (array[i].Tasks.length <= 0) {
continue;
}
array[i].Tasks.orderBy("StartDelay");
}
}
}

10
ryzjsext.min.js vendored
View File

@ -2,11 +2,11 @@
* Ray's JavaScript Extension
* @version v0.1.0.051 (2023/12/24 0115)
*/
Array.prototype.addRange=function(a){if(null==a)return this;for(let b=0;b<a.length;b++)this.push(a[b]);return this};Array.prototype.any=function(a,b){return 0<this.count(a,b)};Array.prototype.copy=function(){return JSON.parse(JSON.stringify(this))};Array.prototype.count=function(a,b){let c=0;for(let d=0;d<this.length;d++)"undefined"!=typeof this[d][a]&&this[d][a]==b&&c++;return c};Array.prototype.countMany=function(...a){let b=0;a.forEach(c=>{b+=this.count(c.propName,c.value)});return b};
Array.prototype.index=function(a,b){for(let c=0;c<this.length;c++)if("undefined"!=typeof this[c][a]&&this[c][a]==b)return c;return-1};Array.prototype.insert=function(a,b){0>a?this.splice(0,0,b):a>=this.length?this.push(b):this.splice(a,0,b);return this};Array.prototype.joinIfNotNullOrWhitespace=function(a){let b="";for(let c=0;c<this.length;c++)String.isNullOrWhitespace(this[c])||(String.isNullOrWhitespace(b)||(b+=a),b+=this[c]);return b};
Array.prototype.first=function(a,b){for(let c=0;c<this.length;c++)if("undefined"!=typeof this[c][a]&&this[c][a]==b)return this[c];return null};Array.prototype.indexes=function(a,b){let c=[];for(let d=0;d<this.length;d++)"undefined"!=typeof this[d][a]&&this[d][a]==b&&c.push(d);return c};Array.prototype.orderBy=function(a){this.sort(function(b,c){return b[a]<c[a]?-1:b[a]>c[a]?1:0});return this};Array.prototype.orderByDesc=function(a){this.sort(function(b,c){return b[a]<c[a]?1:b[a]>c[a]?-1:0});return this};
Array.prototype.remove=function(a){let b=[];for(let c=0;c<this.length;c++)this[c]==a&&b.push(c);for(a=b.length-1;0<=a;a--)this.removeAt(b[a]);return this};Array.prototype.removeAt=function(a){if(0>a||a>=this.length)return this;this.splice(a,1);return this};Array.prototype.removeRange=function(a){for(let b=0;b<a.length;b++)this.remove(a[b]);return this};Array.prototype.select=function(a,b){let c=[];for(let d=0;d<this.length;d++)"undefined"!=typeof this[d][a]&&this[d][a]==b&&c.push(this[d]);return c};
Array.prototype.selectMany=function(...a){let b=this;a.forEach(c=>{b=b.select(c.propName,c.value)});return b};Boolean.isFalse=function(a){return String.isNullOrUndefined(a)?!0:a.toString().containsCI("false","f","y","0","x")};Boolean.isTrue=function(a){return String.isNullOrUndefined(a)?!1:a.toString().containsCI("true","t","n","1","o")};Boolean.ifTrue=function(a,b,c){return Boolean.isTrue(a)?b:c};Date.addDays=function(a,b){a=new Date(a);a.addDays(b);return a};Date.addMonths=function(a,b){a=new Date(a);a.addMonths(b);return a};Date.addYears=function(a,b){a=new Date(a);a.addYears(b);return a};Date.today=function(){let a=new Date;a.setHours(0);a.setMinutes(0);a.setSeconds(0);a.setMilliseconds(0);return a};Date.prototype.addDays=function(a){this.setDate(this.getDate()+parseInt(a))};Date.prototype.addMonths=function(a){this.setMonth(this.getMonth()+parseInt(a))};
Array.toFlatten=function(a,b,c){for(let d=0;d<a.length;d++)c.push(a[d]),0<a[d][b].length&&Array.toFlatten(a[d][b],b,c)};Array.prototype.addRange=function(a){if(null==a)return this;for(let b=0;b<a.length;b++)this.push(a[b]);return this};Array.prototype.any=function(a,b){return 0<this.count(a,b)};Array.prototype.copy=function(){return JSON.parse(JSON.stringify(this))};Array.prototype.count=function(a,b){let c=0;for(let d=0;d<this.length;d++)"undefined"!=typeof this[d][a]&&this[d][a]==b&&c++;return c};
Array.prototype.countMany=function(...a){let b=0;a.forEach(c=>{b+=this.count(c.propName,c.value)});return b};Array.prototype.flatten=function(a){let b=[];Array.toFlatten(this,a,b);return b};Array.prototype.index=function(a,b){for(let c=0;c<this.length;c++)if("undefined"!=typeof this[c][a]&&this[c][a]==b)return c;return-1};Array.prototype.insert=function(a,b){0>a?this.splice(0,0,b):a>=this.length?this.push(b):this.splice(a,0,b);return this};
Array.prototype.joinIfNotNullOrWhitespace=function(a){let b="";for(let c=0;c<this.length;c++)String.isNullOrWhitespace(this[c])||(String.isNullOrWhitespace(b)||(b+=a),b+=this[c]);return b};Array.prototype.first=function(a,b){for(let c=0;c<this.length;c++)if("undefined"!=typeof this[c][a]&&this[c][a]==b)return this[c];return null};Array.prototype.indexes=function(a,b){let c=[];for(let d=0;d<this.length;d++)"undefined"!=typeof this[d][a]&&this[d][a]==b&&c.push(d);return c};
Array.prototype.orderBy=function(a){this.sort(function(b,c){return b[a]<c[a]?-1:b[a]>c[a]?1:0});return this};Array.prototype.orderByDesc=function(a){this.sort(function(b,c){return b[a]<c[a]?1:b[a]>c[a]?-1:0});return this};Array.prototype.remove=function(a){let b=[];for(let c=0;c<this.length;c++)this[c]==a&&b.push(c);for(a=b.length-1;0<=a;a--)this.removeAt(b[a]);return this};Array.prototype.removeAt=function(a){if(0>a||a>=this.length)return this;this.splice(a,1);return this};
Array.prototype.removeRange=function(a){for(let b=0;b<a.length;b++)this.remove(a[b]);return this};Array.prototype.select=function(a,b){let c=[];for(let d=0;d<this.length;d++)"undefined"!=typeof this[d][a]&&this[d][a]==b&&c.push(this[d]);return c};Array.prototype.selectMany=function(...a){let b=this;a.forEach(c=>{b=b.select(c.propName,c.value)});return b};Array.prototype.sortTree=function(a,b){this.orderBy(b);for(let c=0;c<this.length;c++)0>=this[c][a].length||this[c][a].orderBy(b)};Boolean.isFalse=function(a){return String.isNullOrUndefined(a)?!0:a.toString().containsCI("false","f","y","0","x")};Boolean.isTrue=function(a){return String.isNullOrUndefined(a)?!1:a.toString().containsCI("true","t","n","1","o")};Boolean.ifTrue=function(a,b,c){return Boolean.isTrue(a)?b:c};Date.addDays=function(a,b){a=new Date(a);a.addDays(b);return a};Date.addMonths=function(a,b){a=new Date(a);a.addMonths(b);return a};Date.addYears=function(a,b){a=new Date(a);a.addYears(b);return a};Date.today=function(){let a=new Date;a.setHours(0);a.setMinutes(0);a.setSeconds(0);a.setMilliseconds(0);return a};Date.prototype.addDays=function(a){this.setDate(this.getDate()+parseInt(a))};Date.prototype.addMonths=function(a){this.setMonth(this.getMonth()+parseInt(a))};
Date.prototype.addYears=function(a){this.setFullYear(this.getFullYear()+parseInt(a))};
Date.prototype.toCString=function(a){a=a.replace("fffffff",this.getMilliseconds().toString().padStart(7,"0"));a=a.replace("ffffff",this.getMilliseconds().toString().padStart(6,"0"));a=a.replace("fffff",this.getMilliseconds().toString().padStart(5,"0"));a=a.replace("yyyy",this.getFullYear().toString().padStart(4,"0"));a=a.replace("MMMM","{1}");a=a.replace("dddd","{2}");a=a.replace("ffff",this.getMilliseconds().toString().padStart(4,"0"));a=a.replace("yyy",this.getFullYear().toString().padStart(3,"0"));
a=a.replace("MMM","{3}");a=a.replace("ddd","{4}");a=a.replace("fff",this.getMilliseconds().toString().padStart(3,"0"));a=a.replace("zzz","");a=a.replace("yy",this.getFullYear().toString().slice(-2));a=a.replace("MM",(this.getMonth()+1).toString().padStart(2,"0"));a=a.replace("dd",this.getDate().toString().padStart(2,"0"));a=a.replace("HH",this.getHours().toString().padStart(2,"0"));a=a.replace("hh",(12<this.getHours()?this.getHours()-12:this.getHours()).toString().padStart(2,"0"));a=a.replace("mm",