RefGraph.php.view.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // @require variables:
  2. if ('undefined' === typeof HTML_ID_REF_GRAPH) throw "Missing HTML_ID_REF_GRAPH";
  3. if ('undefined' === typeof TYPENAME) throw "Missing TYPENAME";
  4. if ('undefined' === typeof PRIMARY_KEY) throw "Missing PRIMARY_KEY";
  5. // if ('undefined' === typeof FUNCTION_FETCH_CHILDRENS) throw "Missing FUNCTION_FETCH_CHILDRENS";
  6. // if ('undefined' === typeof FUNCTION_FETCH_PARENTS) throw "Missing FUNCTION_FETCH_PARENTS";
  7. // if ('undefined' === typeof JS_CHANNEL_UPDATE_NAME) throw "Missing JS_CHANNEL_UPDATE_NAME";
  8. var DBG = DBG || 0;
  9. var _nodes = new vis.DataSet(); // [ { id: '', label: '' }, ... ]
  10. var _edges = new vis.DataSet(); // [ { id: '', from: '', to: '' }, ... ]
  11. var _network = null; // graph object
  12. var _todoGraphData = [ { nodes: [], edges: [] } ]; // data by levels
  13. var _defaultWfsParams = {
  14. resolve: 'all',
  15. resolveDepth: 2,
  16. }
  17. var _defaultVisJsOptions = {
  18. nodes: {
  19. shape: 'box'
  20. },
  21. interaction: {
  22. dragNodes: false
  23. },
  24. physics: {
  25. enabled: false
  26. },
  27. layout: {
  28. hierarchical: {
  29. direction: 'LR',
  30. levelSeparation: 500, // hierarchical.levelSeparation Number 150 The distance between the different levels.
  31. nodeSpacing: 40, // 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.
  32. 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.
  33. sortMethod: 'directed'
  34. // 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.
  35. // 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!
  36. // 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!
  37. // hierarchical.parentCentralization Boolean true When true, the parents nodes will be centered again after the the layout algorithm has been finished.
  38. // 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.
  39. // hierarchical.sortMethod String 'hubsize' The algorithm used to ascertain the levels of the nodes based on the data. The possible options are: hubsize, directed.
  40. //
  41. // Hubsize takes the nodes with the most edges and puts them at the top. From that the rest of the hierarchy is evaluated.
  42. //
  43. // Directed adheres to the to and from data of the edges. A --> B so B is a level lower than A.
  44. }
  45. }
  46. };
  47. (function () {
  48. var form = document.getElementById('wfs_request')
  49. var featureTypeName = TYPENAME
  50. var wfsParams = Object.assign({}, _defaultWfsParams, {
  51. primaryKey: PRIMARY_KEY,
  52. })
  53. if(DBG)console.log('p5WFS_GetFeature', featureTypeName, wfsParams)
  54. p5WFS_GetFeature(featureTypeName, wfsParams).then(function (features) {
  55. if(DBG)console.log('features', features)
  56. updateWfsResponse(features, featureTypeName)
  57. }).catch(function (e) {
  58. if(DBG)console.warn(e)
  59. p5UI__notifyAjaxCallback({ type: 'error', msg: e })
  60. })
  61. })();
  62. function updateWfsResponse(features, featureTypeName) {
  63. if(DBG)console.log('updateWfsResponse', { features: features, featureTypeName: featureTypeName })
  64. // if (_network !== null) {
  65. // _network.destroy();
  66. // _network = null;
  67. // }
  68. parseResponseRec(_todoGraphData, features, featureTypeName)
  69. var _graphData = {
  70. nodes: _nodes,
  71. edges: _edges,
  72. };
  73. _todoGraphData.forEach(function (levelData) {
  74. if (levelData.nodes && levelData.nodes.length) {
  75. levelData.nodes.forEach(function (node) {
  76. try {
  77. _nodes.add(node)
  78. } catch (e) {
  79. if(DBG)console.log('_graphData.nodes.add error:', e);
  80. }
  81. })
  82. }
  83. if (levelData.edges && levelData.edges.length) {
  84. levelData.edges.forEach(function (edge) {
  85. try {
  86. _edges.add(edge)
  87. } catch (e) {
  88. if(DBG)console.log('_graphData.edges.add error:', e);
  89. }
  90. })
  91. }
  92. })
  93. // create a network
  94. var container = document.getElementById(HTML_ID_REF_GRAPH)
  95. container.style.height = '' + (window.innerHeight - 40) + 'px'
  96. var options = _defaultVisJsOptions
  97. if(DBG)console.log('_graphData', _graphData)
  98. if (!_network) {
  99. _network = new vis.Network(container, _graphData, options); // graphData: { nodes: [], edges: [] }
  100. // add event listeners
  101. _network.on('selectNode', function (params) {
  102. console.log('Selection: ', params.nodes)
  103. var featureID = params.nodes[0]
  104. if (!featureID) return;
  105. if ('-loading' === featureID.substr(-1 * '-loading'.length)) {
  106. // TODO: gui msg...
  107. console.log('loading nodes connected with: "' +featureID.substr(0, featureID.length - '-loading'.length) + '" ...')
  108. return;
  109. }
  110. var selectedNode = _nodes.get(featureID)
  111. if (!selectedNode) return;
  112. if (selectedNode._loaded) return;
  113. try {
  114. var featureEx = featureID.split('.')
  115. if (2 !== featureEx.length) throw "Not supported featureID format"
  116. var featureTypeName = 'default_db__x3A__' + featureEx[0] + ':' + featureEx[0]
  117. var wfsParams = Object.assign({}, _defaultWfsParams, {
  118. primaryKey: featureEx[1]
  119. });
  120. var fakeLoadingNodeID = featureID + '-loading'
  121. _nodes.add({
  122. id: fakeLoadingNodeID,
  123. label: 'loading...',
  124. })
  125. _edges.add({
  126. id: fakeLoadingNodeID,
  127. from: featureID,
  128. to: fakeLoadingNodeID
  129. })
  130. // view-source:http://visjs.org/examples/network/data/dynamicData.html
  131. p5WFS_GetFeature(featureTypeName, wfsParams).then(function (features) {
  132. if(DBG)console.log('features', features)
  133. if (!features.length || 1 !== features.length) throw "Brak danych" // require 1 feature with recurse
  134. var refFields = Object.keys(features[0]).filter(function (fieldName) {
  135. return (fieldName.indexOf(':') > -1)
  136. }).filter(function (fieldName) {
  137. return (features[0][fieldName].length > 0)
  138. })
  139. if (!refFields.length) return "Brak danych"
  140. updateWfsResponse(features, featureTypeName)
  141. return "Pobrano nowe dane"
  142. }).then(function (msg) {
  143. p5UI__notifyAjaxCallback({ type: 'info', msg: msg })
  144. _nodes.remove({ id: fakeLoadingNodeID })
  145. _edges.remove({ id: fakeLoadingNodeID })
  146. _nodes.update({ id: featureID, _loaded: true })
  147. }).catch(function (e) {
  148. if(DBG)console.warn(e)
  149. p5UI__notifyAjaxCallback({ type: 'error', msg: e })
  150. _nodes.remove({ id: fakeLoadingNodeID })
  151. _edges.remove({ id: fakeLoadingNodeID })
  152. _nodes.update({ id: featureID, _loaded: true })
  153. })
  154. } catch (e) {
  155. if(DBG)console.warn(e)
  156. }
  157. });
  158. }
  159. }
  160. function parseResponseRec(_todoGraphData, json, typeName, parentNodeId, level) {
  161. var level = level || 0
  162. var parentNodeId = parentNodeId || null
  163. if(DBG)console.log('DBG::parseResponseRec', {json:json, typeName:typeName, parentNodeId:parentNodeId, isString: p5Utils__isString(json), isArray: p5Utils__isArray(json), isObject: p5Utils__isObject(json)});
  164. if (p5Utils__isArray(json)) {
  165. // TODO: create named group
  166. var isXlinkList = (json.length > 0 && p5Utils__isString(json[0]))
  167. json.forEach(function (subJson) {
  168. if (isXlinkList) {
  169. parseResponseXlinkListRec(_todoGraphData, subJson, typeName, parentNodeId, level)
  170. } else {
  171. parseResponseRec(_todoGraphData, subJson, typeName, parentNodeId, level)
  172. }
  173. })
  174. } else if (p5Utils__isObject(json)) {
  175. var objectName = typeName.substr(typeName.indexOf(':') + 1)
  176. var nodeId = objectName + '.' + json.ID // TODO: primaryKey?
  177. {
  178. // _todoGraphData.nodes.add({ id: nodeId, label: nodeId })
  179. if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
  180. _todoGraphData[level].nodes.push({ id: nodeId, label: nodeId, group: objectName, _loaded: true })
  181. if (parentNodeId) {
  182. // _graphData.edges.add({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
  183. _todoGraphData[level].edges.push({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
  184. }
  185. }
  186. Object.keys(json).filter(function (fieldName) {
  187. return (fieldName.indexOf(':') > -1)
  188. })
  189. .forEach(function (fieldName) {
  190. var value = json[fieldName]
  191. parseResponseRec(_todoGraphData, value, fieldName, nodeId, level + 1)
  192. })
  193. } else if (p5Utils__isString(json)) {
  194. if(DBG)console.log('TODO: Not implemented - parseResponseRec isString');
  195. } else {
  196. if(DBG)console.log('TODO: Not implemented - parseResponseRec is not string, not array and not object');
  197. }
  198. }
  199. function parseResponseXlinkListRec(_todoGraphData, json, typeName, parentNodeId, level) {
  200. if(DBG)console.log('DBG::parseResponseRec:XlinkList', {json:json, typeName:typeName, parentNodeId:parentNodeId, isString: p5Utils__isString(json), isArray: p5Utils__isArray(json), isObject: p5Utils__isObject(json)});
  201. if (p5Utils__isString(json)) { // xlink "https://biuro.biall-net.pl/wfs/default_db/BI_audit_ENERGA_RUM_KONTRAHENCI#BI_audit_ENERGA_RUM_KONTRAHENCI.9233",
  202. var nodeId = json.substr(json.indexOf('#') + 1)
  203. var objectName = typeName
  204. {
  205. // _graphData.nodes.add({ id: nodeId, label: nodeId })
  206. if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
  207. _todoGraphData[level].nodes.push({ id: nodeId, label: nodeId, group: objectName, _loaded: false })
  208. if (parentNodeId) {
  209. // _graphData.edges.add({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
  210. _todoGraphData[level].edges.push({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
  211. }
  212. }
  213. } else if (p5Utils__isObject(json)) {
  214. if(DBG)console.log('TODO: Not implemented - parseResponseRec isObject - fetch more xlink object');
  215. console.log('TODO: Not implemented - parseResponseRec isObject - fetch more xlink object', json);
  216. // var nodeId = json.substr(json.indexOf('#') + 1)
  217. // var objectName = typeName
  218. // {
  219. // // _graphData.nodes.add({ id: nodeId, label: nodeId })
  220. // if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
  221. // _todoGraphData[level].nodes.push({ id: nodeId, label: nodeId, group: objectName })
  222. // if (parentNodeId) {
  223. // // _graphData.edges.add({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
  224. // _todoGraphData[level].edges.push({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
  225. // }
  226. // }
  227. } else {
  228. if(DBG)console.log('TODO: Not implemented - parseResponseRec:XlinkList is not string and not object');
  229. }
  230. }
  231. // global['FUNCTION_FETCH_CHILDRENS'] = 'refGraphFetchChildrens'
  232. // global['FUNCTION_FETCH_PARENTS'] = 'refGraphFetchParents'