From b4c57eb52706fd61f3167dc05eccae1fd7c30513 Mon Sep 17 00:00:00 2001 From: Byron Jones Date: Wed, 26 Sep 2012 01:01:12 +0800 Subject: Bug 437212: Make page refreshing under Firefox update fields r=LpSolit, a=LpSolit --- js/field.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ js/util.js | 21 +++++++++++++++++++++ 2 files changed, 69 insertions(+) (limited to 'js') diff --git a/js/field.js b/js/field.js index caf13217b..7d47169d4 100644 --- a/js/field.js +++ b/js/field.js @@ -927,3 +927,51 @@ function userDisabledTextOnChange(disabledtext) { disable_mail.checked = true; } } + +/** + * Force the browser to honour the selected option when a page is refreshed, + * but only if the user hasn't explicitly selected a different option. + */ +function initDirtyFieldTracking() { + // old IE versions don't provide the information we need to make this fix work + // however they aren't affected by this issue, so it's ok to ignore them + if (YAHOO.env.ua.ie > 0 && YAHOO.env.ua.ie <= 8) return; + var selects = document.getElementById('changeform').getElementsByTagName('select'); + for (var i = 0, l = selects.length; i < l; i++) { + var el = selects[i]; + var el_dirty = document.getElementById(el.name + '_dirty'); + if (!el_dirty) continue; + if (!el_dirty.value) { + var preSelected = bz_preselectedOptions(el); + if (!el.multiple) { + preSelected.selected = true; + } else { + el.selectedIndex = -1; + for (var j = 0, m = preSelected.length; j < m; j++) { + preSelected[j].selected = true; + } + } + } + YAHOO.util.Event.on(el, "change", function(e) { + var el = e.target || e.srcElement; + var preSelected = bz_preselectedOptions(el); + var currentSelected = bz_selectedOptions(el); + var isDirty = false; + if (!el.multiple) { + isDirty = preSelected.index != currentSelected.index; + } else { + if (preSelected.length != currentSelected.length) { + isDirty = true; + } else { + for (var i = 0, l = preSelected.length; i < l; i++) { + if (currentSelected[i].index != preSelected[i].index) { + isDirty = true; + break; + } + } + } + } + document.getElementById(el.name + '_dirty').value = isDirty ? '1' : ''; + }); + } +} diff --git a/js/util.js b/js/util.js index 7ecc2a3d8..6d1f88938 100644 --- a/js/util.js +++ b/js/util.js @@ -229,6 +229,27 @@ function bz_selectedOptions(aSelect) { return selected; } +/** + * Returns all Option elements that have the "selected" attribute, as an array. + * Returns an empty array if nothing is selected. + * + * @param aSelect The select you want the pre-selected values of. + */ +function bz_preselectedOptions(aSelect) { + var options = aSelect.options; + var selected = new Array(); + for (var i = 0, l = options.length; i < l; i++) { + var attributes = options[i].attributes; + for (var j = 0, m = attributes.length; j < m; j++) { + if (attributes[j].name == 'selected') { + if (!aSelect.multiple) return options[i]; + selected.push(options[i]); + } + } + } + return selected; +} + /** * Tells you where (what index) in a -- cgit v1.2.3-24-g4f1b