Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use saner implementation for sort storage
This way we can store other things we need; like search query
  • Loading branch information
zoffixznet committed Nov 14, 2015
1 parent b94e115 commit 9cd8b94
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 5 deletions.
1 change: 1 addition & 0 deletions mojo-app/lib/ModulesPerl6.pm
Expand Up @@ -34,6 +34,7 @@ sub startup {
});
$self->asset('app.js' => qw{
https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js
/js/jquery-deparam.js
/js/main.js
});

Expand Down
97 changes: 97 additions & 0 deletions mojo-app/public/js/jquery-deparam.js
@@ -0,0 +1,97 @@
/*
jQuery deparam is an extraction of the deparam method from Ben Alman's jQuery BBQ
http://benalman.com/projects/jquery-bbq-plugin/
*/
(function ($) {
$.deparam = function (params, coerce) {
var obj = {},
coerce_types = { 'true': !0, 'false': !1, 'null': null };

// Iterate over all name=value pairs.
$.each(params.replace(/\+/g, ' ').split('&'), function (j,v) {
var param = v.split('='),
key = decodeURIComponent(param[0]),
val,
cur = obj,
i = 0,

// If key is more complex than 'foo', like 'a[]' or 'a[b][c]', split it
// into its component parts.
keys = key.split(']['),
keys_last = keys.length - 1;

// If the first keys part contains [ and the last ends with ], then []
// are correctly balanced.
if (/\[/.test(keys[0]) && /\]$/.test(keys[keys_last])) {
// Remove the trailing ] from the last keys part.
keys[keys_last] = keys[keys_last].replace(/\]$/, '');

// Split first keys part into two parts on the [ and add them back onto
// the beginning of the keys array.
keys = keys.shift().split('[').concat(keys);

keys_last = keys.length - 1;
} else {
// Basic 'foo' style key.
keys_last = 0;
}

// Are we dealing with a name=value pair, or just a name?
if (param.length === 2) {
val = decodeURIComponent(param[1]);

// Coerce values.
if (coerce) {
val = val && !isNaN(val) ? +val // number
: val === 'undefined' ? undefined // undefined
: coerce_types[val] !== undefined ? coerce_types[val] // true, false, null
: val; // string
}

if ( keys_last ) {
// Complex key, build deep object structure based on a few rules:
// * The 'cur' pointer starts at the object top-level.
// * [] = array push (n is set to array length), [n] = array if n is
// numeric, otherwise object.
// * If at the last keys part, set the value.
// * For each keys part, if the current level is undefined create an
// object or array based on the type of the next keys part.
// * Move the 'cur' pointer to the next level.
// * Rinse & repeat.
for (; i <= keys_last; i++) {
key = keys[i] === '' ? cur.length : keys[i];
cur = cur[key] = i < keys_last
? cur[key] || (keys[i+1] && isNaN(keys[i+1]) ? {} : [])
: val;
}

} else {
// Simple key, even simpler rules, since only scalars and shallow
// arrays are allowed.

if ($.isArray(obj[key])) {
// val is already an array, so push on the next value.
obj[key].push( val );

} else if (obj[key] !== undefined) {
// val isn't an array, but since a second value has been specified,
// convert val into an array.
obj[key] = [obj[key], val];

} else {
// val is a scalar.
obj[key] = val;
}
}

} else if (key) {
// No value was defined, so set something meaningful.
obj[key] = coerce
? undefined
: '';
}
});

return obj;
};
})(jQuery);
24 changes: 19 additions & 5 deletions mojo-app/public/js/main.js
Expand Up @@ -24,15 +24,15 @@ function setup_table() {

// This lets us restore correct sort order if the user uses "Back"
// button in their browser
sort_order = window.location.hash.match(/sort-([^-]+)-([^-])+/);
if ( sort_order ) {
if ( hash_store('sort-col') ) {
table_plugin.order([
sort_order[1], sort_order[2] == 'a' ? 'asc' : 'desc'
hash_store('sort-col'),
hash_store('sort-dir') == 'a' ? 'asc' : 'desc'
]).draw();
}
el.find('th').click(function(){
window.location.hash = 'sort-' + $(this).index() + '-'
+ $(this).attr('aria-sort').substr(0,1);
hash_store('sort-col', $(this).index() );
hash_store('sort-dir', $(this).attr('aria-sort').substr(0,1));
});

// Mess around with markup to accomodate the table plugin and marry
Expand All @@ -46,3 +46,17 @@ function setup_table() {
filter_container.find('label').remove();
filter.focus();
}

function hash_store (key, value){
var obj;

try { obj = jQuery.deparam(window.location.hash.replace(/^#/,'')) }
catch (e) { obj = {} }

if ( value ) {
obj[key] = value;
window.location.hash = jQuery.param( obj, true );
}

return obj[key];
}

0 comments on commit 9cd8b94

Please sign in to comment.