From caa0c45a1141cf448c1cbc479d6ccf469b6a39df Mon Sep 17 00:00:00 2001 From: barisusakli Date: Fri, 25 Apr 2014 16:34:34 -0400 Subject: [PATCH] closes #1441 --- public/src/modules/topicSelect.js | 57 ++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/public/src/modules/topicSelect.js b/public/src/modules/topicSelect.js index 4fbbc38f12..a5b131a7a2 100644 --- a/public/src/modules/topicSelect.js +++ b/public/src/modules/topicSelect.js @@ -4,21 +4,38 @@ define(function() { var TopicSelect = {}; + var lastSelected; TopicSelect.init = function(onSelect) { - $('#topics-container').on('click', '.select', function() { + + $('#topics-container').on('selectstart', function() { + return false; + }); + + $('#topics-container').on('click', '.select', function(ev) { var select = $(this); - var isChecked = !select.hasClass('fa-square-o'); - select.toggleClass('fa-check-square-o', !isChecked); - select.toggleClass('fa-square-o', isChecked); - select.parents('.category-item').toggleClass('selected', !isChecked); + if (ev.shiftKey) { + selectRange($(this).parents('.category-item').attr('data-tid')); + lastSelected = select; + return false; + } + + var isSelected = select.hasClass('fa-check-square-o'); + toggleSelect(select, !isSelected); + lastSelected = select; if (typeof onSelect === 'function') { onSelect(); } }); }; + function toggleSelect(select, isSelected) { + select.toggleClass('fa-check-square-o', isSelected); + select.toggleClass('fa-square-o', !isSelected); + select.parents('.category-item').toggleClass('selected', isSelected); + } + TopicSelect.getSelectedTids = function() { var tids = []; $('#topics-container .category-item.selected').each(function() { @@ -32,5 +49,35 @@ define(function() { $('#topics-container .select').toggleClass('fa-check-square-o', false).toggleClass('fa-square-o', true); }; + function selectRange(clickedTid) { + + if(!lastSelected) { + lastSelected = $('.category-item[data-tid]').first().find('.select'); + } + + var isClickedSelected = $('.category-item[data-tid="' + clickedTid + '"]').hasClass('selected'); + + var clickedIndex = getIndex(clickedTid); + var lastIndex = getIndex(lastSelected.parents('.category-item[data-tid]').attr('data-tid')); + selectIndexRange(clickedIndex, lastIndex, !isClickedSelected); + } + + function selectIndexRange(start, end, isSelected) { + if (start > end) { + var tmp = start; + start = end; + end = tmp; + } + + for(var i=start; i<=end; ++i) { + var topic = $('.category-item[data-tid]').eq(i); + toggleSelect(topic.find('.select'), isSelected); + } + } + + function getIndex(tid) { + return $('.category-item[data-tid="' + tid + '"]').index('.category-item'); + } + return TopicSelect; }); \ No newline at end of file