RefGraph.php.view.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 _isFullScreen = true; // TODO: get from arg
  10. var _html = {
  11. container: null,
  12. top: null,
  13. fullscreenBtn: null,
  14. selected: null
  15. }
  16. var _nodes = new vis.DataSet(); // [ { id: '', label: '' }, ... ]
  17. var _edges = new vis.DataSet(); // [ { id: '', from: '', to: '' }, ... ]
  18. var _network = null; // graph object
  19. var _todoGraphData = [ { nodes: [], edges: [] } ]; // data by levels
  20. var _defaultWfsParams = {
  21. resolve: 'all',
  22. resolveDepth: 2,
  23. }
  24. var _defaultVisJsOptions = {
  25. nodes: {
  26. shape: 'box'
  27. },
  28. interaction: {
  29. dragNodes: false
  30. },
  31. physics: {
  32. enabled: false
  33. },
  34. layout: {
  35. hierarchical: {
  36. direction: 'LR',
  37. levelSeparation: 500, // hierarchical.levelSeparation Number 150 The distance between the different levels.
  38. 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.
  39. 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.
  40. sortMethod: 'directed'
  41. // 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.
  42. // 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!
  43. // 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!
  44. // hierarchical.parentCentralization Boolean true When true, the parents nodes will be centered again after the the layout algorithm has been finished.
  45. // 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.
  46. // hierarchical.sortMethod String 'hubsize' The algorithm used to ascertain the levels of the nodes based on the data. The possible options are: hubsize, directed.
  47. //
  48. // Hubsize takes the nodes with the most edges and puts them at the top. From that the rest of the hierarchy is evaluated.
  49. //
  50. // Directed adheres to the to and from data of the edges. A --> B so B is a level lower than A.
  51. }
  52. }
  53. };
  54. (function () {
  55. var form = document.getElementById('wfs_request')
  56. var featureTypeName = TYPENAME
  57. var wfsParams = Object.assign({}, _defaultWfsParams, {
  58. primaryKey: PRIMARY_KEY,
  59. })
  60. if(DBG)console.log('p5WFS_GetFeature', featureTypeName, wfsParams)
  61. p5WFS_GetFeature(featureTypeName, wfsParams).then(function (features) {
  62. if(DBG)console.log('features', features)
  63. updateWfsResponse(features, featureTypeName)
  64. }).catch(function (e) {
  65. if(DBG)console.warn(e)
  66. p5UI__notifyAjaxCallback({ type: 'error', msg: e })
  67. })
  68. renderLayout()
  69. })();
  70. function updateWfsResponse(features, featureTypeName) {
  71. if(DBG)console.log('updateWfsResponse', { features: features, featureTypeName: featureTypeName })
  72. // if (_network !== null) {
  73. // _network.destroy();
  74. // _network = null;
  75. // }
  76. parseResponseRec(_todoGraphData, features, featureTypeName)
  77. var _graphData = {
  78. nodes: _nodes,
  79. edges: _edges,
  80. };
  81. _todoGraphData.forEach(function (levelData) {
  82. if (levelData.nodes && levelData.nodes.length) {
  83. levelData.nodes.forEach(function (node) {
  84. try {
  85. _nodes.add(node)
  86. } catch (e) {
  87. if(DBG)console.log('_graphData.nodes.add error:', e);
  88. }
  89. })
  90. }
  91. if (levelData.edges && levelData.edges.length) {
  92. levelData.edges.forEach(function (edge) {
  93. try {
  94. _edges.add(edge)
  95. } catch (e) {
  96. if(DBG)console.log('_graphData.edges.add error:', e);
  97. }
  98. })
  99. }
  100. })
  101. var options = _defaultVisJsOptions
  102. if(DBG)console.log('_graphData', _graphData)
  103. if (!_network) {
  104. _network = new vis.Network(_html['container'], _graphData, options); // graphData: { nodes: [], edges: [] }
  105. // add event listeners
  106. _network.on('selectNode', function (params) {
  107. if(DBG)console.log('Selection: ', params.nodes)
  108. var featureID = params.nodes[0]
  109. if (!featureID) return;
  110. if ('-loading' === featureID.substr(-1 * '-loading'.length)) {
  111. // TODO: gui msg...
  112. if(DBG)console.log('loading nodes connected with: "' +featureID.substr(0, featureID.length - '-loading'.length) + '" ...')
  113. return;
  114. }
  115. var selectedNode = _nodes.get(featureID)
  116. if (!selectedNode) return;
  117. renderSelectedNode(selectedNode)
  118. if (selectedNode._loaded) return;
  119. try {
  120. var featureEx = featureID.split('.')
  121. if (2 !== featureEx.length) throw "Not supported featureID format"
  122. var featureTypeName = 'default_db__x3A__' + featureEx[0] + ':' + featureEx[0]
  123. var wfsParams = Object.assign({}, _defaultWfsParams, {
  124. primaryKey: featureEx[1]
  125. });
  126. var fakeLoadingNodeID = featureID + '-loading'
  127. _nodes.add({
  128. id: fakeLoadingNodeID,
  129. label: 'loading...',
  130. })
  131. _edges.add({
  132. id: fakeLoadingNodeID,
  133. from: featureID,
  134. to: fakeLoadingNodeID
  135. })
  136. // view-source:http://visjs.org/examples/network/data/dynamicData.html
  137. _nodes.update({
  138. id: featureID,
  139. label: makeShortLabel(selectedNode.id)
  140. })
  141. p5WFS_GetFeature(featureTypeName, wfsParams).then(function (features) {
  142. if(DBG)console.log('features', features)
  143. if (!features.length || 1 !== features.length) throw "Brak danych" // require 1 feature with recurse
  144. var refFields = Object.keys(features[0]).filter(function (fieldName) {
  145. return (fieldName.indexOf(':') > -1)
  146. }).filter(function (fieldName) {
  147. return (features[0][fieldName].length > 0)
  148. })
  149. if (!refFields.length) return "Brak danych"
  150. updateWfsResponse(features, featureTypeName)
  151. return "Pobrano nowe dane"
  152. }).then(function (msg) {
  153. p5UI__notifyAjaxCallback({ type: 'info', msg: msg })
  154. _nodes.remove({ id: fakeLoadingNodeID })
  155. _edges.remove({ id: fakeLoadingNodeID })
  156. _nodes.update({ id: featureID, _loaded: true })
  157. }).catch(function (e) {
  158. if(DBG)console.warn(e)
  159. p5UI__notifyAjaxCallback({ type: 'error', msg: e })
  160. _nodes.remove({ id: fakeLoadingNodeID })
  161. _edges.remove({ id: fakeLoadingNodeID })
  162. _nodes.update({ id: featureID, _loaded: true })
  163. })
  164. } catch (e) {
  165. if(DBG)console.warn(e)
  166. }
  167. });
  168. }
  169. }
  170. function parseResponseRec(_todoGraphData, json, typeName, parentNodeId, level) {
  171. var level = level || 0
  172. var parentNodeId = parentNodeId || null
  173. if(DBG)console.log('DBG::parseResponseRec', {json:json, typeName:typeName, parentNodeId:parentNodeId, isString: p5Utils__isString(json), isArray: p5Utils__isArray(json), isObject: p5Utils__isObject(json)});
  174. if (p5Utils__isArray(json)) {
  175. // TODO: create named group
  176. var isXlinkList = (json.length > 0 && p5Utils__isString(json[0]))
  177. json.forEach(function (subJson) {
  178. if (isXlinkList) {
  179. parseResponseXlinkListRec(_todoGraphData, subJson, typeName, parentNodeId, level)
  180. } else {
  181. parseResponseRec(_todoGraphData, subJson, typeName, parentNodeId, level)
  182. }
  183. })
  184. } else if (p5Utils__isObject(json)) {
  185. var objectName = typeName.substr(typeName.indexOf(':') + 1)
  186. var nodeId = objectName + '.' + json.ID // TODO: primaryKey?
  187. {
  188. // _todoGraphData.nodes.add({ id: nodeId, label: nodeId })
  189. if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
  190. _todoGraphData[level].nodes.push({
  191. id: nodeId,
  192. label: makeShortLabel(nodeId),
  193. group: objectName,
  194. _loaded: true,
  195. typeName: typeName,
  196. primaryKey: (json['ID']) ? json['ID'] : null // TODO: _primaryKey
  197. })
  198. if (parentNodeId) {
  199. // _graphData.edges.add({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
  200. _todoGraphData[level].edges.push({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
  201. }
  202. }
  203. Object.keys(json).filter(function (fieldName) {
  204. return (fieldName.indexOf(':') > -1)
  205. })
  206. .forEach(function (fieldName) {
  207. var value = json[fieldName]
  208. parseResponseRec(_todoGraphData, value, fieldName, nodeId, level + 1)
  209. })
  210. } else if (p5Utils__isString(json)) {
  211. if(DBG)console.log('TODO: Not implemented - parseResponseRec isString');
  212. } else {
  213. if(DBG)console.log('TODO: Not implemented - parseResponseRec is not string, not array and not object');
  214. }
  215. }
  216. function parseResponseXlinkListRec(_todoGraphData, json, typeName, parentNodeId, level) {
  217. 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)});
  218. if (p5Utils__isString(json)) { // xlink "https://biuro.biall-net.pl/wfs/default_db/BI_audit_ENERGA_RUM_KONTRAHENCI#BI_audit_ENERGA_RUM_KONTRAHENCI.9233",
  219. var nodeId = json.substr(json.indexOf('#') + 1)
  220. var objectName = typeName
  221. {
  222. // _graphData.nodes.add({ id: nodeId, label: nodeId })
  223. if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
  224. _todoGraphData[level].nodes.push({
  225. id: nodeId,
  226. label: makeShortLabel(nodeId) + ' (?)',
  227. group: objectName,
  228. _loaded: false,
  229. typeName: typeName,
  230. primaryKey: nodeId.substr(nodeId.lastIndexOf('.') + 1)
  231. })
  232. if (parentNodeId) {
  233. // _graphData.edges.add({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
  234. _todoGraphData[level].edges.push({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
  235. }
  236. }
  237. } else if (p5Utils__isObject(json)) {
  238. if(DBG)console.log('TODO: Not implemented - parseResponseRec isObject - fetch more xlink object');
  239. // var nodeId = json.substr(json.indexOf('#') + 1)
  240. // var objectName = typeName
  241. // {
  242. // // _graphData.nodes.add({ id: nodeId, label: nodeId })
  243. // if (!_todoGraphData[level]) _todoGraphData[level] = { nodes: [], edges: [] }
  244. // _todoGraphData[level].nodes.push({ id: nodeId, label: nodeId, group: objectName })
  245. // if (parentNodeId) {
  246. // // _graphData.edges.add({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
  247. // _todoGraphData[level].edges.push({ id: parentNodeId+nodeId, from: parentNodeId, to: nodeId })
  248. // }
  249. // }
  250. } else {
  251. if(DBG)console.log('TODO: Not implemented - parseResponseRec:XlinkList is not string and not object');
  252. }
  253. }
  254. function makeShortLabel(label) { // TODO: shorter name
  255. // BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row_object.1234567
  256. if (label.length < 30) return label;
  257. var exLabel = label.split('.')
  258. if (exLabel.length !== 2) return label;
  259. return exLabel[0].substr(0, 24) + '...' + exLabel[0].substr(-10) + '.' + exLabel[1];
  260. }
  261. function renderLayout(mode) {
  262. if ('undefined' !== typeof mode && _isFullScreen !== mode) _isFullScreen = mode
  263. // create a network
  264. if (!_html['container']) {
  265. _html['container'] = document.getElementById(HTML_ID_REF_GRAPH)
  266. }
  267. _html['container'].style.position = (_isFullScreen) ? 'fixed' : 'relative'
  268. _html['container'].style.top = (_isFullScreen) ? '40px' : ''
  269. _html['container'].style.left = '0px'
  270. _html['container'].style.margin = '0'
  271. _html['container'].style.border = (_isFullScreen) ? '0' : '1px solid silver'
  272. _html['container'].style.padding = '0'
  273. _html['container'].style.backgroundColor = '#fff'
  274. _html['container'].style.height = (_isFullScreen) ? ''+(window.innerHeight-40)+'px' : ''+(window.innerHeight-60-_html['container'].offsetTop)+'px'
  275. _html['container'].style.width = (_isFullScreen) ? '' + (window.innerWidth) + 'px' : '100%'
  276. if (!_html['top']) {
  277. _html['top'] = document.createElement('div')
  278. _html['container'].parentNode.insertBefore(_html['top'], _html['container'].nextSibling)
  279. }
  280. _html['top'].style.position = (_isFullScreen) ? 'fixed' : 'relative'
  281. _html['top'].style.top = '0px'
  282. _html['top'].style.left = '0px'
  283. _html['top'].style.margin = '0'
  284. _html['top'].style.border = '0'
  285. _html['top'].style.padding = '0'
  286. _html['top'].style.backgroundColor = '#101010'
  287. _html['top'].style.height = '40px'
  288. _html['top'].style.width = (_isFullScreen) ? '' + (window.innerWidth) + 'px' : '100%'
  289. _html['top'].style.zIndex = 3
  290. if (!_html['fullscreenBtn']) {
  291. _html['fullscreenBtn'] = document.createElement('button')
  292. _html['fullscreenBtn'].className = 'btn btn-lg btn-link'
  293. _html['top'].appendChild(_html['fullscreenBtn'])
  294. _html['fullscreenBtn'].style.color = '#fff'
  295. _html['fullscreenBtn'].style.float = 'right'
  296. _html['fullscreenBtn'].style.outline = 'none'
  297. _html['fullscreenBtn'].addEventListener('click', function () {
  298. _isFullScreen = !_isFullScreen
  299. renderLayout()
  300. })
  301. }
  302. _html['fullscreenBtn'].innerHTML = (_isFullScreen)
  303. ? '<i class="glyphicon glyphicon-resize-small" title="Zamknij pełny ekran"></i>'
  304. : '<i class="glyphicon glyphicon-resize-full" title="Pełny ekran"></i>'
  305. ;
  306. if (!_html['selected']) {
  307. _html['selected'] = document.createElement('span')
  308. var htmlSelectedWrap = document.createElement('div')
  309. htmlSelectedWrap.style.color = '#fff'
  310. htmlSelectedWrap.style.fontSize = '14px'
  311. htmlSelectedWrap.style.lineHeight = '40px'
  312. htmlSelectedWrap.style.paddingLeft = '12px'
  313. htmlSelectedWrap.appendChild(document.createTextNode("Wybrany: "))
  314. htmlSelectedWrap.appendChild(_html['selected'])
  315. _html['top'].appendChild(htmlSelectedWrap)
  316. _html['selected'].innerHTML = 'brak'
  317. }
  318. }
  319. function renderSelectedNode(node) {
  320. var gotToTableLink = (node.typeName)
  321. ? '<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 + '">' +
  322. '<i class="glyphicon glyphicon-list-alt"></i>' +
  323. '</a>'
  324. : ''
  325. ;
  326. var editLink = (node.typeName && node.primaryKey)
  327. ? '<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 + '">' +
  328. '<i class="glyphicon glyphicon-pencil"></i>' +
  329. '</a>'
  330. : ''
  331. ;
  332. _html['selected'].innerHTML = '' + node.id + ' ' + gotToTableLink + ' ' + editLink;
  333. }
  334. // global['FUNCTION_FETCH_CHILDRENS'] = 'refGraphFetchChildrens'
  335. // global['FUNCTION_FETCH_PARENTS'] = 'refGraphFetchParents'