File:  [LON-CAPA] / loncom / html / adm / quicksearch / quicksearch.js
Revision 1.1: download - view: text, annotated - select for diffs
Mon Oct 13 14:50:34 2014 UTC (9 years, 8 months ago) by goltermann
Branches: MAIN
CVS tags: version_2_12_X, HEAD
added search functionality to authoring space

- users can now search their author space for files. the search functions searches in the file name and title. use of regular expressions and case sensitivity can be enabled. the content table of the current directory will shrink so that only matching entries are shown. a second table appears underneath to show results in other directories.
- german translation has been added.

function applyFilter(thisdir) {

    var pattern = prepareSearchPattern();
    var translations = jQuery.parseJSON(localStorage.getItem('CSTRtrans')).translations;

    updateDisplay(pattern, translations);
    filterTableElements(pattern);
    filterOtherElements(pattern, translations, thisdir);
}

// called everytime the user makes an input into the search field
function updateDisplay(pattern, translations) {
    var header1 = document.getElementById('searchtitle');

    if (pattern.source == '(?:)'){
        header1.style.display = 'none';
    } else {
        // remove escape symbol for better readability
        header1.innerHTML = translations.results+' '+pattern.source.replace('\\','');
        header1.style.display = '';
    }
}

// filters the contents table of the authoring space to only show matching resources in the current dir
function filterTableElements(pattern) {
    var list = document.getElementsByClassName('LC_data_table')[0].childNodes[1].children;

    // i=2 to skip searchtitle and header
    for (var i = 2; i < list.length; i++){
        // get filename: table column,     <span>,       <a>,       name
        var resname = list[i].children[2].children[0].children[0].innerHTML;
        var restitle;

        // check if resource has title and get it
        var titleColumn = list[i].children[3].children;

        if (titleColumn.length > 2){
            restitle = titleColumn[0].innerHTML;
        } else {
            restitle = '';
        }

        // match in filename OR resource title OR if search string is empty
        if ( pattern.test(resname) || pattern.test(restitle) || pattern.source == '' ){
            list[i].style.display = '';     // show element
        } else {
            list[i].style.display = 'none'; // hide element
        }
    }
}

// this is the search function for resources in other locations.
// pattern - the search pattern given by the user
// translations - loncapa generated translation object for the user interface
// thisdir - dir the user is currently in, used to generate relativ links for found files
function filterOtherElements (pattern, translations, thisdir) {

    var otherfiles = jQuery.parseJSON(sessionStorage.getItem('CSTRcache')).resources;
    if (pattern.source == '(?:)'){
        document.getElementById('otherplaces').style.display = 'none';
        return;
    }

    var hits = new Array();
    for (var i = 0; i < otherfiles.length; i++){
        if (otherfiles[i].title == '[untitled]'){
            otherfiles[i].title = '';
        }

        if ( pattern.test(otherfiles[i].name) || pattern.test(otherfiles[i].title) ){
            hits.push(otherfiles[i]);
        }
    }
    if (hits.length < 1){
        document.getElementById('otherplaces').style.display = 'none';
    } else {
        document.getElementById('otherplaces').style.display = '';
        var element = document.getElementsByName("otherplacescontent");

        // remove all old children
        for (index = element.length - 1; index >= 0; index--){
            element[index].parentNode.removeChild(element[index]);
        }

        // add new children
        for (var i = 0; i < hits.length; i ++){
            document.getElementById('otherplacestable').parentNode.appendChild(renderRow(hits[i], i, translations, thisdir));
        }
    }
}

// adds an row to the other places result table, used by filterOtherElements()
function renderRow(element, i, translations, thisdir) {
    var isdir = false;
    var row = '';
// icon
    row += '<td>';
    var extension = /\.(.+?)$/;

    if (element.name.match(extension)){
        var allowed = ['avi','cab','doc','dvi','eps','exam','htm','html','jar','jpeg','jpg','library','mov','mpeg','mpg','page','pdf','png','ppt','problem','ps','qt','quiz','sequence','spreadsheet','survey','task','tex','text','tth','txt','wav','wmv','xls','xml','zip'];
        var match = extension.exec(element.name)[1];
        if (allowed.indexOf(match) > -1) {
            // is supported file type
            row += '<img src="/adm/lonIcons/'+match+'.gif" alt="" />';
        } else {
            // is some other file we dont have an icon for
            row += '<img src="/adm/lonIcons/srvnull.gif" alt="" />';
        }
    } else {
        // is a directory
        isdir = true;
        row += '<img src="/adm/lonIcons/navmap.folder.closed.gif" alt="" />';
    }

    element.path = element.path.replace(/^\/home\/httpd\/html\//,'\/');

// directory
    var location = element.path.replace(/\/priv\//,'');
    if (location == ''){ location = '/'; }
    row += '</td><td>';
    if (isdir) {
        row += '<a href="/priv/'+location+'" target="_parent"></a>';
    } else {
        row += '<a href="/priv/'+location+'" target="_parent">'+location.replace(/(.+?)\/(.+?)\//,'/')+'</a>';
    }
    row += '</td><td>';

    if (isdir) {

    // file link
        row += '<span class="LC_filename"><a href="'+element.path+'/" target="_parent">'+element.name+'</a></span> <br />';
        row += '</td><td></td>';
    // status
        row += '<td class="LC_browser_file_'+element.status+'">&nbsp;&nbsp;</td><td>'+translations.directory+'</td>';
        row += '<td>'+element.cmtime+'</td></tr>';
    } else {
    
    // file link
        row += '<span class="LC_filename"><a href="'+element.path+element.name+'" target="_parent">'+element.name+'</a></span> <br />';
    // editor
        row += '(<a href="'+element.path+element.name+'?editmode=Edit&amp;problemmode=edit">'+translations.edit+'</a>)';
    // xml editor
        row += '(<a href="'+element.path+element.name+'?editmode=Edit&amp;problemmode=editxml">'+translations.editxml+'</a>)';
        row += '</td><td>';
    // edit metadata
        row += '<a href="'+element.path+element.name+'.meta" target="cat">'+element.title+'</a><br />'
        row += '<a href="'+element.path+element.name+'.meta">'+translations.editmeta+'</a>';
        row += '</td>';
    // status
        row += '<td class="LC_browser_file_'+element.status+'">&nbsp;&nbsp;</td><td>'+translations[element.status]+'</td>';
        row += '<td>'+element.cmtime+'</td></tr>';
    }

    var result = document.createElement('tr');
    result.innerHTML += row;
    result.setAttribute('name', 'otherplacescontent');
// set row style
    if (i % 2 == 1){
        result.setAttribute('class', 'LC_odd_row');
    } else {
        result.setAttribute('class', 'LC_even_row');
    }
    return result;
}

// escapes the search pattern for on screen display
function prepareSearchPattern() {
    var searchstring = document.getElementById('quickfilter').value;
    var regopt = document.getElementById('casesens').checked?'':'i';

    if (document.getElementById('regex').checked == false) {
        searchstring = searchstring.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
    } else {
        // a single \ at the end would escape the closing / of the regex
        searchstring = searchstring.replace(/\\$/, '\\\\');
    }
    return new RegExp(searchstring, regopt);
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>