Skip to content

Instantly share code, notes, and snippets.

@hassanrehman
Last active April 20, 2018 10:39
Show Gist options
  • Save hassanrehman/c3a5228bcfff72d223ee32e059ff0c7e to your computer and use it in GitHub Desktop.
Save hassanrehman/c3a5228bcfff72d223ee32e059ff0c7e to your computer and use it in GitHub Desktop.
Adds a button next to BOLT query runner, to reload the file from the filesystem so that the user can change the file in an external editor and re-run the query
//NOTE: use CJS plugin on chrome to append this additional js ...
//TODO: ability to have multiple files present, and the current one to be selected from dropdown
//TODO: memory to keep the list safe
//TODO: file picker to get new instead of manually typing the paths
window.auto_query_config = {
file_path: '/path/to/file.sql'
autoload_file: false
}
window.logger = function(msg) {
console.log(msg)
}
window._stopEventPropogation = function(ev) {
ev = ev || window.event;
ev.cancelBubble = true;
ev.stopPropagation();
ev.stopImmediatePropagation();
ev.preventDefault();
}
window.query_frame = function(){
frame_div = $('.wcIFrame:not(.wcIFramePanelHidden) iframe')
if( frame_div.length == 0 ){
// logger("couldn't detect frame")
return undefined;
}
return frame_div.contents();
}
window.select_file_in_manager = function(file_path) {
if( !(frame = query_frame()) ) return;
input_file = $('.file_manager input#file-input-path', frame)
if( input_file.length == 0 ) {
logger("couldn't detect file manager")
return
}
ok_button = $('.file_manager_ok', frame)
query_button = $('#btn-flash', frame)
input_file.val(file_path)
ok_button.attr('disabled', false).trigger('click')
setTimeout(function(){
logger("running query...")
query_button.trigger('click')
}, 500)
}
window.load_file = function(file_path){
if( !(frame = query_frame()) ) return;
logger("opening dialog box..")
$('.btn-load-file', frame).trigger('click')
setTimeout(function(){
logger("selecting file...")
window.select_file_in_manager(file_path)
}, 500)
}
window.auto_flash_btn_click = function(){
load_file(auto_query_config.file_path)
}
window.add_auto_flash_btn = function(){
if( !(frame = query_frame()) ) return;
auto_query_button = $('#btn-auto-flash', frame)
if( auto_query_button.length > 0 ) return; //already exists .. don't add
btn_flash = $('#btn-flash', frame)
if( btn_flash.length == 0 ){
logger("add bomb btn - could'nt find query button for reference...")
return;
}
btn_flash_group = btn_flash.parent()
btn_auto_flash_group = $('<div>', {class: 'btn-group', role: 'group'})
//basic button
btn_auto_flash = $(btn_flash[0].outerHTML).attr('id', 'btn-auto-flash').attr('title', "Reload File and Execute")
$(btn_auto_flash).find("i").attr('class', 'fa fa-bomb').on('click', auto_flash_btn_click)
$(btn_auto_flash_group).append(btn_auto_flash)
//dropdown caret
dropdown_caret = $(btn_flash_group.find("#btn-query-dropdown")[0].outerHTML).attr('title', 'Auto Execute Options')
$(btn_auto_flash_group).append(dropdown_caret)
//ul dropdown
dropdown = $("<ul>").addClass('dropdown-menu').attr('role', 'menu')
//li auto run, based on auto-commit
li = $(btn_flash_group.find("ul.dropdown-menu li a#btn-auto-commit").parent()[0].outerHTML)
$(li).find("a").attr('id', 'btn-auto-reload').find("i")
.attr('class', 'auto-reload fa fa-check').addClass(auto_query_config.autoload_file ? '' : 'visibility-hidden')
.parent().find("span").text("Auto Reload?")
$(li).find("a").on('click', function(ev){
_stopEventPropogation(ev)
auto_query_config.autoload_file = !auto_query_config.autoload_file
auto_query_config.autoload_file ? $(this).find("i").removeClass('visibility-hidden') : $(this).find("i").addClass('visibility-hidden')
})
dropdown.append(li)
$(btn_auto_flash_group).append(dropdown)
$(btn_flash_group).after(btn_auto_flash_group)
}
window.auto_add_auto_flash_button_timer = setInterval(add_auto_flash_btn, 2000)
window.load_current_file_if_changed = function(){
$.ajax({
"method": "POST",
"contentType": "application/json",
"url": "/sqleditor/load_file/",
"data": JSON.stringify({ "file_name" : auto_query_config.file_path }),
"success": function(data){
query_content = data.trim().replace(/\s+/g, " ")
if( auto_query_config._current_query_content != query_content ) {
console.log("file changed on source .. reloading..")
auto_query_config._current_query_content = query_content
load_file( auto_query_config.file_path )
}
}
})
}
window.monitor_current_file = function(){
if( !auto_query_config.autoload_file ) return;
load_current_file_if_changed()
}
window.auto_reload_query_from_file_timer = setInterval(monitor_current_file, 2000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment