GanttChart.js 44 KB

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