forked from bunkat/schedule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.min.js
1 lines (1 loc) · 8.96 KB
/
schedule.min.js
1
"undefined"!=typeof module&&module.exports&&(this.later=require("later")),schedule=function(e){"use strict";function r(e){return e.id}function n(e){return e.available}function t(e){return e.isNotReservable||!1}function a(e){return e.id}function i(e){return e.duration}function s(e){return e.available}function u(e){return e.resources}function o(e){return e.dependsOn}function l(e){return e.minSchedule}function c(e){return e.priority}var d={version:"0.6.3"};if(!e)throw new Error("Laterjs must be included before Schedulejs.");return Array.isArray||(Array.isArray=function(e){return"[object Array]"===Object.prototype.toString.call(e)}),Array.prototype.indexOf||(Array.prototype.indexOf=function(e){if(null==this)throw new TypeError;var r=Object(this),n=r.length>>>0;if(0===n)return-1;var t=0;if(arguments.length>1&&(t=Number(arguments[1]),t!=t?t=0:0!=t&&1/0!=t&&t!=-1/0&&(t=(t>0||-1)*Math.floor(Math.abs(t)))),t>=n)return-1;for(var a=t>=0?t:Math.max(n-Math.abs(t),0);n>a;a++)if(a in r&&r[a]===e)return a;return-1}),d.date={},d.date.UTC=function(){e.date.UTC()},d.date.localTime=function(){e.date.localTime()},d.functor=function(e){return"function"==typeof e?e:function(){return e}},d.memoizedRangeFn=function(e){var r={};return function(n){if(!r[n]){var t=e(1,n);r[n]=[t[0]?t[0].getTime():41024448e5,t[1]?t[1].getTime():41024448e5]}return r[n]}},d.sort={},d.sort.tasks=function(e,r){r.sort(function(r,n){var t=e.tasks[r],a=e.tasks[n];return a.priority&&(!t.priority||a.priority>t.priority)?-1:t.priority&&(!a.priority||t.priority>a.priority)?1:e.tasks[n].floatAmt>e.tasks[r].floatAmt})},d.resources=function(){function e(e){for(var r=[],n=d.functor(a),t=d.functor(i),u=d.functor(s),o=0,l=e.length;l>o;o++){var c=e[o],f=n.call(this,c,o),h=t.call(this,c,o),v=u.call(this,c,o);r.push({id:f,available:h,isNotReservable:v})}return r}var a=r,i=n,s=t;return e.id=function(r){return arguments.length?(a=r,e):a},e.available=function(r){return arguments.length?(i=r,e):i},e.isNotReservable=function(r){return arguments.length?(s=r,e):s},e},d.tasks=function(){function e(e){for(var a=[],i=d.functor(r),s=d.functor(n),u=d.functor(t),o=d.functor(f),l=d.functor(h),c=d.functor(v),p=d.functor(g),y=0,m=e.length;m>y;y++){var b=e[y],x={id:i.call(this,b,y),duration:s.call(this,b,y),available:u.call(this,b,y),resources:o.call(this,b,y),dependsOn:l.call(this,b,y),minSchedule:c.call(this,b,y),priority:p.call(this,b,y)};a.push(x)}return a}var r=a,n=i,t=s,f=u,h=o,v=l,g=c;return e.id=function(n){return arguments.length?(r=n,e):r},e.duration=function(r){return arguments.length?(n=r,e):n},e.available=function(r){return arguments.length?(t=r,e):t},e.resources=function(r){return arguments.length?(f=r,e):f},e.dependsOn=function(r){return arguments.length?(h=r,e):h},e.minSchedule=function(r){return arguments.length?(v=r,e):v},e.priority=function(r){return arguments.length?(g=r,e):g},e},d.dependencyGraph=function(e){function r(e){for(var r={tasks:{},roots:[],leaves:[],resources:[],depth:0,end:0},l=0,c=e.length;c>l;l++){var d=e[l];r.tasks[d.id]={id:d.id,duration:d.duration,priority:d.priority,schedule:d.schedule,minSchedule:d.minSchedule,dependsOn:d.dependsOn,resources:d.resources}}return n(r),t(r.tasks),a(r),i(r,r.leaves,0),r.depth+=1,s(r,{},r.roots,0),u(r,r.leaves),o(r,{},r.leaves,r.end),r}function n(e){for(var r in e.tasks){var n=e.tasks[r];if(!c(n.resources))for(var t=0,a=n.resources.length;a>t;t++){var i=n.resources[t];-1===e.resources.indexOf(i)&&e.resources.push(i)}}}function t(e){for(var r in e){var n=e[r],t=n.dependsOn;if(!c(t))for(var a=0,i=t.length;i>a;a++){var s=e[t[a]];(s.requiredBy||(s.requiredBy=[])).push(n.id)}}}function a(e){for(var r in e.tasks){var n=e.tasks[r];c(n.dependsOn)&&e.roots.push(n.id),c(n.requiredBy)&&e.leaves.push(n.id)}}function i(e,r,n){for(var t=0,a=r.length;a>t;t++){var s=e.tasks[r[t]],u=s.dependsOn;s.depth=!s.depth||n>s.depth?n:s.depth,e.depth=n>e.depth?n:e.depth,c(u)||i(e,u,s.depth+1)}}function s(e,r,n,t){l(r,n,t);for(var a=0,i=n.length;i>a;a++){var u=n[a],o=e.tasks[u],d=o.dependsOn,f=r[u];!o.earlyFinish&&(c(d)||f&&f[0]===d.length)&&(o.earlyStart=f[1],o.earlyFinish=f[1]+o.duration,c(o.requiredBy)||s(e,r,o.requiredBy,o.earlyFinish))}}function u(e,r){for(var n=0,t=r.length;t>n;n++){var a=e.tasks[r[n]].earlyFinish;e.end=a>e.end?a:e.end}}function o(e,r,n,t){l(r,n,t,!0);for(var a=0,i=n.length;i>a;a++){var s=n[a],u=e.tasks[s],d=u.requiredBy,f=r[s];(c(d)||f&&f[0]===d.length)&&(u.lateStart=f[1]-u.duration,u.lateFinish=f[1],u.floatAmt=u.lateFinish-u.earlyFinish,c(u.dependsOn)||o(e,r,u.dependsOn,u.lateStart))}}function l(e,r,n,t){for(var a=t?function(e,r){return r>e}:function(e,r){return e>r},i=0,s=r.length;s>i;i++){var u=r[i];e[u]?(e[u][0]=e[u][0]+1,e[u][1]=a(n,e[u][1])?n:e[u][1]):e[u]=[1,n]}}function c(e){return!e||0===e.length}return r(e)},d.resourceManager=function(r,n){function t(e,r){var n={};if(e)for(var t=0,i=e.length;i>t;t++)a(n,e[t],r);return n}function a(r,n,t){var a=JSON.parse(JSON.stringify(n.available||p)),i=d.memoizedRangeFn(e.schedule(a).nextRange);r[n.id]={schedule:a,next:i,nextAvail:i(t)}}function i(e,r,n,t){var a,i=[],o={},c=50;for(s(e,r,i,o);!(a=u(i,n,t)).success&&--c;)l(i,f(i),o);return a.delays=o,a}function s(e,r,n,t){for(var a=0,i=e.length;i>a;a++){var u=e[a];if(Array.isArray(u)){var o=[],l={};s(u,r,o,l);var c=v(l);c&&(t[c]=l[c]);var d={subRanges:o};h(d),n.push(d)}else{var f=y[u],g=f.nextAvail[0]>=r?f.nextAvail:f.next(r);g[0]>r&&"_proj"!==u&&(t[u]={needed:r,available:g[0]}),n.push({id:u,range:g})}}}function u(r,n,t){for(var a,i,s={success:!1},u=[],l=0,c=r.length;c>l;l++){var d=r[l],f=d.range;g(d)||u.push(d.id),a=!a||f[0]>a?f[0]:a,i=!i||f[1]<i?f[1]:i}var h=(i-a)/e.MIN;return(h>=n||h>=t)&&(h=t&&h>t?t:h,s=o(u,a,h)),s}function o(r,n,t){var a=n+t*e.MIN,i={resources:r,start:n,end:a,duration:t,success:!0};return c(r,n,a),i}function l(e,r,n){for(var t=0,a=e.length;a>t;t++){var i=e[t];i.range[1]>r||(i.subRanges?(l(i.subRanges,r,{}),h(i)):(i.range=y[i.id].next(r),"_proj"===i.id||n[i.id]||(n[i.id]={needed:r,available:i.range[0]})))}}function c(r,n,t){for(var a=0,i=r.length;i>a;a++){var s=y[r[a]];s.isNotReservable||(n!==s.nextAvail[0]&&(s.schedule.exceptions||(s.schedule.exceptions=[]),s.schedule.exceptions.push({fd_a:[n],fd_b:[t]}),s.next=d.memoizedRangeFn(e.schedule(s.schedule).nextRange),t=s.nextAvail[0]),s.nextAvail=s.next(t))}}function f(e){for(var r,n=0,t=e.length;t>n;n++){var a=e[n].range[1];r=!r||r>a?a:r}return r}function h(e){for(var r,n,t=0,a=e.subRanges.length;a>t;t++){var i=e.subRanges[t];(!r||i.range[0]<n[0])&&(r=i.id,n=i.range)}e.id=r,e.range=n}function v(e){var r,n;for(var t in e){var a=e[t].available;(!r||r>a)&&(r=a,n=t)}return n}function g(e){return"_"===e.id[0]}var p={schedules:[{fd_a:[n.getTime()]}]},y=t(r,n);return{getResource:function(e){return y[e]},addResource:function(e,r,n){for(var t=0,i=e.length;i>t;t++){var s="object"!=typeof e[t]?{id:r+e[t]}:{id:r+e[t].id,available:e[t].available,isNotReservable:e[t].isNotReservable};y[s.id]||a(y,s,n)}},makeReservation:function(e,r,n,t){return r=r?new Date(r):new Date,i(e,r.getTime(),n||1,t)},optimize:function(r){for(var n in y){var t=y[n];if(t.schedule.exceptions){var a=t.schedule.exceptions;t.schedule.exceptions=[];for(var i=0,s=a.length;s>i;i++)(!a[i].fd_b||a[i].fd_b>r)&&t.schedule.exceptions.push(a[i]);t.next=d.memoizedRangeFn(e.schedule(t.schedule).nextRange)}t.nextAvail[0]<r&&(t.nextAvail=t.next(r))}}}},d.create=function(r,n,t,a){function i(){var e,n=[];return g.addResource(v.resources,"",h),g.addResource([{id:"_proj",available:t}],"",h),g.addResource(r,"_task",h),s(v.roots),e=o(r,n),f(v.leaves,e[1]),{scheduledTasks:p,failedTasks:n.length?n:null,success:0===n.length,start:e[0],end:e[1]}}function s(e){for(var r=e.slice(0),n={},t=0,a=e.length;a>t;t++)n[e[t]]=[0,h.getTime()];for(;r.length;){d.sort.tasks(v,r);var i=v.tasks[r.pop()],s=n[i.id][1],o=u(i,s);o&&i.requiredBy&&(l(r,n,i.requiredBy,o),g.optimize(c(n)))}}function u(e,r){for(var n=["_proj","_task"+e.id],t=e.resources?n.concat(e.resources):n,a=e.duration,i=r,s={schedule:[],duration:e.duration};a;){var u=g.makeReservation(t,i,e.minSchedule||1,a);if(!u.success)return void 0;s.earlyStart=s.earlyStart||u.start,s.schedule.push(u),a-=u.duration,i=u.end}return s.earlyFinish=i,p[e.id]=s,i}function o(e,r){for(var n,t,a=0,i=e.length;i>a;a++){var s=p[e[a].id];s?(n=!n||s.earlyStart<n?s.earlyStart:n,t=!t||s.earlyFinish>t?s.earlyFinish:t):r.push(e[a].id)}return[n,t]}function l(e,r,n,t){for(var a=0,i=n.length;i>a;a++){var s=n[a],u=v.tasks[s].dependsOn,o=r[s]||(r[s]=[0,0]);o[0]+=1,o[1]=t>o[1]?t:o[1],(!u||o[0]>=u.length)&&e.push(s)}}function c(e){var r;for(var n in e)(!r||r>e[n][1])&&(r=e[n][1]);return r}function f(r,n){for(var t=0,a=r.length;a>t;t++){var i=p[r[t]],s=v.tasks[r[t]].dependsOn;i&&(i.lateFinish=n,i.floatAmt=(i.lateFinish-i.earlyFinish)/e.MIN,s&&f(s,i.earlyStart))}}if(!Array.isArray(r))throw new Error("Tasks are required and must be passed in as an array.");if(n&&!Array.isArray(n))throw new Error("Resources must be passed in as an array.");var h=a?new Date(a):new Date;if(!h||!h.getTime())throw new Error("Invalid start date specified.");var v=d.dependencyGraph(r),g=d.resourceManager(n,h),p={};return i()},d}(this.later);