view static/js/download.js @ 350:987caba84824

downloads: highlight preferred options with <em>
author David Champion <dgc@uchicago.edu>
date Sun, 21 Nov 2010 01:26:01 -0600
parents fb3ce83c1a48
children e4d31654a9d3
line wrap: on
line source

function Download (source) {
    this.version = source[0];
    this.regex = source[1];
    this.url = source[2];
    this.desc = source[3];
}

Download.prototype = {
    matches: function (ua) {
        if (ua.match(this.regex))
            return true;
        return false;
    },

    download: function () {
        document.location.href = this.url;
        return false;
    },

    attr: function (key) {
        return this[key];
    },

    write: function (key) {
        document.write(this[key]);
    }
}


var Downloader = {
    downloads: [],

    init: function (sources) {
        for (i in sources) {
            var source = new Download(sources[i]);
            this.downloads.push(source);
        }
    },

    select: function () {
        var ua = navigator.userAgent;
        for (i in this.downloads) {
            if (this.downloads[i].matches(ua)) {
                return this.downloads[i];
            }
        }
        return null;
    },

    listall: function () {
        // copy the download list
        var downloads = this.downloads.slice(0);
        // alpha-sort it by description (case-folded)
        downloads.sort(function (a, b) {
            a = a.desc.toLowerCase();
            b = b.desc.toLowerCase();
            return (b < a) - (a < b);
        });

        var desc;
        for (i in downloads) {
            var dl = downloads[i];
            var ua = navigator.userAgent;
            if (dl.matches(ua))
                desc = '<em>' + dl.desc + '</em>';
            else
                desc = dl.desc;
            document.write('<tr>\n<td>' + desc + '</td>' +
                           '<td></td>' +
                           '<td><a href="' + dl.url + '">download</a></td>' +
                           '</tr>');
        }
    }
};