integration/js/taghome.js
author veltr
Fri, 14 Dec 2012 13:07:58 +0100
changeset 50 9cc1b66d0880
child 64 458cc4576415
permissions -rw-r--r--
Added directory for Level 2 Integration

function showTags() {
    var tagsOuter = $(".taglist_container"),
        tagsInner = $(".taglist_container table"),
        acInput = $("#form_tag input[type=search]"),
        acdata = [],
        labels = {
            content: "Séquence",
            tag: "Tag"
        };
    
    acInput.autocomplete({
        source: function( request, response ) {
            response($.ui.autocomplete.filter(acdata, request.term));
        },
        select: function(event, ui) {
            document.location.href = ui.item.href;
        }
    }).data("autocomplete")._renderItem = function(ul, item) {
        return $( "<li>" )
            .data( "item.autocomplete", item )
            .append( "<a href='" + item.href + "'>" + labels[item.type] + " : " + item.label + "</a>" )
            .appendTo( ul );
    };
    
    $("#form_tag").submit(function() {
        return false;
    });
        
    tagsInner.draggable();
    
    function resizeTags() {
        tagsInner.css({
            "margin-left": - Math.floor(tagsInner.width()/2) + "px",
            "margin-top": - Math.floor(tagsInner.height()/2) + "px",
            "left": Math.floor(tagsOuter.width()/2) + "px",
            "top": Math.floor(tagsOuter.height()/2) + "px"
        });
    }
    $(window).on("resize",resizeTags);
    
    $.ajax({
        url: endpoints.contents_api,
        dataType: "jsonp", //TODO: JSON if on Platform
        data: {
            limit: 0,
            format: "jsonp"
        },
        success: function(data) {
            _(data.objects).each(function(content) {
                var fpmatch = content.front_project.match(/projects\/([0-9a-f\-]+)/),
                    fproj = fpmatch ? fpmatch[1] : "";
                acdata.push({
                    label: content.title,
                    type: "content",
                    href: endpoints.content_page.replace("__CONTENT_ID__", content.iri_id).replace("__FRONT_PROJECT_ID__", fproj)
                });
            });
        }
    })
    
    $.ajax({
        url: endpoints.tag_api,
        dataType: "json",
        data: {
            contents: "all",
            format: "json",
            limit: 0,
            steps: 100
        },
        success: function(data) {
            var grouped = {};
            _(data.objects).each(function(t) {
                var upt = t.name.toUpperCase(),
                    w = t.weight + (grouped[upt] || 0);
                grouped[upt] = w;
            });
            _(grouped).each(function(v, k) {
                acdata.push({
                    label: k,
                    type: "tag",
                    href: endpoints.tag_page.replace("__TAG__", k)
                });
            });
            var ordered = _(grouped)
                .chain()
                .map(function(v, k) {
                    return {
                        label: k,
                        weight: v
                    }
                })
                .sortBy(function(t) {
                    return -t.weight;
                })
                .value();
            var l = ordered.length,
                max = ordered[0].weight,
                min = Math.min(max - 1, ordered[l - 1].weight),
                colorscale = 128 / (max - min),
                ncols = Math.floor(.65*Math.sqrt(l)),
                nlines = Math.ceil(l/ncols);
            var cells = _(nlines * ncols)
                .chain()
                .range()
                .map(function(i) {
                    var x = i % ncols,
                        y = Math.floor(i / ncols),
                        dx = x - ((ncols - 1) / 2),
                        dy = y - ((nlines - 1) / 2),
                        d = Math.sqrt(dx*dx + dy*dy);
                    return {
                        n: i,
                        x: x,
                        y: y,
                        d: d
                    }
                })
                .sortBy(function(c) {
                    return c.d;
                })
                .value();
            _(ordered).each(function(v, k) {
                cells[k].tag = v;
            });
            cells = _(cells).sortBy(function(c) { return c.n });
            var _html = "";
            _(cells).each(function(c) {
                if (c.x === 0) {
                    _html += "<tr>";
                }
                _html += "<td>";
                if (c.tag) {
                    var color = Math.floor( 127 + (c.tag.weight - min) * colorscale );
                    _html += '<a href="'
                        + endpoints.tag_page.replace("__TAG__",encodeURIComponent(c.tag.label))
                        + '" style="color: rgb('
                        + [color,color,color].join(",")
                        + ')">'
                        + c.tag.label
                        + '</a>';
                }
                _html += "</td>";
                if (c.x === (ncols - 1)) {
                    _html += "</tr>";
                }
            });
            tagsInner.html(_html);
            resizeTags();
        }
    });
}