Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

557 linhas
17 KiB

  1. /*
  2. Copyright (c) 2012-2017 Open Lab
  3. Written by Roberto Bicchierai and Silvia Chelazzi http://roberto.open-lab.com
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. $.gridify = function (table, opt) {
  22. var options = {
  23. resizeZoneWidth: 10
  24. };
  25. $.extend(options, opt);
  26. var box = $("<div>").addClass("gdfWrapper");
  27. box.append(table);
  28. var head = table.clone();
  29. head.addClass("table ganttFixHead");
  30. //remove non head
  31. head.find("tbody").remove();
  32. box.append(head);
  33. box.append(table);
  34. var hTh = head.find(".gdfColHeader");
  35. var cTh = table.find(".gdfColHeader");
  36. for (var i = 0; i < hTh.length; i++) {
  37. hTh.eq(i).data("fTh", cTh.eq(i));
  38. }
  39. //--------- set table to 0 to prevent a strange 100%
  40. table.width(0);
  41. head.width(0);
  42. //---------------------- header management start
  43. head.find("th.gdfColHeader:not(.gdfied)").mouseover(function () {
  44. $(this).addClass("gdfColHeaderOver");
  45. }).on("mouseout.gdf", function () {
  46. $(this).removeClass("gdfColHeaderOver");
  47. if (!$.gridify.columInResize) {
  48. $("body").removeClass("gdfHResizing");
  49. }
  50. }).on("mousemove.gdf", function (e) {
  51. if (!$.gridify.columInResize) {
  52. var colHeader = $(this);
  53. var nextCol = colHeader.next();
  54. if (nextCol.length > 0 && nextCol.width() < options.resizeZoneWidth)
  55. colHeader = nextCol;
  56. if (!colHeader.is(".gdfResizable"))
  57. return;
  58. var mousePos = e.pageX - colHeader.offset().left;
  59. if (colHeader.width() - mousePos < options.resizeZoneWidth) {
  60. $("body").addClass("gdfHResizing");
  61. } else {
  62. $("body").removeClass("gdfHResizing");
  63. }
  64. }
  65. }).on("mousedown.gdf", function (e) {
  66. //console.debug("mousedown.gdf")
  67. var colHeader = $(this);
  68. var nextCol = colHeader.next();
  69. if (nextCol.length > 0 && nextCol.width() < options.resizeZoneWidth)
  70. colHeader = nextCol;
  71. if (!colHeader.is(".gdfResizable"))
  72. return;
  73. var mousePos = e.pageX - colHeader.offset().left;
  74. if (colHeader.width() - mousePos < options.resizeZoneWidth) {
  75. $("body").unselectable();
  76. $.gridify.columInResize = colHeader;
  77. //on event for start resizing
  78. //console.debug("start resizing");
  79. $(document).on("mousemove.gdf", function (e) {
  80. e.preventDefault();
  81. $("body").addClass("gdfHResizing");
  82. //manage resizing
  83. var w = e.pageX - $.gridify.columInResize.offset().left;
  84. w = w <= 1 ? 1 : w;
  85. $.gridify.columInResize.width(w);
  86. $.gridify.columInResize.data("fTh").width(w);
  87. //on mouse up on body to stop resizing
  88. }).on("mouseup.gdf", function () {
  89. //console.debug("mouseup.gdf")
  90. //$("body").css({cursor: "auto"});
  91. $(this).off("mousemove.gdf").off("mouseup.gdf").clearunselectable();
  92. $("body").removeClass("gdfHResizing");
  93. delete $.gridify.columInResize;
  94. //save columns dimension
  95. storeGridState();
  96. });
  97. }
  98. }).on("dblclick.gdf", function () {
  99. //console.debug("dblclick.gdf")
  100. var col = $(this);
  101. if (!col.is(".gdfResizable"))
  102. return;
  103. var idx = $("th", col.parents("table")).index(col);
  104. var columnTd = $("td:nth-child(" + (idx + 1) + ")", table);
  105. var w = 0;
  106. columnTd.each(function () {
  107. var td = $(this);
  108. var content = td.children("input").length ? td.children("input").val() : td.html();
  109. var tmp = $("<div/>").addClass("columnWidthTest").html(content).css({position: "absolute"});
  110. $("body").append(tmp);
  111. w = Math.max(w, tmp.width() + parseFloat(td.css("padding-left")));
  112. tmp.remove();
  113. });
  114. w = w + 5;
  115. col.width(w);
  116. col.data("fTh").width(w);
  117. //save columns dimension
  118. storeGridState();
  119. return false;
  120. }).addClass("gdfied unselectable").attr("unselectable", "true");
  121. function storeGridState() {
  122. //console.debug("storeGridState");
  123. if (localStorage) {
  124. var gridState = {};
  125. var colSizes = [];
  126. $(".gdfTable .gdfColHeader").each(function () {
  127. colSizes.push($(this).outerWidth());
  128. });
  129. gridState.colSizes = colSizes;
  130. localStorage.setObject("TWPGanttGridState", gridState);
  131. //console.debug("gridState",localStorage.getItem("TWPGanttGridState"));
  132. }
  133. }
  134. function loadGridState() {
  135. //console.debug("loadGridState")
  136. if (localStorage) {
  137. if (localStorage.getObject("TWPGanttGridState")) {
  138. var gridState = localStorage.getObject("TWPGanttGridState");
  139. if (gridState.colSizes) {
  140. box.find(".gdfTable .gdfColHeader").each(function (i) {
  141. $(this).width(gridState.colSizes[i]);
  142. });
  143. }
  144. }
  145. }
  146. }
  147. loadGridState();
  148. return box;
  149. };
  150. $.splittify = {
  151. init: function (where, first, second, perc) {
  152. //perc = perc || 50;
  153. var element = $("<div>").addClass("splitterContainer");
  154. var firstBox = $("<div>").addClass("splitElement splitBox1");
  155. var splitterBar = $("<div>").addClass("splitElement vSplitBar").attr("unselectable", "on").css("padding-top", where.height() / 2 + "px");
  156. var secondBox = $("<div>").addClass("splitElement splitBox2");
  157. var splitter = new Splitter(element, firstBox, secondBox, splitterBar);
  158. splitter.perc = perc;
  159. //override with saved one
  160. loadPosition();
  161. var toLeft = $("<div>").addClass("toLeft").html("{").click(function () {splitter.resize(0.001, 300);});
  162. splitterBar.append(toLeft);
  163. var toCenter = $("<div>").addClass("toCenter").html("&#xa9;").click(function () {splitter.resize(50, 300);});
  164. splitterBar.append(toCenter);
  165. var toRight = $("<div>").addClass("toRight").html("}").click(function () {splitter.resize(99.9999, 300);});
  166. splitterBar.append(toRight);
  167. firstBox.append(first);
  168. secondBox.append(second);
  169. element.append(firstBox).append(secondBox).append(splitterBar);
  170. where.append(element);
  171. var totalW = where.innerWidth();
  172. var splW = splitterBar.width();
  173. var fbw = totalW * perc / 100 - splW;
  174. //var realW = firstBox.get(0).scrollWidth;
  175. //fbw = fbw > realW? realW: fbw;
  176. fbw = fbw > totalW - splW - splitter.secondBoxMinWidth ? totalW - splW - splitter.secondBoxMinWidth : fbw;
  177. firstBox.width(fbw).css({left: 0});
  178. splitterBar.css({left: firstBox.width()});
  179. secondBox.width(totalW - fbw - splW).css({left: firstBox.width() + splW});
  180. splitterBar.on("mousedown.gdf", function (e) {
  181. e.preventDefault();
  182. $("body").addClass("gdfHResizing");
  183. $.splittify.splitterBar = $(this);
  184. //on event for start resizing
  185. //console.debug("start splitting");
  186. //var realW = firstBox.get(0).scrollWidth;
  187. $("body").unselectable().on("mousemove.gdf", function (e) {
  188. //manage resizing
  189. //console.debug(e.pageX - $.gridify.columInResize.offset().left)
  190. e.preventDefault();
  191. var sb = $.splittify.splitterBar;
  192. var pos = e.pageX - sb.parent().offset().left;
  193. var w = sb.parent().width();
  194. var fbw = firstBox;
  195. pos = pos > splitter.firstBoxMinWidth ? pos : splitter.firstBoxMinWidth;
  196. //pos = pos < realW - 10 ? pos : realW - 10;
  197. pos = pos > totalW - splW - splitter.secondBoxMinWidth ? totalW - splW - splitter.secondBoxMinWidth : pos;
  198. sb.css({left: pos});
  199. firstBox.width(pos);
  200. secondBox.css({left: pos + sb.width(), width: w - pos - sb.width()});
  201. splitter.perc = (firstBox.width() / splitter.element.width()) * 100;
  202. //on mouse up on body to stop resizing
  203. }).on("mouseup.gdf", function () {
  204. //console.debug("stop splitting");
  205. $(this).off("mousemove.gdf").off("mouseup.gdf").clearunselectable();
  206. delete $.splittify.splitterBar;
  207. $("body").removeClass("gdfHResizing");
  208. storePosition();
  209. });
  210. });
  211. // keep both side in synch when scroll
  212. var stopScroll = false;
  213. var fs = firstBox.add(secondBox);
  214. fs.scroll(function (e) {
  215. var el = $(this);
  216. var top = el.scrollTop();
  217. if (el.is(".splitBox1") && stopScroll != "splitBox2") {
  218. stopScroll = "splitBox1";
  219. secondBox.scrollTop(top);
  220. } else if (el.is(".splitBox2") && stopScroll != "splitBox1") {
  221. stopScroll = "splitBox2";
  222. firstBox.scrollTop(top);
  223. }
  224. firstBox.find(".ganttFixHead").css('top', top);
  225. secondBox.find(".ganttFixHead").css('top', top);
  226. where.stopTime("reset").oneTime(100, "reset", function () {stopScroll = "";})
  227. });
  228. firstBox.on('mousewheel MozMousePixelScroll', function (event) {
  229. event.preventDefault();
  230. var deltaY = event.originalEvent.wheelDeltaY;
  231. var deltaX = event.originalEvent.wheelDeltaX;
  232. if (event.originalEvent.axis) {
  233. deltaY = event.originalEvent.axis == 2 ? -event.originalEvent.detail : null;
  234. deltaX = event.originalEvent.axis == 1 ? -event.originalEvent.detail : null;
  235. }
  236. deltaY = Math.abs(deltaY) < 40 ? 40 * (Math.abs(deltaY) / deltaY) : deltaY;
  237. deltaX = Math.abs(deltaX) < 40 ? 40 * (Math.abs(deltaX) / deltaX) : deltaX;
  238. var scrollToY = secondBox.scrollTop() - deltaY;
  239. var scrollToX = firstBox.scrollLeft() - deltaX;
  240. // console.debug( firstBox.scrollLeft(), Math.abs(deltaX), Math.abs(deltaY));
  241. if (deltaY) secondBox.scrollTop(scrollToY);
  242. if (deltaX) firstBox.scrollLeft(scrollToX);
  243. return false;
  244. });
  245. function Splitter(element, firstBox, secondBox, splitterBar) {
  246. this.element = element;
  247. this.firstBox = firstBox;
  248. this.secondBox = secondBox;
  249. this.splitterBar = splitterBar;
  250. this.perc = 0;
  251. this.firstBoxMinWidth = 0;
  252. this.secondBoxMinWidth = 30;
  253. this.resize = function (newPerc, anim) {
  254. var animTime = anim ? anim : 0;
  255. this.perc = newPerc ? newPerc : this.perc;
  256. var totalW = this.element.width();
  257. var splW = this.splitterBar.width();
  258. var newW = totalW * this.perc / 100;
  259. newW = newW > this.firstBoxMinWidth ? newW : this.firstBoxMinWidth;
  260. newW = newW > totalW - splW - splitter.secondBoxMinWidth ? totalW - splW - splitter.secondBoxMinWidth : newW;
  261. this.firstBox.animate({width: newW}, animTime, function () {$(this).css("overflow-x", "auto")});
  262. this.splitterBar.animate({left: newW}, animTime);
  263. this.secondBox.animate({left: newW + this.splitterBar.width(), width: totalW - newW - splW}, animTime, function () {$(this).css("overflow", "auto")});
  264. storePosition();
  265. };
  266. var self = this;
  267. this.splitterBar.on("dblclick", function () {
  268. self.resize(50, true);
  269. })
  270. }
  271. function storePosition () {
  272. //console.debug("storePosition",splitter.perc);
  273. if (localStorage) {
  274. localStorage.setItem("TWPGanttSplitPos",splitter.perc);
  275. }
  276. }
  277. function loadPosition () {
  278. //console.debug("loadPosition");
  279. if (localStorage) {
  280. if (localStorage.getItem("TWPGanttSplitPos")) {
  281. splitter.perc=parseFloat(localStorage.getItem("TWPGanttSplitPos"));
  282. }
  283. }
  284. }
  285. return splitter;
  286. }
  287. };
  288. //<%------------------------------------------------------------------------ UTILITIES ---------------------------------------------------------------%>
  289. function computeStart(start) {
  290. return computeStartDate(start).getTime();
  291. }
  292. function computeStartDate(start) {
  293. var d = new Date(start + 3600000 * 12);
  294. d.setHours(0, 0, 0, 0);
  295. //move to next working day
  296. while (isHoliday(d)) {
  297. d.setDate(d.getDate() + 1);
  298. }
  299. d.setHours(0, 0, 0, 0);
  300. return d;
  301. }
  302. function computeEnd(end) {
  303. return computeEndDate(end).getTime()
  304. }
  305. function computeEndDate(end) {
  306. var d = new Date(end - 3600000 * 12);
  307. d.setHours(23, 59, 59, 999);
  308. //move to next working day
  309. while (isHoliday(d)) {
  310. d.setDate(d.getDate() + 1);
  311. }
  312. d.setHours(23, 59, 59, 999);
  313. return d;
  314. }
  315. function computeEndByDuration(start, duration) {
  316. var d = new Date(start);
  317. //console.debug("computeEndByDuration start ",d,duration)
  318. var q = duration - 1;
  319. while (q > 0) {
  320. d.setDate(d.getDate() + 1);
  321. if (!isHoliday(d))
  322. q--;
  323. }
  324. d.setHours(23, 59, 59, 999);
  325. return d.getTime();
  326. }
  327. function incrementDateByWorkingDays(date, days) {
  328. var d = new Date(date);
  329. d.incrementDateByWorkingDays(days);
  330. return d.getTime();
  331. }
  332. function recomputeDuration(start, end) {
  333. //console.debug("recomputeDuration");
  334. return new Date(start).distanceInWorkingDays(new Date(end)) + 1;
  335. }
  336. function resynchDates(leavingField, startField, startMilesField, durationField, endField, endMilesField) {
  337. //console.debug("resynchDates",leavingField.prop("name"), startField.prop("name"), startMilesField.prop("name"), durationField.prop("name"), endField.prop("name"), endMilesField.prop("name"));
  338. function resynchDatesSetFields(command) {
  339. //console.debug("resynchDatesSetFields",command);
  340. //var duration = parseInt(durationField.val());
  341. var duration = daysFromString(durationField.val(), true);
  342. if (!duration || duration < 1)
  343. duration = 1;
  344. var start = computeStart(Date.parseString(startField.val()).getTime());
  345. var end = endField.val();
  346. if (end.length > 0) {
  347. end = Date.parseString(end);
  348. end.setHours(23, 59, 59, 999);
  349. end = computeEnd(end.getTime());
  350. }
  351. var date = new Date();
  352. if ("CHANGE_END" == command) {
  353. date.setTime(start);
  354. var workingDays = duration - 1;
  355. date.incrementDateByWorkingDays(workingDays);
  356. date.setHours(23, 59, 59, 999);
  357. end = computeEnd(date.getTime());
  358. } else if ("CHANGE_START" == command) {
  359. date.setTime(end);
  360. var workingDays = duration - 1;
  361. date.incrementDateByWorkingDays(-workingDays);
  362. date.setHours(0, 0, 0, 0);
  363. start = computeStart(date.getTime());
  364. } else if ("CHANGE_DURATION" == command) {
  365. //console.debug("CHANGE_DURATION",new Date(start),new Date(end))
  366. duration = new Date(start).distanceInWorkingDays(new Date(end)) + 1;
  367. }
  368. startField.val(new Date(start).format());
  369. endField.val(new Date(end).format());
  370. durationField.val(duration);
  371. return {start: start, end: end, duration: duration};
  372. }
  373. var leavingFieldName = leavingField.prop("name");
  374. var durIsFilled = durationField.val().length > 0;
  375. var startIsFilled = startField.val().length > 0;
  376. var endIsFilled = endField.val().length > 0;
  377. var startIsMilesAndFilled = startIsFilled && (startMilesField.prop("checked") || startField.is("[readOnly]"));
  378. var endIsMilesAndFilled = endIsFilled && (endMilesField.prop("checked") || endField.is("[readOnly]"));
  379. if (durIsFilled) {
  380. if (parseInt(durationField.val()) == NaN || parseInt(durationField.val()) < 1)
  381. durationField.val(1);
  382. }
  383. if (leavingFieldName.indexOf("Milestone") > 0) {
  384. if (startIsMilesAndFilled && endIsMilesAndFilled) {
  385. durationField.prop("readOnly", true);
  386. } else {
  387. durationField.prop("readOnly", false);
  388. }
  389. return;
  390. }
  391. //need at least two values to resynch the third
  392. if ((durIsFilled ? 1 : 0) + (startIsFilled ? 1 : 0) + (endIsFilled ? 1 : 0) < 2)
  393. return;
  394. var ret;
  395. if (leavingFieldName == 'start' && startIsFilled) {
  396. if (endIsMilesAndFilled && durIsFilled) {
  397. ret = resynchDatesSetFields("CHANGE_DURATION");
  398. } else if (durIsFilled) {
  399. ret = resynchDatesSetFields("CHANGE_END");
  400. }
  401. } else if (leavingFieldName == 'duration' && durIsFilled && !(endIsMilesAndFilled && startIsMilesAndFilled)) {
  402. if (endIsMilesAndFilled && !startIsMilesAndFilled) {
  403. ret = resynchDatesSetFields("CHANGE_START");
  404. } else if (!endIsMilesAndFilled) {
  405. //document.title=('go and change end!!');
  406. ret = resynchDatesSetFields("CHANGE_END");
  407. }
  408. } else if (leavingFieldName == 'end' && endIsFilled) {
  409. ret = resynchDatesSetFields("CHANGE_DURATION");
  410. }
  411. return ret;
  412. }
  413. //This prototype is provided by the Mozilla foundation and
  414. //is distributed under the MIT license.
  415. //http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
  416. if (!Array.prototype.filter) {
  417. Array.prototype.filter = function (fun) {
  418. var len = this.length;
  419. if (typeof fun != "function")
  420. throw new TypeError();
  421. var res = new Array();
  422. var thisp = arguments[1];
  423. for (var i = 0; i < len; i++) {
  424. if (i in this) {
  425. var val = this[i]; // in case fun mutates this
  426. if (fun.call(thisp, val, i, this))
  427. res.push(val);
  428. }
  429. }
  430. return res;
  431. };
  432. }
  433. function goToPage(url) {
  434. if (!canILeave()) return;
  435. window.location.href = url;
  436. }