Skip to content

Instantly share code, notes, and snippets.

@dlukes
Last active April 3, 2017 09:05
Show Gist options
  • Save dlukes/e454d43b2efe1ab556d9 to your computer and use it in GitHub Desktop.
Save dlukes/e454d43b2efe1ab556d9 to your computer and use it in GitHub Desktop.
kontext-v0.9.x-queryBox.js
// ==UserScript==
// @name KonText Interface Layout Switcher
// @namespace https://trnka.korpus.cz/~lukes
// @version 3.2.0 (for KonText v0.9.x)
// @description Adds a quick query box to KonText above the concordance
// @author David Lukeš
// @match https://kontext.korpus.cz/*
// @grant none
// @require http://code.jquery.com/jquery-latest.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// ==/UserScript==
// This code is licensed under the GNU GPL v3, see http://www.gnu.org/copyleft/gpl.html.
(function() {
var qParams = parseQueryString();
delete qParams.q;
function kilsAlert(msg) {
alert("KonText Interface Layout Switcher error: " + msg);
}
function parseQueryString(URL) {
URL = typeof URL == 'undefined' ? document.URL : URL;
asArray = URL.split("?");
if (asArray.length > 2) {
kilsAlert("Expected " + asArray + " to have no more than 2 elements!");
}
var keyVals = {};
asArray[1].split("&").forEach(function(keyVal) {
keyVal = keyVal.split("=");
keyVals[decodeURIComponent(keyVal[0])] = decodeURIComponent(keyVal[1]);
});
return keyVals;
}
function encodeQueryString(obj) {
var qString = [];
for (var k in obj) {
if (obj.hasOwnProperty(k)) {
qString.push(encodeURIComponent(k) + "=" + encodeURIComponent(obj[k]));
}
}
return qString.join("&");
}
function addGlobalStyle(css) {
var head, style;
head = document.getElementsByTagName('head')[0];
if (!head) return;
style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
head.appendChild(style);
}
function appendQueryBoxOnce() {
var boxAppended = false;
return function(node) {
if (!boxAppended && $('#conc-wrapper').length) {
var usesubcorp = qParams.usesubcorp;
var queryDescUrl = $("a.args").attr("href").replace("view?", "concdesc_json?");
var query;
var defaultAttr;
var tourl;
node.before(queryBox);
// fetch query description and fill the queryBox
$.ajax(queryDescUrl, {
dataType: 'json',
success: function(data) {
var queryDesc = data.Desc[0].arg;
matches = queryDesc.match(/^(\w+)?,?([^]*)/);
defaultAttr = matches[1];
defaultAttr = defaultAttr === undefined ? "word" : defaultAttr;
query = matches[2];
tourl = data.Desc[0].tourl;
$("#query").val(decodeURIComponent(query));
$("#query-box").attr('title', 'Ctrl + Enter: použít upravený CQL dotaz (implicitní atribut: ' + defaultAttr + ')');
}
});
// execute a new search with emended query upon pressing CTRL + ENTER in queryBox
$('#query').keydown(function(e) {
if (e.ctrlKey && e.keyCode == 13) { // the enter key code
var newQuery = $("#query").val();
qParams.queryselector = "cqlrow";
qParams.default_attr = "word";
qParams.cql = newQuery;
tourl = 'first?' + encodeQueryString(qParams);
tourl = usesubcorp ? tourl + '&usesubcorp=' + usesubcorp : tourl;
window.location.assign(tourl);
}
});
boxAppended = true;
}
};
}
var style = '';
style = '\
#query-box {\
padding: .5em 2em 0 30px;\
overflow: auto;\
}\
.navig-wrapper {\
padding-top: .5em !important;\
}\
\
/* based on Bootstrap */\
.form-control {\
display: inline-block;\
font-family: "Cousine", "Courier New", monospace;\
width: 100%;\
min-height: 60px;\
padding: 6px 12px;\
font-size: 14px;\
color: #555;\
background-color: #fff;\
margin: 0;\
-webkit-box-sizing: border-box;\
-moz-box-sizing: border-box;\
box-sizing: border-box;\
-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075);\
box-shadow: inset 0 1px 1px rgba(0,0,0,.075);\
-webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;\
-o-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;\
transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;\
resize: vertical;\
border-radius: 9px;\
}\
.form-control:focus {\
border-color: #009de0;\
outline: 0;\
-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6);\
box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6);\
}\
';
var queryBox = '\
<div id="query-box">\
<textarea class="form-control" id="query"/>\
</div>\
';
waitForKeyElements(".trigger", function(node) {
node.off();
setTimeout(function() {
node.parent().removeClass("active");
$("#menu-level-2-wrapper").remove();
node.off();
}, 1000);
});
waitForKeyElements("#menu-bar", function(node) {
addGlobalStyle(style);
});
waitForKeyElements("#topbar", appendQueryBoxOnce());
// the first query form will go under the floated left-side #menu-bar unless a min-width is set on #content
waitForKeyElements("#content", function(node) {
// left-margin of #content + inner width + padding of #menu-bar + right-padding of section
var minWidth = 20 + 198 + 20 + 15;
var firstQueryForm = node.find("section");
if (firstQueryForm.length > 0) {
minWidth += parseInt(firstQueryForm.width());
node.css("min-width", minWidth);
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment