Skip to content

Instantly share code, notes, and snippets.

@rhettl
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhettl/c3e25088025ed6374138 to your computer and use it in GitHub Desktop.
Save rhettl/c3e25088025ed6374138 to your computer and use it in GitHub Desktop.
/**
* preset Telerik's RadComboBox based on URL fragment parameters
*
* This function will autorun at startup. it requires Telerik and jQuery to be installed.
* It listens to the URL Fragment, ex: 'http://example.com/page?unimportant=stuff#greeting=hello%20world' would produce {greeting: "hello world"}
* It presently watches for 2 values, "search" and "category", and is separated similarly to the query line, i.e. = and &
* category IS case sensitive and space sensitive. Spaces __can__ be in the form of ' ' but __should__ be in the form of '%20' URL encoded
*
* @author Rhett Lowe <rng2ml@gmail.com>
* @param {Object} $ The windows jQuery object
* @param {Object} loc The window.location object
*
*/
(function($, loc){
// if No jQuery or no path or wrong path, exit
// $find comes from telerik. checking to make sure it is there for "RadComboBox".
if (!$ || !loc || !loc.pathname || !/^\/files/.test(loc.pathname) || !$find) {
return;
}
function changeSearch(txt){
// set text
return $('.assetSearch input.assetSearchBox[type="text"]')
// set value
.val(txt)
// find sibling
.parent().children('span.input-group-btn')
// get button
.children('input[type="button"]')
// click button
.trigger('click');
}
function selectCat(val){
// Get RadComboBox Element, don't use id since that may change, use parents and classses
var elem = document.querySelector('.assetSearch .assetSearchCat .RadComboBox'),
// use element id to get element with telerik's built in find function, $find()
combo = $find(elem.id),
// try to find given text in options
item = combo.findItemByText(val);
// if found, select.
if (item) {
item.select();
}
}
// Run on successful page load
$(function(){
// if no hash, nothing to do
if (!loc.hash) {
return;
}
// Make fragment object
var hash = {};
loc.hash
// remove hash
.replace(/#/, '')
// split into pairs
.split(/&/g)
// make object
.forEach(function(i){
var t = i.split(/=/);
hash[t[0].toLowerCase()] = decodeURI(t[1]);
});
if (hash.search) {
changeSearch(hash.search);
}
if (hash.category) {
selectCat(hash.category);
}
});
})(window.jQuery, window.location);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment