Skip to content

Commit

Permalink
[feature] Allow loading more devices using pagination #117
Browse files Browse the repository at this point in the history
closes #117
  • Loading branch information
totallynotvaishnav committed Sep 1, 2022
1 parent 441c123 commit d804a94
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion dist/netjsongraph.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions examples/netjsonmap-nodeTiles.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@

if (typeof tilesMap[curZoom] === "string") {
// json update function
this.utils
.JSONParamParse(folderName + tilesMap[curZoom])
this.utils.paginatedDataParse
.call(this, folderName + tilesMap[curZoom])
.then((JSONData) => {
// store the data.
tilesMap[curZoom] = JSONData;
Expand Down
1 change: 1 addition & 0 deletions src/js/netjsongraph.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const NetJSONGraphDefaultConfig = {
metadata: true,
svgRender: false,
switchMode: false,
maxPointsFetched: 10000,
showMetaOnNarrowScreens: false,
echartsOption: {
aria: {
Expand Down
5 changes: 2 additions & 3 deletions src/js/netjsongraph.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ class NetJSONGraph {
this.config.onRender.call(this);
this.event.once("onReady", this.config.onReady.bind(this));
this.event.once("onLoad", this.config.onLoad.bind(this));

this.utils
.JSONParamParse(JSONParam)
this.utils.paginatedDataParse
.call(this, JSONParam)
.then((JSONData) => {
if (this.utils.isNetJSON(JSONData)) {
this.type = "netjson";
Expand Down
4 changes: 2 additions & 2 deletions src/js/netjsongraph.update.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class NetJSONGraphUpdate extends NetJSONGraphUtil {
const self = this;
self.config.onUpdate.call(self);

return self.utils
.JSONParamParse(Data)
return self.utils.paginatedDataParse
.call(self, Data)
.then((JSONData) => {
function update() {
// override data.
Expand Down
33 changes: 27 additions & 6 deletions src/js/netjsongraph.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,40 @@ class NetJSONGraphUtil {
Accept: "application/json",
},
})
.then((response) => {
if (response.json) {
return response.json();
}
return response;
})
.then((response) => response)
.catch((msg) => {
console.error(msg);
});
}
return Promise.resolve(JSONParam);
}

async paginatedDataParse(JSONParam) {
let res;
let data;
try {
let paginatedResponse = await this.utils.JSONParamParse(JSONParam);
if (paginatedResponse.json) {
res = await paginatedResponse.json();
data = res.results ? res.results : res;
while (res.next && data.nodes.length <= this.config.maxPointsFetched) {
// eslint-disable-next-line no-await-in-loop
paginatedResponse = await this.utils.JSONParamParse(res.next);
// eslint-disable-next-line no-await-in-loop
res = await paginatedResponse.json();
data.nodes = data.nodes.concat(res.results.nodes);
data.links = data.links.concat(res.results.links);
}
} else {
data = paginatedResponse;
}
} catch (e) {
console.error(e);
}

return data;
}

/**
* @function
* @name dateParse
Expand Down

0 comments on commit d804a94

Please sign in to comment.