pbs/PBSlib.js

1 line
7.5 KiB
JavaScript
Raw Normal View History

2016-04-14 10:03:36 +02:00
var PBS,bind=function(fn,me){return function(){return fn.apply(me,arguments)}},slice=[].slice;PBS=function(){function PBS(obj,verbose,errListener){var ref;this.verbose=verbose,this.errListener=errListener,this.calculateAllChainedDelays=bind(this.calculateAllChainedDelays,this),this.calculateAllCriticalPaths=bind(this.calculateAllCriticalPaths,this),this.calculateEndDays=bind(this.calculateEndDays,this),this.insertDay=bind(this.insertDay,this),this.highestID=bind(this.highestID,this),this.calculateChainedDelays=bind(this.calculateChainedDelays,this),this.calculateEndDay=bind(this.calculateEndDay,this),this.calculateStartDay=bind(this.calculateStartDay,this),this.calculateFreeDelay=bind(this.calculateFreeDelay,this),this.toResource=bind(this.toResource,this),this.toActivity=bind(this.toActivity,this),this.compileResources=bind(this.compileResources,this),this.days=[],this.resources=[],this.criticalPaths=[],this.verbose=!0,obj.push?this.list=obj:obj.activities?(this.list=obj.activities,null!=(null!=(ref=obj.resources)?ref.push:void 0)&&(this.resources=obj.resources)):(this.list=[],this.err('data is not an array nor a object with "activities" array'))}return PBS.prototype.log=function(){var x;return x=1<=arguments.length?slice.call(arguments,0):[],this.verbose?"undefined"!=typeof chalk&&null!==chalk?console.log(chalk.bold.apply(chalk,["[ Pert ]"].concat(slice.call(x)))):console.log.apply(console,["[ Pert ]"].concat(slice.call(x))):void 0},PBS.prototype.err=function(){var ref,x;return x=1<=arguments.length?slice.call(arguments,0):[],"undefined"!=typeof chalk&&null!==chalk?console.log(chalk.bold.apply(chalk,[chalk.red("[ Pert ]")].concat(slice.call(x)))):console.log.apply(console,["[ !Pert! ]"].concat(slice.call(x))),null!=(null!=(ref=this.errListener)?ref.call:void 0)&&this.errListener(x),x},PBS.prototype.compileResources=function(){return this.log("compiling resources..."),null!=this.resources?(this.resources.forEach(function(_this){return function(x){var ref;return _this.log("processing resource",x),null!=(null!=(ref=x.assignedTo)?ref.push:void 0)?x.assignedTo.forEach(function(i){var a;return a=_this.toActivity(i),null==a.assigned&&(a.assigned=[]),a.assigned.push(x.name||x.id)}):void 0}}(this)),this.list.forEach(function(_this){return function(x){var item,ref;return item=_this.toActivity(x),null!=(null!=(ref=item.assigned)?ref.push:void 0)?item.assigned.forEach(function(i){var res;return res=_this.toResource(i),res?(_this.log("found",res,"assigned to",item),null==res.assignedTo&&(res.assignedTo=[]),res.assignedTo.push(i)):void 0}):void 0}}(this))):void 0},PBS.prototype.maxa=function(l){return Math.max.apply(null,l)},PBS.prototype.toActivity=function(id){var i,item,ref,x;null==this.list&&this.err("list is",this.list),item={},ref=this.list;for(i in ref)x=ref[i],x.id===id&&(item=x);return item},PBS.prototype.toResource=function(id){var i,item,ref,ref1,x;if(null!=(null!=(ref=this.resources)?ref.push:void 0)){item={},ref1=this.resources;for(i in ref1)x=ref1[i],x.id!==id&&x.name!==id||(item=x);return item}},PBS.prototype.calculateFreeDelay=function(dependencyID,dependantID){var dependant,i;return this.log("checking freeDelay to dependency",dependencyID,"of",dependantID),i=this.toActivity(dependencyID),dependant=this.toActivity(dependantID),null==i.dependant?i.dependant=[dependant.id]:i.dependant.push(dependant.id),null==i.permittedDelay?(i.permittedDelay=dependant.startDay-this.calculateEndDay(i),this.log("written permittedDelay to dependency",dependencyID,"of",dependant,"as",i.permittedDelay)):this.log("aborting permittedDelay: already calculated"),this.log("permitted delay of",dependencyID,"is",i.permittedDelay)},PBS.prototype.calculateStartDay=function(item){var j,ref,x;if(null==item.depends||0===item.depends.length)return this.insertDay(0),item.startDay=0;item.startDay=this.maxa(item.depends.map(this.toActivity).map(this.calculateEndDay)),this.log("start day of",item.id,"is",item.startDay),ref=item.depends;for(j in ref)x=ref[j],this.calculateFreeDelay(x,item.id);return this.insertDay(item.startDay),item.startDay},PBS.prototy