custom-search.js 12 KB
Newer Older
1 2 3
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
 *
5 6
 * This Source Code Form is "Incompatible With Secondary Licenses", as
 * defined by the Mozilla Public License, v. 2.0.
7 8 9
 */

var PAREN_INDENT_EM = 2;
10 11 12 13 14 15 16 17 18 19 20 21 22
var ANY_ALL_SELECT_CLASS = 'any_all_select';

// When somebody chooses to "Hide Advanced Features" for Custom Search,
// we don't want to hide "Not" checkboxes if they've been checked. We
// accomplish this by removing the custom_search_advanced class from Not
// checkboxes when they are checked.
//
// We never add the custom_search_advanced class back. If we did, TUI
// would get confused in this case: Check Not, Hide Advanced Features,
// Uncheck Not, Show Advanced Features. (It hides "Not" when it shouldn't.)
function custom_search_not_changed(id) {
    var container = document.getElementById('custom_search_not_container_' + id);
    YAHOO.util.Dom.removeClass(container, 'custom_search_advanced');
23 24

    fix_query_string(container);
25
}
26 27 28 29 30

function custom_search_new_row() {
    var row = document.getElementById('custom_search_last_row');
    var clone = row.cloneNode(true);
    
31
    _cs_fix_row_ids(clone);
32 33 34 35 36 37 38 39 40 41 42 43 44 45
   
    // We only want one copy of the buttons, in the new row. So the old
    // ones get deleted.
    var op_button = document.getElementById('op_button');
    row.removeChild(op_button);
    var cp_button = document.getElementById('cp_container');
    row.removeChild(cp_button);
    var add_button = document.getElementById('add_button');
    row.removeChild(add_button);
    _remove_any_all(clone);

    // Always make sure there's only one row with this id.
    row.id = null;
    row.parentNode.appendChild(clone);
46
    cs_reconfigure(row);
47
    fix_query_string(row);
48 49 50
    return clone;
}

51
var _cs_source_any_all;
52 53 54
function custom_search_open_paren() {
    var row = document.getElementById('custom_search_last_row');

55 56 57 58 59 60 61 62 63 64 65 66 67
    // create a copy of j_top and use that as the source, so we can modify
    // j_top if required
    if (!_cs_source_any_all) {
        var j_top = document.getElementById('j_top');
        _cs_source_any_all = j_top.cloneNode(true);
    }

    // find the parent any/all select, and remove the grouped option
    var structure = _cs_build_structure(row);
    var old_id = _cs_get_row_id(row);
    var parent_j = document.getElementById(_cs_get_join(structure, 'f' + old_id));
    _cs_remove_and_g(parent_j);

68 69
    // If there's an "Any/All" select in this row, it needs to stay as
    // part of the parent paren set.
70 71
    var old_any_all = _remove_any_all(row);
    if (old_any_all) {
72 73
        var any_all_row = row.cloneNode(false);
        any_all_row.id = null;
74
        any_all_row.appendChild(old_any_all);
75 76 77 78 79 80 81 82 83
        row.parentNode.insertBefore(any_all_row, row);
    }

    // We also need a "Not" checkbox to stay in the parent paren set.
    var new_not = YAHOO.util.Dom.getElementsByClassName(
        'custom_search_not_container', null, row);
    var not_for_paren = new_not[0].cloneNode(true);

    // Preserve the values when modifying the row.
84
    var id = _cs_fix_row_ids(row, true);
85 86 87 88 89
    var prev_id = id - 1;

    var paren_row = row.cloneNode(false);
    paren_row.id = null;
    paren_row.innerHTML = '(<input type="hidden" name="f' + prev_id
90
                        + '" id="f' + prev_id + '" value="OP">';
91 92
    paren_row.insertBefore(not_for_paren, paren_row.firstChild);
    row.parentNode.insertBefore(paren_row, row);
93

94
    // New paren set needs a new "Any/All" select.
95 96
    var any_all_container = document.createElement('div');
    YAHOO.util.Dom.addClass(any_all_container, ANY_ALL_SELECT_CLASS);
97
    var any_all = _cs_source_any_all.cloneNode(true);
98 99 100
    any_all.name = 'j' + prev_id;
    any_all.id = any_all.name;
    any_all_container.appendChild(any_all);
101 102 103 104 105 106 107
    row.insertBefore(any_all_container, row.firstChild);

    var margin = YAHOO.util.Dom.getStyle(row, 'margin-left');
    var int_match = margin.match(/\d+/);
    var new_margin = parseInt(int_match[0]) + PAREN_INDENT_EM;
    YAHOO.util.Dom.setStyle(row, 'margin-left', new_margin + 'em');
    YAHOO.util.Dom.removeClass('cp_container', 'bz_default_hidden');
108

109
    cs_reconfigure(any_all_container);
110
    fix_query_string(any_all_container);
111 112 113 114 115 116 117
}

