Here's a function I wrote - am I getting DRY about right? I could add another argument to paramFilter
maybe, for even less reuse. Or have I gone too overboard as it is?
function goMageHack() {
var ampFilter = function (str) {
var amps = ["&", "amp%3B"];
for (var j = 0; j < amps.length; j++) {
str = fullReplace(amps[j],"amp;",str);
}
return str;
}
var paramFilter = function(element, param) {
var $element = jQuery(element);
for (var i = 0; i < param.length; i++) {
if ($element.attr(param[i])) {
$element.attr(param[i], ampFilter($element.attr(param[i])));
$element.attr(param[i], fullReplace("amp;", "&", $element.attr(param[i])));
}
}
}
jQuery(".pages,.sort-by").find("a").each(function (_, element) {
paramFilter(element, ["data-param", "href"]);
});
jQuery(".limiter").find("option").each(function(_, element){
paramFilter(element, ["value"]);
});
}
For completeness, since it is called from that function:
function fullReplace(needle, haystack, str) {
str = String(str);
var newStr;
while ((newStr = str.replace(needle, haystack)) !== str) {
str = newStr;
}
return newStr;
}
Addition
function handleChangingDropBox(attrib, that) {
var $this = jQuery(that);
var activeSet = Number($this.children(":selected").data("checkboxkey"));
var checkBoxes = checkBoxSets[activeSet];
var size;
if (currentlySelectedDropDownBox !== null) {
checkBoxSets[currentlySelectedDropDownBox].trigger("click");
}
currentlySelectedDropDownBox = activeSet;
if (size = checkBoxes.size()) {
var lastCheckBox = checkBoxes.last();
if (size !== 1) {
if ($this.val() === "choose") {
var dirtyParams = splitUrlIntoParams(lastCheckBox.data('param'));
delete dirtyParams[attrib];
lastCheckBox.attr("data-param", createAttribString(dirtyParams));
lastCheckBox.trigger("click");
currentlySelectedDropDownBox = null;
}
else {
var newParams = [];
var not = checkBoxes.not(lastCheckBox);
not.each(
function (index, element) {
var $element = jQuery(element);
newParams.push(getTheDiffForATextBox($element, lastCheckBox, attrib));
}
);
createTheDataAttribsForTheLastCheckBox(lastCheckBox, newParams, not.first(), attrib);
}
}
else {
var differentindex = (activeSet == 0) ? 1 : 0;
var notCheckBox = checkBoxSets[differentindex].first();
createTheDataAttribsForTheLastCheckBox(lastCheckBox, [], notCheckBox, attrib);
}
lastCheckBox.trigger("click");
}