Last active
August 29, 2015 14:21
-
-
Save dlukes/a99dca231db63c9d5bb7 to your computer and use it in GitHub Desktop.
An improved version of my Greasemonkey/Tampermonkey userscript to tweak the layout of the KonText corpus concordancer (https://kontext.korpus.cz).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name KonText Interface Layout Switcher | |
// @namespace https://trnka.korpus.cz/~lukes | |
// @version 1.0.0 | |
// @description turns the KonText topbar into a sidebar, adds a query box | |
// @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 getURLParameter(param, URL) { | |
URL = typeof URL == 'undefined' ? location.search : URL; | |
return decodeURIComponent((new RegExp('[?|&]' + param + '=' + '([^&;]+?)(&|#|;|$)').exec(URL)||[,""])[1].replace(/\+/g, '%20'))||null; | |
} | |
function replaceURLParamater(param, replacement, URL) { | |
URL = typeof URL == 'undefined' ? document.URL : URL; | |
var replace = '$1' + encodeURIComponent(replacement); | |
var match = new RegExp('([\?&]' + param + '=)[^&]+'); | |
return URL.replace(match, replace); | |
} | |
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 = getURLParameter('usesubcorp'); | |
var queryDescUrl = $('#query-overview-trigger').data('json-href'); | |
var query; | |
var defaultAttr; | |
var tourl; | |
node.after(queryBox); | |
// fetch query description and fill the queryBox | |
$.ajax(queryDescUrl, { | |
dataType: 'json', | |
success: function (data) { | |
var queryDesc = data.Desc[0].arg; | |
var sep = queryDesc.indexOf(','); | |
defaultAttr = queryDesc.slice(0, sep); | |
query = queryDesc.slice(sep + 1); | |
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(); | |
tourl = 'view' + replaceURLParamater('q', 'a' + defaultAttr + ',' + newQuery, '?' + tourl); | |
tourl = usesubcorp ? tourl + '&usesubcorp=' + usesubcorp : tourl; | |
window.location.assign(tourl); | |
} | |
}); | |
boxAppended = true; | |
} | |
}; | |
} | |
var style = '\ | |
#menu-bar {\ | |
float: left !important;\ | |
min-width: 0px !important;\ | |
max-width: 198px !important;\ | |
margin: 0px 20px 0px 0px !important;\ | |
padding: 20px 0px !important;\ | |
height: 100% !important;\ | |
border-radius: 9px !important;\ | |
overflow: hidden !important;\ | |
box-shadow: 0 0 4px #a0a0a0 !important;\ | |
background-color: #d2edc0 !important;\ | |
}\ | |
#menu-level-1 {\ | |
margin: 0 15px !important;\ | |
}\ | |
#menu-level-1 ul {\ | |
display: block !important;\ | |
list-style: none !important;\ | |
margin: 0 !important;\ | |
padding: 0 !important;\ | |
}\ | |
#menu-level-1 li {\ | |
display: list-item !important;\ | |
white-space: normal;\ | |
margin: 0 !important;\ | |
padding: 0 !important;\ | |
}\ | |
#menu-bar li li a:hover {\ | |
color: #009EE0 !important;\ | |
}\ | |
#menu-level-1 li li {\ | |
padding-left: 5px !important;\ | |
font-weight: normal !important;\ | |
}\ | |
#menu-bar .separator {\ | |
width: 100% !important;\ | |
height: 1px !important;\ | |
background-color: #a0a0a0 !important;\ | |
}\ | |
#content {\ | |
clear: both !important;\ | |
}\ | |
#topbar {\ | |
float: left !important;\ | |
padding-right: 7px !important;\ | |
}\ | |
#query-box {\ | |
padding: 28px 30px 0px 0px;\ | |
overflow: auto;\ | |
}\ | |
\ | |
/* 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) { | |
node.prependTo("#content"); | |
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