function custom_search_close_paren() {
    var new_row = custom_search_new_row();
    
    // We need to up the new row's id by one more, because we're going
    // to insert a "CP" before it.
118
    var id = _cs_fix_row_ids(new_row);
119 120 121 122 123 124 125 126 127

    var margin = YAHOO.util.Dom.getStyle(new_row, 'margin-left');
    var int_match = margin.match(/\d+/);
    var new_margin = parseInt(int_match[0]) - PAREN_INDENT_EM;
    YAHOO.util.Dom.setStyle(new_row, 'margin-left', new_margin + 'em');

    var paren_row = new_row.cloneNode(false);
    paren_row.id = null;
    paren_row.innerHTML = ')<input type="hidden" name="f' + (id - 1)
128
                        + '" id="f' + (id - 1) + '" value="CP">';
129 130 131 132 133 134
  
    new_row.parentNode.insertBefore(paren_row, new_row);

    if (new_margin == 0) {
        YAHOO.util.Dom.addClass('cp_container', 'bz_default_hidden');
    }
135

136
    cs_reconfigure(new_row);
137 138 139 140 141 142 143 144 145 146
    fix_query_string(new_row);
}

// When a user goes Back in their browser after searching, some browsers
// (Chrome, as of September 2011) do not remember the DOM that was created
// by the Custom Search JS. (In other words, their whole entered Custom
// Search disappears.) The solution is to update the History object,
// using the query string, which query.cgi can read to re-create the page
// exactly as the user had it before.
function fix_query_string(form_member) {
147 148 149
    // window.History comes from history.js.
    // It falls back to the normal window.history object for HTML5 browsers.
    if (!(window.History && window.History.replaceState))
150 151 152
        return;

    var form = YAHOO.util.Dom.getAncestorByTagName(form_member, 'form');
153 154 155 156 157 158
    // Disable the token field so setForm doesn't include it
    var reenable_token = false;
    if (form['token'] && !form['token'].disabled) {
      form['token'].disabled = true;
      reenable_token = true;
    }
159
    var query = YAHOO.util.Connect.setForm(form);
160 161
    if (reenable_token)
      form['token'].disabled = false;
162
    window.History.replaceState(null, document.title, '?' + query);
163 164
}

165 166 167 168 169 170 171 172 173 174 175 176 177
// Browsers that do not support HTML5's history.replaceState gain an emulated
// version via history.js, with the full query_string in the location's hash.
// We rewrite the URL to include the hash and redirect to the new location,
// which causes custom search fields to work.
function redirect_html4_browsers() {
    var hash = document.location.hash;
    if (hash == '') return;
    hash = hash.substring(1);
    if (hash.substring(0, 10) != 'query.cgi?') return;
    var url = document.location + "";
    url = url.substring(0, url.indexOf('query.cgi#')) + hash;
    document.location = url;
}
178

179
function _cs_fix_row_ids(row, preserve_values) {
180
    // Update the label of the checkbox.
181
    var label = YAHOO.util.Dom.getElementBy(function() { return true }, 'label', row);
182 183 184 185
    var id_match = label.htmlFor.match(/\d+$/);
    var id = parseInt(id_match[0]) + 1;
    label.htmlFor = label.htmlFor.replace(/\d+$/, id);

186
    // Sets all the inputs in the row back to their default
187 188
    // and fixes their id.
    var fields =
189
        YAHOO.util.Dom.getElementsByClassName('custom_search_form_field', null, row);
190 191 192 193 194 195 196 197 198 199 200
    for (var i = 0; i < fields.length; i++) {
        var field = fields[i];

        if (!preserve_values) {
            if (field.type == "checkbox") {
                field.checked = false;
            }
            else {
                field.value = '';
            }
        }
201 202

        // Update the numeric id for the row.
203 204 205
        field.name = field.name.replace(/\d+$/, id);
        field.id = field.name;
    }
206

207 208 209
    return id;
}

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
function _cs_build_structure(form_member) {
    // build a map of the structure of the custom fields
    var form = YAHOO.util.Dom.getAncestorByTagName(form_member, 'form');
    var last_id = _get_last_cs_row_id(form);
    var structure = [ 'j_top' ];
    var nested = [ structure ];
    for (var id = 1; id <= last_id; id++) {
        var f = form['f' + id];
        if (!f || !f.parentNode.parentNode) continue;

        if (f.value == 'OP') {
            var j = [ 'j' + id ];
            nested[nested.length - 1].push(j);
            nested.push(j);
            continue;
        } else if (f.value == 'CP') {
            nested.pop();
            continue;
        } else {
            nested[nested.length - 1].push('f' + id);
        }
    }
    return structure;
}

