/**
* Plugin for embed mode in Confluence Connect post version 1.4.8
*/
Draw.loadPlugin(function(ui)
{
// Handle data governess by modifying external services URLs
var allowedRegions = {
eu: 1,
us: 1
};
if (allowedRegions[urlParams['dataGov']])
{
var region = urlParams['dataGov'];
var urls = {
'EXPORT_URL': 'export',
'PLANT_URL': 'plant',
'VSD_CONVERT_URL': 'vsd',
'EMF_CONVERT_URL': 'emf',
'OPEN_URL': 'import'
};
for (var key in urls)
{
var val = window[key];
if (val)
{
window[key] = '/region-' + urls[key] + '-' + region;
}
}
}
// Extracts macro data from JSON protocol
var macroData = {};
mxEvent.addListener(window, 'message', mxUtils.bind(this, function(evt)
{
var data = evt.data;
try
{
data = JSON.parse(data);
if (data.action == 'load')
{
if (data.macroData != null)
{
macroData = data.macroData;
if (ui.format != null)
{
ui.format.refresh();
}
}
ui.initComments(macroData.contentId || macroData.custContentId);
macroData.diagramDisplayName = data.title;
//Fetch notifications
ui.fetchAndShowNotification('conf');
}
}
catch (e)
{
data = null;
}
}));
var renameAction = ui.actions.get("rename");
renameAction.visible = true;
renameAction.isEnabled = function()
{
return macroData.diagramDisplayName != null;
}
function descriptorChangedListener()
{
var curFile = ui.getCurrentFile();
var fileTitle = curFile.getTitle();
//Update file name in the UI
var tmp = document.createElement('span');
mxUtils.write(tmp, mxUtils.htmlEntities(fileTitle));
if (ui.embedFilenameSpan != null)
{
ui.embedFilenameSpan.parentNode.removeChild(ui.embedFilenameSpan);
}
ui.buttonContainer.appendChild(tmp);
ui.embedFilenameSpan = tmp;
macroData.diagramDisplayName = fileTitle;
var vSettings = curFile.desc.viewerSettings;
if (vSettings != null)
{
macroData.tbstyle = vSettings.tbstyle;
macroData.links = vSettings.links;
macroData.simple = vSettings.simple;
macroData.lbox = vSettings.lbox;
macroData.zoom = vSettings.zoom;
macroData.pCenter = vSettings.pCenter;
macroData.aspect = vSettings.aspect;
macroData.hiResPreview = vSettings.hiResPreview;
if (ui.format != null)
{
ui.format.refresh();
}
}
};
var xdm_e = decodeURIComponent(urlParams['site']);
var license = urlParams['atlas-lic'];
ui.remoteInvoke('checkConfLicense', [license, xdm_e], null, function(licenseValid)
{
if (!licenseValid)
{
ui.menus.get('file').funct = function(menu, parent)
{
menu.addItem(mxResources.get('licenseRequired'), null, function()
{
// do nothing
}, parent, null, false);
}
ui.menus.get('insertAdvanced').funct = function(menu, parent)
{
menu.addItem(mxResources.get('licenseRequired'), null, function()
{
// do nothing
}, parent, null, false);
}
if (typeof(MathJax) !== 'undefined')
{
ui.actions.get('mathematicalTypesetting').funct = function()
{
ui.alert(mxResources.get('licenseRequired'));
};
}
EditorUi.prototype.insertPage = function(page, index)
{
this.alert(mxResources.get('licenseRequired'));
};
Sidebar.prototype.searchEntries = function(searchTerms, count, page, success, error)
{
success();
};
Sidebar.prototype.insertSearchHint = function(div, searchTerm, count, page, results, len, more, terms)
{
var link = document.createElement('div');
link.className = 'geTitle';
link.style.cssText = 'background-color:#ffd350;border-radius:6px;color:black;' +
'border:1px solid black !important;text-align:center;white-space:normal;' +
'padding:6px 0px 6px 0px !important;margin:4px 4px 8px 2px;font-size:12px;';
mxUtils.write(link, mxResources.get('licenseRequired'));
div.appendChild(link);
};
DrawioFileSync.prototype.fileChangedNotify = function()
{
//Disable RT syncing
};
ui.importFiles = function()
{
//Disable DnD and file import
ui.alert(mxResources.get('licenseRequired'));
}
//Disable comments
ui.getComments = function(success, error)
{
error({message: mxResources.get('licenseRequired')});
}
ui.addComment = function(comment, success, error)
{
error();
}
}
},
function(){});
renameAction.funct = function()
{
var dlg = new FilenameDialog(ui, macroData.diagramDisplayName || "",
mxResources.get('rename'), function(newName)
{
if (newName != null && newName.length > 0)
{
//TODO This is not needed with RT since title is added to desc
macroData.diagramDisplayName = newName;
var parent = window.opener || window.parent;
parent.postMessage(JSON.stringify({event: 'rename', name: newName}), '*');
//Update and sync new name
ui.getCurrentFile().rename(newName);
}
}, mxResources.get('rename'), function(name)
{
var err = "";
if (name == null || name.length == 0)
{
err = 'Filename too short';
}
else if (/[&\*+=\\;/{}|\":<>\?~]/g.test(name))
{
err = 'Invalid characters \\ / | : { } < > & + ? = ; * " ~';
}
else
{
return true;
}
ui.showError(mxResources.get('error'), err, mxResources.get('ok'));
return false;
});
ui.showDialog(dlg.container, 300, 80, true, true);
dlg.init();
}
// Returns modified macro data to client
var uiCreateLoadMessage = ui.createLoadMessage;
ui.createLoadMessage = function(eventName)
{
var msg = uiCreateLoadMessage.apply(this, arguments);
if (eventName == 'export')
{
msg.macroData = macroData;
var desc = ui.getCurrentFile().getDescriptor();
//Until app.min.js is propagated, this code is necessary
if (desc != null)
{
if (desc.key == null)
{
desc.key = Editor.guid(32);
desc.channel = Editor.guid(32);
desc.etagP = Editor.guid(32);
desc.title = macroData.diagramDisplayName;
}
else if (desc.title)
{
macroData.diagramDisplayName = desc.title;
}
msg.desc = desc;
}
else
{
msg.desc = {};
}
}
return msg;
};
// Adds new section for confluence cloud
var diagramFormatPanelInit = DiagramFormatPanel.prototype.init;
DiagramFormatPanel.prototype.init = function()
{
this.container.appendChild(this.addViewerOptions(this.createPanel()));
diagramFormatPanelInit.apply(this, arguments);
};
// Adds viewer config to style options and refreshes
DiagramFormatPanel.prototype.addViewerOptions = function(div)
{
var ui = this.editorUi;
var editor = ui.editor;
var graph = editor.graph;
div.appendChild(this.createTitle(mxResources.get('viewerSettings')));
// Viewer simple
div.appendChild(this.createOption(mxResources.get('simpleViewer'), function()
{
return macroData.simple == '1';
}, function(checked)
{
macroData.simple = (checked) ? '1' : '0';
}));
// Viewer lightbox
div.appendChild(this.createOption(mxResources.get('lightbox'), function()
{
return macroData.lbox != '0';
}, function(checked)
{
macroData.lbox = (checked) ? '1' : '0';
}));
// Viewer centering
div.appendChild(this.createOption(mxResources.get('center'), function()
{
return macroData.pCenter == '1';
}, function(checked)
{
macroData.pCenter = (checked) ? '1' : '0';
}));
// High Resolution Preview
div.appendChild(this.createOption(mxResources.get('hiResPreview', null, 'High Res Preview'), function()
{
return (macroData.hiResPreview == null && Editor.config != null && Editor.config.hiResPreview) || macroData.hiResPreview == '1';
}, function(checked)
{
macroData.hiResPreview = (checked) ? '1' : '0';
ui.remoteInvoke('setHiResPreview', [checked], null, function(){}, function(){}); //Notify plugin of the change, ignoring both success and error callbacks
}));
// Toolbar
var stylePanel = this.createPanel();
stylePanel.style.position = 'relative';
stylePanel.style.borderWidth = '0px';
stylePanel.style.marginLeft = '0px';
stylePanel.style.paddingTop = '8px';
stylePanel.style.paddingBottom = '4px';
stylePanel.style.fontWeight = 'normal';
stylePanel.className = 'geToolbarContainer';
mxUtils.write(stylePanel, mxResources.get('toolbar'));
// Adds toolbar options
var tbSelect = document.createElement('select');
tbSelect.style.position = 'absolute';
tbSelect.style.right = '20px';
tbSelect.style.width = '97px';
tbSelect.style.marginTop = '-2px';
var opts = [{value: 'top', title: mxResources.get('top')},
{value: 'inline', title: mxResources.get('embed')},
{value: 'hidden', title: mxResources.get('hidden')}]
var validTb = false;
for (var i = 0; i < opts.length; i++)
{
validTb = validTb || macroData.tbstyle == opts[i].value;
var tbOption = document.createElement('option');
tbOption.setAttribute('value', opts[i].value);
mxUtils.write(tbOption, opts[i].title);
tbSelect.appendChild(tbOption);
}
tbSelect.value = (validTb) ? macroData.tbstyle : 'top';
stylePanel.appendChild(tbSelect);
div.appendChild(stylePanel);
mxEvent.addListener(tbSelect, 'change', function(evt)
{
macroData.tbstyle = tbSelect.value;
mxEvent.consume(evt);
});
// Links
stylePanel = stylePanel.cloneNode(false);
stylePanel.style.paddingTop = '4px';
mxUtils.write(stylePanel, mxResources.get('links'));
// Adds links options
var linksSelect = document.createElement('select');
linksSelect.style.position = 'absolute';
linksSelect.style.right = '20px';
linksSelect.style.width = '97px';
linksSelect.style.marginTop = '-2px';
var opts = [{value: 'auto', title: mxResources.get('automatic')},
{value: 'blank', title: mxResources.get('openInNewWindow')},
{value: 'self', title: mxResources.get('openInThisWindow')}]
var validLinks = false;
for (var i = 0; i < opts.length; i++)
{
validLinks = validLinks || macroData.links == opts[i].value;
var linkOption = document.createElement('option');
linkOption.setAttribute('value', opts[i].value);
mxUtils.write(linkOption, opts[i].title);
linksSelect.appendChild(linkOption);
}
linksSelect.value = (validLinks) ? macroData.links : 'auto';
stylePanel.appendChild(linksSelect);
div.appendChild(stylePanel);
mxEvent.addListener(linksSelect, 'change', function(evt)
{
macroData.links = linksSelect.value;
mxEvent.consume(evt);
});
// Zoom
var zoomOpt = this.createRelativeOption(mxResources.get('zoom'), null, null, function(input)
{
var value = (input.value == '') ? 100 : parseInt(input.value);
value = Math.max(0, (isNaN(value)) ? 100 : value);
input.value = value + ' %';
macroData.zoom = value / 100;
}, function(input)
{
input.value = (parseFloat(macroData.zoom || 1) * 100) + '%';
});
zoomOpt.style.fontWeight = 'normal';
zoomOpt.style.paddingBottom = '6px';
zoomOpt.style.paddingTop = '6px';
zoomOpt.style.border = 'none';
div.appendChild(zoomOpt);
//Page and layers settings
div.appendChild(this.createTitle(mxResources.get('pageLayers', null, 'Page and Layers')));
var hasAspect = false;
var pageId = null, layerIds = null;
var customizeBtn = mxUtils.button(mxResources.get('customize', null, 'Customize'), function()
{
var dlg = new AspectDialog(ui, pageId, layerIds, function(info)
{
pageId = info.pageId;
layerIds = info.layerIds;
macroData.aspect = pageId + ' ' + layerIds.join(' ');
ui.remoteInvoke('setAspect', [macroData.aspect], null, function(){}, function(){}); //Notify plugin of the change, ignoring both success and error callbacks
});
ui.showDialog(dlg.container, 700, 465, true, true);
dlg.init();
});
customizeBtn.className = 'geColorBtn';
customizeBtn.style.marginLeft = '10px';
customizeBtn.style.padding = '2px';
customizeBtn.setAttribute('disabled', 'disabled');
if (macroData.aspect != null)
{
var aspectArray = macroData.aspect.split(' ');
if (aspectArray.length > 0)
{
pageId = aspectArray[0];
layerIds = aspectArray.slice(1);
hasAspect = true;
customizeBtn.removeAttribute('disabled');
}
}
var firstPageRadio = ui.addRadiobox(div, 'pageLayers', mxResources.get('firstPage', null, 'First Page (All Layers)'), !hasAspect);
firstPageRadio.style.marginTop = '4px';
mxEvent.addListener(firstPageRadio, 'change', function()
{
if (this.checked)
{
macroData.aspect = null;
ui.remoteInvoke('setAspect', [macroData.aspect], null, function(){}, function(){}); //Notify plugin of the change, ignoring both success and error callbacks
customizeBtn.setAttribute('disabled', 'disabled');
}
});
var currentStateRadio = ui.addRadiobox(div, 'pageLayers', mxResources.get('curEditorState', null, 'Current Editor State'), false);
currentStateRadio.style.marginTop = '8px';
mxEvent.addListener(currentStateRadio, 'change', function()
{
if (this.checked)
{
var curPage = ui.updatePageRoot(ui.currentPage);
var layerIds = [], layers = curPage.root.children;
for (var i = 0; i < layers.length; i++)
{
if (layers[i].visible != false)
{
layerIds.push(layers[i].id);
}
}
macroData.aspect = curPage.getId() + ' ' + layerIds.join(' ');
ui.remoteInvoke('setAspect', [macroData.aspect], null, function(){}, function(){}); //Notify plugin of the change, ignoring both success and error callbacks
customizeBtn.setAttribute('disabled', 'disabled');
}
});
var customStateRadio = ui.addRadiobox(div, 'pageLayers', mxResources.get('custom', null, 'Custom'), hasAspect, false, true);
customStateRadio.style.marginTop = '8px';
mxEvent.addListener(customStateRadio, 'change', function()
{
if (this.checked)
{
customizeBtn.removeAttribute('disabled');
}
});
div.appendChild(customizeBtn);
return div;
};
if (ui.format != null)
{
ui.format.refresh();
}
//Adding Link to Confluence Page Anchor
var origLinkDialog = LinkDialog;
LinkDialog = function(editorUi, initialValue, btnLabel, fn, showPages)
{
function modFn(link, selDoc)
{
if (anchorRadio.checked)
{
fn('data:confluence/anchor,' + anchorSelect.value);
}
else
{
fn(link, selDoc);
}
};
origLinkDialog.call(this, editorUi, initialValue, btnLabel, modFn, showPages);
var baseUrl = '';
ui.remoteInvoke('getBaseUrl', null, null, function(url)
{
baseUrl = url;
},
function()
{
//Extremely rare, we can safely ignore since the editor won't work
});
var inner = this.container.querySelector('.geTitle'), urlInput = inner.querySelector('input[type="text"]'), urlCheck = urlInput.previousSibling;
urlInput.style.width = '680px';
var lbl = document.createElement('div');
mxUtils.write(lbl, mxResources.get('confAnchor') + ':');
inner.appendChild(lbl);
function addOption(select, name, value, isDisabled, isSelected)
{
var opt = document.createElement('option');
if (isDisabled)
{
opt.setAttribute('disabled', 'disabled');
}
if (isSelected)
{
opt.setAttribute('selected', 'selected');
}
if (value)
{
opt.setAttribute('value', value);
}
mxUtils.write(opt, name);
select.appendChild(opt);
}
var anchorRadio = document.createElement('input');
anchorRadio.style.cssText = 'margin-right:8px;margin-bottom:8px;';
anchorRadio.setAttribute('value', 'url');
anchorRadio.setAttribute('type', 'radio');
var anchorSelect = document.createElement('select');
anchorSelect.style.marginTop = '6px';
anchorSelect.style.width = '680px';
var anchorBusyIcn = document.createElement('img');
anchorBusyIcn.src = '/images/spin.gif';
anchorBusyIcn.style.position = 'absolute';
var selAnchor = null;
if (initialValue != null && initialValue.substring(0, 23) == 'data:confluence/anchor,')
{
urlInput.value = '';
selAnchor = initialValue.substring(23);
anchorRadio.setAttribute('checked', 'checked');
anchorRadio.defaultChecked = true;
}
ui.remoteInvoke('getCurPageAnchors', null, null, function(headings)
{
addOption(anchorSelect, headings.length == 0? mxResources.get('noAnchorsFound') : mxResources.get('confAnchor'), null, true, selAnchor == null);
if (headings.length == 0)
{
anchorSelect.setAttribute('disabled', 'disabled');
anchorRadio.setAttribute('disabled', 'disabled');
}
else
{
for(var i = 0; i < headings.length; i++)
{
addOption(anchorSelect, headings[i], headings[i], false, selAnchor == headings[i]);
}
}
anchorBusyIcn.style.display = 'none';
}, function()
{
anchorSelect.style.border = '1px solid red';
anchorSelect.setAttribute('disabled', 'disabled');
anchorRadio.setAttribute('disabled', 'disabled');
anchorBusyIcn.style.display = 'none';
});
mxEvent.addListener(anchorSelect, 'focus', function()
{
anchorRadio.setAttribute('checked', 'checked');
anchorRadio.checked = true;
});
inner.appendChild(anchorRadio);
inner.appendChild(anchorSelect);
inner.appendChild(anchorBusyIcn);
//Attachments select
lbl = document.createElement('div');
mxUtils.write(lbl, mxResources.get('attachments') + ':');
inner.appendChild(lbl);
var attSelect = document.createElement('select');
attSelect.style.margin = '6px 0 5px 0';
attSelect.style.width = '705px';
var attBusyIcn = document.createElement('img');
attBusyIcn.src = '/images/spin.gif';
attBusyIcn.style.position = 'absolute';
var attMap = {};
ui.remoteInvoke('getCurPageAttachments', null, null, function(atts)
{
addOption(attSelect, atts.length == 0? mxResources.get('noAttachments') : mxResources.get('attachments'), null, true, true);
if (atts.length == 0)
{
attSelect.setAttribute('disabled', 'disabled');
}
else
{
atts = atts.filter(function(a)
{
//Exclude draft and temp files
return a.metadata.mediaType != 'application/vnd.jgraph.mxfile.cached' && !/^\~.+\.tmp$/.test(a.title);
});
for(var i = 0; i < atts.length; i++)
{
attMap[atts[i].id] = atts[i];
addOption(attSelect, atts[i].title, atts[i].id, false, false);
}
}
attBusyIcn.style.display = 'none';
}, function()
{
attSelect.style.border = '1px solid red';
attSelect.setAttribute('disabled', 'disabled');
attBusyIcn.style.display = 'none';
});
function setUrlValue(content)
{
//Attachment webui link doesn't work, so build it
if (content.type == 'attachment')
{
var pageId = content._expandable.container.match(/\d+/)[0];
urlInput.value = baseUrl + '/pages/viewpageattachments.action?pageId='
+ pageId
+ '&preview=/' + pageId + '/' + content.id.replace('att', '') + '/'
+ encodeURIComponent(content.title);
}
else
{
urlInput.value = baseUrl + content._links.webui;
}
urlCheck.checked = true;
};
mxEvent.addListener(attSelect, 'change', function()
{
var att = attMap[attSelect.value];
if (att.metadata.mediaType == 'application/vnd.jgraph.mxfile')
{
attBusyIcn.style.display = '';
var pageId = att._expandable.container;
pageId = pageId.substr(pageId.lastIndexOf('/') + 1);
ui.remoteInvoke('getPageDrawioDiagrams', [pageId], null, function(drawioCCs)
{
var attCC = drawioCCs.filter(function(c)
{
return c.info.name == att.title;
})[0];
if (attCC)
{
setUrlValue(attCC.obj);
}
else
{
setUrlValue(att);
}
attBusyIcn.style.display = 'none';
}, function()
{
attSelect.style.border = '1px solid red';
attBusyIcn.style.display = 'none';
});
}
else
{
setUrlValue(att);
}
});
inner.appendChild(attSelect);
inner.appendChild(attBusyIcn);
//Search
lbl = document.createElement('div');
mxUtils.write(lbl, mxResources.get('search') + ':');
inner.appendChild(lbl);
var searchInput = document.createElement('input');
searchInput.placeholder = mxResources.get('search');
searchInput.style.margin = '6px 5px 5px 0';
searchInput.style.width = '490px';
var spaceSelect = document.createElement('select');
spaceSelect.style.marginTop = '6px';
spaceSelect.style.width = '202px';
var spaceBusyIcn = document.createElement('img');
spaceBusyIcn.src = '/images/spin.gif';
spaceBusyIcn.style.position = 'absolute';
var searchResult = document.createElement('div');
searchResult.style.cssText = 'border: 1px solid black;width: 705px;height:200px;overflow-y:auto; overflow-x:hidden';
addOption(spaceSelect, mxResources.get('allSpaces'), '*', false, true);
var typesMap = {
'page': mxResources.get('page'),
'attachment': mxResources.get('attachment', null, 'Attachment'),
'blogpost': mxResources.get('blog'),
'ac:com.mxgraph.confluence.plugins.diagramly:drawio-diagram': mxResources.get('drawDiag')
};
ui.remoteInvoke('getAvailableSpaces', null, null, function(spaces)
{
for(var i = 0; i < spaces.length; i++)
{
addOption(spaceSelect, spaces[i].title, spaces[i].space.key, false, false);
}
spaceBusyIcn.style.display = 'none';
}, function()
{
//We'll use all spaces and ignore error
spaceBusyIcn.style.display = 'none';
});
var searchTimeout = null, searchResultsMap = {};
function resultRowClick()
{
var cId = this.getAttribute('data-url');
setUrlValue(searchResultsMap[cId]);
};
function doSearch()
{
clearTimeout(searchTimeout);
if(searchInput.value != '')
{
searchResult.innerHTML = '';
searchResultsMap = {};
ui.remoteInvoke('contentSearch', [searchInput.value, spaceSelect.value == '*'? null : [spaceSelect.value]], null, function(results)
{
searchResult.innerHTML = '';
results = results.filter(function(r)
{
//Exclude draft files and diagram files (since it is returned as custom contents)
return r.metadata.mediaType != 'application/vnd.jgraph.mxfile.cached' && r.metadata.mediaType != 'application/vnd.jgraph.mxfile';
});
if (results.length == 0)
{
searchResult.innerHTML = mxResources.get('noSearchResults');
}
else
{
var table = document.createElement('table');
table.className = 'geStripedTable';
table.innerHTML = '