integration/v2/js/keyword-mosaic.js
author Anthony Ly <anthonyly.com@gmail.com>
Thu, 27 Jun 2013 17:31:15 +0200
changeset 187 be4eb4db3418
parent 179 b7fabb9e5d9f
child 188 aa163c5df6e3
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
179
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
     1
$(function() {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
     2
    
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
     3
    $('.masonry-177').masonry({
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
     4
        columnWidth: 177,
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
     5
        itemSelector: '.item-masonry'
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
     6
    });
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
     7
    
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
     8
    function updateMasonry() {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
     9
        $('.masonry-177').data('masonry').layout();
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    10
    }
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    11
    
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    12
    var keywordsobj = {};
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    13
    
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    14
    $(".item-masonry").each(function(i, element) {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    15
        $(element)
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    16
            .attr("data-keywords")
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    17
            .split(",")
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    18
            .forEach(function(kw) {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    19
                var keyword = kw.replace(/(^\s+|\s+$)/g,''),
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    20
                    basekw = keyword.toLowerCase();
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    21
                if (!keywordsobj.hasOwnProperty(basekw)) {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    22
                    keywordsobj[basekw] = {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    23
                        keyword: keyword,
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    24
                        basekeyword: basekw,
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    25
                        count: 0,
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    26
                        items: $(),
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    27
                        enabled: true
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    28
                    };
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    29
                }
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    30
                kwobj = keywordsobj[basekw];
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    31
                kwobj.count++;
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    32
                kwobj.items.push(element);
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    33
            });
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    34
    });
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    35
    
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    36
    var keywordslist = [];
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    37
    
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    38
    for (var k in keywordsobj) {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    39
        if (keywordsobj.hasOwnProperty(k)) {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    40
            keywordslist.push(keywordsobj[k]);
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    41
        }
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    42
    }
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    43
    
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    44
    keywordslist.sort(function(a, b) {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    45
        return b.count - a.count;
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    46
    });
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    47
    
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    48
    var filterlist = $(".filters");
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    49
    
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    50
    function updateKeywords() {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    51
        var enabledItems = $();
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    52
        keywordslist.forEach(function(kw) {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    53
            if (kw.enabled) {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    54
                enabledItems = enabledItems.add(kw.items);
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    55
                kw.li.removeClass("disabled");
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    56
            } else {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    57
                kw.li.addClass("disabled");
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    58
            }
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    59
        });
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    60
        $(".item-masonry").addClass("disabled");
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    61
        enabledItems.removeClass("disabled");
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    62
    }
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    63
    
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    64
    keywordslist.slice(0,100).forEach(function(kw) {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    65
        var li = $('<li class="box-shadow-2">'),
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    66
            outera = $('<a class="display-keyword" href="#">'),
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    67
            innera = $('<a class="icon-action" href="#">');
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    68
        outera.text(kw.keyword + ' ');
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    69
        outera.append(innera);
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    70
        li.append(outera);
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    71
        kw.li = li;
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    72
        li.click(function() {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    73
            if (kw.enabled) {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    74
                keywordslist.forEach(function(k) {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    75
                    k.enabled = false;
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    76
                });
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    77
                kw.enabled = true;
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    78
            } else {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    79
                keywordslist.forEach(function(k) {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    80
                    k.enabled = true;
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    81
                });
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    82
            }
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    83
            updateKeywords();
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    84
            return false;
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    85
        });
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    86
        innera.click(function() {
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    87
            kw.enabled = !kw.enabled;
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    88
            updateKeywords();
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    89
            return false;
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    90
        });
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    91
        filterlist.append(li);
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    92
    })
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    93
        
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    94
    $('.toggle-comment').click(function(e){
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    95
        e.preventDefault();
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    96
        $('.show-comment, .hide-comment').hide();
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    97
        if($('.mosaic').length){
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    98
            $('.mosaic').removeClass('mosaic').addClass('mosaic-comment');
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
    99
            $('.hide-comment').show();
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
   100
        }else{
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
   101
            $('.mosaic-comment').removeClass('mosaic-comment').addClass('mosaic');
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
   102
            $('.show-comment').show();
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
   103
        }
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
   104
        updateMasonry();
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
   105
    });
187
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   106
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   107
//##################
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   108
/*
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   109
    $(document).on('click','.display-keyword', function(e){
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   110
        e.preventDefault();
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   111
        var ul = $(this).parents('ul'),
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   112
            li = $(this).parents('li'),
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   113
            iconAction = li.find('.icon-action'),
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   114
            index = li.index(),
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   115
            keyword = $(this).attr('data-keyword');
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   116
        
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   117
        if(!li.hasClass('disabled') && ul.find('li.disabled').length){
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   118
            ul.find('li').each(function(k, v){
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   119
                var displayKeyword = $(v).find('.display-keyword'),
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   120
                    iconAction = $(v).find('.icon-action'),
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   121
                    keyword = $.trim(displayKeyword.attr('data-keyword'));
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   122
                enabledDisplayKeyword($(v), keyword, iconAction);
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   123
            });
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   124
        }else{
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   125
            ul.find('li').each(function(k, v){
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   126
                if(index != k){
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   127
                    var displayKeyword = $(v).find('.display-keyword'),
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   128
                        iconAction = $(v).find('.icon-action'),
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   129
                        keyword = displayKeyword.attr('data-keyword');
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   130
                    disabledDisplayKeyword($(v), keyword, iconAction);
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   131
                }
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   132
            });
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   133
            enabledDisplayKeyword(li, keyword, iconAction);
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   134
        }
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   135
    });
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   136
    $('.icon-action').bind('click', function(e){
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   137
        e.preventDefault();
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   138
        var li = $(this).parents('li');
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   139
        toggleDisplayKeyword(li);
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   140
    });
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   141
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   142
    function toggleDisplayKeyword(li){
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   143
        var displayKeyword = li.find('.display-keyword'),
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   144
            iconAction = li.find('.icon-action'),
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   145
            keyword = displayKeyword.attr('data-keyword');
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   146
        if(li.hasClass('disabled')){
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   147
            enabledDisplayKeyword(li, keyword, iconAction);
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   148
        }else{
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   149
            disabledDisplayKeyword(li, keyword, iconAction);
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   150
        }
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   151
    }
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   152
    function enabledDisplayKeyword(li, keyword, iconAction){
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   153
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   154
        $('ul.mosaic > li').each(function(k, v){
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   155
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   156
            var keywords = $(v).attr('data-keywords'),
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   157
                item = $(v);
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   158
            keywords = keywords.split(',');
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   159
            for(var i=0; i<= keywords.length; i++){
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   160
                if($.trim(keywords[i]) == keyword){
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   161
                    item.find('.curtain').hide();
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   162
                }
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   163
            }
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   164
        });
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   165
        $('li.keyword-'+keyword).find('.curtain').hide();
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   166
        iconAction.addClass('cross');
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   167
    }
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   168
    function disabledDisplayKeyword(li, keyword, iconAction){
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   169
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   170
        $('ul.mosaic > li').each(function(k, v){
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   171
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   172
            var keywords = $(v).attr('data-keywords'),
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   173
                item = $(v);
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   174
            keywords = keywords.split(',');
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   175
            for(var i=0; i<= keywords.length; i++){
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   176
                if($.trim(keywords[i]) == keyword){
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   177
                    item.find('.curtain').show();
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   178
                }
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   179
            }
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   180
        });
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   181
        iconAction.addClass('circle');
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   182
    }
Anthony Ly <anthonyly.com@gmail.com>
parents: 179
diff changeset
   183
*/
179
b7fabb9e5d9f updated tagcloud behaviour
veltr
parents:
diff changeset
   184
});