Skip to content

Commit 0a6bccc

Browse files
committed
Added lazy loading for jsTree #88
* Added findAllJSTree to return JSTree format from the File Table * Updated tests and test files Signed-off-by: Jillian Daguil <[email protected]>
1 parent b4dd8a4 commit 0a6bccc

File tree

3 files changed

+48
-45
lines changed

3 files changed

+48
-45
lines changed

assets/js/aboutCodeDB.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,27 @@ class AboutCodeDB {
8484
return this.db.then(() => this.Component.findOne(query));
8585
}
8686

87+
// Uses findAll to return JSTree format from the File Table
88+
findAllJSTree(query) {
89+
return this.db
90+
.then(() => {
91+
return this.File.findAll($.extend(query, {
92+
attributes: ["path", "parent", "name", "type"]
93+
}));
94+
})
95+
.then((files) => {
96+
return files.map((file) => {
97+
return {
98+
id: file.path,
99+
text: file.name,
100+
parent: file.parent,
101+
type: file.type,
102+
children: file.type === "directory"
103+
};
104+
});
105+
});
106+
}
107+
87108
// Uses the components table to create or set a component
88109
setComponent(component) {
89110
return this.findComponent({
@@ -152,31 +173,6 @@ class AboutCodeDB {
152173
}));
153174
}
154175

155-
// Format for jstree
156-
// [
157-
// {id: root, text: root, parent: #, type: directory}
158-
// {id: root/file1, text: file1, parent: root, type: file},
159-
// {id: root/file2, text: file2, parent: root, type: file}
160-
// ]
161-
toJSTreeFormat() {
162-
return this.db
163-
.then(() => {
164-
return this.File.findAll({
165-
attributes: ["path", "parent", "name", "type"]
166-
});
167-
})
168-
.then((files) => {
169-
return files.map((file) => {
170-
return {
171-
id: file.path,
172-
text: file.name,
173-
parent: file.parent,
174-
type: file.type
175-
};
176-
});
177-
});
178-
}
179-
180176
// ScanCode Scan Details Model definitions
181177
static scanCodeModel(sequelize) {
182178
return sequelize.define("scancode", {

assets/js/renderer.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ $(document).ready(function () {
3131
// Create and setup the jstree, and the click-event logic
3232
const jstree = $("#jstree").jstree(
3333
{
34+
"core": {
35+
"data": function (currentDirectory, callback) {
36+
aboutCodeDB
37+
.findAllJSTree({
38+
where: {
39+
parent: currentDirectory.id
40+
}
41+
})
42+
.then((children) => {
43+
callback.call(this, children)
44+
});
45+
}
46+
},
3447
"types": {
3548
"directory": {
3649
"icon": "glyphicon glyphicon-folder-close"
@@ -376,11 +389,7 @@ $(document).ready(function () {
376389
nodeView = new AboutCodeNodeView(aboutCodeDB, onNodeClick);
377390

378391
// loading data into jstree
379-
aboutCodeDB.toJSTreeFormat()
380-
.then(function(data) {
381-
jstree.jstree(true).settings.core.data = data;
382-
jstree.jstree(true).refresh(true);
383-
});
392+
jstree.jstree(true).refresh(true);
384393
})
385394
.catch(function(reason) {
386395
throw reason;
@@ -514,11 +523,7 @@ $(document).ready(function () {
514523
nodeView = new AboutCodeNodeView(aboutCodeDB, onNodeClick);
515524

516525
// loading data into jstree
517-
aboutCodeDB.toJSTreeFormat()
518-
.then(function (data) {
519-
jstree.jstree(true).settings.core.data = data;
520-
jstree.jstree(true).refresh(true);
521-
});
526+
jstree.jstree(true).refresh(true);
522527
})
523528
.catch(function (reason) {
524529
throw reason;

test/aboutCodeDB.test.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,29 @@ describe("checkAboutCodeDB", function() {
8585
});
8686
});
8787

88-
describe("toJSTreeFormat", function() {
88+
describe("findAllJSTree", function() {
8989
it("should format ScanCode results to jsTree Format", function() {
9090
let aboutCodeDB = new AboutCodeDB();
9191
let expectedJSTreeFormat= [
9292
{
93-
id: 'samples/README',
94-
text: 'README',
95-
parent: 'samples',
96-
type: 'file'
93+
id: "samples/README",
94+
text: "README",
95+
parent: "samples",
96+
type: "file",
97+
children: false
9798
},
9899
{
99-
id: 'samples/JGroups/src',
100-
text: 'src',
101-
parent: 'samples/JGroups',
102-
type: 'directory'
100+
id: "samples/JGroups/src",
101+
text: "src",
102+
parent: "samples/JGroups",
103+
type: "directory",
104+
children: true
103105
}
104106
];
105107

106108
return aboutCodeDB.db
107109
.then(() => aboutCodeDB.addScanData(scanCodeJSONResults))
108-
.then(() => aboutCodeDB.toJSTreeFormat())
110+
.then(() => aboutCodeDB.findAllJSTree())
109111
.then((scanCodeJSONResults) => {
110112
assert.deepEqual(expectedJSTreeFormat, scanCodeJSONResults)
111113
})

0 commit comments

Comments
 (0)