integration/js/taghome.js
changeset 50 9cc1b66d0880
child 64 458cc4576415
equal deleted inserted replaced
49:cb8403125d4d 50:9cc1b66d0880
       
     1 function showTags() {
       
     2     var tagsOuter = $(".taglist_container"),
       
     3         tagsInner = $(".taglist_container table"),
       
     4         acInput = $("#form_tag input[type=search]"),
       
     5         acdata = [],
       
     6         labels = {
       
     7             content: "Séquence",
       
     8             tag: "Tag"
       
     9         };
       
    10     
       
    11     acInput.autocomplete({
       
    12         source: function( request, response ) {
       
    13             response($.ui.autocomplete.filter(acdata, request.term));
       
    14         },
       
    15         select: function(event, ui) {
       
    16             document.location.href = ui.item.href;
       
    17         }
       
    18     }).data("autocomplete")._renderItem = function(ul, item) {
       
    19         return $( "<li>" )
       
    20             .data( "item.autocomplete", item )
       
    21             .append( "<a href='" + item.href + "'>" + labels[item.type] + " : " + item.label + "</a>" )
       
    22             .appendTo( ul );
       
    23     };
       
    24     
       
    25     $("#form_tag").submit(function() {
       
    26         return false;
       
    27     });
       
    28         
       
    29     tagsInner.draggable();
       
    30     
       
    31     function resizeTags() {
       
    32         tagsInner.css({
       
    33             "margin-left": - Math.floor(tagsInner.width()/2) + "px",
       
    34             "margin-top": - Math.floor(tagsInner.height()/2) + "px",
       
    35             "left": Math.floor(tagsOuter.width()/2) + "px",
       
    36             "top": Math.floor(tagsOuter.height()/2) + "px"
       
    37         });
       
    38     }
       
    39     $(window).on("resize",resizeTags);
       
    40     
       
    41     $.ajax({
       
    42         url: endpoints.contents_api,
       
    43         dataType: "jsonp", //TODO: JSON if on Platform
       
    44         data: {
       
    45             limit: 0,
       
    46             format: "jsonp"
       
    47         },
       
    48         success: function(data) {
       
    49             _(data.objects).each(function(content) {
       
    50                 var fpmatch = content.front_project.match(/projects\/([0-9a-f\-]+)/),
       
    51                     fproj = fpmatch ? fpmatch[1] : "";
       
    52                 acdata.push({
       
    53                     label: content.title,
       
    54                     type: "content",
       
    55                     href: endpoints.content_page.replace("__CONTENT_ID__", content.iri_id).replace("__FRONT_PROJECT_ID__", fproj)
       
    56                 });
       
    57             });
       
    58         }
       
    59     })
       
    60     
       
    61     $.ajax({
       
    62         url: endpoints.tag_api,
       
    63         dataType: "json",
       
    64         data: {
       
    65             contents: "all",
       
    66             format: "json",
       
    67             limit: 0,
       
    68             steps: 100
       
    69         },
       
    70         success: function(data) {
       
    71             var grouped = {};
       
    72             _(data.objects).each(function(t) {
       
    73                 var upt = t.name.toUpperCase(),
       
    74                     w = t.weight + (grouped[upt] || 0);
       
    75                 grouped[upt] = w;
       
    76             });
       
    77             _(grouped).each(function(v, k) {
       
    78                 acdata.push({
       
    79                     label: k,
       
    80                     type: "tag",
       
    81                     href: endpoints.tag_page.replace("__TAG__", k)
       
    82                 });
       
    83             });
       
    84             var ordered = _(grouped)
       
    85                 .chain()
       
    86                 .map(function(v, k) {
       
    87                     return {
       
    88                         label: k,
       
    89                         weight: v
       
    90                     }
       
    91                 })
       
    92                 .sortBy(function(t) {
       
    93                     return -t.weight;
       
    94                 })
       
    95                 .value();
       
    96             var l = ordered.length,
       
    97                 max = ordered[0].weight,
       
    98                 min = Math.min(max - 1, ordered[l - 1].weight),
       
    99                 colorscale = 128 / (max - min),
       
   100                 ncols = Math.floor(.65*Math.sqrt(l)),
       
   101                 nlines = Math.ceil(l/ncols);
       
   102             var cells = _(nlines * ncols)
       
   103                 .chain()
       
   104                 .range()
       
   105                 .map(function(i) {
       
   106                     var x = i % ncols,
       
   107                         y = Math.floor(i / ncols),
       
   108                         dx = x - ((ncols - 1) / 2),
       
   109                         dy = y - ((nlines - 1) / 2),
       
   110                         d = Math.sqrt(dx*dx + dy*dy);
       
   111                     return {
       
   112                         n: i,
       
   113                         x: x,
       
   114                         y: y,
       
   115                         d: d
       
   116                     }
       
   117                 })
       
   118                 .sortBy(function(c) {
       
   119                     return c.d;
       
   120                 })
       
   121                 .value();
       
   122             _(ordered).each(function(v, k) {
       
   123                 cells[k].tag = v;
       
   124             });
       
   125             cells = _(cells).sortBy(function(c) { return c.n });
       
   126             var _html = "";
       
   127             _(cells).each(function(c) {
       
   128                 if (c.x === 0) {
       
   129                     _html += "<tr>";
       
   130                 }
       
   131                 _html += "<td>";
       
   132                 if (c.tag) {
       
   133                     var color = Math.floor( 127 + (c.tag.weight - min) * colorscale );
       
   134                     _html += '<a href="'
       
   135                         + endpoints.tag_page.replace("__TAG__",encodeURIComponent(c.tag.label))
       
   136                         + '" style="color: rgb('
       
   137                         + [color,color,color].join(",")
       
   138                         + ')">'
       
   139                         + c.tag.label
       
   140                         + '</a>';
       
   141                 }
       
   142                 _html += "</td>";
       
   143                 if (c.x === (ncols - 1)) {
       
   144                     _html += "</tr>";
       
   145                 }
       
   146             });
       
   147             tagsInner.html(_html);
       
   148             resizeTags();
       
   149         }
       
   150     });
       
   151 }