reworked script editor page

This commit is contained in:
gabryon99 2020-04-14 22:05:48 +02:00
parent 3f1fabc73f
commit 1e5376da95
8 changed files with 166 additions and 110 deletions

View file

@ -8,6 +8,7 @@ package.path = dirs.installdir .. "/scripts/lua/modules/?.lua;" .. package.path
local code_editor = {}
local os_utils = require "os_utils"
local template_utils = require("template_utils")
-- ################################################################
@ -21,6 +22,21 @@ end
-- ################################################################
-- Flat the subdirs hierarchy in one array
local function flat_subdirs(subdir, elements)
if (subdir.subdirs == nil) then
table.insert(elements, { title = subdir.title, active = subdir.active, href = subdir.href, })
return
end
for _, dir in ipairs(subdir.subdirs) do
flat_subdirs(dir, elements)
end
end
-- ################################################################
-- This recursive function is in charge of printing dropdown menu entries for all the plugin files (except manifest.lua in the root)
-- All the plugin subdirectories are navigated recursively, and their files printed as menu entries.
-- @param string plugin_file_path The path to the plugin file as received from the original caller (never changes during recursion)
@ -29,69 +45,61 @@ end
-- @param string subdir_level The current level of recursion (starts from 1)
-- @param string subdir_header The header printed as dropdown-header for the current subdir_path
local function plugin_subdir_files_dropdown(plugin_file_path, plugin_path, subdir_path, subdir_level, subdir_header)
local tree = {}
local plugin_contents = ntop.readdir(subdir_path)
for plugin_content, _ in pairsByKeys(plugin_contents) do
if plugin_content then
local subdir_path = os_utils.fixPath(string.format("%s/%s", subdir_path, plugin_content))
if ntop.isdir(subdir_path) then
-- If this is a directory, and the recursion level is 1, then a dropdown-header is printed.
-- Headers are not printed at every recursion level to keep the dropdown as simple as possible
if subdir_level == 1 then
print[[
<h6 class="dropdown-header">]] print(plugin_content) print[[</h6>
]]
-- The dropdown-header is also propagated
subdir_header = subdir_path
end
local leaf = {}
local subdir_path = os_utils.fixPath(string.format("%s/%s", subdir_path, plugin_content))
-- Do the actual recursive call
plugin_subdir_files_dropdown(plugin_file_path, plugin_path, subdir_path, subdir_level + 1, subdir_header)
elseif subdir_level > 1 then
-- If this is a file and is not on the root (files in the root are not shown in this function)...
local label = subdir_path:gsub(subdir_header, '')
label = label:gsub('^/+', '')
if ntop.isdir(subdir_path) then
-- If this is a directory, and the recursion level is 1, then a dropdown-header is printed.
-- Headers are not printed at every recursion level to keep the dropdown as simple as possible
if subdir_level == 1 then
leaf = { header = plugin_content }
-- The dropdown-header is also propagated
subdir_header = subdir_path
end
local active = ''
if string.ends(subdir_path, plugin_file_path) then
active = 'active'
end
-- Do the actual recursive call
leaf.subdirs = plugin_subdir_files_dropdown(plugin_file_path, plugin_path, subdir_path, subdir_level + 1, subdir_header)
tree[#tree+1] = leaf
print[[
<a class="dropdown-item ]] print(active) print[[" href="]] print(file_url(plugin_path, subdir_path)) print[[">]] print(label) print[[</a>
]]
end
elseif subdir_level > 1 then
-- If this is a file and is not on the root (files in the root are not shown in this function)...
local label = subdir_path:gsub(subdir_header, '')
label = label:gsub('^/+', '')
leaf.active = (string.ends(subdir_path, plugin_file_path))
leaf.href = file_url(plugin_path, subdir_path)
leaf.title = label
tree[#tree+1] = leaf
end
end
end
return tree
end
-- ################################################################
local function plugin_files_dropdown(plugin_file_path, plugin_path)
print[[
<div class="input-group mb-3">
<div class="input-group-prepend">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
]] print(i18n("plugin_contents")) print[[
</button>
<div class="dropdown-menu scrollable-dropdown" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item ]] print(ternary(string.ends(plugin_file_path, 'manifest.lua'), "active", '')) print[[" href="]] print(file_url(plugin_path, os_utils.fixPath(string.format("%s/%s", plugin_path, 'manifest.lua')))) print[[">manifest.lua</a>
]]
plugin_subdir_files_dropdown(plugin_file_path, plugin_path, plugin_path, 1)
print[[
</div>
</div>
<span class="input-group-text" id="">]] print(plugin_file_path) print[[</span>
</div>
]]
return {
class_dropdown_item = ternary(string.ends(plugin_file_path, 'manifest.lua'), "active", ''),
href_dropdown_item = file_url(plugin_path, os_utils.fixPath(string.format("%s/%s", plugin_path, 'manifest.lua'))),
elements = plugin_subdir_files_dropdown(plugin_file_path, plugin_path, plugin_path, 1)
}
end
-- ################################################################
function code_editor.editor(plugin_file_path, plugin_path)
function code_editor.editor(plugin_file_path, plugin_path, referarl_script_page)
local plugin_file_url
if starts(plugin_file_path, "/plugins/") then
@ -105,57 +113,29 @@ function code_editor.editor(plugin_file_path, plugin_path)
plugin_path = nil
end
plugin_files_dropdown(plugin_file_path, plugin_path)
local context = {
monaco_editor = {
plugin_file_url = plugin_file_url,
plugin_file_path = plugin_file_path,
plugin_path = plugin_path,
referarl_script_page = referarl_script_page,
dropdown = plugin_files_dropdown(plugin_file_path, plugin_path)
}
}
print[[
<script>var require = { paths: { 'vs': ']] print(ntop.getHttpPrefix()) print[[/monaco-editor/min/vs' } };</script>
<script src="]] print(ntop.getHttpPrefix()) print[[/monaco-editor/min/vs/loader.js"></script>
<script src="]] print(ntop.getHttpPrefix()) print[[/monaco-editor/min/vs/editor/editor.main.nls.js"></script>
<script src="]] print(ntop.getHttpPrefix()) print[[/monaco-editor/min/vs/editor/editor.main.js"></script>
-- Flat the subdirs hierarchy
for _, tree in ipairs(context.monaco_editor.dropdown.elements) do
<div id="container" style="width:1200px;height:600px;border:1px solid grey"></div>
<script>
function getSourceFile(theURL) {
var strReturn = "";
jQuery.ajax({
url: theURL,
success: function(html) {
strReturn = html;
},
async:false
});
return strReturn;
}
var src = "";
]]
if plugin_file_url then
print [[
src = getSourceFile(']] print(plugin_file_url) print[[')
]]
local flatten = {}
for _, dir in ipairs(tree.subdirs) do
flat_subdirs(dir, flatten)
end
tree.flatten_subdirs = flatten
end
print [[
console.log(src);
require.config({ paths: { 'vs': ']] print(ntop.getHttpPrefix()) print[[/monaco-editor/min/vs' }});
require(['vs/editor/editor.main'], function() {
var editor = monaco.editor.create(document.getElementById('container'), {
value: src,
readOnly: true,
language: 'lua'
});
});
</script>
]]
print(template_utils.gen('pages/components/monaco-editor.template', context))
end
-- #################################
return code_editor
return code_editor