Sort2Hash: Add sort2Hash_replaceHistory option

This commit is contained in:
Rob Garrison 2017-08-31 21:35:00 -05:00
parent 6fe2cdf810
commit f0426f80f4
2 changed files with 33 additions and 10 deletions

View File

@ -42,6 +42,9 @@
sort2Hash_directionText : [ 0, 1 ], // default values
// if true, override saveSort widget sort, if used & stored sort is available
sort2Hash_overrideSaveSort : true, // default = false
// if true, hash changes are not saved to browser history
sort2Hash_replaceHistory : false,
sort2Hash_encodeHash : null,
/* how to use encodeHash
sort2Hash_encodeHash : function( config, tableId, component, value, rawValue ) {
@ -209,6 +212,17 @@
<td>if <code>true</code>, the hash sort will override any stored sort (saveSort widget).</td>
</tr>
<tr id="sort2hash_replacehistory">
<td><a href="#" class="permalink">sort2Hash_replaceHistory</a></td>
<td><code>false</code></td>
<td>Change how the browser history is managed (<span class="version">v2.29.0</span>)
<div class="collapsible">
<p>If <code>true</code>, all hash changes are not saved to browser history, so when the user presses the back arrow, they will be returned to the previous page.</p>
If <code>false</code>, all hash changes are preserved in the browser history, so when the user presses the back arrow, the hash will show the previous state, but the table won't update unless the page is refreshed.
</div>
</td>
</tr>
<tr id="sort2hash_encodehash">
<td><a href="#" class="permalink">sort2Hash_encodeHash</a></td>
<td><code>null</code></td>

View File

@ -226,18 +226,26 @@
str += value;
});
var hashChar = wo.sort2Hash_hash;
// Combine new hash with any existing hashes
var hashChar = c.widgetOptions.sort2Hash_hash;
var newHash = ( ( window.location.hash || '' ).replace( hashChar, '' ).length ? hash : wo.sort2Hash_hash ) + str;
var baseUrl = window.location.href.split(hashChar)[0];
// Ensure that there is a leading hash character
var firstChar = newHash[0];
if (firstChar != hashChar) {
newHash = hashChar + newHash;
}
var newHash = (
( window.location.hash || '' ).replace( hashChar, '' ).length ?
hash : hashChar
) + str;
// Update URL in browser
window.location.replace(baseUrl + newHash);
if (wo.sort2Hash_replaceHistory) {
var baseUrl = window.location.href.split(hashChar)[0];
// Ensure that there is a leading hash character
var firstChar = newHash[0];
if (firstChar != hashChar) {
newHash = hashChar + newHash;
}
// Update URL in browser
window.location.replace(baseUrl + newHash);
} else {
// Add updated hash
window.location.hash = newHash;
}
}
};
@ -250,6 +258,7 @@
sort2Hash_headerTextAttr : 'data-header', // data attribute containing alternate header text
sort2Hash_directionText : [ 0, 1 ], // [ 'asc', 'desc' ],
sort2Hash_overrideSaveSort : false, // if true, override saveSort widget if saved sort available
sort2Hash_replaceHistory : false, // if true, hash changes are not saved to browser history
// this option > table ID > table index on page
sort2Hash_tableId : null,