forked from finos/jupyterlab_templates
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP move to tree-finder, fixes finos#221
- Loading branch information
Showing
7 changed files
with
347 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/****************************************************************************** | ||
* | ||
* Copyright (c) 2020, the jupyterlab_templates authors. | ||
* | ||
* This file is part of the jupyterlab_templates library, distributed under the terms of | ||
* the Apache License 2.0. The full license can be found in the LICENSE file. | ||
* | ||
*/ | ||
import {Dialog, showDialog} from "@jupyterlab/apputils"; | ||
import {PageConfig} from "@jupyterlab/coreutils"; | ||
import {request} from "requests-helper"; | ||
import {OpenTemplateWidget} from "./widget"; | ||
|
||
export const execute = (templates, app, browser) => { | ||
const gobutton = Dialog.okButton({label: "GO"}); | ||
showDialog({ | ||
body: new OpenTemplateWidget(templates, gobutton), | ||
buttons: [Dialog.cancelButton(), gobutton], | ||
focusNodeSelector: "input", | ||
title: "Template", | ||
}).then((result) => { | ||
if (result.button.label === "Cancel") { | ||
return; | ||
} | ||
if (result.value) { | ||
request("get", `${PageConfig.getBaseUrl()}templates/get`, { | ||
template: result.value, | ||
}).then((res2) => { | ||
const data = res2.json(); | ||
const {path} = browser.tracker.currentWidget.model; | ||
|
||
return new Promise((resolve) => { | ||
const ext = data.filename.split(".").pop().toLowerCase(); | ||
const isNotebook = ext === "ipynb"; | ||
app.commands | ||
.execute("docmanager:new-untitled", { | ||
ext, | ||
path, | ||
type: isNotebook ? "notebook" : "file", | ||
}) | ||
.then((model) => { | ||
app.commands | ||
.execute("docmanager:open", { | ||
factory: isNotebook ? "Notebook" : null, | ||
path: model.path, | ||
}) | ||
.then((widget) => { | ||
// eslint-disable-next-line no-param-reassign | ||
widget.isUntitled = true; | ||
widget.context.ready.then(() => { | ||
if (isNotebook) { | ||
widget.model.fromString(data.content); | ||
} else { | ||
widget.content.editor._editor.setValue(data.content); | ||
} | ||
resolve(widget); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
}); | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/****************************************************************************** | ||
* | ||
* Copyright (c) 2020, the jupyterlab_templates authors. | ||
* | ||
* This file is part of the jupyterlab_templates library, distributed under the terms of | ||
* the Apache License 2.0. The full license can be found in the LICENSE file. | ||
* | ||
*/ | ||
import {Widget} from "@lumino/widgets"; | ||
import {IContentRow, Path} from "@tree-finder/base"; | ||
import "@tree-finder/base/dist/tree-finder.css"; | ||
import "@tree-finder/base/style/theme/material.css"; | ||
|
||
export function bool() { | ||
return Math.random() < 0.5; | ||
} | ||
|
||
// randomize array in-place using Durstenfeld shuffle algorithm | ||
// ref: https://stackoverflow.com/a/12646864 | ||
export function shuffle(arr, inPlace = false) { | ||
arr = inPlace ? arr : [...arr]; | ||
|
||
for (let i = arr.length - 1; i > 0; i--) { | ||
const j = Math.floor(Math.random() * (i + 1)); | ||
[arr[i], arr[j]] = [arr[j], arr[i]]; | ||
} | ||
|
||
return arr; | ||
} | ||
export const ALLIED_PHONETIC = [ | ||
"able", | ||
"baker", | ||
"charlie", | ||
"dog", | ||
"easy", | ||
"fox", | ||
"george", | ||
"how", | ||
"item", | ||
"jig", | ||
"king", | ||
"love", | ||
"mike", | ||
"nan", | ||
"oboe", | ||
"peter", | ||
"queen", | ||
"roger", | ||
"sugar", | ||
"tare", | ||
"uncle", | ||
"victor", | ||
"william", | ||
"xray", | ||
"yoke", | ||
"zebra", | ||
]; | ||
|
||
const _mockCache = {}; | ||
let mockFileIx = 0; | ||
let modDaysIx = -1; | ||
|
||
function mockContent(props) { | ||
// infinite recursive mock contents | ||
const {path, kind, modDays = modDaysIx++, nchildren = 100, ndirectories = 10, randomize = false} = props; | ||
const modified = new Date(modDays * 24 * 60 * 60 * 1000); | ||
const writable = randomize && bool(); | ||
|
||
if (kind === "dir") { | ||
// is a dir | ||
return { | ||
kind, | ||
path, | ||
modified, | ||
writable, | ||
getChildren: async () => { | ||
const pathstr = path.join("/"); | ||
|
||
if (pathstr in _mockCache) { | ||
return _mockCache[pathstr]; | ||
} | ||
|
||
const children = []; | ||
const dirNames = randomize ? shuffle(ALLIED_PHONETIC) : ALLIED_PHONETIC; | ||
|
||
for (let i = 0; i < nchildren; i++) { | ||
children.push( | ||
mockContent({ | ||
kind: i < ndirectories ? "dir" : "text", | ||
path: [...path, i < ndirectories ? `${dirNames[i]}` : `file_${`${mockFileIx++}`.padStart(7, "0")}.txt`], | ||
nchildren, | ||
ndirectories, | ||
randomize, | ||
}), | ||
); | ||
} | ||
|
||
_mockCache[pathstr] = children; | ||
return children; | ||
}, | ||
}; | ||
} | ||
// is a file | ||
return { | ||
kind, | ||
path, | ||
modified, | ||
writable, | ||
}; | ||
} | ||
|
||
const root = mockContent({ | ||
kind: "dir", | ||
path: [], | ||
randomize: true, | ||
}); | ||
|
||
export class OpenTemplateWidget extends Widget { | ||
constructor(templates, gobutton) { | ||
const body = document.createElement("div"); | ||
body.classList.add("jp-Template-Browser"); | ||
const label = document.createElement("label"); | ||
label.textContent = "Template:"; | ||
|
||
// const package_input = document.createElement("select"); | ||
// const notebook_input = document.createElement("select"); | ||
|
||
// Object.keys(templates).forEach((package_name) => { | ||
// const package_option = document.createElement("option"); | ||
// package_option.label = package_name; | ||
// package_option.text = package_name; | ||
// package_option.value = package_name; | ||
// package_input.appendChild(package_option); | ||
// }); | ||
|
||
// const fill = (package_name) => { | ||
// while (notebook_input.lastChild) { | ||
// notebook_input.removeChild(notebook_input.lastChild); | ||
// } | ||
|
||
// templates[package_name].forEach((notebook) => { | ||
// const notebook_option = document.createElement("option"); | ||
// notebook_option.label = notebook.name; | ||
// notebook_option.text = notebook.name; | ||
// notebook_option.value = notebook.name; | ||
// notebook_input.appendChild(notebook_option); | ||
// }); | ||
// }; | ||
|
||
// package_input.addEventListener("change", (event) => { | ||
// const package_name = event.target.value; | ||
// fill(package_name); | ||
// }); | ||
|
||
// if (Object.keys(templates).length > 0) { | ||
// fill(Object.keys(templates)[0]); | ||
// } | ||
|
||
// body.appendChild(label); | ||
// body.appendChild(package_input); | ||
// body.appendChild(notebook_input); | ||
super({node: body}); | ||
} | ||
|
||
onAfterAttach(msg) { | ||
super.onAfterAttach(msg); | ||
console.log("HERE"); | ||
this.treeFinder = document.createElement("tree-finder-panel"); | ||
this.treeFinder.classList.add("jp-Template-Browser"); | ||
this.node.appendChild(this.treeFinder); | ||
this.init(); | ||
} | ||
|
||
init = async () => { | ||
await this.treeFinder.init({ | ||
root, | ||
gridOptions: { | ||
doWindowResize: true, | ||
showFilter: true, | ||
}, | ||
}); | ||
}; | ||
|
||
getValue = () => this.node.getElementsByTagName("select")[1].value; | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters