nquery 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +36 -0
- data/README.md +54 -0
- data/app/assets/builds/nquery/application.css +110 -6
- data/app/assets/builds/nquery/application.js +401 -37
- data/app/controllers/concerns/nquery/breadcrumbs.rb +2 -13
- data/app/controllers/concerns/nquery/chart_actions.rb +5 -5
- data/app/controllers/concerns/nquery/chart_results.rb +70 -9
- data/app/controllers/concerns/nquery/sharing_page.rb +38 -0
- data/app/controllers/nquery/application_controller.rb +2 -0
- data/app/controllers/nquery/dashboard/cards_controller.rb +28 -0
- data/app/controllers/nquery/dashboards_controller.rb +56 -11
- data/app/controllers/nquery/embed/charts_controller.rb +15 -13
- data/app/controllers/nquery/embed/dashboards_controller.rb +34 -8
- data/app/controllers/nquery/public/charts_controller.rb +19 -0
- data/app/controllers/nquery/public/dashboards_controller.rb +33 -0
- data/app/controllers/nquery/public_resources_controller.rb +79 -0
- data/app/controllers/nquery/queries_controller.rb +25 -48
- data/app/controllers/nquery/sharing/base_controller.rb +89 -0
- data/app/controllers/nquery/sharing/embed_tokens_controller.rb +41 -0
- data/app/controllers/nquery/sharing/embeddings_controller.rb +17 -0
- data/app/controllers/nquery/sharing/public_links_controller.rb +21 -0
- data/app/helpers/nquery/charts_helper.rb +13 -0
- data/app/helpers/nquery/sharing_helper.rb +67 -0
- data/app/javascript/nquery/controllers/chart_preview_controller.js +19 -1
- data/app/javascript/nquery/controllers/clipboard_controller.js +23 -0
- data/app/models/concerns/nquery/shareable.rb +43 -0
- data/app/models/nquery/chart.rb +18 -2
- data/app/models/nquery/dashboard.rb +131 -0
- data/app/models/nquery/data_source.rb +1 -1
- data/app/models/nquery/query.rb +72 -0
- data/app/services/nquery/application_service.rb +9 -0
- data/app/services/nquery/dashboards/card_parameters.rb +37 -0
- data/app/services/nquery/dashboards/filters.rb +75 -0
- data/app/services/nquery/dashboards/parameter_charts.rb +31 -0
- data/app/services/nquery/dashboards/parameter_rows.rb +60 -0
- data/app/views/layouts/nquery/application.html.erb +8 -0
- data/app/views/layouts/nquery/embed.html.erb +3 -1
- data/app/views/nquery/charts/_builder_form.html.erb +13 -6
- data/app/views/nquery/charts/_variable_row.html.erb +10 -0
- data/app/views/nquery/charts/_variables.html.erb +28 -0
- data/app/views/nquery/charts/embed.html.erb +5 -11
- data/app/views/nquery/charts/new.html.erb +2 -1
- data/app/views/nquery/charts/update.turbo_stream.erb +3 -0
- data/app/views/nquery/dashboard/charts/new.html.erb +2 -1
- data/app/views/nquery/dashboards/_chart_card.html.erb +27 -1
- data/app/views/nquery/dashboards/_parameter_chart_ids.html.erb +6 -0
- data/app/views/nquery/dashboards/_parameters.html.erb +159 -0
- data/app/views/nquery/dashboards/edit.html.erb +10 -0
- data/app/views/nquery/dashboards/embed.html.erb +9 -0
- data/app/views/nquery/dashboards/show.html.erb +41 -1
- data/app/views/nquery/embed/charts/show.html.erb +7 -8
- data/app/views/nquery/embed/dashboards/show.html.erb +12 -6
- data/app/views/nquery/embed/unavailable.html.erb +1 -0
- data/app/views/nquery/shared/_icon.html.erb +10 -0
- data/app/views/nquery/shared/_shared_chart_card.html.erb +20 -0
- data/app/views/nquery/shared/_sharing_panel.html.erb +94 -0
- data/app/views/nquery/shared/_sidebar.html.erb +1 -1
- data/app/views/nquery/shared/_topbar.html.erb +3 -0
- data/config/routes.rb +24 -1
- data/db/migrate/20260907120000_add_sharing_to_nquery_charts_and_dashboards.rb +19 -0
- data/db/migrate/20260907130000_remove_embedding_params_from_nquery_charts_and_dashboards.rb +8 -0
- data/db/migrate/20261005145000_add_parameters_to_nquery_dashboards.rb +7 -0
- data/db/migrate/20261005190000_add_parameters_to_nquery_queries.rb +7 -0
- data/lib/nquery/boolean.rb +9 -0
- data/lib/nquery/configuration.rb +6 -0
- data/lib/nquery/dashboard_parameters.rb +56 -0
- data/lib/nquery/data_sources/adapter.rb +10 -1
- data/lib/nquery/data_sources/mysql_adapter.rb +2 -2
- data/lib/nquery/data_sources/postgresql_adapter.rb +2 -2
- data/lib/nquery/data_sources/rails_adapter.rb +2 -7
- data/lib/nquery/data_sources/row_limit.rb +211 -0
- data/lib/nquery/embed_token_service.rb +10 -5
- data/lib/nquery/encryption.rb +1 -1
- data/lib/nquery/parameter_value.rb +97 -0
- data/lib/nquery/query_runner.rb +37 -4
- data/lib/nquery/seeder.rb +29 -7
- data/lib/nquery/statement_binds.rb +112 -0
- data/lib/nquery/version.rb +1 -1
- data/lib/nquery.rb +5 -0
- data/nquery.gemspec +2 -2
- metadata +40 -9
- data/app/javascript/nquery/controllers/query_editor_controller.js +0 -41
- data/app/views/nquery/queries/edit.html.erb +0 -40
- data/app/views/nquery/queries/new.html.erb +0 -37
|
@@ -263,7 +263,7 @@ function initButtonLoaders() {
|
|
|
263
263
|
|
|
264
264
|
document.addEventListener("click", (event) => {
|
|
265
265
|
const button = event.target.closest("a.nq-btn, button.nq-btn[type=button], input.nq-btn[type=button]")
|
|
266
|
-
if (!button || button.classList.contains("is-loading") || button.dataset.managesLoading === "true") return
|
|
266
|
+
if (!button || button.classList.contains("is-loading") || button.dataset.managesLoading === "true" || button.hasAttribute("data-copy-button")) return
|
|
267
267
|
setButtonLoading(button, true)
|
|
268
268
|
})
|
|
269
269
|
|
|
@@ -343,12 +343,23 @@ function buildPreviewChartConfig(type, data, xCol, yCol) {
|
|
|
343
343
|
}
|
|
344
344
|
}
|
|
345
345
|
|
|
346
|
+
const NQ_CHART_FONT = '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif'
|
|
347
|
+
|
|
348
|
+
function applyChartFontDefaults() {
|
|
349
|
+
if (typeof Chart === "undefined") return
|
|
350
|
+
|
|
351
|
+
Chart.defaults.font.family = NQ_CHART_FONT
|
|
352
|
+
Chart.defaults.color = document.body.classList.contains("nq-embed-theme-night") ? "#e2e8f0" : "#1e293b"
|
|
353
|
+
}
|
|
354
|
+
|
|
346
355
|
function initChartPreviews() {
|
|
347
356
|
if (typeof Chart === "undefined") {
|
|
348
357
|
requestAnimationFrame(initChartPreviews)
|
|
349
358
|
return
|
|
350
359
|
}
|
|
351
360
|
|
|
361
|
+
applyChartFontDefaults()
|
|
362
|
+
|
|
352
363
|
document.querySelectorAll("[data-controller='chart-preview']").forEach(el => {
|
|
353
364
|
if (el.dataset.chartPreviewInitialized === "true") return
|
|
354
365
|
el.dataset.chartPreviewInitialized = "true"
|
|
@@ -368,11 +379,17 @@ function initChartPreviews() {
|
|
|
368
379
|
rows: [["Jan", 1200], ["Feb", 1800], ["Mar", 2400], ["Apr", 2100]]
|
|
369
380
|
}
|
|
370
381
|
|
|
382
|
+
if (data?.error) {
|
|
383
|
+
el.classList.add("is-error")
|
|
384
|
+
el.innerHTML = `<div class="nq-chart-error">${escapeHtml(data.error)}</div>`
|
|
385
|
+
return
|
|
386
|
+
}
|
|
387
|
+
|
|
371
388
|
if (type === "number") {
|
|
372
389
|
const yIndex = columnIndex(demo.columns || [], yCol || demo.columns?.[1] || demo.columns?.[0])
|
|
373
390
|
const value = demo.rows?.[0]?.[yIndex] ?? "—"
|
|
374
391
|
el.classList.add("is-number")
|
|
375
|
-
el.innerHTML = `<div class="nq-number-display">${value}</div>`
|
|
392
|
+
el.innerHTML = `<div class="nq-number-display">${escapeHtml(value)}</div>`
|
|
376
393
|
return
|
|
377
394
|
}
|
|
378
395
|
|
|
@@ -413,6 +430,166 @@ function initChartPreviews() {
|
|
|
413
430
|
})
|
|
414
431
|
}
|
|
415
432
|
|
|
433
|
+
function chartVariableNames(sql) {
|
|
434
|
+
const names = []
|
|
435
|
+
const seen = new Set()
|
|
436
|
+
const pattern = /\{\{\s*([a-z][a-z0-9_]*)\s*\}\}/g
|
|
437
|
+
let match
|
|
438
|
+
while ((match = pattern.exec(sql))) {
|
|
439
|
+
if (seen.has(match[1])) continue
|
|
440
|
+
seen.add(match[1])
|
|
441
|
+
names.push(match[1])
|
|
442
|
+
}
|
|
443
|
+
return names
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
let focusVariableName = null
|
|
447
|
+
let chartVariableMarks = []
|
|
448
|
+
|
|
449
|
+
function chartVariableParameters() {
|
|
450
|
+
return Array.from(document.querySelectorAll("#chart_variables [data-variable-name]")).map(row => ({
|
|
451
|
+
name: row.dataset.variableName,
|
|
452
|
+
type: row.querySelector("[data-variable-type]")?.value || "string",
|
|
453
|
+
default: row.querySelector("[data-variable-default]")?.value || ""
|
|
454
|
+
})).filter(row => row.name)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function nextParameterName(sql) {
|
|
458
|
+
const names = new Set(chartVariableNames(sql))
|
|
459
|
+
if (!names.has("parameter")) return "parameter"
|
|
460
|
+
let index = 2
|
|
461
|
+
while (names.has(`parameter_${index}`)) index += 1
|
|
462
|
+
return `parameter_${index}`
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function selectedVariableField(selector) {
|
|
466
|
+
const name = document.getElementById("chart_variable_options")?.dataset.variableName
|
|
467
|
+
if (!name) return null
|
|
468
|
+
const row = document.querySelector(`#chart_variables [data-variable-name="${CSS.escape(name)}"]`)
|
|
469
|
+
return row?.querySelector(selector) || null
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function clearChartVariableMarks() {
|
|
473
|
+
chartVariableMarks.forEach(mark => mark.clear())
|
|
474
|
+
chartVariableMarks = []
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function highlightChartVariable(name, { scroll = false } = {}) {
|
|
478
|
+
clearChartVariableMarks()
|
|
479
|
+
const editor = document.querySelector("[data-chart-builder-target='statement']")?._sqlEditor
|
|
480
|
+
if (!editor || !name) return
|
|
481
|
+
|
|
482
|
+
const pattern = new RegExp(`\\{\\{\\s*${name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\s*\\}\\}`, "g")
|
|
483
|
+
const text = editor.getValue()
|
|
484
|
+
let match
|
|
485
|
+
let first = null
|
|
486
|
+
while ((match = pattern.exec(text))) {
|
|
487
|
+
const from = editor.posFromIndex(match.index)
|
|
488
|
+
const to = editor.posFromIndex(match.index + match[0].length)
|
|
489
|
+
if (!first) first = from
|
|
490
|
+
chartVariableMarks.push(editor.markText(from, to, { className: "nq-sql-parameter" }))
|
|
491
|
+
}
|
|
492
|
+
if (scroll && first) editor.scrollIntoView(first, 80)
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
function clearChartVariableSelection() {
|
|
496
|
+
clearChartVariableMarks()
|
|
497
|
+
const options = document.getElementById("chart_variable_options")
|
|
498
|
+
if (options) {
|
|
499
|
+
options.hidden = true
|
|
500
|
+
delete options.dataset.variableName
|
|
501
|
+
}
|
|
502
|
+
document.querySelectorAll("#chart_variables [data-variable-select]").forEach(button => {
|
|
503
|
+
button.classList.remove("is-selected")
|
|
504
|
+
button.setAttribute("aria-pressed", "false")
|
|
505
|
+
})
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function selectChartVariable(name, { focus = true } = {}) {
|
|
509
|
+
const row = document.querySelector(`#chart_variables [data-variable-name="${CSS.escape(name)}"]`)
|
|
510
|
+
const options = document.getElementById("chart_variable_options")
|
|
511
|
+
if (!row || !options) return
|
|
512
|
+
|
|
513
|
+
document.querySelectorAll("#chart_variables [data-variable-select]").forEach(button => {
|
|
514
|
+
const selected = button.closest("[data-variable-name]")?.dataset.variableName === name
|
|
515
|
+
button.classList.toggle("is-selected", selected)
|
|
516
|
+
button.setAttribute("aria-pressed", selected ? "true" : "false")
|
|
517
|
+
})
|
|
518
|
+
|
|
519
|
+
options.hidden = false
|
|
520
|
+
options.dataset.variableName = name
|
|
521
|
+
const label = options.querySelector("[data-chart-variable-options-name]")
|
|
522
|
+
if (label) label.textContent = `{{${name}}}`
|
|
523
|
+
const typeMirror = options.querySelector("[data-chart-variable-options-type]")
|
|
524
|
+
const defaultMirror = options.querySelector("[data-chart-variable-options-default]")
|
|
525
|
+
const type = row.querySelector("[data-variable-type]")
|
|
526
|
+
const fallback = row.querySelector("[data-variable-default]")
|
|
527
|
+
if (typeMirror && type) typeMirror.value = type.value
|
|
528
|
+
if (defaultMirror && fallback) defaultMirror.value = fallback.value
|
|
529
|
+
highlightChartVariable(name, { scroll: true })
|
|
530
|
+
if (focus) typeMirror?.focus()
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
function syncChartVariables(sql, { selectAdded = false } = {}) {
|
|
534
|
+
const panel = document.getElementById("chart_variables")
|
|
535
|
+
const rows = panel?.querySelector("[data-chart-variables-target='rows']")
|
|
536
|
+
const template = panel?.querySelector("[data-chart-variables-target='template']")
|
|
537
|
+
if (!rows || !template) return
|
|
538
|
+
|
|
539
|
+
const names = chartVariableNames(sql)
|
|
540
|
+
const previous = new Set()
|
|
541
|
+
const kept = new Map()
|
|
542
|
+
rows.querySelectorAll("[data-variable-name]").forEach(row => {
|
|
543
|
+
if (!row.dataset.variableName) return
|
|
544
|
+
previous.add(row.dataset.variableName)
|
|
545
|
+
kept.set(row.dataset.variableName, row)
|
|
546
|
+
})
|
|
547
|
+
|
|
548
|
+
rows.replaceChildren()
|
|
549
|
+
names.forEach(name => {
|
|
550
|
+
const existing = kept.get(name)
|
|
551
|
+
if (existing) {
|
|
552
|
+
rows.appendChild(existing)
|
|
553
|
+
return
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
const fragment = template.content.cloneNode(true)
|
|
557
|
+
const row = fragment.querySelector("[data-variable-name]")
|
|
558
|
+
row.dataset.variableName = name
|
|
559
|
+
const label = row.querySelector("[data-variable-label]")
|
|
560
|
+
if (label) label.textContent = `{{${name}}}`
|
|
561
|
+
const nameInput = row.querySelector("[data-variable-name-input]")
|
|
562
|
+
const typeInput = row.querySelector("[data-variable-type]")
|
|
563
|
+
const defaultInput = row.querySelector("[data-variable-default]")
|
|
564
|
+
if (nameInput) {
|
|
565
|
+
nameInput.value = name
|
|
566
|
+
nameInput.id = `chart_variable_${name}_name`
|
|
567
|
+
}
|
|
568
|
+
if (typeInput) typeInput.id = `chart_variable_${name}_type`
|
|
569
|
+
if (defaultInput) defaultInput.id = `chart_variable_${name}_default`
|
|
570
|
+
rows.appendChild(fragment)
|
|
571
|
+
})
|
|
572
|
+
|
|
573
|
+
const present = names.length > 0
|
|
574
|
+
panel.querySelector("[data-chart-variables-target='empty']")?.toggleAttribute("hidden", present)
|
|
575
|
+
|
|
576
|
+
const selected = document.getElementById("chart_variable_options")?.dataset.variableName
|
|
577
|
+
if (selected && !names.includes(selected)) clearChartVariableSelection()
|
|
578
|
+
|
|
579
|
+
if (selectAdded) {
|
|
580
|
+
const added = names.filter(name => !previous.has(name))
|
|
581
|
+
if (added.length) {
|
|
582
|
+
const name = added[added.length - 1]
|
|
583
|
+
const focus = focusVariableName === name
|
|
584
|
+
focusVariableName = null
|
|
585
|
+
selectChartVariable(name, { focus })
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
const current = document.getElementById("chart_variable_options")?.dataset.variableName
|
|
590
|
+
if (current && names.includes(current)) highlightChartVariable(current)
|
|
591
|
+
}
|
|
592
|
+
|
|
416
593
|
function initChartBuilders() {
|
|
417
594
|
document.querySelectorAll("[data-controller='chart-builder']:not([data-chart-builder-initialized])").forEach(root => {
|
|
418
595
|
root.dataset.chartBuilderInitialized = "true"
|
|
@@ -448,6 +625,8 @@ function initChartBuilders() {
|
|
|
448
625
|
root.querySelector("[data-action*='chart-builder#format']")
|
|
449
626
|
const saveStatus = root.querySelector("[data-chart-builder-target='saveStatus']")
|
|
450
627
|
const querySaveUrl = root.dataset.querySaveUrl
|
|
628
|
+
const queryRunUrl = root.dataset.queryRunUrl
|
|
629
|
+
const querySchemaUrl = root.dataset.querySchemaUrl
|
|
451
630
|
|
|
452
631
|
let currentResult = null
|
|
453
632
|
let chartInstance = null
|
|
@@ -457,6 +636,7 @@ function initChartBuilders() {
|
|
|
457
636
|
let statementEditor = null
|
|
458
637
|
let lastFormattedSql = null
|
|
459
638
|
let lastSavedSql = statement?.value ?? ""
|
|
639
|
+
let lastSavedParameters = JSON.stringify(chartVariableParameters())
|
|
460
640
|
let ignoreEditorChanges = false
|
|
461
641
|
let autosaveTimer = null
|
|
462
642
|
let saveChain = Promise.resolve()
|
|
@@ -498,9 +678,11 @@ function initChartBuilders() {
|
|
|
498
678
|
|
|
499
679
|
const run = async () => {
|
|
500
680
|
const statementText = getStatement()
|
|
681
|
+
const parameters = chartVariableParameters()
|
|
682
|
+
const parametersJson = JSON.stringify(parameters)
|
|
501
683
|
syncStatementField()
|
|
502
684
|
|
|
503
|
-
if (statementText === lastSavedSql) {
|
|
685
|
+
if (statementText === lastSavedSql && parametersJson === lastSavedParameters) {
|
|
504
686
|
if (notice) showClientFlash(notice)
|
|
505
687
|
return { saved: true, skipped: true }
|
|
506
688
|
}
|
|
@@ -513,12 +695,13 @@ function initChartBuilders() {
|
|
|
513
695
|
Accept: "application/json",
|
|
514
696
|
"X-CSRF-Token": csrf
|
|
515
697
|
},
|
|
516
|
-
body: JSON.stringify({ query: { statement: statementText } })
|
|
698
|
+
body: JSON.stringify({ query: { statement: statementText, parameters } })
|
|
517
699
|
})
|
|
518
700
|
const data = await res.json().catch(() => ({}))
|
|
519
701
|
if (!res.ok) throw new Error(data.error || "Failed to save query.")
|
|
520
702
|
|
|
521
703
|
lastSavedSql = statementText
|
|
704
|
+
lastSavedParameters = parametersJson
|
|
522
705
|
if (notice) showClientFlash(notice)
|
|
523
706
|
return { saved: true }
|
|
524
707
|
}
|
|
@@ -547,6 +730,7 @@ function initChartBuilders() {
|
|
|
547
730
|
if (ignoreEditorChanges) return
|
|
548
731
|
|
|
549
732
|
const current = getStatement()
|
|
733
|
+
syncChartVariables(current, { selectAdded: true })
|
|
550
734
|
if (lastFormattedSql === null || current !== lastFormattedSql) setFormatEnabled(true)
|
|
551
735
|
scheduleAutosave()
|
|
552
736
|
}
|
|
@@ -678,7 +862,7 @@ function initChartBuilders() {
|
|
|
678
862
|
const yCol = yAxis?.value || result.columns[1] || result.columns[0]
|
|
679
863
|
const yIndex = columnIndex(result.columns, yCol)
|
|
680
864
|
const value = result.rows[0]?.[yIndex] ?? "—"
|
|
681
|
-
numberWrap.innerHTML = `<div class="nq-number-display">${value}</div>`
|
|
865
|
+
numberWrap.innerHTML = `<div class="nq-number-display">${escapeHtml(value)}</div>`
|
|
682
866
|
chartWrap?.setAttribute("hidden", "")
|
|
683
867
|
numberWrap?.removeAttribute("hidden")
|
|
684
868
|
}
|
|
@@ -830,10 +1014,14 @@ function initChartBuilders() {
|
|
|
830
1014
|
setButtonLoading(button, true)
|
|
831
1015
|
try {
|
|
832
1016
|
const csrf = document.querySelector('meta[name="csrf-token"]')?.content
|
|
833
|
-
const res = await fetch(
|
|
1017
|
+
const res = await fetch(queryRunUrl, {
|
|
834
1018
|
method: "POST",
|
|
835
1019
|
headers: { "Content-Type": "application/json", "X-CSRF-Token": csrf },
|
|
836
|
-
body: JSON.stringify({
|
|
1020
|
+
body: JSON.stringify({
|
|
1021
|
+
statement: getStatement(),
|
|
1022
|
+
data_source_id: dataSource?.value,
|
|
1023
|
+
parameters: chartVariableParameters()
|
|
1024
|
+
})
|
|
837
1025
|
})
|
|
838
1026
|
const data = await res.json()
|
|
839
1027
|
if (!res.ok || data.error) {
|
|
@@ -851,7 +1039,7 @@ function initChartBuilders() {
|
|
|
851
1039
|
|
|
852
1040
|
const loadSchema = async () => {
|
|
853
1041
|
if (!schema || !dataSource) return
|
|
854
|
-
const response = await fetch(
|
|
1042
|
+
const response = await fetch(`${querySchemaUrl}?data_source_id=${dataSource.value}`)
|
|
855
1043
|
const data = await response.json()
|
|
856
1044
|
renderSchemaTree(schema, data.tables)
|
|
857
1045
|
}
|
|
@@ -900,6 +1088,63 @@ function initChartBuilders() {
|
|
|
900
1088
|
})
|
|
901
1089
|
|
|
902
1090
|
lastSavedSql = getStatement()
|
|
1091
|
+
syncChartVariables(lastSavedSql)
|
|
1092
|
+
lastSavedParameters = JSON.stringify(chartVariableParameters())
|
|
1093
|
+
|
|
1094
|
+
const variableOptions = document.getElementById("chart_variable_options")
|
|
1095
|
+
variableOptions?.querySelector("[data-chart-variable-options-type]")?.addEventListener("change", (event) => {
|
|
1096
|
+
const input = selectedVariableField("[data-variable-type]")
|
|
1097
|
+
if (!input || input.value === event.target.value) return
|
|
1098
|
+
input.value = event.target.value
|
|
1099
|
+
input.dispatchEvent(new Event("change", { bubbles: true }))
|
|
1100
|
+
})
|
|
1101
|
+
variableOptions?.querySelector("[data-chart-variable-options-default]")?.addEventListener("input", (event) => {
|
|
1102
|
+
const input = selectedVariableField("[data-variable-default]")
|
|
1103
|
+
if (!input || input.value === event.target.value) return
|
|
1104
|
+
input.value = event.target.value
|
|
1105
|
+
input.dispatchEvent(new Event("input", { bubbles: true }))
|
|
1106
|
+
})
|
|
1107
|
+
|
|
1108
|
+
const insertChartParameter = () => {
|
|
1109
|
+
const name = nextParameterName(getStatement())
|
|
1110
|
+
const token = `{{${name}}}`
|
|
1111
|
+
focusVariableName = name
|
|
1112
|
+
if (statementEditor) {
|
|
1113
|
+
if (!statementEditor.hasFocus()) {
|
|
1114
|
+
const line = statementEditor.lastLine()
|
|
1115
|
+
statementEditor.setCursor({ line, ch: statementEditor.getLine(line).length })
|
|
1116
|
+
}
|
|
1117
|
+
const cursor = statementEditor.getCursor()
|
|
1118
|
+
const before = statementEditor.getLine(cursor.line).slice(0, cursor.ch)
|
|
1119
|
+
statementEditor.replaceSelection(`${before.length > 0 && !/\s$/.test(before) ? " " : ""}${token}`)
|
|
1120
|
+
return
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
if (!statement) return
|
|
1124
|
+
const start = statement.selectionStart ?? statement.value.length
|
|
1125
|
+
const end = statement.selectionEnd ?? start
|
|
1126
|
+
statement.value = `${statement.value.slice(0, start)}${token}${statement.value.slice(end)}`
|
|
1127
|
+
handleStatementChange()
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
root.addEventListener("click", (event) => {
|
|
1131
|
+
if (event.target.closest("[data-chart-variables-target='add']")) {
|
|
1132
|
+
event.preventDefault()
|
|
1133
|
+
insertChartParameter()
|
|
1134
|
+
return
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
const chip = event.target.closest("[data-variable-select]")
|
|
1138
|
+
if (!chip || !root.contains(chip)) return
|
|
1139
|
+
const name = chip.closest("[data-variable-name]")?.dataset.variableName
|
|
1140
|
+
if (name) selectChartVariable(name)
|
|
1141
|
+
})
|
|
1142
|
+
root.addEventListener("input", (event) => {
|
|
1143
|
+
if (event.target.closest("#chart_variables")) scheduleAutosave()
|
|
1144
|
+
})
|
|
1145
|
+
root.addEventListener("change", (event) => {
|
|
1146
|
+
if (event.target.closest("#chart_variables")) scheduleAutosave()
|
|
1147
|
+
})
|
|
903
1148
|
if (statementEditor) statementEditor.on("change", handleStatementChange)
|
|
904
1149
|
else statement?.addEventListener("input", handleStatementChange)
|
|
905
1150
|
|
|
@@ -959,34 +1204,6 @@ function initChartBuilders() {
|
|
|
959
1204
|
})
|
|
960
1205
|
}
|
|
961
1206
|
|
|
962
|
-
function initQueryEditors() {
|
|
963
|
-
document.querySelectorAll("[data-controller='query-editor']").forEach(el => {
|
|
964
|
-
const statement = el.querySelector("[data-query-editor-target='statement']")
|
|
965
|
-
const results = el.querySelector("[data-query-editor-target='results']")
|
|
966
|
-
const dataSource = el.querySelector("[data-query-editor-target='dataSource']")
|
|
967
|
-
el.querySelector("[data-action*='query-editor#run']")?.addEventListener("click", async (event) => {
|
|
968
|
-
const button = event.currentTarget
|
|
969
|
-
setButtonLoading(button, true)
|
|
970
|
-
try {
|
|
971
|
-
const csrf = document.querySelector('meta[name="csrf-token"]')?.content
|
|
972
|
-
const res = await fetch("/queries/run", {
|
|
973
|
-
method: "POST",
|
|
974
|
-
headers: { "Content-Type": "application/json", "X-CSRF-Token": csrf },
|
|
975
|
-
body: JSON.stringify({ statement: statement?.value, data_source_id: dataSource?.value })
|
|
976
|
-
})
|
|
977
|
-
results.textContent = JSON.stringify(await res.json(), null, 2)
|
|
978
|
-
} finally {
|
|
979
|
-
setButtonLoading(button, false)
|
|
980
|
-
}
|
|
981
|
-
})
|
|
982
|
-
el.querySelectorAll("[data-action*='insertTable']").forEach(item => {
|
|
983
|
-
item.addEventListener("click", () => {
|
|
984
|
-
if (statement) statement.value = `SELECT * FROM ${item.dataset.table} LIMIT 100`
|
|
985
|
-
})
|
|
986
|
-
})
|
|
987
|
-
})
|
|
988
|
-
}
|
|
989
|
-
|
|
990
1207
|
function initDataSourceForms() {
|
|
991
1208
|
document.querySelectorAll("[data-controller='data-source-form']").forEach(root => {
|
|
992
1209
|
if (root.dataset.dataSourceFormInitialized === "true") return
|
|
@@ -1060,15 +1277,162 @@ function initDataSourceForms() {
|
|
|
1060
1277
|
})
|
|
1061
1278
|
}
|
|
1062
1279
|
|
|
1280
|
+
function initCopyButtons() {
|
|
1281
|
+
document.querySelectorAll("[data-copy-button]").forEach(button => {
|
|
1282
|
+
if (button.dataset.copyInitialized === "true") return
|
|
1283
|
+
button.dataset.copyInitialized = "true"
|
|
1284
|
+
|
|
1285
|
+
button.addEventListener("click", async () => {
|
|
1286
|
+
const target = button.dataset.copyTarget ? document.querySelector(button.dataset.copyTarget) : null
|
|
1287
|
+
const text = target ? target.textContent : (button.dataset.copyText || "")
|
|
1288
|
+
const original = button.textContent
|
|
1289
|
+
|
|
1290
|
+
try {
|
|
1291
|
+
await navigator.clipboard.writeText(text.trim())
|
|
1292
|
+
button.textContent = "Copied"
|
|
1293
|
+
} catch {
|
|
1294
|
+
button.textContent = "Copy failed"
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
setTimeout(() => { button.textContent = original }, 1500)
|
|
1298
|
+
})
|
|
1299
|
+
})
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
const SIDEBAR_STORAGE_KEY = "nq-sidebar-collapsed"
|
|
1303
|
+
|
|
1304
|
+
function sidebarIsMobile() {
|
|
1305
|
+
return window.matchMedia("(max-width: 768px)").matches
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
function sidebarCollapsedPreference() {
|
|
1309
|
+
try {
|
|
1310
|
+
return localStorage.getItem(SIDEBAR_STORAGE_KEY) === "true"
|
|
1311
|
+
} catch {
|
|
1312
|
+
return false
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
function rememberSidebarCollapsed(collapsed) {
|
|
1317
|
+
try {
|
|
1318
|
+
localStorage.setItem(SIDEBAR_STORAGE_KEY, collapsed ? "true" : "false")
|
|
1319
|
+
} catch {
|
|
1320
|
+
// Private browsing can block storage.
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
function sidebarElements() {
|
|
1325
|
+
return {
|
|
1326
|
+
layout: document.querySelector(".nq-layout"),
|
|
1327
|
+
sidebar: document.getElementById("nq-sidebar"),
|
|
1328
|
+
toggle: document.querySelector(".nq-sidebar-toggle"),
|
|
1329
|
+
backdrop: document.querySelector(".nq-sidebar-backdrop")
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1333
|
+
function syncSidebar(layout, sidebar, toggle, backdrop) {
|
|
1334
|
+
if (!layout || !sidebar || !toggle) return
|
|
1335
|
+
|
|
1336
|
+
const showing = sidebarIsMobile()
|
|
1337
|
+
? layout.classList.contains("is-sidebar-open")
|
|
1338
|
+
: !layout.classList.contains("is-sidebar-collapsed")
|
|
1339
|
+
|
|
1340
|
+
sidebar.toggleAttribute("inert", !showing)
|
|
1341
|
+
sidebar.setAttribute("aria-hidden", showing ? "false" : "true")
|
|
1342
|
+
toggle.setAttribute("aria-expanded", showing ? "true" : "false")
|
|
1343
|
+
toggle.setAttribute("aria-label", showing ? "Hide navigation" : "Show navigation")
|
|
1344
|
+
if (backdrop) backdrop.hidden = !(sidebarIsMobile() && showing)
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
function applySidebarPreference() {
|
|
1348
|
+
const { layout, sidebar, toggle, backdrop } = sidebarElements()
|
|
1349
|
+
if (!layout || !sidebar || !toggle) return
|
|
1350
|
+
|
|
1351
|
+
const collapsed = !sidebarIsMobile() && (
|
|
1352
|
+
document.documentElement.classList.contains("nq-sidebar-collapsed") || sidebarCollapsedPreference()
|
|
1353
|
+
)
|
|
1354
|
+
|
|
1355
|
+
layout.classList.toggle("is-sidebar-collapsed", collapsed)
|
|
1356
|
+
document.documentElement.classList.remove("nq-sidebar-collapsed")
|
|
1357
|
+
syncSidebar(layout, sidebar, toggle, backdrop)
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
function initSidebarToggle() {
|
|
1361
|
+
const { sidebar, toggle, backdrop } = sidebarElements()
|
|
1362
|
+
if (!sidebar || !toggle) return
|
|
1363
|
+
|
|
1364
|
+
applySidebarPreference()
|
|
1365
|
+
if (toggle.dataset.sidebarToggleInitialized === "true") return
|
|
1366
|
+
toggle.dataset.sidebarToggleInitialized = "true"
|
|
1367
|
+
|
|
1368
|
+
toggle.addEventListener("click", () => {
|
|
1369
|
+
const current = sidebarElements()
|
|
1370
|
+
if (!current.layout) return
|
|
1371
|
+
|
|
1372
|
+
if (sidebarIsMobile()) {
|
|
1373
|
+
current.layout.classList.toggle("is-sidebar-open")
|
|
1374
|
+
} else {
|
|
1375
|
+
const collapsed = !current.layout.classList.contains("is-sidebar-collapsed")
|
|
1376
|
+
current.layout.classList.toggle("is-sidebar-collapsed", collapsed)
|
|
1377
|
+
rememberSidebarCollapsed(collapsed)
|
|
1378
|
+
}
|
|
1379
|
+
|
|
1380
|
+
syncSidebar(current.layout, current.sidebar, current.toggle, current.backdrop)
|
|
1381
|
+
})
|
|
1382
|
+
|
|
1383
|
+
backdrop?.addEventListener("click", () => {
|
|
1384
|
+
const current = sidebarElements()
|
|
1385
|
+
current.layout?.classList.remove("is-sidebar-open")
|
|
1386
|
+
syncSidebar(current.layout, current.sidebar, current.toggle, current.backdrop)
|
|
1387
|
+
})
|
|
1388
|
+
|
|
1389
|
+
sidebar.addEventListener("click", (event) => {
|
|
1390
|
+
if (!sidebarIsMobile() || !event.target.closest("a")) return
|
|
1391
|
+
|
|
1392
|
+
const current = sidebarElements()
|
|
1393
|
+
current.layout?.classList.remove("is-sidebar-open")
|
|
1394
|
+
syncSidebar(current.layout, current.sidebar, current.toggle, current.backdrop)
|
|
1395
|
+
})
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
function bindSidebarToggleOnce() {
|
|
1399
|
+
if (window.nqSidebarToggleBound) return
|
|
1400
|
+
window.nqSidebarToggleBound = true
|
|
1401
|
+
|
|
1402
|
+
document.addEventListener("keydown", (event) => {
|
|
1403
|
+
if (event.key !== "Escape") return
|
|
1404
|
+
|
|
1405
|
+
const { layout, sidebar, toggle, backdrop } = sidebarElements()
|
|
1406
|
+
if (!layout?.classList.contains("is-sidebar-open")) return
|
|
1407
|
+
|
|
1408
|
+
layout.classList.remove("is-sidebar-open")
|
|
1409
|
+
syncSidebar(layout, sidebar, toggle, backdrop)
|
|
1410
|
+
toggle?.focus()
|
|
1411
|
+
})
|
|
1412
|
+
|
|
1413
|
+
window.matchMedia("(max-width: 768px)").addEventListener("change", () => {
|
|
1414
|
+
sidebarElements().layout?.classList.remove("is-sidebar-open")
|
|
1415
|
+
applySidebarPreference()
|
|
1416
|
+
})
|
|
1417
|
+
|
|
1418
|
+
document.addEventListener("turbo:before-render", (event) => {
|
|
1419
|
+
if (sidebarIsMobile() || !sidebarCollapsedPreference()) return
|
|
1420
|
+
event.detail.newBody.querySelector(".nq-layout")?.classList.add("is-sidebar-collapsed")
|
|
1421
|
+
})
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1063
1424
|
function initPage() {
|
|
1425
|
+
applyChartFontDefaults()
|
|
1064
1426
|
initButtonLoaders()
|
|
1065
1427
|
initToastEvents()
|
|
1066
1428
|
observeToastStack()
|
|
1067
1429
|
initFlashCards()
|
|
1068
|
-
initQueryEditors()
|
|
1069
1430
|
initChartBuilders()
|
|
1070
1431
|
initChartPreviews()
|
|
1071
1432
|
initDataSourceForms()
|
|
1433
|
+
initCopyButtons()
|
|
1434
|
+
bindSidebarToggleOnce()
|
|
1435
|
+
initSidebarToggle()
|
|
1072
1436
|
}
|
|
1073
1437
|
|
|
1074
1438
|
function bootPage() {
|
|
@@ -53,9 +53,9 @@ module Nquery
|
|
|
53
53
|
case action_name
|
|
54
54
|
when "show"
|
|
55
55
|
append_resource_breadcrumb(crumbs, breadcrumb_dashboard)
|
|
56
|
-
when "edit"
|
|
56
|
+
when "edit", "embed"
|
|
57
57
|
append_resource_breadcrumb(crumbs, breadcrumb_dashboard, path: dashboard_path(breadcrumb_dashboard), link: true)
|
|
58
|
-
append_terminal_breadcrumb(crumbs,
|
|
58
|
+
append_terminal_breadcrumb(crumbs, action_name.titleize)
|
|
59
59
|
when "new"
|
|
60
60
|
append_terminal_breadcrumb(crumbs, "New dashboard")
|
|
61
61
|
end
|
|
@@ -86,9 +86,6 @@ module Nquery
|
|
|
86
86
|
when "new"
|
|
87
87
|
append_terminal_breadcrumb(crumbs, "New chart")
|
|
88
88
|
end
|
|
89
|
-
when "nquery/queries"
|
|
90
|
-
append_section_breadcrumb(crumbs, "Queries", nil)
|
|
91
|
-
append_terminal_breadcrumb(crumbs, query_breadcrumb_label)
|
|
92
89
|
when "nquery/charts"
|
|
93
90
|
append_section_breadcrumb(crumbs, "Collections", collections_path)
|
|
94
91
|
if breadcrumb_chart&.collection
|
|
@@ -208,14 +205,6 @@ module Nquery
|
|
|
208
205
|
@breadcrumb_data_source ||= @data_source || DataSource.find_by(id: params[:id])
|
|
209
206
|
end
|
|
210
207
|
|
|
211
|
-
def query_breadcrumb_label
|
|
212
|
-
case action_name
|
|
213
|
-
when "new" then "New SQL query"
|
|
214
|
-
when "edit" then "Edit query: #{@query&.name.presence || 'Untitled'}"
|
|
215
|
-
else @query&.name.presence || "Query"
|
|
216
|
-
end
|
|
217
|
-
end
|
|
218
|
-
|
|
219
208
|
def append_section_breadcrumb(crumbs, label, path)
|
|
220
209
|
crumbs << { label: label, path: path, section: true }
|
|
221
210
|
end
|
|
@@ -6,6 +6,7 @@ module Nquery
|
|
|
6
6
|
|
|
7
7
|
included do
|
|
8
8
|
include ChartResults
|
|
9
|
+
include SharingPage
|
|
9
10
|
before_action :set_chart, only: %i[show edit update embed destroy archive]
|
|
10
11
|
before_action :authorize_chart_view!, only: %i[show embed]
|
|
11
12
|
before_action :authorize_chart_curate!, only: %i[edit update destroy archive]
|
|
@@ -47,8 +48,7 @@ module Nquery
|
|
|
47
48
|
end
|
|
48
49
|
|
|
49
50
|
def embed
|
|
50
|
-
|
|
51
|
-
@embed_url = @embed_token ? embed_public_chart_url(token: EmbedTokenService.signed_token_for(@embed_token)) : nil
|
|
51
|
+
assign_sharing_page(@chart)
|
|
52
52
|
render "nquery/charts/embed"
|
|
53
53
|
end
|
|
54
54
|
|
|
@@ -88,14 +88,14 @@ module Nquery
|
|
|
88
88
|
params.require(:chart).permit(
|
|
89
89
|
:name,
|
|
90
90
|
visualization: {},
|
|
91
|
-
query_attributes:
|
|
91
|
+
query_attributes: [:id, :name, :statement, :data_source_id, { parameters: [:name, :type, :default] }]
|
|
92
92
|
)
|
|
93
93
|
end
|
|
94
94
|
|
|
95
95
|
def load_chart_builder_assigns
|
|
96
96
|
@data_sources = DataSource.active.order(:name)
|
|
97
|
-
@schema = schema_for(@chart&.
|
|
98
|
-
@result = chart_builder_result(@chart) if @chart&.persisted? && @chart.
|
|
97
|
+
@schema = schema_for(@chart&.data_source)
|
|
98
|
+
@result = chart_builder_result(@chart) if @chart&.persisted? && @chart.statement?
|
|
99
99
|
end
|
|
100
100
|
|
|
101
101
|
def schema_for(data_source = nil)
|