(function(){
var apiUrl = "https://export.dhtmlx.com/gantt";
var templates = [
"leftside_text",
"rightside_text",
"task_text",
"progress_text",
"task_class"
];
function xdr(url, pack, cb){
if (gantt.env.isIE){
gantt.env.isIE = false;
gantt.ajax.post(url, pack, cb);
gantt.env.isIE = true;
} else {
gantt.ajax.post(url, pack, cb);
}
}
function defaults(obj, std){
for (var key in std)
if (!obj[key])
obj[key] = std[key];
return obj;
}
//compatibility for new versions of gantt
if(!gantt.ajax){
if(window.dhtmlxAjax){
gantt.ajax = window.dhtmlxAjax;
}else if(window.dhx4){
gantt.ajax = window.dhx4.ajax;
}
}
function mark_columns(base){
var columns = base.config.columns;
if (columns)
for (var i = 0; i < columns.length; i++) {
if (columns[i].template)
columns[i].$template = true;
}
}
function add_export_methods(gantt){
var color_box = null;
var color_hash = {};
function get_styles(css){
if (!color_box){
var color_box = document.createElement("DIV");
color_box.style.cssText = "position:absolute; display:none;";
document.body.appendChild(color_box);
}
if (color_hash[css])
return color_hash[css];
color_box.className = css;
return (color_hash[css] = get_color(color_box, "color")+";"+get_color(color_box, "backgroundColor"));
}
gantt._getWorktimeSettings = function() {
var defaultWorkTimes = {
hours : [0, 24],
dates : {0:true, 1:true, 2:true, 3:true, 4:true, 5:true, 6:true}
};
var time;
if(!gantt.config.work_time){
time = defaultWorkTimes;
}else{
var wTime = gantt._working_time_helper;
if (wTime && wTime.get_calendar) {
time = wTime.get_calendar();
} else if (wTime) {
time = {
hours : wTime.hours,
dates : wTime.dates
};
} else if (gantt.config.worktimes && gantt.config.worktimes.global) {
time = {
hours : gantt.config.worktimes.global.hours,
dates : gantt.config.worktimes.global.dates
};
} else {
time = defaultWorkTimes;
};
}
return time;
};
gantt.exportToPDF = function(config){
if (config && config.raw){
config = defaults(config, {
name:"gantt.pdf", data:this._serialize_html()
});
} else {
config = defaults((config || {}), {
name:"gantt.pdf",
data:this._serialize_all(),
config:this.config
});
fix_columns(gantt, config.config.columns);
}
config.version = this.version;
this._send_to_export(config, "pdf");
};
gantt.exportToPNG = function(config){
if (config && config.raw){
config = defaults(config, {
name:"gantt.png", data:this._serialize_html()
});
} else {
config = defaults((config || {}), {
name:"gantt.png",
data:this._serialize_all(),
config:this.config
});
fix_columns(gantt, config.config.columns);
}
config.version = this.version;
this._send_to_export(config, "png");
};
gantt.exportToICal = function(config){
config = defaults((config || {}), {
name:"gantt.ical",
data:this._serialize_plain().data,
version:this.version
});
this._send_to_export(config, "ical");
};
function eachTaskTimed(start, end){
return function(code, parent, master){
parent = parent || this.config.root_id;
master = master || this;
var branch = this.getChildren(parent);
if (branch)
for (var i=0; i start) && (!end || item.start_date < end))
code.call(master, item);
if (this.hasChild(item.id))
this.eachTask(code, item.id, master);
}
};
}
gantt.exportToExcel = function(config){
config = config || {};
var tasks, dates;
var state, scroll;
if (config.start || config.end){
state = this.getState();
dates = [ this.config.start_date, this.config.end_date ];
scroll = this.getScrollState();
var convert = this.date.str_to_date(this.config.date_format);
tasks = this.eachTask;
if (config.start)
this.config.start_date = convert(config.start);
if (config.end)
this.config.end_date = convert(config.end);
this.render();
this.eachTask = eachTaskTimed(this.config.start_date, this.config.end_date);
}
this._no_progress_colors =config.visual === "base-colors";
config = defaults(config, {
name:"gantt.xlsx",
title:"Tasks",
data:this._serialize_table(config).data,
columns:this._serialize_columns({ rawDates: true }),
version:this.version
});
if (config.visual)
config.scales = this._serialize_scales(config);
this._send_to_export(config, "excel");
if (config.start || config.end){
this.config.start_date = state.min_date;
this.config.end_date = state.max_date;
this.eachTask = tasks;
this.render();
this.scrollTo(scroll.x, scroll.y);
this.config.start_date = dates[0];
this.config.end_date = dates[1];
}
};
gantt.exportToJSON = function(config){
config = defaults((config || {}), {
name:"gantt.json",
data:this._serialize_all(),
config: this.config,
columns:this._serialize_columns(),
worktime : gantt._getWorktimeSettings(),
version:this.version
});
this._send_to_export(config, "json");
};
function sendImportAjax(config){
var url = config.server || apiUrl;
var store = config.store || 0;
var formData = config.data;
var callback = config.callback;
formData.append("type", "excel-parse");
formData.append("data", JSON.stringify({
sheet: config.sheet || 0
}));
if(store)
formData.append("store", store);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function (e) {
if(xhr.readyState == 4 && xhr.status == 0){// network error
if(callback){
callback(null);
}
}
};
xhr.onload = function() {
var fail = xhr.status > 400;
var info = null;
if (!fail){
try{
info = JSON.parse(xhr.responseText);
}catch(e){}
}
if(callback){
callback(info);
}
};
xhr.open('POST', url, true);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.send(formData);
}
gantt.importFromExcel = function(config){
var formData = config.data;
if(formData instanceof FormData){
}else if(formData instanceof File){
var data = new FormData();
data.append("file", formData);
config.data = data;
}
sendImportAjax(config);
};
gantt._msp_config = function(config){
if (config.project)
for (var i in config.project){
if (!config._custom_data)
config._custom_data = {};
config._custom_data[i] = config.project[i](this.config);
}
if (config.tasks)
for (var j = 0; j < config.data.length; j++){
var el = this.getTask(config.data[j].id);
if (!el._custom_data)
el._custom_data = {};
for (var i in config.tasks)
el._custom_data[i] = config.tasks[i](el, this.config);
}
delete config.project;
delete config.tasks;
config.time = gantt._getWorktimeSettings();
var p_dates = this.getSubtaskDates();
var format = this.date.date_to_str("%d-%m-%Y %H:%i:%s");
config.start_end = {
start_date: format(p_dates.start_date),
end_date: format(p_dates.end_date)
};
};
gantt._msp_data = function(){
var old_xml_format = this.templates.xml_format;
var old_format_date = this.templates.format_date;
this.templates.xml_format = this.date.date_to_str("%d-%m-%Y %H:%i:%s");
this.templates.format_date = this.date.date_to_str("%d-%m-%Y %H:%i:%s");
var data = this._serialize_all();
this.templates.xml_format = old_xml_format;
this.templates.format_date = old_format_date;
return data;
};
gantt._ajax_to_export = function(data, type, callback){
delete data.callback;
var url = data.server || apiUrl;
var pack = "type="+type+"&store=1&data="+encodeURIComponent(JSON.stringify(data));
var cb = function(loader){
var xdoc = loader.xmlDoc || loader;
var fail = xdoc.status > 400;
var info = null;
if (!fail){
try{
info = JSON.parse(xdoc.responseText);
}catch(e){}
}
callback(info);
};
xdr(url, pack, cb);
};
gantt._send_to_export = function(data, type){
var convert = this.date.date_to_str(this.config.date_format || this.config.xml_date);
if (data.config){
data.config = this.copy(data.config);
mark_columns(data, type);
if(data.config.start_date && data.config.end_date){
if(data.config.start_date instanceof Date){
data.config.start_date = convert(data.config.start_date)
}
if(data.config.end_date instanceof Date){
data.config.end_date = convert(data.config.end_date)
}
}
}
if (data.callback)
return gantt._ajax_to_export(data, type, data.callback);
var form = this._create_hidden_form();
form.firstChild.action = data.server || apiUrl;
form.firstChild.childNodes[0].value = JSON.stringify(data);
form.firstChild.childNodes[1].value = type;
form.firstChild.submit();
};
gantt._create_hidden_form = function(){
if (!this._hidden_export_form){
var t = this._hidden_export_form = document.createElement("div");
t.style.display = "none";
t.innerHTML = "";
document.body.appendChild(t);
}
return this._hidden_export_form;
};
//patch broken json serialization in gantt 2.1
var original = gantt.json._copyObject;
function copy_object_base(obj){
var copy = {};
for (var key in obj){
if (key.charAt(0) == "$")
continue;
copy[key] = obj[key];
}
var formatDate = gantt.templates.xml_format || gantt.templates.format_date;
copy.start_date = formatDate(copy.start_date);
if (copy.end_date)
copy.end_date = formatDate(copy.end_date);
return copy;
}
function copy_object_plain(obj){
var text = gantt.templates.task_text(obj.start_date, obj.end_date, obj);
var copy = copy_object_base(obj);
copy.text = text || copy.text;
return copy;
}
function get_color(node, style){
var value = node.currentStyle ? node.currentStyle[style] : getComputedStyle(node, null)[style];
var rgb = value.replace(/\s/g,'').match(/^rgba?\((\d+),(\d+),(\d+)/i);
return ((rgb && rgb.length === 4) ?
("0" + parseInt(rgb[1],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[2],10).toString(16)).slice(-2) +
("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : value).replace("#","");
}
// Excel interprets UTC time as local time in every timezone, send local time instead of actual UTC time.
// https://github.com/SheetJS/js-xlsx/issues/126#issuecomment-60531614
var toISOstring = gantt.date.date_to_str("%Y-%m-%dT%H:%i:%s.000Z");
// excel serialization
function copy_object_table(obj){
var copy = copy_object_columns(obj, copy_object_plain(obj));
if (copy.start_date)
copy.start_date = toISOstring(obj.start_date);
if (copy.end_date)
copy.end_date = toISOstring(obj.end_date);
// private gantt._day_index_by_date was replaced by public gantt.columnIndexByDate in gantt 5.0
var getDayIndex = gantt._day_index_by_date ? gantt._day_index_by_date : gantt.columnIndexByDate;
copy.$start = getDayIndex.call(gantt, obj.start_date);
copy.$end = getDayIndex.call(gantt, obj.end_date);
copy.$level = obj.$level;
copy.$type = obj.$rendered_type;
var tmps = gantt.templates;
copy.$text = tmps.task_text(obj.start, obj.end_date, obj);
copy.$left = tmps.leftside_text ? tmps.leftside_text(obj.start, obj.end_date, obj) : "";
copy.$right = tmps.rightside_text ? tmps.rightside_text(obj.start, obj.end_date, obj) : "";
return copy;
}
function copy_object_colors(obj){
var copy = copy_object_table(obj);
var node = gantt.getTaskNode(obj.id);
if (node && node.firstChild){
var color = get_color((gantt._no_progress_colors ? node : node.firstChild), "backgroundColor");
if (color == "363636")
color = get_color(node, "backgroundColor");
copy.$color = color;
} else if (obj.color)
copy.$color = obj.color;
return copy;
}
function copy_object_columns(obj, copy){
for(var i=0; i= 5.0
var timeline = gantt.$ui.getView("timeline");
if (timeline) {
var availWidth = timeline.$config.width;
if (gantt.config.autosize == "x" || gantt.config.autosize == "xy") {
availWidth = Math.max(gantt.config.autosize_min_width, 0);
}
var state = gantt.getState(),
scales = timeline._getScales(),
min_width = gantt.config.min_column_width,
height = gantt.config.scale_height-1,
rtl = gantt.config.rtl;
return timeline.$scaleHelper.prepareConfigs(scales, min_width, availWidth, height, state.min_date, state.max_date, rtl);
}
}
}
gantt._serialize_table = function(config){
gantt.json._copyObject = config.visual ? copy_object_colors : copy_object_table;
var data = export_serialize();
gantt.json._copyObject = original;
delete data.links;
if (config.cellColors){
var css = this.templates.timeline_cell_class || this.templates.task_cell_class;
if (css){
var raw = get_raw();
var steps = raw[0].trace_x;
for (var i=1; i steps.length)
steps = raw[i].trace_x;
for (var i=0; i 400;
var info = null;
if (!fail){
try{
info = JSON.parse(xhr.responseText);
}catch(e){}
}
if(callback){
callback(info);
}
};
xhr.open('POST', url, true);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.send(formData);
}
gantt.importFromMSProject = function(config){
var formData = config.data;
if(formData instanceof FormData){
}else if(formData instanceof File){
var data = new FormData();
data.append("file", formData);
config.data = data;
}
sendImportAjax(config);
};
gantt.importFromPrimaveraP6 = function(config){
config.type = "primaveraP6-parse";
return gantt.importFromMSProject(config);
};
}
add_export_methods(gantt);
if (window.Gantt && Gantt.plugin)
Gantt.plugin(add_export_methods);
})();