| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- // @require variables:
- if ('undefined' === typeof HTML_ID_REF_GRAPH) throw "Missing HTML_ID_REF_GRAPH";
- if ('undefined' === typeof TYPENAME) throw "Missing TYPENAME";
- if ('undefined' === typeof PRIMARY_KEY) throw "Missing PRIMARY_KEY";
- // if ('undefined' === typeof FUNCTION_FETCH_CHILDRENS) throw "Missing FUNCTION_FETCH_CHILDRENS";
- // if ('undefined' === typeof FUNCTION_FETCH_PARENTS) throw "Missing FUNCTION_FETCH_PARENTS";
- // if ('undefined' === typeof JS_CHANNEL_UPDATE_NAME) throw "Missing JS_CHANNEL_UPDATE_NAME";
- var DBG = DBG || false;
- var DBG1 = true;
- var _isFullScreen = true; // TODO: get from arg
- var _html = {
- container: null,
- top: null,
- fullscreenBtn: null,
- selected: null
- }
- var _nodes = new vis.DataSet(); // [ { id: '', label: '' }, ... ]
- var _edges = new vis.DataSet(); // [ { id: '', from: '', to: '' }, ... ]
- var _network = null; // graph object
- var _todoGraphData = [ { nodes: [], edges: [] } ]; // data by levels
- var _defaultWfsParams = {
- resolve: 'all',
- resolveDepth: 2,
- }
- if ('undefined' !== typeof WFS_URL) {
- _defaultWfsParams['WFS_URL'] = WFS_URL
- }
- var _defaultVisJsOptions = {
- nodes: {
- shape: 'box'
- },
- interaction: {
- dragNodes: false
- },
- physics: {
- enabled: false
- },
- layout: {
- hierarchical: {
- direction: 'LR',
- levelSeparation: 500, // hierarchical.levelSeparation Number 150 The distance between the different levels.
- nodeSpacing: 50, // hierarchical.nodeSpacing Number 100 Minimum distance between nodes on the free axis. This is only for the initial layout. If you enable physics, the node distance there will be the effective node distance.
- treeSpacing: 500, // hierarchical.treeSpacing Number 200 Distance between different trees (independent networks). This is only for the initial layout. If you enable physics, the repulsion model will denote the distance between the trees.
- sortMethod: 'directed'
- // hierarchical.enabled Boolean false Toggle the usage of the hierarchical layout system. If this option is not defined, it is set to true if any of the properties in this object are defined.
- // hierarchical.blockShifting Boolean true Method for reducing whitespace. Can be used alone or together with edge minimization. Each node will check for whitespace and will shift it's branch along with it for as far as it can, respecting the nodeSpacing on any level. This is mainly for the initial layout. If you enable physics, they layout will be determined by the physics. This will greatly speed up the stabilization time though!
- // hierarchical.edgeMinimization Boolean true Method for reducing whitespace. Can be used alone or together with block shifting. Enabling block shifting will usually speed up the layout process. Each node will try to move along its free axis to reduce the total length of it's edges. This is mainly for the initial layout. If you enable physics, they layout will be determined by the physics. This will greatly speed up the stabilization time though!
- // hierarchical.parentCentralization Boolean true When true, the parents nodes will be centered again after the the layout algorithm has been finished.
- // hierarchical.direction String 'UD' The direction of the hierarchical layout. The available options are: UD, DU, LR, RL. To simplify: up-down, down-up, left-right, right-left.
- // hierarchical.sortMethod String 'hubsize' The algorithm used to ascertain the levels of the nodes based on the data. The possible options are: hubsize, directed.
- //
- // Hubsize takes the nodes with the most edges and puts them at the top. From that the rest of the hierarchy is evaluated.
- //
- // Directed adheres to the to and from data of the edges. A --> B so B is a level lower than A.
- }
- }
- };
- (function () {
- var form = document.getElementById('wfs_request')
- var featureTypeName = TYPENAME
- var wfsParams = Object.assign({}, _defaultWfsParams, {
- primaryKey: PRIMARY_KEY,
- })
- DBG && console.log('p5WFS_GetFeature', featureTypeName, wfsParams)
- p5WFS_GetFeature(featureTypeName, wfsParams).then(function (features) {
- DBG && console.log('features', features)
- updateWfsResponse(features, featureTypeName)
- }).catch(function (e) {
- DBG && console.warn(e)
- p5UI__notifyAjaxCallback({ type: 'error', msg: e })
- })
- renderLayout()
- })();
- function updateWfsResponse(features, featureTypeName) {
- DBG && console.log('updateWfsResponse', { features: features, featureTypeName: featureTypeName })
- // if (_network !== null) {
- // _network.destroy();
- // _network = null;
- // }
- parseResponseRec(_todoGraphData, features, featureTypeName)
- var _graphData = {
- nodes: _nodes,
- edges: _edges,
- };
- _todoGraphData.forEach(function (levelData) {
- if (levelData.nodes && levelData.nodes.length) {
- levelData.nodes.forEach(function (node) {
- try {
- _nodes.add(node)
- } catch (e) {
- DBG && console.log('_graphData.nodes.add error:', e);
- }
- })
- }
- if (levelData.edges && levelData.edges.length) {
- levelData.edges.forEach(function (edge) {
- try {
- _edges.add(edge)
- } catch (e) {
- DBG && console.log('_graphData.edges.add error:', e);
- }
- })
- }
- })
- _todoGraphData = [ { nodes: [], edges: [] } ]; // clear to default values
- var options = _defaultVisJsOptions
- DBG && console.log('_graphData', _graphData)
- if (!_network) {
- _network = new vis.Network(_html['container'], _graphData, options); // graphData: { nodes: [], edges: [] }
- _network.on('selectNode', handleNetworkSelectNode);
- // _network.on('stabilized', handleNetworkStabilized);
- }
- }
- var handleNetworkSelectNode = function (params) {
- DBG && console.log('Selection: ', params.nodes)
- var featureID = params.nodes[0]
- if (!featureID) return;
- if ('-loading' === featureID.substr(-1 * '-loading'.length)) {
- // TODO: gui msg...
- DBG && console.log('loading nodes connected with: "' +featureID.substr(0, featureID.length - '-loading'.length) + '" ...')
- return;
- }
- var selectedNode = _nodes.get(featureID)
- if (!selectedNode) return;
- if ('ref' === selectedNode._type) {
- fetchNodeMoreRefs(selectedNode, featureID)
- } else {
- renderSelectedNode(selectedNode)
- if (selectedNode._loaded) return;
- fetchNodeRecurse(selectedNode, featureID)
- }
- };
- function parseResponseRec(_todoGraphData, json, typeName, parentNodeId, level) {
- var level = level || 0
- var parentNodeId = parentNodeId || null
- DBG && console.log('DBG::parseResponseRec', {json:json, typeName:typeName, parentNodeId:parentNodeId, isString: p5Utils__isString(json), isArray: p5Utils__isArray(json), isObject: p5Utils__isObject(json)});
- if (p5Utils__isArray(json)) {
- // TODO: create named group
- var isXlinkList = (json.length > 0 && p5Utils__isString(json[0]))
- json.forEach(function (subJson) {
- if (isXlinkList) {
- parseResponseXlinkListRec(_todoGraphData, subJson, typeName, parentNodeId, level)
- } else {
- parseResponseRec(_todoGraphData, subJson, typeName, parentNodeId, level)
- }
- })
- } else if (p5Utils__isObject(json)) {
- var objectName = typeName.substr(typeName.indexOf(':') + 1)
- var nodeId = objectName + '.' + json.ID // TODO: primaryKey?
- {
- // _todoGraphData.nodes.add({ id: nodeId, label: nodeId })
- if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
- _todoGraphData[level].nodes.push({
- id: nodeId,
- label: makeShortLabel(nodeId),
- group: objectName,
- _loaded: true,
- typeName: typeName,
- primaryKey: (json['ID']) ? json['ID'] : null // TODO: _primaryKey
- })
- if (parentNodeId) {
- // _graphData.edges.add({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
- _todoGraphData[level].edges.push({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
- }
- }
- Object.keys(json).filter(function (fieldName) {
- return (fieldName.indexOf(':') > -1)
- })
- .forEach(function (fieldName) {
- var value = json[fieldName]
- parseResponseRec(_todoGraphData, value, fieldName, nodeId, level + 1)
- })
- } else if (p5Utils__isString(json)) {
- DBG && console.log('TODO: Not implemented - parseResponseRec isString');
- } else {
- DBG && console.log('TODO: Not implemented - parseResponseRec is not string, not array and not object');
- }
- }
- function parseResponseXlinkListRec(_todoGraphData, json, typeName, parentNodeId, level) {
- DBG && console.log('DBG::parseResponseRec:XlinkList', {json:json, typeName:typeName, parentNodeId:parentNodeId, isString: p5Utils__isString(json), isArray: p5Utils__isArray(json), isObject: p5Utils__isObject(json)});
- if (p5Utils__isString(json)) { // xlink "https://biuro.biall-net.pl/wfs/default_db/BI_audit_ENERGA_RUM_KONTRAHENCI#BI_audit_ENERGA_RUM_KONTRAHENCI.9233",
- var nodeId = json.substr(json.indexOf('#') + 1)
- var objectName = typeName.substr(typeName.indexOf(':') + 1)
- {
- // _graphData.nodes.add({ id: nodeId, label: nodeId })
- if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
- _todoGraphData[level].nodes.push({
- id: nodeId,
- label: makeShortLabel(nodeId) + ' (+)',
- group: objectName,
- _loaded: false,
- typeName: typeName,
- primaryKey: nodeId.substr(nodeId.lastIndexOf('.') + 1)
- })
- if (parentNodeId) {
- // _graphData.edges.add({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
- _todoGraphData[level].edges.push({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
- }
- }
- } else if (p5Utils__isObject(json)) {
- DBG && console.log('parseResponseRec isObject - fetch more xlink object');
- // example json: { type: "next",
- // @backRefNS: "default_db/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA/BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA",
- // @backRefPK: "42",
- // @typeName: "default_db__x3A__BI_audit_ENERGA_RUM_KONTRAHENCI_P…ow:BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row",
- // @startIndex: "10" }
- if (!parentNodeId) throw "Missing parentNodeId for ref link object";
- var objectName = typeName.substr(typeName.indexOf(':') + 1)
- {
- // _graphData.nodes.add({ id: nodeId, label: nodeId })
- if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
- switch (json.type) {
- case 'next': {
- DBG && console.log('TODO: next "'+json.type+'" - fetch more xlink object');
- var nodeId = 'fetch-more-features-'+json.type+'-on-' + objectName;
- _todoGraphData[level].nodes.push({
- id: nodeId,
- label: 'Pobierz więcej (+)',
- group: objectName,
- _loaded: false,
- _type: 'ref',
- parentNodeId: parentNodeId,
- ref: json,
- // typeName: typeName,
- // primaryKey: nodeId.substr(nodeId.lastIndexOf('.') + 1)
- })
- // _graphData.edges.add({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
- _todoGraphData[level].edges.push({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
- } break;
- default: {
- DBG && console.log('TODO: Not implemented - parseResponseRec isObject with type "'+json.type+'" - fetch more xlink object');
- }
- }
- }
- // var nodeId = json.substr(json.indexOf('#') + 1)
- // var objectName = typeName
- // {
- // // _graphData.nodes.add({ id: nodeId, label: nodeId })
- // if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
- // _todoGraphData[level].nodes.push({ id: nodeId, label: nodeId, group: objectName })
- // if (parentNodeId) {
- // // _graphData.edges.add({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
- // _todoGraphData[level].edges.push({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
- // }
- // }
- } else {
- DBG && console.log('TODO: Not implemented - parseResponseRec:XlinkList is not string and not object');
- }
- }
- function fetchNodeMoreRefs(selectedNode, featureID) {
- try {
- if (!selectedNode) throw "Missing featureID"
- if (!featureID) throw "Missing featureID"
- var idSelectedNode = selectedNode.id
- var idSelectedEdge = selectedNode.parentNodeId + selectedNode.id
- var parentNodeId = selectedNode.parentNodeId
- var featureTypeName = selectedNode.ref['@typeName']
- var backRefNS = selectedNode.ref['@backRefNS']
- var backRefPK = selectedNode.ref['@backRefPK']
- var backRefField = selectedNode.ref['@typeName']
- var startIndex = selectedNode.ref['@startIndex']
- _nodes.update({
- id: idSelectedNode,
- label: "Pobieranie danych..."
- })
- var wfsParams = {
- backRefNS: backRefNS,
- backRefPK: backRefPK,
- backRefField: backRefField,
- startIndex: startIndex,
- // count: 3,
- };
- p5WFS_GetFeature(featureTypeName, wfsParams).then(function (features) {
- DBG && console.log('features', features)
- if (!features.length) throw "Brak danych"
- features.forEach(function (feature) {
- // TODO: validate
- var objectName = featureTypeName.substr(featureTypeName.indexOf(':') + 1)
- var nodeId = objectName + '.' + feature.ID // TODO: primaryKey?
- _todoGraphData[0].edges.push({ id: parentNodeId + nodeId, from: parentNodeId, to: nodeId })
- // _nodes.add({
- // id: fakeLoadingNodeID,
- // label: 'loading...',
- // })
- // _edges.add({
- // id: parentNodeId + nodeId,
- // from: parentNodeId,
- // to: nodeId
- // })
- })
- // var refFields = Object.keys(features[0]).filter(function (fieldName) {
- // return (fieldName.indexOf(':') > -1)
- // }).filter(function (fieldName) {
- // return (features[0][fieldName].length > 0)
- // })
- // if (!refFields.length) return "Brak danych"
- // features = features.map(function (feature) {
- // return {
- // feature
- // }
- // })
- // console.log('TODO: features: ', features);
- // updateWfsResponse(features[0], featureTypeName) // TODO: RMME DBG
- // updateWfsResponse(features[1], featureTypeName) // TODO: RMME DBG
- // var feature = features[1]
- // var nodeId = featureTypeName + '.' + feature['ID']
- // try {
- // _edges.add({
- // id: parentNodeId + nodeId,
- // from: parentNodeId,
- // to: nodeId
- // })
- // } catch (e) {
- // DBG && console.warn('TODO: XXX: ' + e)
- // }
- DBG && console.log('TODO: remove selected after: ', {
- idSelectedNode: idSelectedNode,
- idSelectedEdge: idSelectedEdge,
- })
- features.forEach(function (feature) {
- updateWfsResponse(feature, featureTypeName)
- })
- return "Pobrano nowe dane"
- }).then(function (msg) {
- p5UI__notifyAjaxCallback({ type: 'info', msg: msg })
- _nodes.remove({ id: idSelectedNode })
- _edges.remove({ id: idSelectedEdge })
- }).catch(function (e) {
- DBG && console.warn(e)
- p5UI__notifyAjaxCallback({ type: 'error', msg: e })
- _nodes.remove({ id: idSelectedNode })
- _edges.remove({ id: idSelectedEdge })
- })
- } catch (e) {
- DBG && console.warn(e)
- }
- }
- function fetchNodeRecurse(selectedNode, featureID) {
- try {
- var selectedNodeId = selectedNode.id
- if (!selectedNode) throw "Missing featureID"
- if (!featureID) throw "Missing featureID"
- var featureEx = featureID.split('.')
- if (2 !== featureEx.length) throw "Not supported featureID format"
- var featureTypeName = 'default_db__x3A__' + featureEx[0] + ':' + featureEx[0]
- var wfsParams = Object.assign({}, _defaultWfsParams, {
- primaryKey: featureEx[1]
- });
- // var fakeLoadingNodeID = featureID + '-loading'
- // _nodes.add({
- // id: fakeLoadingNodeID,
- // label: 'loading...',
- // })
- // _edges.add({
- // id: fakeLoadingNodeID,
- // from: featureID,
- // to: fakeLoadingNodeID
- // })
- // view-source:http://visjs.org/examples/network/data/dynamicData.html
- _nodes.update({
- id: featureID,
- label: makeShortLabel(selectedNodeId) + ' ...'
- })
- p5WFS_GetFeature(featureTypeName, wfsParams).then(function (features) {
- DBG && console.log('features', features)
- if (!features.length || 1 !== features.length) throw "Brak danych" // require 1 feature with recurse
- var refFields = Object.keys(features[0]).filter(function (fieldName) {
- return (fieldName.indexOf(':') > -1)
- }).filter(function (fieldName) {
- return (features[0][fieldName].length > 0)
- })
- if (!refFields.length) return "Brak danych"
- updateWfsResponse(features, featureTypeName)
- return "Pobrano nowe dane"
- }).then(function (msg) {
- p5UI__notifyAjaxCallback({ type: 'info', msg: msg })
- // _nodes.remove({ id: fakeLoadingNodeID })
- // _edges.remove({ id: fakeLoadingNodeID })
- _nodes.update({ id: featureID, label: makeShortLabel(selectedNodeId), _loaded: true })
- }).catch(function (e) {
- DBG && console.warn(e)
- p5UI__notifyAjaxCallback({ type: 'error', msg: e })
- // _nodes.remove({ id: fakeLoadingNodeID })
- // _edges.remove({ id: fakeLoadingNodeID })
- _nodes.update({ id: featureID, label: makeShortLabel(selectedNodeId), _loaded: true })
- })
- } catch (e) {
- DBG && console.warn(e)
- }
- }
- function makeShortLabel(label) { // TODO: shorter name
- // BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object.1234567
- if (label.length < 30) return label;
- var exLabel = label.split('.')
- if (exLabel.length !== 2) return label;
- return exLabel[0].substr(0, 24) + '...' + exLabel[0].substr(-10) + '.' + exLabel[1];
- }
- function renderLayout(mode) {
- if ('undefined' !== typeof mode && _isFullScreen !== mode) _isFullScreen = mode
- // create a network
- if (!_html['container']) {
- _html['container'] = document.getElementById(HTML_ID_REF_GRAPH)
- }
- _html['container'].style.position = (_isFullScreen) ? 'fixed' : 'relative'
- _html['container'].style.top = (_isFullScreen) ? '40px' : ''
- _html['container'].style.left = '0px'
- _html['container'].style.margin = '0'
- _html['container'].style.border = (_isFullScreen) ? '0' : '1px solid silver'
- _html['container'].style.padding = '0'
- _html['container'].style.backgroundColor = '#fff'
- _html['container'].style.height = (_isFullScreen) ? ''+(window.innerHeight-40)+'px' : ''+(window.innerHeight-60-_html['container'].offsetTop)+'px'
- _html['container'].style.width = (_isFullScreen) ? '' + (window.innerWidth) + 'px' : '100%'
- if (!_html['top']) {
- _html['top'] = document.createElement('div')
- _html['container'].parentNode.insertBefore(_html['top'], _html['container'].nextSibling)
- }
- _html['top'].style.position = (_isFullScreen) ? 'fixed' : 'relative'
- _html['top'].style.top = '0px'
- _html['top'].style.left = '0px'
- _html['top'].style.margin = '0'
- _html['top'].style.border = '0'
- _html['top'].style.padding = '0'
- _html['top'].style.backgroundColor = '#101010'
- _html['top'].style.height = '40px'
- _html['top'].style.width = (_isFullScreen) ? '' + (window.innerWidth) + 'px' : '100%'
- _html['top'].style.zIndex = 3
- if (!_html['fullscreenBtn']) {
- _html['fullscreenBtn'] = document.createElement('button')
- _html['fullscreenBtn'].className = 'btn btn-lg btn-link'
- _html['top'].appendChild(_html['fullscreenBtn'])
- _html['fullscreenBtn'].style.color = '#fff'
- _html['fullscreenBtn'].style.float = 'right'
- _html['fullscreenBtn'].style.outline = 'none'
- _html['fullscreenBtn'].addEventListener('click', function () {
- _isFullScreen = !_isFullScreen
- renderLayout()
- })
- }
- _html['fullscreenBtn'].innerHTML = (_isFullScreen)
- ? '<i class="glyphicon glyphicon-resize-small" title="Zamknij pełny ekran"></i>'
- : '<i class="glyphicon glyphicon-resize-full" title="Pełny ekran"></i>'
- ;
- if (!_html['selected']) {
- _html['selected'] = document.createElement('span')
- var htmlSelectedWrap = document.createElement('div')
- htmlSelectedWrap.style.color = '#fff'
- htmlSelectedWrap.style.fontSize = '14px'
- htmlSelectedWrap.style.lineHeight = '40px'
- htmlSelectedWrap.style.paddingLeft = '12px'
- htmlSelectedWrap.appendChild(document.createTextNode("Wybrany: "))
- htmlSelectedWrap.appendChild(_html['selected'])
- _html['top'].appendChild(htmlSelectedWrap)
- _html['selected'].innerHTML = 'brak'
- }
- }
- function renderSelectedNode(node) {
- var gotToTableLink = (node.typeName)
- ? '<a href="index.php?_route=ViewTableAjax&namespace=' + node.typeName.replace('__x3A__', '/').replace(':', '/') + '" class="btn btn-link" style="color:#fff" title="Przejdź do tabeli ' + node.typeName + '">' +
- '<i class="glyphicon glyphicon-list-alt"></i>' +
- '</a>'
- : ''
- ;
- var editLink = (node.typeName && node.primaryKey)
- ? '<a href="index.php?_route=ViewTableAjax&namespace=' + node.typeName.replace('__x3A__', '/').replace(':', '/') + '#EDIT/' + node.primaryKey + '" class="btn btn-link" style="color:#fff" title="Edytuj rekord ' + node.primaryKey + '">' +
- '<i class="glyphicon glyphicon-pencil"></i>' +
- '</a>'
- : ''
- ;
- _html['selected'].innerHTML = '' + node.id + ' ' + gotToTableLink + ' ' + editLink;
- }
- // global['FUNCTION_FETCH_CHILDRENS'] = 'refGraphFetchChildrens'
- // global['FUNCTION_FETCH_PARENTS'] = 'refGraphFetchParents'
|