Blame view

media/regularlabs/js/multiselect.js 6.46 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
/**
 * @package         Regular Labs Library
 * @version         18.2.10140
 * 
 * @author          Peter van Westen <info@regularlabs.com>
 * @link            http://www.regularlabs.com
 * @copyright       Copyright © 2018 Regular Labs All Rights Reserved
 * @license         http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */


var RegularLabsMultiSelect = null;

(function($) {
	"use strict";

	$(document).ready(function() {
		$('.rl_multiselect').each(function() {
			RegularLabsMultiSelect.init($(this));
		});
	});

	RegularLabsMultiSelect = {
		init: function(element) {
			var controls  = element.find('div.rl_multiselect-controls');
			var list      = element.find('ul.rl_multiselect-ul');
			var menu      = element.find('div.rl_multiselect-menu-block').html();
			var maxheight = list.css('max-height');

			list.find('li').each(function() {
				var $li  = $(this);
				var $div = $li.find('div.rl_multiselect-item:first');

				// Add icons
				$li.prepend('<span class="pull-left icon-"></span>');

				// Append clearfix
				$div.after('<div class="clearfix"></div>');

				if ($li.find('ul.rl_multiselect-sub').length) {
					// Add classes to Expand/Collapse icons
					$li.find('span.icon-').addClass('rl_multiselect-toggle icon-minus');

					// Append drop down menu in nodes
					$div.find('label:first').after(menu);

					if (!$li.find('ul.rl_multiselect-sub ul.rl_multiselect-sub').length) {
						$li.find('div.rl_multiselect-menu-expand').remove();
					}
				}
			});

			// Takes care of the Expand/Collapse of a node
			list.find('span.rl_multiselect-toggle').click(function() {
				var $icon = $(this);

				// Take care of parent UL
				if ($icon.parent().find('ul.rl_multiselect-sub').is(':visible')) {
					$icon.removeClass('icon-minus').addClass('icon-plus');
					$icon.parent().find('ul.rl_multiselect-sub').hide();
					$icon.parent().find('ul.rl_multiselect-sub span.rl_multiselect-toggle').removeClass('icon-minus').addClass('icon-plus');
				} else {
					$icon.removeClass('icon-plus').addClass('icon-minus');
					$icon.parent().find('ul.rl_multiselect-sub').show();
					$icon.parent().find('ul.rl_multiselect-sub span.rl_multiselect-toggle').removeClass('icon-plus').addClass('icon-minus');
				}
			});

			// Takes care of the filtering
			controls.find('input.rl_multiselect-filter').keyup(function() {
				var $text = $(this).val().toLowerCase();
				list.find('li').each(function() {
					var $li = $(this);
					if ($li.text().toLowerCase().indexOf($text) < 0) {
						$li.hide();
					} else {
						$li.show();
					}
				});
			});

			// Checks all checkboxes in the list
			controls.find('a.rl_multiselect-checkall').click(function() {
				list.find('input').prop('checked', true);
			});

			// Unchecks all checkboxes in the list
			controls.find('a.rl_multiselect-uncheckall').click(function() {
				list.find('input').prop('checked', false);
			});

			// Toggles all checkboxes in the list
			controls.find('a.rl_multiselect-toggleall').click(function() {
				list.find('input').each(function() {
					var $input = $(this);
					if ($input.prop('checked')) {
						$input.prop('checked', false);
					} else {
						$input.prop('checked', true);
					}
				});
			});

			// Expands all sub-items in the list
			controls.find('a.rl_multiselect-expandall').click(function() {
				list.find('ul.rl_multiselect-sub').show();
				list.find('span.rl_multiselect-toggle').removeClass('icon-plus').addClass('icon-minus');
			});

			// Hides all sub-items in the list
			controls.find('a.rl_multiselect-collapseall').click(function() {
				list.find('ul.rl_multiselect-sub').hide();
				list.find('span.rl_multiselect-toggle').removeClass('icon-minus').addClass('icon-plus');
			});

			// Shows all selected items in the list
			controls.find('a.rl_multiselect-showall').click(function() {
				list.find('li').show();
			});

			// Shows all selected items in the list
			controls.find('a.rl_multiselect-showselected').click(function() {
				list.find('li').each(function() {
					var $li   = $(this);
					var $hide = true;
					$li.find('input').each(function() {
						if ($(this).prop('checked')) {
							$hide = false;
							return false;
						}
					});

					if ($hide) {
						$li.hide();
						return;
					}

					$li.show();
				});
			});

			// Maximizes the list
			controls.find('a.rl_multiselect-maximize').click(function() {
				list.css('max-height', '');
				controls.find('a.rl_multiselect-maximize').hide();
				controls.find('a.rl_multiselect-minimize').show();
			});

			// Minimizes the list
			controls.find('a.rl_multiselect-minimize').click(function() {
				list.css('max-height', maxheight);
				controls.find('a.rl_multiselect-minimize').hide();
				controls.find('a.rl_multiselect-maximize').show();
			});

			// Take care of children check/uncheck all
			element.find('a.checkall').click(function() {
				$(this).parent().parent().parent().parent().parent().parent().find('ul.rl_multiselect-sub input').prop('checked', true);
			});
			element.find('a.uncheckall').click(function() {
				$(this).parent().parent().parent().parent().parent().parent().find('ul.rl_multiselect-sub input').prop('checked', false);
			});

			// Take care of children toggle all
			element.find('a.expandall').click(function() {
				var $parent = $(this).parent().parent().parent().parent().parent().parent().parent();
				$parent.find('ul.rl_multiselect-sub').show();
				$parent.find('ul.rl_multiselect-sub span.rl_multiselect-toggle').removeClass('icon-plus').addClass('icon-minus');
			});
			element.find('a.collapseall').click(function() {
				var $parent = $(this).parent().parent().parent().parent().parent().parent().parent();
				$parent.find('li ul.rl_multiselect-sub').hide();
				$parent.find('li span.rl_multiselect-toggle').removeClass('icon-minus').addClass('icon-plus');
			});
			element.find('div.rl_multiselect-item.hidechildren').click(function() {
				var $parent = $(this).parent();

				$(this).find('input').each(function() {
					var $sub   = $parent.find('ul.rl_multiselect-sub').first();
					var $input = $(this);
					if ($input.prop('checked')) {
						$parent.find('span.rl_multiselect-toggle, div.rl_multiselect-menu').css('visibility', 'hidden');
						if (!$sub.parent().hasClass('hidelist')) {
							$sub.wrap('<div style="display:none;" class="hidelist"></div>');
						}
					} else {
						$parent.find('span.rl_multiselect-toggle, div.rl_multiselect-menu').css('visibility', 'visible');
						if ($sub.parent().hasClass('hidelist')) {
							$sub.unwrap();
						}
					}
				});
			});
		}
	};
})(jQuery);