GanttChart.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.gantt.GanttChart"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.gantt.GanttChart"] = true;
  8. dojo.provide("dojox.gantt.GanttChart");
  9. dojo.require("dijit.Tooltip");
  10. dojo.require("dojox.gantt.GanttProjectItem");
  11. dojo.require("dojox.gantt.GanttResourceItem");
  12. dojo.require("dojox.gantt.TabMenu");
  13. dojo.require("dojo.date.locale");
  14. (function(){
  15. dojo.declare("dojox.gantt.GanttChart", null, {
  16. constructor: function(configuration, node){
  17. this.resourceChartHeight = configuration.resourceChartHeight !== undefined ? configuration.resourceChartHeight : false;
  18. this.withResource = configuration.withResource !== undefined ? configuration.withResource : true;
  19. this.correctError = configuration.autoCorrectError !== undefined ? configuration.autoCorrectError : false;
  20. this.isShowConMenu = this.isContentEditable = !configuration.readOnly;
  21. this.withTaskId = configuration.withTaskId !== undefined ? configuration.withTaskId : !configuration.readOnly;
  22. this.animation = configuration.animation !== undefined ? configuration.animation : true;
  23. this.saveProgramPath = configuration.saveProgramPath || "saveGanttData.php";
  24. this.dataFilePath = configuration.dataFilePath || "gantt_default.json";
  25. this.contentHeight = configuration.height || 400;
  26. this.contentWidth = configuration.width || 600;
  27. this.content = dojo.byId(node);
  28. this.scrollBarWidth = 18;
  29. this.panelTimeHeight = 102;
  30. this.maxWidthPanelNames = 150;
  31. this.maxWidthTaskNames = 150;
  32. this.minWorkLength = 8;
  33. this.heightTaskItem = 12;
  34. this.heightTaskItemExtra = 11;
  35. this.pixelsPerDay = 24;//px
  36. this.hsPerDay = 8;
  37. this.pixelsPerWorkHour = this.pixelsPerDay / this.hsPerDay;//px
  38. this.pixelsPerHour = this.pixelsPerDay / 24;//px
  39. this.countDays = 0;
  40. this.totalDays = 0;
  41. this.startDate = null;
  42. this.initialPos = 0;
  43. this.contentDataHeight = 0;
  44. this.panelTimeExpandDelta = 20;
  45. this.divTimeInfo = null;
  46. this.panelNames = null;
  47. this.panelTime = null;
  48. this.contentData = null;
  49. this.tabMenu = null;
  50. this.project = [];
  51. this.arrProjects = [];
  52. this.xmlLoader = null;
  53. this.isMoving = false;
  54. this.isResizing = false;
  55. this.animationNodes = [];
  56. this.scale = 1;
  57. this.tempDayInPixels = 0;
  58. this.resource = null;
  59. this.months = dojo.date.locale.getNames("months", "wide");
  60. this._events = [];
  61. },
  62. getProject: function(id){
  63. return dojo.filter(this.arrProjects, function(proj){
  64. return proj.project.id == id;
  65. }, this)[0];
  66. },
  67. checkPosPreviousTask: function(predTask, task){
  68. var widthPred = this.getWidthOnDuration(predTask.duration);
  69. var posPred = this.getPosOnDate(predTask.startTime);
  70. var posChild = this.getPosOnDate(task.startTime);
  71. if((widthPred + posPred) > posChild){
  72. return false;
  73. }
  74. return true;
  75. },
  76. correctPosPreviousTask: function(predTask, ctask, ctaskObj){
  77. var newDate = new Date(predTask.startTime);
  78. newDate.setHours(newDate.getHours() + (predTask.duration / this.hsPerDay * 24))
  79. if(newDate.getHours() > 0){
  80. newDate.setHours(0);
  81. newDate.setDate(newDate.getDate() + 1);
  82. }
  83. ctaskObj ? (ctaskObj.setStartTime(newDate, true)) : (ctask.startTime = newDate);
  84. if(ctask.parentTask){
  85. if(!this.checkPosParentTask(ctask.parentTask, ctask)){
  86. var newDate2 = new Date(ctask.parentTask.startTime);
  87. newDate2.setHours(newDate2.getHours() + (ctask.parentTask.duration / this.hsPerDay * 24))
  88. ctask.duration = parseInt((parseInt((newDate2 - ctask.startTime) / (1000 * 60 * 60))) * this.hsPerDay / 24);
  89. }
  90. }
  91. },
  92. correctPosParentTask: function(parentTask, ctask){
  93. if(!ctask.previousTask){
  94. if(parentTask.startTime > ctask.startTime){
  95. ctask.startTime = new Date(parentTask.startTime);
  96. }
  97. if(!this.checkPosParentTask(parentTask, ctask)){
  98. ctask.duration = parentTask.duration;
  99. }
  100. }else{
  101. this.correctPosPreviousTask(ctask.previousTask, ctask);
  102. }
  103. },
  104. checkPosParentTaskInTree: function(parentTask){
  105. var exception = false;
  106. for(var i = 0; i < parentTask.cldTasks.length; i++){
  107. var pcTask = parentTask.cldTasks[i];
  108. if(!this.checkPosParentTask(parentTask, pcTask)){
  109. if(!this.correctError){
  110. return true;
  111. }else{
  112. this.correctPosParentTask(parentTask, pcTask);
  113. }
  114. }
  115. if(parentTask.startTime > pcTask.startTime){
  116. if(!this.correctError){
  117. return true;
  118. }else{
  119. this.correctPosParentTask(parentTask, pcTask);
  120. }
  121. }
  122. if(pcTask.cldTasks.length > 0){
  123. exception = this.checkPosParentTaskInTree(pcTask);
  124. }
  125. }
  126. return exception;
  127. },
  128. setPreviousTask: function(project){
  129. var exception = false;
  130. for(var i = 0; i < project.parentTasks.length; i++){
  131. var ppTask = project.parentTasks[i];
  132. if(ppTask.previousTaskId){
  133. ppTask.previousTask = project.getTaskById(ppTask.previousTaskId);
  134. if(!ppTask.previousTask){
  135. if(!this.correctError){
  136. return true;
  137. }
  138. }
  139. ppTask.previousTask.cldPreTasks.push(ppTask);
  140. }
  141. if(ppTask.previousTask){
  142. if(!this.checkPosPreviousTask(ppTask.previousTask, ppTask)){
  143. if(!this.correctError){
  144. return true;
  145. }else{
  146. this.correctPosPreviousTask(ppTask.previousTask, ppTask);
  147. }
  148. }
  149. }
  150. exception = this.setPreviousTaskInTree(ppTask);
  151. }
  152. return exception;
  153. },
  154. setPreviousTaskInTree: function(parentTask){
  155. var exception = false;
  156. for(var i = 0; i < parentTask.cldTasks.length; i++){
  157. var pcTask = parentTask.cldTasks[i];
  158. if(pcTask.previousTaskId){
  159. pcTask.previousTask = parentTask.project.getTaskById(pcTask.previousTaskId);
  160. if(!pcTask.previousTask){
  161. if(!this.correctError){
  162. return true;
  163. }
  164. }
  165. if(!this.checkPosPreviousTask(pcTask.previousTask, pcTask)){
  166. if(!this.correctError){
  167. return true;
  168. }else{
  169. this.correctPosPreviousTask(pcTask.previousTask, pcTask);
  170. }
  171. }
  172. pcTask.previousTask.cldPreTasks.push(pcTask);
  173. }
  174. if(pcTask.cldTasks.length > 0){
  175. exception = this.setPreviousTaskInTree(pcTask);
  176. }
  177. }
  178. return exception;
  179. },
  180. checkPosParentTask: function(parentTask, task){
  181. var widthParent = this.getWidthOnDuration(parentTask.duration);
  182. var posParent = this.getPosOnDate(parentTask.startTime);
  183. var posChild = this.getPosOnDate(task.startTime);
  184. var widthChild = this.getWidthOnDuration(task.duration);
  185. return (widthParent + posParent) >= (posChild + widthChild);
  186. },
  187. addProject: function(projectItem){
  188. this.project.push(projectItem);
  189. },
  190. deleteProject: function(id){
  191. var project = this.getProject(id);
  192. if(project){
  193. if(project.arrTasks.length > 0){
  194. while(project.arrTasks.length > 0){
  195. project.deleteChildTask(project.arrTasks[0]);
  196. }
  197. }
  198. var rowHeight = this.heightTaskItemExtra + this.heightTaskItem;
  199. project.nextProject && project.shiftNextProject(project, -rowHeight); //rowHeight: 23
  200. this.project = dojo.filter(this.project, function(proj){
  201. return proj.id != project.project.id;
  202. }, this);
  203. if((project.previousProject) && (project.nextProject)){
  204. var previousProject = project.previousProject;
  205. previousProject.nextProject = project.nextProject;
  206. }
  207. if((project.previousProject) && !(project.nextProject)){
  208. var previousProject = project.previousProject;
  209. previousProject.nextProject = null;
  210. }
  211. if(!(project.previousProject) && (project.nextProject)){
  212. var nextProject = project.nextProject;
  213. nextProject.previousProject = null;
  214. }
  215. for(var i = 0; i < this.arrProjects.length; i++){
  216. if(this.arrProjects[i].project.id == id){
  217. this.arrProjects.splice(i, 1);
  218. }
  219. }
  220. project.projectItem[0].parentNode.removeChild(project.projectItem[0]);
  221. project.descrProject.parentNode.removeChild(project.descrProject);
  222. project.projectNameItem.parentNode.removeChild(project.projectNameItem);
  223. this.contentDataHeight -= this.heightTaskItemExtra + this.heightTaskItem;
  224. if(this.project.length == 0){
  225. var d = new Date(this.startDate);
  226. var t = new Date(d.setDate(d.getDate() + 1));
  227. var pi = new dojox.gantt.GanttProjectItem({
  228. id: 1,
  229. name: "New Project",
  230. startDate: t
  231. });
  232. this.project.push(pi);
  233. var project = new dojox.gantt.GanttProjectControl(this, pi);
  234. project.create();
  235. this.arrProjects.push(project);
  236. this.contentDataHeight += this.heightTaskItemExtra + this.heightTaskItem;
  237. }
  238. this.checkPosition();
  239. }
  240. },
  241. insertProject: function(id, name, startDate){
  242. if(this.startDate >= startDate){
  243. return false;
  244. }
  245. if(this.getProject(id)){
  246. return false;
  247. }
  248. this.checkHeighPanelTasks();
  249. var project = new dojox.gantt.GanttProjectItem({
  250. id: id,
  251. name: name,
  252. startDate: startDate
  253. });
  254. this.project.push(project);
  255. var _project = new dojox.gantt.GanttProjectControl(this, project);
  256. for(var i = 0; i < this.arrProjects.length; i++){
  257. var curProject = this.arrProjects[i],
  258. preProject = this.arrProjects[i-1],
  259. nextProject = this.arrProjects[i+1];
  260. if(startDate < curProject.project.startDate){
  261. this.arrProjects.splice(i, 0, _project);
  262. if(i > 0){
  263. _project.previousProject = preProject;
  264. preProject.nextProject = _project;
  265. }
  266. if(i + 1 <= this.arrProjects.length){
  267. _project.nextProject = nextProject;
  268. nextProject.previousProject = _project;
  269. var rowHeight = this.heightTaskItem + this.heightTaskItemExtra;
  270. _project.shiftNextProject(_project, rowHeight);
  271. }
  272. _project.create();
  273. _project.hideDescrProject();
  274. this.checkPosition();
  275. return _project;
  276. }
  277. }
  278. if(this.arrProjects.length > 0){
  279. this.arrProjects[this.arrProjects.length - 1].nextProject = _project;
  280. _project.previousProject = this.arrProjects[this.arrProjects.length - 1];
  281. }
  282. this.arrProjects.push(_project);
  283. _project.create();
  284. _project.hideDescrProject();
  285. this.checkPosition();
  286. return _project;
  287. },
  288. openTree: function(parentTask){
  289. var lastParentTask = this.getLastCloseParent(parentTask);
  290. this.openNode(lastParentTask);
  291. parentTask.taskItem.id != lastParentTask.taskItem.id && this.openTree(parentTask);
  292. },
  293. openNode: function(parentTask){
  294. if(!parentTask.isExpanded){
  295. dojo.removeClass(parentTask.cTaskNameItem[2], "ganttImageTreeExpand");
  296. dojo.addClass(parentTask.cTaskNameItem[2], "ganttImageTreeCollapse");
  297. parentTask.isExpanded = true;
  298. parentTask.shiftCurrentTasks(parentTask, parentTask.hideTasksHeight);
  299. parentTask.showChildTasks(parentTask, parentTask.isExpanded);
  300. parentTask.hideTasksHeight = 0;
  301. }
  302. },
  303. getLastCloseParent: function(task){
  304. if(task.parentTask){
  305. if((!task.parentTask.isExpanded) ||
  306. (task.parentTask.cTaskNameItem[2].style.display == "none")){
  307. return this.getLastCloseParent(task.parentTask);
  308. }else{
  309. return task;
  310. }
  311. }else{
  312. return task;
  313. }
  314. },
  315. getProjectItemById: function(id){
  316. return dojo.filter(this.project, function(proj){
  317. return proj.id == id;
  318. }, this)[0];
  319. },
  320. clearAll: function(){
  321. this.contentDataHeight = 0;
  322. this.startDate = null;
  323. this.clearData();
  324. this.clearItems();
  325. this.clearEvents();
  326. },
  327. clearEvents: function(){
  328. dojo.forEach(this._events, dojo.disconnect);
  329. this._events = [];
  330. },
  331. clearData: function(){
  332. this.project = [];
  333. this.arrProjects = [];
  334. },
  335. clearItems: function(){
  336. this.contentData.removeChild(this.contentData.firstChild);
  337. this.contentData.appendChild(this.createPanelTasks());
  338. this.panelNames.removeChild(this.panelNames.firstChild);
  339. this.panelNames.appendChild(this.createPanelNamesTasks());
  340. this.panelTime.removeChild(this.panelTime.firstChild);
  341. },
  342. buildUIContent: function(){
  343. this.project.sort(this.sortProjStartDate);
  344. this.startDate = this.getStartDate();
  345. this.panelTime.appendChild(this.createPanelTime());
  346. for(var i = 0; i < this.project.length; i++){
  347. var proj = this.project[i];
  348. for(var k = 0; k < proj.parentTasks.length; k++){
  349. var ppTask = proj.parentTasks[k];
  350. if(ppTask.startTime){
  351. this.setStartTimeChild(ppTask);
  352. }else{
  353. return;
  354. }
  355. if(this.setPreviousTask(proj)){
  356. return;
  357. }
  358. }
  359. for(var k = 0; k < proj.parentTasks.length; k++){
  360. var ppTask = proj.parentTasks[k];
  361. if(ppTask.startTime < proj.startDate){
  362. return;
  363. }
  364. if(this.checkPosParentTaskInTree(ppTask)) return;
  365. }
  366. this.sortTasksByStartTime(proj);
  367. }
  368. for(var i = 0; i < this.project.length; i++){
  369. var proj = this.project[i];
  370. var project = new dojox.gantt.GanttProjectControl(this, proj);
  371. if(this.arrProjects.length > 0){
  372. var previousProject = this.arrProjects[this.arrProjects.length - 1];
  373. project.previousProject = previousProject;
  374. previousProject.nextProject = project;
  375. }
  376. project.create();
  377. this.checkHeighPanelTasks();
  378. this.arrProjects.push(project);
  379. this.createTasks(project);
  380. }
  381. this.resource && this.resource.reConstruct();
  382. this.postLoadData();
  383. this.postBindEvents();
  384. },
  385. loadJSONData: function(filename){
  386. var _this = this;
  387. _this.dataFilePath = filename || _this.dataFilePath;
  388. dojo.xhrGet({
  389. url: _this.dataFilePath,
  390. sync: true,
  391. load: function(text, ioArgs){
  392. _this.loadJSONString(text);
  393. _this.buildUIContent();
  394. alert("Successfully! Loaded data from: " + _this.dataFilePath);
  395. },
  396. error: function(err, ioArgs){
  397. alert("Failed! Load error: " + _this.dataFilePath);
  398. }
  399. });
  400. },
  401. loadJSONString: function(content){
  402. //load data
  403. if(!content){ return; }
  404. this.clearAll();
  405. var jsonObj = dojo.fromJson(content);
  406. var items = jsonObj.items;
  407. dojo.forEach(items, function(pItem){
  408. var startDate = pItem.startdate.split("-");
  409. var project = new dojox.gantt.GanttProjectItem({
  410. id: pItem.id,
  411. name: pItem.name,
  412. startDate: new Date(startDate[0], (parseInt(startDate[1]) - 1), startDate[2])
  413. });
  414. var tItems = pItem.tasks;
  415. dojo.forEach(tItems, function(tItem){
  416. var id = tItem.id,
  417. name = tItem.name,
  418. starttime = tItem.starttime.split("-");
  419. duration = tItem.duration,
  420. percentage = tItem.percentage,
  421. previousTaskId = tItem.previousTaskId,
  422. taskOwner = tItem.taskOwner;
  423. var task = new dojox.gantt.GanttTaskItem({
  424. id: id,
  425. name: name,
  426. startTime: new Date(starttime[0], (parseInt(starttime[1]) - 1), starttime[2]),
  427. duration: duration,
  428. percentage: percentage,
  429. previousTaskId: previousTaskId,
  430. taskOwner: taskOwner
  431. });
  432. var ctItems = tItem.children;
  433. if(ctItems.length != 0){
  434. this.buildChildTasksData(task, ctItems);
  435. }
  436. project.addTask(task);
  437. }, this);
  438. this.addProject(project);
  439. }, this);
  440. },
  441. buildChildTasksData: function(parentTask, childTaskItems){
  442. childTaskItems && dojo.forEach(childTaskItems, function(ctItem){
  443. var id = ctItem.id,
  444. name = ctItem.name,
  445. starttime = ctItem.starttime.split("-"),
  446. duration = ctItem.duration,
  447. percentage = ctItem.percentage,
  448. previousTaskId = ctItem.previousTaskId,
  449. taskOwner = ctItem.taskOwner;
  450. var task = new dojox.gantt.GanttTaskItem({
  451. id: id,
  452. name: name,
  453. startTime: new Date(starttime[0], (parseInt(starttime[1]) - 1), starttime[2]),
  454. duration: duration,
  455. percentage: percentage,
  456. previousTaskId: previousTaskId,
  457. taskOwner: taskOwner
  458. });
  459. task.parentTask = parentTask;
  460. parentTask.addChildTask(task);
  461. var ctItems = ctItem.children;
  462. if(ctItems.length != 0){
  463. this.buildChildTasksData(task, ctItems);
  464. }
  465. }, this);
  466. },
  467. getJSONData: function(){
  468. var jsonObj = {identifier: 'id', items: []};
  469. dojo.forEach(this.project, function(proj){
  470. var project = {
  471. id: proj.id,
  472. name: proj.name,
  473. startdate: proj.startDate.getFullYear() + '-' + (proj.startDate.getMonth() + 1) + '-' + proj.startDate.getDate(),
  474. tasks: []
  475. };
  476. jsonObj.items.push(project);
  477. dojo.forEach(proj.parentTasks, function(pTask){
  478. var task = {
  479. id: pTask.id,
  480. name: pTask.name,
  481. starttime: pTask.startTime.getFullYear() + '-' + (pTask.startTime.getMonth() + 1) + '-' + pTask.startTime.getDate(),
  482. duration: pTask.duration,
  483. percentage: pTask.percentage,
  484. previousTaskId: (pTask.previousTaskId || ''),
  485. taskOwner: (pTask.taskOwner || ''),
  486. children: this.getChildTasksData(pTask.cldTasks)
  487. };
  488. project.tasks.push(task);
  489. }, this);
  490. }, this);
  491. return jsonObj;
  492. },
  493. getChildTasksData: function(childTasks){
  494. var cTaskObj = [];
  495. childTasks && childTasks.length > 0 && dojo.forEach(childTasks, function(childTask){
  496. var ctask = {
  497. id: childTask.id,
  498. name: childTask.name,
  499. starttime: childTask.startTime.getFullYear() + '-' + (childTask.startTime.getMonth() + 1) + '-' + childTask.startTime.getDate(),
  500. duration: childTask.duration,
  501. percentage: childTask.percentage,
  502. previousTaskId: (childTask.previousTaskId || ''),
  503. taskOwner: (childTask.taskOwner || ''),
  504. children: this.getChildTasksData(childTask.cldTasks)
  505. };
  506. cTaskObj.push(ctask);
  507. }, this);
  508. return cTaskObj;
  509. },
  510. saveJSONData: function(fileName){
  511. var _this = this;
  512. _this.dataFilePath = (fileName && dojo.trim(fileName).length > 0) ? fileName : this.dataFilePath;
  513. try {
  514. var td = dojo.xhrPost({
  515. url: _this.saveProgramPath,
  516. content: {filename: _this.dataFilePath, data: dojo.toJson(_this.getJSONData())},
  517. handle: function(res, ioArgs){
  518. if((dojo._isDocumentOk(ioArgs.xhr))||
  519. (ioArgs.xhr.status == 405)
  520. ){
  521. alert("Successfully! Saved data to " + _this.dataFilePath);
  522. }else{
  523. alert("Failed! Saved error");
  524. }
  525. }
  526. });
  527. } catch (e){
  528. alert("exception: " + e.message);
  529. }
  530. },
  531. sortTaskStartTime: function(a, b){
  532. return a.startTime < b.startTime ? -1 : (a.startTime > b.startTime ? 1 : 0);
  533. },
  534. sortProjStartDate: function(a, b){
  535. return a.startDate < b.startDate ? -1 : (a.startDate > b.startDate ? 1 : 0);
  536. },
  537. setStartTimeChild: function(parentTask){
  538. dojo.forEach(parentTask.cldTasks, function(pcTask){
  539. if(!pcTask.startTime){
  540. pcTask.startTime = parentTask.startTime;
  541. }
  542. if(pcTask.cldTasks.length != 0){
  543. this.setStartTimeChild(pcTask);
  544. }
  545. }, this);
  546. },
  547. createPanelTasks: function(){
  548. var panelTask = dojo.create("div", {
  549. className: "ganttTaskPanel"
  550. });
  551. dojo.style(panelTask, {
  552. height: (this.contentHeight - this.panelTimeHeight - this.scrollBarWidth) + "px"
  553. });
  554. return panelTask;
  555. },
  556. refreshParams: function(pixelsPerDay){
  557. this.pixelsPerDay = pixelsPerDay;
  558. this.pixelsPerWorkHour = this.pixelsPerDay / this.hsPerDay;
  559. this.pixelsPerHour = this.pixelsPerDay / 24;
  560. },
  561. createPanelNamesTasksHeader: function(){
  562. var _this = this;
  563. var panelHeader = dojo.create("div", {className: "ganttPanelHeader"});
  564. var tblHeader = dojo.create("table", {
  565. cellPadding: "0px",
  566. border: "0px",
  567. cellSpacing: "0px",
  568. bgColor: "#FFFFFF",
  569. className: "ganttToolbar"
  570. }, panelHeader);
  571. var firstRow = tblHeader.insertRow(tblHeader.rows.length);
  572. var secondRow = tblHeader.insertRow(tblHeader.rows.length);
  573. var thirdRow = tblHeader.insertRow(tblHeader.rows.length);
  574. var forthRow = tblHeader.insertRow(tblHeader.rows.length);
  575. var zoomIn = dojo.create("td", {
  576. align: "center",
  577. vAlign: "middle",
  578. className: "ganttToolbarZoomIn"
  579. }, firstRow);
  580. var zoomInFn = dojo.hitch(this, function(){
  581. if(this.scale * 2 > 5){return;}
  582. this.scale = this.scale * 2;
  583. this.switchTeleMicroView(this.pixelsPerDay * this.scale);
  584. });
  585. dojo.disconnect(this.zoomInClickEvent);
  586. this.zoomInClickEvent = dojo.connect(zoomIn, "onclick", this, zoomInFn);
  587. //a11y support
  588. dojo.disconnect(this.zoomInKeyEvent);
  589. this.zoomInKeyEvent = dojo.connect(zoomIn, "onkeydown", this, function(e){
  590. if(e.keyCode != dojo.keys.ENTER){return;}
  591. zoomInFn();
  592. });
  593. dojo.attr(zoomIn, "tabIndex", 0);
  594. var zoomOut = dojo.create("td", {
  595. align: "center",
  596. vAlign: "middle",
  597. className: "ganttToolbarZoomOut"
  598. }, firstRow);
  599. var zoomOutFn = dojo.hitch(this, function(){
  600. if(this.scale * 0.5 < 0.2){return;}
  601. this.scale = this.scale * 0.5;
  602. this.switchTeleMicroView(this.pixelsPerDay * this.scale);
  603. });
  604. dojo.disconnect(this.zoomOutClickEvent);
  605. this.zoomOutClickEvent = dojo.connect(zoomOut, "onclick", this, zoomOutFn);
  606. //a11y support
  607. dojo.disconnect(this.zoomOutKeyEvent);
  608. this.zoomOutKeyEvent = dojo.connect(zoomOut, "onkeydown", this, function(e){
  609. if(e.keyCode != dojo.keys.ENTER){return;}
  610. zoomOutFn();
  611. });
  612. dojo.attr(zoomOut, "tabIndex", 0);
  613. var micro = dojo.create("td", {
  614. align: "center",
  615. vAlign: "middle",
  616. className: "ganttToolbarMicro"
  617. }, secondRow);
  618. dojo.disconnect(this.microClickEvent);
  619. this.microClickEvent = dojo.connect(micro, "onclick", this, dojo.hitch(this, this.refresh, this.animation?15:1, 0, 2));
  620. //a11y support
  621. dojo.disconnect(this.microKeyEvent);
  622. this.microKeyEvent = dojo.connect(micro, "onkeydown", this, function(e){
  623. if(e.keyCode != dojo.keys.ENTER){return;}
  624. micro.blur();
  625. this.refresh(this.animation?15:1, 0, 2);
  626. });
  627. dojo.attr(micro, "tabIndex", 0);
  628. var tele = dojo.create("td", {
  629. align: "center",
  630. vAlign: "middle",
  631. className: "ganttToolbarTele"
  632. }, secondRow);
  633. dojo.disconnect(this.teleClickEvent);
  634. this.teleClickEvent = dojo.connect(tele, "onclick", this, dojo.hitch(this, this.refresh, this.animation?15:1, 0, 0.5));
  635. //a11y support
  636. dojo.disconnect(this.teleKeyEvent);
  637. this.teleKeyEvent = dojo.connect(tele, "onkeydown", this, function(e){
  638. if(e.keyCode != dojo.keys.ENTER){return;}
  639. tele.blur();
  640. this.refresh(this.animation?15:1, 0, 0.5);
  641. });
  642. dojo.attr(tele, "tabIndex", 0);
  643. var save = dojo.create("td", {
  644. align: "center",
  645. vAlign: "middle",
  646. className: "ganttToolbarSave"
  647. }, thirdRow);
  648. dojo.disconnect(this.saveClickEvent);
  649. this.saveClickEvent = dojo.connect(save, "onclick", this, dojo.hitch(this, this.saveJSONData, ""));
  650. //a11y support
  651. dojo.disconnect(this.saveKeyEvent);
  652. this.saveKeyEvent = dojo.connect(save, "onkeydown", this, function(e){
  653. if(e.keyCode != dojo.keys.ENTER){return;}
  654. this.saveJSONData("");
  655. });
  656. dojo.attr(save, "tabIndex", 0);
  657. var load = dojo.create("td", {
  658. align: "center",
  659. vAlign: "middle",
  660. className: "ganttToolbarLoad"
  661. }, thirdRow);
  662. dojo.disconnect(this.loadClickEvent);
  663. this.loadClickEvent = dojo.connect(load, "onclick", this, dojo.hitch(this, this.loadJSONData, ""));
  664. //a11y support
  665. dojo.disconnect(this.loadKeyEvent);
  666. this.loadKeyEvent = dojo.connect(load, "onkeydown", this, function(e){
  667. if(e.keyCode != dojo.keys.ENTER){return;}
  668. this.loadJSONData("");
  669. });
  670. dojo.attr(load, "tabIndex", 0);
  671. //action popup description
  672. var actions = [zoomIn, zoomOut, micro, tele, save, load],
  673. titles = ["Enlarge timeline", "Shrink timeline", "Zoom in time zone(microscope view)", "Zoom out time zone(telescope view)",
  674. "Save gantt data to json file", "Load gantt data from json file"];
  675. dojo.forEach(actions, function(action, i){
  676. var title = titles[i];
  677. var tooltipShow = function(){
  678. dojo.addClass(action, "ganttToolbarActionHover");
  679. dijit.showTooltip(title, action, ["above", "below"]);
  680. };
  681. action.onmouseover = tooltipShow;
  682. //a11y support
  683. action.onfocus = tooltipShow;
  684. var tooltipHide = function(){
  685. dojo.removeClass(action, "ganttToolbarActionHover");
  686. action && dijit.hideTooltip(action);
  687. };
  688. action.onmouseout = tooltipHide;
  689. action.onblur = tooltipHide;
  690. }, this);
  691. return panelHeader;
  692. },
  693. createPanelNamesTasks: function(){
  694. var panelNameTask = dojo.create("div", {
  695. innerHTML: "&nbsp;",
  696. className: "ganttPanelNames"
  697. });
  698. dojo.style(panelNameTask, {
  699. height: (this.contentHeight - this.panelTimeHeight - this.scrollBarWidth) + "px",
  700. width: this.maxWidthPanelNames + "px"
  701. });
  702. return panelNameTask;
  703. },
  704. createPanelTime: function(){
  705. var panelTime = dojo.create("div", {className: "ganttPanelTime"});
  706. var tblTime = dojo.create("table", {
  707. cellPadding: "0px",
  708. border: "0px",
  709. cellSpacing: "0px",
  710. bgColor: "#FFFFFF",
  711. className: "ganttTblTime"
  712. }, panelTime);
  713. this.totalDays = this.countDays;
  714. //year
  715. var newYearRow = tblTime.insertRow(tblTime.rows.length), newYear = oldYear = new Date(this.startDate).getFullYear(), ycount = 0;
  716. for(var i = 0; i < this.countDays; i++, ycount++){
  717. var date = new Date(this.startDate);
  718. date.setDate(date.getDate() + i);
  719. newYear = date.getFullYear();
  720. if(newYear != oldYear){
  721. this.addYearInPanelTime(newYearRow, ycount, oldYear);
  722. ycount = 0;
  723. oldYear = newYear;
  724. }
  725. }
  726. this.addYearInPanelTime(newYearRow, ycount, newYear);
  727. dojo.style(newYearRow, "display", "none");
  728. //month
  729. var newMonthRow = tblTime.insertRow(tblTime.rows.length), newMonth = oldMonth = new Date(this.startDate).getMonth(), mcount = 0, lastYear = 1970;
  730. for(var i = 0; i < this.countDays; i++, mcount++){
  731. var date = new Date(this.startDate);
  732. date.setDate(date.getDate() + i);
  733. newMonth = date.getMonth();
  734. lastYear = date.getFullYear();
  735. if(newMonth != oldMonth){
  736. this.addMonthInPanelTime(newMonthRow, mcount, oldMonth, lastYear);
  737. mcount = 0;
  738. oldMonth = newMonth;
  739. }
  740. }
  741. this.addMonthInPanelTime(newMonthRow, mcount, newMonth, lastYear);
  742. //week
  743. var newWeekRow = tblTime.insertRow(tblTime.rows.length), newWeek = oldWeek = dojo.date.locale._getWeekOfYear(new Date(this.startDate)), mcount = 0;
  744. for(var i = 0; i < this.countDays; i++, mcount++){
  745. var date = new Date(this.startDate);
  746. date.setDate(date.getDate() + i);
  747. newWeek = dojo.date.locale._getWeekOfYear(date);
  748. if(newWeek != oldWeek){
  749. this.addWeekInPanelTime(newWeekRow, mcount, oldWeek);
  750. mcount = 0;
  751. oldWeek = newWeek;
  752. }
  753. }
  754. this.addWeekInPanelTime(newWeekRow, mcount, newWeek);
  755. //day
  756. var newDayRow = tblTime.insertRow(tblTime.rows.length);
  757. for(var i = 0; i < this.countDays; i++){
  758. this.addDayInPanelTime(newDayRow);
  759. }
  760. //hour
  761. var newHourRow = tblTime.insertRow(tblTime.rows.length);
  762. for(var i = 0; i < this.countDays; i++){
  763. this.addHourInPanelTime(newHourRow);
  764. }
  765. dojo.style(newHourRow, "display", "none");
  766. return panelTime;
  767. },
  768. adjustPanelTime: function(width){
  769. var maxEndPos = dojo.map(this.arrProjects, function(project){
  770. return (parseInt(project.projectItem[0].style.left) + parseInt(project.projectItem[0].firstChild.style.width)
  771. + project.descrProject.offsetWidth + this.panelTimeExpandDelta);
  772. }, this).sort(function(a,b){return b-a})[0];
  773. if(this.maxTaskEndPos != maxEndPos){
  774. //reset panel time
  775. var prows = this.panelTime.firstChild.firstChild.rows;
  776. for(var i = 0; i <= 4; i++){//prows.length
  777. this.removeCell(prows[i]);
  778. };
  779. var countDays = Math.round((maxEndPos+this.panelTimeExpandDelta) / this.pixelsPerDay);
  780. this.totalDays = countDays;
  781. //year
  782. var newYear = oldYear = new Date(this.startDate).getFullYear(), ycount = 0;
  783. for(var i = 0; i < countDays; i++, ycount++){
  784. var date = new Date(this.startDate);
  785. date.setDate(date.getDate() + i);
  786. newYear = date.getFullYear();
  787. if(newYear != oldYear){
  788. this.addYearInPanelTime(prows[0], ycount, oldYear);
  789. ycount = 0;
  790. oldYear = newYear;
  791. }
  792. }
  793. this.addYearInPanelTime(prows[0], ycount, newYear);
  794. //month
  795. var newMonth = oldMonth = new Date(this.startDate).getMonth(), mcount = 0, lastYear = 1970;
  796. for(var i = 0; i < countDays; i++, mcount++){
  797. var date = new Date(this.startDate);
  798. date.setDate(date.getDate() + i);
  799. newMonth = date.getMonth();
  800. lastYear = date.getFullYear();
  801. if(newMonth != oldMonth){
  802. this.addMonthInPanelTime(prows[1], mcount, oldMonth, lastYear);
  803. mcount = 0;
  804. oldMonth = newMonth;
  805. }
  806. }
  807. this.addMonthInPanelTime(prows[1], mcount, newMonth, lastYear);
  808. //week
  809. var newWeek = oldWeek = dojo.date.locale._getWeekOfYear(new Date(this.startDate)), mcount = 0;
  810. for(var i = 0; i < countDays; i++, mcount++){
  811. var date = new Date(this.startDate);
  812. date.setDate(date.getDate() + i);
  813. newWeek = dojo.date.locale._getWeekOfYear(date);
  814. if(newWeek != oldWeek){
  815. this.addWeekInPanelTime(prows[2], mcount, oldWeek);
  816. mcount = 0;
  817. oldWeek = newWeek;
  818. }
  819. }
  820. this.addWeekInPanelTime(prows[2], mcount, newWeek);
  821. //day
  822. for(var i = 0; i < countDays; i++){
  823. this.addDayInPanelTime(prows[3]);
  824. }
  825. //hour
  826. for(var i = 0; i < countDays; i++){
  827. this.addHourInPanelTime(prows[4]);
  828. }
  829. this.panelTime.firstChild.firstChild.style.width = this.pixelsPerDay * (prows[3].cells.length) + "px";
  830. this.contentData.firstChild.style.width = this.pixelsPerDay * (prows[3].cells.length) + "px";
  831. this.maxTaskEndPos = maxEndPos;
  832. }
  833. },
  834. addYearInPanelTime: function(row, count, year){
  835. var data = "Year " + year;
  836. var newCell = dojo.create("td", {
  837. colSpan: count,
  838. align: "center",
  839. vAlign: "middle",
  840. className: "ganttYearNumber",
  841. innerHTML: this.pixelsPerDay * count > 20 ? data : "",
  842. innerHTMLData: data
  843. }, row);
  844. dojo.style(newCell, "width", (this.pixelsPerDay * count) + "px");
  845. },
  846. addMonthInPanelTime: function(row, count, month, year){
  847. var data = this.months[month] + (year ? " of " + year : "");
  848. var newCell = dojo.create("td", {
  849. colSpan: count,
  850. align: "center",
  851. vAlign: "middle",
  852. className: "ganttMonthNumber",
  853. innerHTML: this.pixelsPerDay * count > 30 ? data : "",
  854. innerHTMLData: data
  855. }, row);
  856. dojo.style(newCell, "width", (this.pixelsPerDay * count) + "px");
  857. },
  858. addWeekInPanelTime: function(row, count, week){
  859. var data = "Week " + week;
  860. var newCell = dojo.create("td", {
  861. colSpan: count,
  862. align: "center",
  863. vAlign: "middle",
  864. className: "ganttWeekNumber",
  865. innerHTML: this.pixelsPerDay * count > 20 ? data : "",
  866. innerHTMLData: data
  867. }, row);
  868. dojo.style(newCell, "width", (this.pixelsPerDay * count) + "px");
  869. },
  870. addDayInPanelTime: function(row){
  871. var date = new Date(this.startDate);
  872. date.setDate(date.getDate() + parseInt(row.cells.length));
  873. var newCell = dojo.create("td", {
  874. align: "center",
  875. vAlign: "middle",
  876. className: "ganttDayNumber",
  877. innerHTML: this.pixelsPerDay > 20 ? date.getDate() : "",
  878. innerHTMLData: String(date.getDate()),
  879. data: row.cells.length
  880. }, row);
  881. dojo.style(newCell, "width", this.pixelsPerDay + "px");
  882. (date.getDay() >= 5) && dojo.addClass(newCell, "ganttDayNumberWeekend");
  883. this._events.push(
  884. dojo.connect(newCell, "onmouseover", this, function(event){
  885. var dayTime = event.target || event.srcElement;
  886. var date = new Date(this.startDate.getTime());
  887. date.setDate(date.getDate() + parseInt(dojo.attr(dayTime, "data")));
  888. dijit.showTooltip(date.getFullYear() + "." + (date.getMonth() + 1) + "." + date.getDate(), newCell, ["above", "below"]);
  889. })
  890. );
  891. this._events.push(
  892. dojo.connect(newCell, "onmouseout", this, function(event){
  893. var dayTime = event.target || event.srcElement;
  894. dayTime && dijit.hideTooltip(dayTime);
  895. })
  896. );
  897. },
  898. addHourInPanelTime: function(row){
  899. var newCell = dojo.create("td", {
  900. align: "center",
  901. vAlign: "middle",
  902. className: "ganttHourNumber",
  903. data: row.cells.length
  904. }, row);
  905. dojo.style(newCell, "width", this.pixelsPerDay + "px");
  906. var hourTable = dojo.create("table", {
  907. cellPadding: "0",
  908. cellSpacing: "0"
  909. }, newCell);
  910. var newRow = hourTable.insertRow(hourTable.rows.length);
  911. for(var i = 0; i < this.hsPerDay; i++){
  912. var hourTD = dojo.create("td", {
  913. className: "ganttHourClass"
  914. }, newRow);
  915. dojo.style(hourTD, "width", (this.pixelsPerDay / this.hsPerDay) + "px");
  916. dojo.attr(hourTD, "innerHTMLData", String(9 + i));
  917. if(this.pixelsPerDay / this.hsPerDay > 5){
  918. dojo.attr(hourTD, "innerHTML", String(9 + i));
  919. }
  920. dojo.addClass(hourTD, i <= 3?"ganttHourNumberAM":"ganttHourNumberPM");
  921. }
  922. },
  923. incHeightPanelTasks: function(height){
  924. var containerTasks = this.contentData.firstChild;
  925. containerTasks.style.height = parseInt(containerTasks.style.height) + height + "px";
  926. },
  927. incHeightPanelNames: function(height){
  928. var containerNames = this.panelNames.firstChild;
  929. containerNames.style.height = parseInt(containerNames.style.height) + height + "px";
  930. },
  931. checkPosition: function(){
  932. dojo.forEach(this.arrProjects, function(project){
  933. dojo.forEach(project.arrTasks, function(task){
  934. task.checkPosition();
  935. }, this);
  936. }, this);
  937. },
  938. checkHeighPanelTasks: function(){
  939. this.contentDataHeight += this.heightTaskItemExtra + this.heightTaskItem;
  940. if((parseInt(this.contentData.firstChild.style.height) <= this.contentDataHeight)){
  941. this.incHeightPanelTasks(this.heightTaskItem + this.heightTaskItemExtra);
  942. this.incHeightPanelNames(this.heightTaskItem + this.heightTaskItemExtra);
  943. }
  944. },
  945. sortTasksByStartTime: function(project){
  946. project.parentTasks.sort(this.sortTaskStartTime);
  947. for(var i = 0; i < project.parentTasks.length; i++){
  948. project.parentTasks[i] = this.sortChildTasks(project.parentTasks[i]);
  949. }
  950. },
  951. sortChildTasks: function(parenttask){
  952. parenttask.cldTasks.sort(this.sortTaskStartTime);
  953. for(var i = 0; i < parenttask.cldTasks.length; i++){
  954. if(parenttask.cldTasks[i].cldTasks.length > 0) this.sortChildTasks(parenttask.cldTasks[i]);
  955. }
  956. return parenttask;
  957. },
  958. refresh: function(count, current, multi){
  959. //return if no task items
  960. if(this.arrProjects.length <= 0){return;}
  961. if(this.arrProjects[0].arrTasks.length <= 0){return;}
  962. //Show panel of names
  963. if(!count || current > count){
  964. this.refreshController();
  965. if(this.resource){
  966. this.resource.refresh();
  967. }
  968. this.tempDayInPixels = 0;
  969. this.panelNameHeadersCover && dojo.style(this.panelNameHeadersCover, "display", "none");
  970. return;
  971. }
  972. if(this.tempDayInPixels == 0){
  973. this.tempDayInPixels = this.pixelsPerDay;
  974. }
  975. this.panelNameHeadersCover && dojo.style(this.panelNameHeadersCover, "display", "");
  976. var dip = this.tempDayInPixels + this.tempDayInPixels * (multi - 1) * Math.pow((current / count), 2);
  977. this.refreshParams(dip);
  978. dojo.forEach(this.arrProjects, function(project){
  979. dojo.forEach(project.arrTasks, function(task){
  980. task.refresh();
  981. }, this);
  982. project.refresh();
  983. }, this);
  984. setTimeout(dojo.hitch(this, function(){
  985. this.refresh(count, ++current, multi);
  986. }), 15);
  987. },
  988. switchTeleMicroView: function(dip){
  989. var plChild = this.panelTime.firstChild.firstChild;
  990. for(var i = 0; i < 5; i++){//0:Y 1:M 2:W 3:D 4:H
  991. if(dip > 40){
  992. dojo.style(plChild.rows[i], "display", (i==0||i==1)?"none":"");
  993. }else if(dip < 20){
  994. dojo.style(plChild.rows[i], "display", (i==2||i==4)?"none":"");
  995. }else{
  996. dojo.style(plChild.rows[i], "display", (i==0||i==4)?"none":"");
  997. }
  998. }
  999. },
  1000. refreshController: function(){
  1001. this.contentData.firstChild.style.width = Math.max(1200, this.pixelsPerDay * this.totalDays) + "px";
  1002. this.panelTime.firstChild.style.width = this.pixelsPerDay * this.totalDays + "px";
  1003. this.panelTime.firstChild.firstChild.style.width = this.pixelsPerDay * this.totalDays + "px";
  1004. this.switchTeleMicroView(this.pixelsPerDay);
  1005. dojo.forEach(this.panelTime.firstChild.firstChild.rows, function(row){
  1006. dojo.forEach(row.childNodes, function(td){
  1007. var cs = parseInt(dojo.attr(td, "colSpan") || 1);
  1008. var idata = dojo.trim(dojo.attr(td, "innerHTMLData")||"");
  1009. if(idata.length > 0){
  1010. dojo.attr(td, "innerHTML", this.pixelsPerDay * cs < 20 ? "" : idata);
  1011. }else{
  1012. dojo.forEach(td.firstChild.rows[0].childNodes, function(td){
  1013. var sdata = dojo.trim(dojo.attr(td, "innerHTMLData")||"");
  1014. dojo.attr(td, "innerHTML", this.pixelsPerDay / this.hsPerDay > 10 ? sdata : "");
  1015. }, this);
  1016. }
  1017. if(cs == 1){
  1018. dojo.style(td, "width", (this.pixelsPerDay*cs) + "px");
  1019. if(idata.length <= 0){
  1020. dojo.forEach(td.firstChild.rows[0].childNodes, function(td){
  1021. dojo.style(td, "width", (this.pixelsPerDay*cs / this.hsPerDay) + "px");
  1022. }, this);
  1023. }
  1024. }
  1025. }, this);
  1026. }, this);
  1027. },
  1028. init: function(){
  1029. this.startDate = this.getStartDate();
  1030. dojo.style(this.content, {
  1031. width: this.contentWidth + "px",
  1032. height: this.contentHeight + "px"
  1033. });
  1034. //create Table
  1035. this.tableControl = dojo.create("table", {
  1036. cellPadding: "0",
  1037. cellSpacing: "0",
  1038. className: "ganttTabelControl"
  1039. });
  1040. var newRowTblControl = this.tableControl.insertRow(this.tableControl.rows.length);
  1041. //Add to content Table
  1042. this.content.appendChild(this.tableControl);
  1043. this.countDays = this.getCountDays();
  1044. //Creation panel of time
  1045. this.panelTime = dojo.create("div", {className: "ganttPanelTimeContainer"});
  1046. dojo.style(this.panelTime, "height", this.panelTimeHeight + "px");
  1047. this.panelTime.appendChild(this.createPanelTime());
  1048. //Creation panel contentData
  1049. this.contentData = dojo.create("div", {className: "ganttContentDataContainer"});
  1050. dojo.style(this.contentData, "height", (this.contentHeight - this.panelTimeHeight) + "px");
  1051. this.contentData.appendChild(this.createPanelTasks());
  1052. //Creation panel of names
  1053. var newCellTblControl = dojo.create("td", {
  1054. vAlign: "top"
  1055. });
  1056. //Creation panel of task header
  1057. this.panelNameHeaders = dojo.create("div", {className: "ganttPanelNameHeaders"}, newCellTblControl);
  1058. dojo.style(this.panelNameHeaders, {
  1059. height: this.panelTimeHeight + "px",
  1060. width: this.maxWidthPanelNames + "px"
  1061. });
  1062. this.panelNameHeaders.appendChild(this.createPanelNamesTasksHeader());
  1063. this.panelNames = dojo.create("div", {className: "ganttPanelNamesContainer"}, newCellTblControl);
  1064. this.panelNames.appendChild(this.createPanelNamesTasks());
  1065. newRowTblControl.appendChild(newCellTblControl);
  1066. //add to control contentData and dataTime
  1067. newCellTblControl = dojo.create("td", {
  1068. vAlign: "top"
  1069. });
  1070. var divCell = dojo.create("div", {className: "ganttDivCell"});
  1071. divCell.appendChild(this.panelTime);
  1072. divCell.appendChild(this.contentData);
  1073. newCellTblControl.appendChild(divCell);
  1074. newRowTblControl.appendChild(newCellTblControl);
  1075. //Show panel of names
  1076. dojo.style(this.panelNames, "height", (this.contentHeight - this.panelTimeHeight - this.scrollBarWidth) + "px");
  1077. dojo.style(this.panelNames, "width", this.maxWidthPanelNames + "px");
  1078. dojo.style(this.contentData, "width", (this.contentWidth - this.maxWidthPanelNames) + "px");
  1079. dojo.style(this.contentData.firstChild, "width", this.pixelsPerDay * this.countDays + "px");
  1080. dojo.style(this.panelTime, "width", (this.contentWidth - this.maxWidthPanelNames - this.scrollBarWidth) + "px");
  1081. dojo.style(this.panelTime.firstChild, "width", this.pixelsPerDay * this.countDays + "px");
  1082. if(this.isShowConMenu){
  1083. this.tabMenu = new dojox.gantt.TabMenu(this);
  1084. }
  1085. var _this = this;
  1086. this.contentData.onscroll = function(){
  1087. _this.panelTime.scrollLeft = this.scrollLeft;
  1088. if(_this.panelNames){
  1089. _this.panelNames.scrollTop = this.scrollTop;
  1090. if(_this.isShowConMenu){
  1091. _this.tabMenu.hide();
  1092. }
  1093. }
  1094. if(_this.resource){
  1095. _this.resource.contentData.scrollLeft = this.scrollLeft;
  1096. }
  1097. }
  1098. this.project.sort(this.sortProjStartDate);
  1099. for(var i = 0; i < this.project.length; i++){
  1100. var proj = this.project[i];
  1101. for(var k = 0; k < proj.parentTasks.length; k++){
  1102. var ppTask = proj.parentTasks[k];
  1103. if(!ppTask.startTime){
  1104. ppTask.startTime = proj.startDate;
  1105. }
  1106. this.setStartTimeChild(ppTask);
  1107. if(this.setPreviousTask(proj)){
  1108. return;
  1109. }
  1110. }
  1111. for(var k = 0; k < proj.parentTasks.length; k++){
  1112. var ppTask = proj.parentTasks[k];
  1113. if(ppTask.startTime < proj.startDate){
  1114. if(!this.correctError){
  1115. return;
  1116. }else{
  1117. ppTask.startTime = proj.startDate;
  1118. }
  1119. }
  1120. if(this.checkPosParentTaskInTree(ppTask)){
  1121. return;
  1122. }
  1123. }
  1124. this.sortTasksByStartTime(proj);
  1125. }
  1126. for(var i = 0; i < this.project.length; i++){
  1127. //creation project
  1128. var proj = this.project[i];
  1129. var project = new dojox.gantt.GanttProjectControl(this, proj);
  1130. if(this.arrProjects.length > 0){
  1131. var previousProject = this.arrProjects[this.arrProjects.length - 1];
  1132. project.previousProject = previousProject;
  1133. previousProject.nextProject = project;
  1134. }
  1135. project.create();
  1136. this.checkHeighPanelTasks();
  1137. this.arrProjects.push(project);
  1138. this.createTasks(project);
  1139. }
  1140. if(this.withResource){
  1141. this.resource = new dojox.gantt.GanttResourceItem(this);
  1142. this.resource.create();
  1143. }
  1144. this.postLoadData();
  1145. this.postBindEvents();
  1146. return this;
  1147. },
  1148. postLoadData: function(){
  1149. dojo.forEach(this.arrProjects, function(project){
  1150. dojo.forEach(project.arrTasks, function(task){
  1151. task.postLoadData();
  1152. }, this);
  1153. project.postLoadData();
  1154. }, this);
  1155. //toolbar cover div
  1156. var cpos = dojo.coords(this.panelNameHeaders);
  1157. if(!this.panelNameHeadersCover){
  1158. this.panelNameHeadersCover = dojo.create("div", {className: "ganttHeaderCover"}, this.panelNameHeaders.parentNode);
  1159. dojo.style(this.panelNameHeadersCover, {
  1160. left: cpos.l+"px",
  1161. top: cpos.t+"px",
  1162. height: cpos.h+"px",
  1163. width: cpos.w+"px",
  1164. display: "none"
  1165. });
  1166. }
  1167. },
  1168. postBindEvents: function(){
  1169. //highlight row
  1170. var pos = dojo.position(this.tableControl, true);
  1171. !dojo.isIE && this._events.push(
  1172. dojo.connect(this.tableControl, "onmousemove", this, function(event){
  1173. var elem = event.srcElement || event.target;
  1174. if(elem == this.panelNames.firstChild || elem == this.contentData.firstChild){
  1175. //23: this.heightTaskItem + this.heightTaskItemExtra
  1176. var rowHeight = this.heightTaskItem + this.heightTaskItemExtra;
  1177. var hlTop = parseInt(event.layerY / rowHeight) * rowHeight + this.panelTimeHeight - this.contentData.scrollTop;
  1178. if(hlTop != this.oldHLTop && hlTop < (pos.h - 50)){
  1179. if(this.highLightDiv){
  1180. dojo.style(this.highLightDiv, "top", (pos.y + hlTop) + "px");
  1181. }else{
  1182. this.highLightDiv = dojo.create("div", {
  1183. className: "ganttRowHighlight"
  1184. }, dojo.body());
  1185. dojo.style(this.highLightDiv, {
  1186. top: (pos.y + hlTop) + "px",
  1187. left: pos.x + "px",
  1188. width: (pos.w - 20) + "px",
  1189. height: rowHeight + "px"
  1190. });
  1191. }
  1192. }
  1193. this.oldHLTop = hlTop;
  1194. }
  1195. })
  1196. );
  1197. //TODO: other event bindings
  1198. },
  1199. getStartDate: function(){
  1200. dojo.forEach(this.project, function(proj){
  1201. if(this.startDate){
  1202. if(proj.startDate < this.startDate){
  1203. this.startDate = new Date(proj.startDate);
  1204. }
  1205. }else{
  1206. this.startDate = new Date(proj.startDate);
  1207. }
  1208. }, this);
  1209. this.initialPos = 24 * this.pixelsPerHour;
  1210. return this.startDate ? new Date(this.startDate.setHours(this.startDate.getHours() - 24)) : new Date();
  1211. },
  1212. getCountDays: function(){
  1213. return parseInt((this.contentWidth - this.maxWidthPanelNames) / (this.pixelsPerHour * 24));
  1214. },
  1215. createTasks: function(project){
  1216. dojo.forEach(project.project.parentTasks, function(pppTask, i){
  1217. if(i > 0){
  1218. project.project.parentTasks[i - 1].nextParentTask = pppTask;
  1219. pppTask.previousParentTask = project.project.parentTasks[i - 1];
  1220. }
  1221. var task = new dojox.gantt.GanttTaskControl(pppTask, project, this);
  1222. project.arrTasks.push(task);
  1223. task.create();
  1224. this.checkHeighPanelTasks();
  1225. if(pppTask.cldTasks.length > 0){
  1226. this.createChildItemControls(pppTask.cldTasks, project);
  1227. }
  1228. }, this);
  1229. },
  1230. createChildItemControls: function(arrChildTasks, project){
  1231. arrChildTasks && dojo.forEach(arrChildTasks, function(cTask, i){
  1232. if(i > 0){
  1233. cTask.previousChildTask = arrChildTasks[i - 1];
  1234. arrChildTasks[i - 1].nextChildTask = cTask;
  1235. }
  1236. var task = new dojox.gantt.GanttTaskControl(cTask, project, this);
  1237. task.create();
  1238. this.checkHeighPanelTasks();
  1239. if(cTask.cldTasks.length > 0){
  1240. this.createChildItemControls(cTask.cldTasks, project);
  1241. }
  1242. }, this);
  1243. },
  1244. getPosOnDate: function(startTime){
  1245. return (startTime - this.startDate) / (60 * 60 * 1000) * this.pixelsPerHour;
  1246. },
  1247. getWidthOnDuration: function(duration){
  1248. return Math.round(this.pixelsPerWorkHour * duration);
  1249. },
  1250. getLastChildTask: function(task){
  1251. return task.childTask.length > 0 ? this.getLastChildTask(task.childTask[task.childTask.length - 1]) : task;
  1252. },
  1253. removeCell: function(row){
  1254. while(row.cells[0]){
  1255. row.deleteCell(row.cells[0]);
  1256. }
  1257. }
  1258. });
  1259. })();
  1260. }