function cs_reconfigure(form_member) {
    var structure = _cs_build_structure(form_member);
    _cs_add_listeners(structure);
    _cs_trigger_j_listeners(structure);
    fix_query_string(form_member);

    var j = _cs_get_join(structure, 'f' + _get_last_cs_row_id());
    document.getElementById('op_button').disabled = document.getElementById(j).value == 'AND_G';
}

function _cs_add_listeners(parents) {
    for (var i = 0, l = parents.length; i < l; i++) {
        if (typeof(parents[i]) == 'object') {
            // nested
            _cs_add_listeners(parents[i]);
        } else if (i == 0) {
            // joiner
            YAHOO.util.Event.removeListener(parents[i], 'change', _cs_j_change);
            YAHOO.util.Event.addListener(parents[i], 'change', _cs_j_change, parents);
        } else {
            // field
            YAHOO.util.Event.removeListener(parents[i], 'change', _cs_f_change);
            YAHOO.util.Event.addListener(parents[i], 'change', _cs_f_change, parents);
        }
    }
}

function _cs_trigger_j_listeners(fields) {
    var has_children = false;
    for (var i = 0, l = fields.length; i < l; i++) {
        if (typeof(fields[i]) == 'undefined') {
            continue;
        } else if (typeof(fields[i]) == 'object') {
            // nested
            _cs_trigger_j_listeners(fields[i]);
            has_children = true;
        } else if (i == 0) {
            _cs_j_change(undefined, fields);
        }
    }
    if (has_children) {
        _cs_remove_and_g(document.getElementById(fields[0]));
    }
}

function _cs_get_join(parents, field) {
    for (var i = 0, l = parents.length; i < l; i++) {
        if (typeof(parents[i]) == 'object') {
            // nested
            var result = _cs_get_join(parents[i], field);
            if (result) return result;
        } else if (parents[i] == field) {
            return parents[0];
        }
    }
    return false;
}

function _cs_remove_and_g(join_field) {
    var index = bz_optionIndex(join_field, 'AND_G');
    join_field.options[index] = null;
    join_field.options[bz_optionIndex(join_field, 'AND')].innerHTML = cs_and_label;
    join_field.options[bz_optionIndex(join_field, 'OR')].innerHTML = cs_or_label;
}

function _cs_j_change(evt, fields, field) {
    var j = document.getElementById(fields[0]);
    if (j && j.value == 'AND_G') {
        for (var i = 1, l = fields.length; i < l; i++) {
            if (typeof(fields[i]) == 'object') continue;
            if (!field) {
                field = document.getElementById(fields[i]).value;
            } else {
                document.getElementById(fields[i]).value = field;
            }
        }
        if (evt) {
            fix_query_string(j);
        }
        if ('f' + _get_last_cs_row_id() == fields[fields.length - 1]) {
            document.getElementById('op_button').style.display = 'none';
        }
    } else {
        document.getElementById('op_button').style.display = '';
    }
}

function _cs_f_change(evt, args) {
    var field = YAHOO.util.Event.getTarget(evt);
    _cs_j_change(evt, args, field.value);
}

function _get_last_cs_row_id() {
    return _cs_get_row_id('custom_search_last_row');
}

function _cs_get_row_id(row) {
    var label = YAHOO.util.Dom.getElementBy(function() { return true }, 'label', row);
    return parseInt(label.htmlFor.match(/\d+$/)[0]);
}

336
function _remove_any_all(parent) {
337 338
    var any_all = YAHOO.util.Dom.getElementsByClassName(
        ANY_ALL_SELECT_CLASS, null, parent);
339 340 341 342 343 344
    if (any_all[0]) {
        parent.removeChild(any_all[0]);
        return any_all[0];
    }
    return null;
}