Skip to content

Commit 0a0ca19

Browse files
authored
Merge pull request #336 from antman3351/master
Added basic print function
2 parents 2710351 + 79436a4 commit 0a0ca19

File tree

8 files changed

+170
-8
lines changed

8 files changed

+170
-8
lines changed

Documentation.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,20 @@ g.Draw();
546546
</script>
547547
```
548548
549+
## Printing
550+
`g.printChart(width, height);` will set the page size (via an injected css) and trigger the browser's print.
551+
The default injected style can be changed by passing a different style as the 3rd parameter to `printChart`.
552+
553+
The default style is:
554+
```css
555+
@page {
556+
size: ${width}mm ${height}mm;
557+
}
558+
.gchartcontainer {
559+
width: ${width}mm;
560+
}
561+
```
562+
549563
550564
## Troubleshooting
551565

dist/jsgantt.css

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,32 @@ td.gspanning div {
787787
.glistgrid {
788788
float: none !important;
789789
}
790+
791+
/* Hide scrollbar for Chrome, Safari and Opera */
792+
.gchartcontainer *::-webkit-scrollbar {
793+
display: none;
794+
}
795+
796+
/* Hide scrollbar for IE, Edge and Firefox */
797+
.gchartcontainer * {
798+
-ms-overflow-style: none; /* IE and Edge */
799+
scrollbar-width: none; /* Firefox */
800+
}
801+
802+
/* Hide tool-tip */
803+
.JSGanttToolTip {
804+
display: none !important;
805+
}
806+
807+
/* This is the default css, injected by the JSGantt.printChart(width,height); function -->
808+
@page {
809+
size: ${width}mm ${height}mm;
810+
}
811+
812+
.gchartcontainer {
813+
width: ${width}mm;
814+
}
815+
<-- This will be injected by the JSGantt.printChart() function */
790816
}
791817

792818
/* if using setUseSingleCell(1) the following is a suggested set of CSS3 styles to recreate the table grid - won't work on old browsers
@@ -812,16 +838,20 @@ td.gspanning div {
812838

813839
display: flex;
814840
flex-direction: column;
841+
/* Allow resize */
842+
resize: horizontal;
815843
}
816844

817845
.gmainright {
818846
overflow: hidden;
819-
flex: auto;
847+
flex: 1 1 auto;
820848
}
821849

822850
.gmainleft {
823851
overflow: hidden;
824852
flex: 0 0 50%;
853+
/* Allow side to grow and shrink */
854+
flex: 1 0 auto;
825855
}
826856

827857
.gtasktableh tr,

dist/jsgantt.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.JSGantt = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
22
"use strict";
33
Object.defineProperty(exports, "__esModule", { value: true });
4+
exports.JSGantt = void 0;
45
var jsGantt = require("./src/jsgantt");
56
module.exports = jsGantt.JSGantt;
67
exports.JSGantt = jsGantt.JSGantt;
78

89
},{"./src/jsgantt":6}],2:[function(require,module,exports){
910
"use strict";
1011
Object.defineProperty(exports, "__esModule", { value: true });
12+
exports.GanttChart = void 0;
1113
var lang = require("./lang");
1214
var events_1 = require("./events");
1315
var general_utils_1 = require("./utils/general_utils");
@@ -160,6 +162,7 @@ exports.GanttChart = function (pDiv, pFormat) {
160162
this.DrawDependencies = draw_dependencies_1.DrawDependencies.bind(this);
161163
this.getArrayLocationByID = draw_utils_1.getArrayLocationByID.bind(this);
162164
this.drawSelector = draw_utils_1.drawSelector.bind(this);
165+
this.printChart = general_utils_1.printChart.bind(this);
163166
this.clearDependencies = function () {
164167
var parent = this.getLines();
165168
if (this.vEventsChange.line &&
@@ -840,6 +843,7 @@ exports.GanttChart = function (pDiv, pFormat) {
840843
},{"./draw_columns":3,"./draw_dependencies":4,"./events":5,"./lang":8,"./options":9,"./task":10,"./utils/date_utils":11,"./utils/draw_utils":12,"./utils/general_utils":13,"./xml":14}],3:[function(require,module,exports){
841844
"use strict";
842845
Object.defineProperty(exports, "__esModule", { value: true });
846+
exports.draw_task_headings = exports.draw_bottom = exports.draw_header = exports.COLUMN_ORDER = void 0;
843847
var date_utils_1 = require("./utils/date_utils");
844848
var task_1 = require("./task");
845849
var events_1 = require("./events");
@@ -1009,6 +1013,7 @@ exports.draw_task_headings = function (column, vTmpRow, vLangs, vLang, vAddition
10091013
},{"./events":5,"./task":10,"./utils/date_utils":11,"./utils/draw_utils":12}],4:[function(require,module,exports){
10101014
"use strict";
10111015
Object.defineProperty(exports, "__esModule", { value: true });
1016+
exports.DrawDependencies = exports.drawDependency = void 0;
10121017
exports.drawDependency = function (x1, y1, x2, y2, pType, pClass) {
10131018
var vDir = 1;
10141019
var vBend = false;
@@ -1105,6 +1110,7 @@ exports.DrawDependencies = function (vDebug) {
11051110
},{}],5:[function(require,module,exports){
11061111
"use strict";
11071112
Object.defineProperty(exports, "__esModule", { value: true });
1113+
exports.addListenerDependencies = exports.addListenerInputCell = exports.addListenerClickCell = exports.addScrollListeners = exports.addFormatListeners = exports.addFolderListeners = exports.updateGridHeaderWidth = exports.addThisRowListeners = exports.addTooltipListeners = exports.syncScroll = exports.removeListener = exports.addListener = exports.showToolTip = exports.mouseOut = exports.mouseOver = exports.show = exports.hide = exports.folder = void 0;
11081114
var general_utils_1 = require("./utils/general_utils");
11091115
// Function to open/close and hide/show children of specified task
11101116
exports.folder = function (pID, ganttObj) {
@@ -1505,6 +1511,7 @@ var vColumnsNames = {
15051511
Copyright (c) 2009, Shlomy Gantz BlueBrick Inc.
15061512
*/
15071513
Object.defineProperty(exports, "__esModule", { value: true });
1514+
exports.JSGantt = void 0;
15081515
var events_1 = require("./events");
15091516
var general_utils_1 = require("./utils/general_utils");
15101517
var xml_1 = require("./xml");
@@ -1568,10 +1575,11 @@ exports.JSGantt.criticalPath = general_utils_1.criticalPath;
15681575
},{"./draw":2,"./events":5,"./json":7,"./task":10,"./utils/date_utils":11,"./utils/general_utils":13,"./xml":14}],7:[function(require,module,exports){
15691576
"use strict";
15701577
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
1578+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15711579
return new (P || (P = Promise))(function (resolve, reject) {
15721580
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
15731581
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
1574-
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
1582+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
15751583
step((generator = generator.apply(thisArg, _arguments || [])).next());
15761584
});
15771585
};
@@ -1603,6 +1611,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
16031611
}
16041612
};
16051613
Object.defineProperty(exports, "__esModule", { value: true });
1614+
exports.addJSONTask = exports.parseJSONString = exports.parseJSON = void 0;
16061615
var task_1 = require("./task");
16071616
var general_utils_1 = require("./utils/general_utils");
16081617
/**
@@ -1760,6 +1769,7 @@ exports.addJSONTask = function (pGanttVar, pJsonObj) {
17601769
},{"./task":10,"./utils/general_utils":13}],8:[function(require,module,exports){
17611770
"use strict";
17621771
Object.defineProperty(exports, "__esModule", { value: true });
1772+
exports.ko = exports.hu = exports.cs = exports.ja = exports.tr = exports.id = exports.nl = exports.sv = exports.cn = exports.ru = exports.fr = exports.pt = exports.de = exports.es = exports.en = void 0;
17631773
var es = {
17641774
'january': 'Enero',
17651775
'february': 'Febrero',
@@ -2872,6 +2882,7 @@ exports.ko = ko;
28722882
},{}],9:[function(require,module,exports){
28732883
"use strict";
28742884
Object.defineProperty(exports, "__esModule", { value: true });
2885+
exports.includeGetSet = void 0;
28752886
var date_utils_1 = require("./utils/date_utils");
28762887
var draw_columns_1 = require("./draw_columns");
28772888
exports.includeGetSet = function () {
@@ -3094,6 +3105,7 @@ var __assign = (this && this.__assign) || function () {
30943105
return __assign.apply(this, arguments);
30953106
};
30963107
Object.defineProperty(exports, "__esModule", { value: true });
3108+
exports.processRows = exports.ClearTasks = exports.RemoveTaskItem = exports.AddTaskItemObject = exports.AddTaskItem = exports.createTaskInfo = exports.TaskItem = exports.TaskItemObject = exports.sortTasks = exports.taskLink = void 0;
30973109
var general_utils_1 = require("./utils/general_utils");
30983110
var draw_utils_1 = require("./utils/draw_utils");
30993111
var date_utils_1 = require("./utils/date_utils");
@@ -3815,6 +3827,7 @@ exports.processRows = function (pList, pID, pRow, pLevel, pOpen, pUseSort, vDebu
38153827
},{"./utils/date_utils":11,"./utils/draw_utils":12,"./utils/general_utils":13}],11:[function(require,module,exports){
38163828
"use strict";
38173829
Object.defineProperty(exports, "__esModule", { value: true });
3830+
exports.getIsoWeek = exports.parseDateFormatStr = exports.formatDateStr = exports.parseDateStr = exports.coerceDate = exports.getMaxDate = exports.getMinDate = void 0;
38183831
/**
38193832
* DATES
38203833
*/
@@ -4110,6 +4123,7 @@ exports.getIsoWeek = function (pDate) {
41104123
},{}],12:[function(require,module,exports){
41114124
"use strict";
41124125
Object.defineProperty(exports, "__esModule", { value: true });
4126+
exports.drawSelector = exports.sLine = exports.CalcTaskXY = exports.getArrayLocationByID = exports.newNode = exports.makeInput = void 0;
41134127
var events_1 = require("../events");
41144128
exports.makeInput = function (formattedValue, editable, type, value, choices) {
41154129
if (type === void 0) { type = 'text'; }
@@ -4274,6 +4288,7 @@ exports.drawSelector = function (pPos) {
42744288
},{"../events":5}],13:[function(require,module,exports){
42754289
"use strict";
42764290
Object.defineProperty(exports, "__esModule", { value: true });
4291+
exports.printChart = exports.calculateStartEndFromDepend = exports.makeRequestOldBrowsers = exports.makeRequest = exports.moveToolTip = exports.updateFlyingObj = exports.isParentElementOrSelf = exports.criticalPath = exports.hashKey = exports.hashString = exports.fadeToolTip = exports.hideToolTip = exports.isIE = exports.getOffset = exports.calculateCurrentDateOffset = exports.getScrollbarWidth = exports.getScrollPositions = exports.benchMark = exports.getZoomFactor = exports.delayedHide = exports.stripUnwanted = exports.stripIds = exports.changeFormat = exports.findObj = exports.internalPropertiesLang = exports.internalProperties = void 0;
42774292
exports.internalProperties = ['pID', 'pName', 'pStart', 'pEnd', 'pClass', 'pLink', 'pMile', 'pRes', 'pComp', 'pGroup', 'pParent',
42784293
'pOpen', 'pDepend', 'pCaption', 'pNotes', 'pGantt', 'pCost', 'pPlanStart', 'pPlanEnd'];
42794294
exports.internalPropertiesLang = {
@@ -4724,10 +4739,26 @@ exports.makeRequestOldBrowsers = function (pFile, vDebug) {
47244739
};
47254740
exports.calculateStartEndFromDepend = function (tasksList) {
47264741
};
4742+
exports.printChart = function (width, height, css) {
4743+
if (css === void 0) { css = undefined; }
4744+
if (css === undefined) {
4745+
css = // Default injected CSS
4746+
"@media print {\n @page {\n size: " + width + "mm " + height + "mm;\n }\n /* set gantt container to the same width as the page */\n .gchartcontainer {\n width: " + width + "mm;\n }\n };";
4747+
}
4748+
var $container = document.querySelector('.gchartcontainer');
4749+
$container.insertAdjacentHTML('afterbegin', "<style>" + css + "</style>");
4750+
// Remove the print CSS when the print dialog is closed
4751+
window.addEventListener('afterprint', function () {
4752+
$container.removeChild($container.children[0]);
4753+
}, { 'once': true });
4754+
// Trigger the print
4755+
window.print();
4756+
};
47274757

47284758
},{}],14:[function(require,module,exports){
47294759
"use strict";
47304760
Object.defineProperty(exports, "__esModule", { value: true });
4761+
exports.getXMLTask = exports.getXMLProject = exports.AddXMLTask = exports.getXMLNodeValue = exports.findXMLNode = exports.parseXMLString = exports.parseXML = void 0;
47314762
var task_1 = require("./task");
47324763
var date_utils_1 = require("./utils/date_utils");
47334764
var draw_utils_1 = require("./utils/draw_utils");

docs/demo.html

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
height: 22px;
3232
background: #777777;
3333
}
34+
@media print {
35+
.no-print {
36+
display: none;
37+
}
38+
}
3439
</style>
3540
<!-- External resources -->
3641
<!-- jQuery + Ajax [required by Bootstrap] -->
@@ -60,7 +65,7 @@
6065

6166
<body data-spy="scroll" data-target="#my-navbar-nav" style="padding: 30px;">
6267

63-
68+
<div class="no-print">
6469
<div class="row">
6570
<h3 class="row">Hide and Show Properties</h3>
6671

@@ -188,7 +193,7 @@ <h3 class="row">Hide and Show Properties</h3>
188193
<br />
189194

190195

191-
<span>
196+
<!-- <span>-->
192197
<label>Height:</label>
193198
<input type="text" id="vTotalHeight" fl onchange="start(event)" />
194199
(CSS style height like "300px". Empty for auto height)
@@ -234,17 +239,34 @@ <h3 class="row">Hide and Show Properties</h3>
234239
</span>
235240

236241
<span>
237-
<button onclick="print()">Print data in console</button>
242+
<button onclick="printTasksInConsole()">Print data in console</button>
238243
</span>
239244

240245
<br />
246+
247+
<select id="print_page_size">
248+
<option value="210,297">A4 Portrait</option>
249+
<option value="297,210">A4 Landscape</option>
250+
<option value="297,420">A3 Portrait</option>
251+
<option value="420,297">A3 Landscape</option>
252+
<option value="420,594">A2 Portrait</option>
253+
<option value="594,420">A2 Landscape</option>
254+
<option value="594,841">A1 Portrait</option>
255+
<option value="841,594">A1 Landscape</option>
256+
<option value="841,1189">A0 Portrait</option>
257+
<option value="1189,841">A0 Landscape</option>
258+
</select>
259+
<button onclick="printChart()">Print</button>
260+
261+
<br>
241262

242263
<small>
243264
* Click events in table are binded to console.log for testing
244265
</small>
245266

246267

247268
<br /><br />
269+
</div>
248270
<div id="embedded-Gantt">
249271
</div>
250272

docs/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,17 @@ function clearTasks() {
201201
g.Draw()
202202
}
203203

204-
function print() {
204+
function printTasksInConsole() {
205205
const tasks = g.vTaskList.map(e => ({ ...e.getAllData(), ...e.getDataObject() }));
206206
console.log(tasks);
207207
}
208208

209+
function printChart(){
210+
let width, height;
211+
[ width, height ] = document.querySelector('#print_page_size').value.split(',');
212+
g.printChart( width, height );
213+
}
214+
209215

210216
function editValue(list, task, event, cell, column) {
211217
console.log(list, task, event, cell, column)

src/draw.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from './events';
1717
import {
1818
calculateCurrentDateOffset,
19-
getOffset, getScrollbarWidth
19+
getOffset, getScrollbarWidth, printChart
2020
} from './utils/general_utils';
2121
import { createTaskInfo, AddTaskItem, AddTaskItemObject, RemoveTaskItem, processRows, ClearTasks } from './task';
2222

@@ -178,6 +178,7 @@ export const GanttChart = function (pDiv, pFormat) {
178178
this.DrawDependencies = DrawDependencies.bind(this);
179179
this.getArrayLocationByID = getArrayLocationByID.bind(this);
180180
this.drawSelector = drawSelector.bind(this);
181+
this.printChart = printChart.bind(this);
181182

182183
this.clearDependencies = function () {
183184
let parent = this.getLines();

src/jsgantt.css

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,32 @@ td.gspanning div {
787787
.glistgrid {
788788
float: none !important;
789789
}
790+
791+
/* Hide scrollbar for Chrome, Safari and Opera */
792+
.gchartcontainer *::-webkit-scrollbar {
793+
display: none;
794+
}
795+
796+
/* Hide scrollbar for IE, Edge and Firefox */
797+
.gchartcontainer * {
798+
-ms-overflow-style: none; /* IE and Edge */
799+
scrollbar-width: none; /* Firefox */
800+
}
801+
802+
/* Hide tool-tip */
803+
.JSGanttToolTip {
804+
display: none !important;
805+
}
806+
807+
/* This is the default css, injected by the JSGantt.printChart(width,height); function -->
808+
@page {
809+
size: ${width}mm ${height}mm;
810+
}
811+
812+
.gchartcontainer {
813+
width: ${width}mm;
814+
}
815+
<-- This will be injected by the JSGantt.printChart() function */
790816
}
791817

792818
/* if using setUseSingleCell(1) the following is a suggested set of CSS3 styles to recreate the table grid - won't work on old browsers
@@ -812,16 +838,20 @@ td.gspanning div {
812838

813839
display: flex;
814840
flex-direction: column;
841+
/* Allow resize */
842+
resize: horizontal;
815843
}
816844

817845
.gmainright {
818846
overflow: hidden;
819-
flex: auto;
847+
flex: 1 1 auto;
820848
}
821849

822850
.gmainleft {
823851
overflow: hidden;
824852
flex: 0 0 50%;
853+
/* Allow side to grow and shrink */
854+
flex: 1 0 auto;
825855
}
826856

827857
.gtasktableh tr,

0 commit comments

Comments
 (0)