From b98df8997135a866c1b689cecf81dcab58aa7942 Mon Sep 17 00:00:00 2001 From: fanategorius <31000416+fanategorius@users.noreply.github.com> Date: Mon, 6 Jul 2026 12:51:15 +0300 Subject: [PATCH] Update index.js --- js/index.js | 132 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 87 insertions(+), 45 deletions(-) diff --git a/js/index.js b/js/index.js index 4641951..ed9b2d7 100644 --- a/js/index.js +++ b/js/index.js @@ -1,50 +1,92 @@ -(function (OC, window, $, undefined) { - 'use strict'; - $(function() { - function scrollToBottom(){ - var html = $('html'); - html.scrollTop(html.prop('scrollHeight')); - } - var baseUrl = OC.generateUrl('/apps/occweb'); - $.get(baseUrl + '/cmd', function(response){ - $('#app-content').terminal(function(command, term) { - switch (command) { - case "c": - this.clear(); - break; - case "exit": - this.reset(); - break; - default: - var occCommand = { - command: command - }; - term.pause(); - $.ajax({ - url: baseUrl + '/cmd', - type: 'POST', - contentType: 'application/json', - data: JSON.stringify(occCommand) - }).done(function (response) { - term.echo('\n' + response).resume(); - }).fail(function (response, code) { - term.echo('\n' + response).resume(); - }); +(function(OC, window, $, undefined) { +'use strict'; + +$(document).ready(function() { + var sqlMode = false; + + var term = $('body').terminal(function(command, term) { + if (command === '') { + return; } - }, { - greetings: function (callback) { - callback('[[;green;]' + new Date().toString().slice(0, 24) + "]\n\nPress [[;#ff5e99;]Enter] for more information on [[;#009ae3;]occ] commands.\n") - }, - name: 'occ', + + // Команды переключения режимов + if (command === ':sql') { + sqlMode = true; + term.set_prompt('sql $ '); + term.echo('SQL mode enabled. Type :occ to return to OCC mode.'); + return; + } + + if (command === ':occ') { + sqlMode = false; + term.set_prompt('occ $ '); + term.echo('OCC mode enabled.'); + return; + } + + if (command === ':help') { + term.echo('Available commands:'); + term.echo(' :sql - Switch to SQL mode'); + term.echo(' :occ - Switch to OCC mode'); + term.echo(' :help - Show this help'); + term.echo(''); + term.echo('In OCC mode: execute Nextcloud occ commands'); + term.echo('In SQL mode: execute SQL queries directly'); + return; + } + + term.pause(); + + if (sqlMode) { + // SQL режим + $.post(OC.generateUrl('/apps/occweb/db/query'), { + sql: command + }, function(response) { + if (response.success) { + response.results.forEach(function(result) { + if (result.type === 'select') { + term.echo('Query: ' + result.query); + term.echo('Rows: ' + result.count); + if (result.data.length > 0) { + term.echo(JSON.stringify(result.data, null, 2)); + } else { + term.echo('(no results)'); + } + } else if (result.type === 'write') { + term.echo('Query: ' + result.query); + term.echo('Affected rows: ' + result.affected_rows); + } else if (result.type === 'set') { + term.echo('Skipped: ' + result.query); + } + term.echo(''); + }); + } else { + term.echo('ERROR: ' + response.error); + } + term.resume(); + }).fail(function(xhr, status, error) { + term.echo('ERROR: Request failed - ' + error); + term.resume(); + }); + } else { + // OCC режим + $.post(OC.generateUrl('/apps/occweb/cmd'), { + command: command + }, function(response) { + term.echo('\n' + response).resume(); + }).fail(function(xhr, status, error) { + term.echo('ERROR: Request failed - ' + error); + term.resume(); + }); + } + }, { prompt: 'occ $ ', - completion: response, - onResize: function(){ - scrollToBottom() + name: 'occweb', + greetings: 'Nextcloud OCC Web Terminal\nType :help for available commands\n', + onBlur: function() { + return false; } - }); }); - $('html').keypress(function(){ - scrollToBottom() - }) - }); +}); + })(OC, window, jQuery);