|
441 | 441 | if ($items.length === that.$selectItems.length) { |
442 | 442 | that[checked ? 'checkAll' : 'uncheckAll'](); |
443 | 443 | } else { // when the filter option is true |
444 | | - that.$selectGroups.prop('checked', checked); |
445 | | - $items.prop('checked', checked); |
| 444 | + if (that.$selectGroups) { |
| 445 | + that.$selectGroups.prop('checked', checked); |
| 446 | + } |
| 447 | + if ($items) { |
| 448 | + $items.prop('checked', checked); |
| 449 | + } |
446 | 450 | that.options[checked ? 'onCheckAll' : 'onUncheckAll'](); |
447 | 451 | that.update(); |
448 | 452 | } |
|
452 | 456 | $items = that.$selectItems.filter(':visible'), |
453 | 457 | $children = $items.filter(sprintf('[data-group="%s"]', group)), |
454 | 458 | checked = $children.length !== $children.filter(':checked').length; |
455 | | - |
456 | | - $children.prop('checked', checked); |
| 459 | + if ($children) { |
| 460 | + $children.prop('checked', checked); |
| 461 | + } |
457 | 462 | that.updateSelectAll(); |
458 | 463 | that.update(); |
459 | 464 | that.options.onOptgroupClick({ |
|
495 | 500 | return; |
496 | 501 | } |
497 | 502 | this.options.isOpen = true; |
| 503 | + this.$parent.addClass('ms-parent-open'); |
498 | 504 | this.$choice.find('>div').addClass('open'); |
499 | 505 | this.$drop[this.animateMethod('show')](); |
500 | 506 |
|
|
573 | 579 |
|
574 | 580 | close: function () { |
575 | 581 | this.options.isOpen = false; |
| 582 | + this.$parent.removeClass('ms-parent-open'); |
576 | 583 | this.$choice.find('>div').removeClass('open'); |
577 | 584 | this.$drop[this.animateMethod('hide')](); |
578 | 585 | if (this.options.container) { |
|
762 | 769 | } |
763 | 770 |
|
764 | 771 | if (this.options.addTitle) { |
765 | | - var selectType = (this.options.useSelectOptionLabel || this.options.useSelectOptionLabelToHtml) ? 'label' : 'text' |
766 | | - $span.prop('title', this.getSelects(selectType)); |
| 772 | + var selectType = (this.options.useSelectOptionLabel || this.options.useSelectOptionLabelToHtml) ? 'label' : 'text'; |
| 773 | + if ($span) { |
| 774 | + $span.prop('title', this.getSelects(selectType)); |
| 775 | + } |
767 | 776 | } |
768 | 777 |
|
769 | 778 | // set selects to select |
|
787 | 796 | if (!isInit) { |
788 | 797 | $items = $items.filter(':visible'); |
789 | 798 | } |
790 | | - this.$selectAll.prop('checked', $items.length && |
791 | | - $items.length === $items.filter(':checked').length); |
792 | | - if (!isInit && this.$selectAll.prop('checked')) { |
793 | | - this.options.onCheckAll(); |
| 799 | + if (this.$selectAll) { |
| 800 | + this.$selectAll.prop('checked', $items.length && |
| 801 | + $items.length === $items.filter(':checked').length); |
| 802 | + if (!isInit && this.$selectAll.prop('checked')) { |
| 803 | + this.options.onCheckAll(); |
| 804 | + } |
794 | 805 | } |
795 | 806 | }, |
796 | 807 |
|
|
832 | 843 | values.push($(this).val()); |
833 | 844 | }); |
834 | 845 |
|
835 | | - if (type === 'text' && this.$selectGroups.length) { |
| 846 | + if (type === 'text' && this.$selectGroups && this.$selectGroups.length) { |
836 | 847 | texts = []; |
837 | 848 | this.$selectGroups.each(function () { |
838 | 849 | var html = [], |
|
841 | 852 | $children = that.$drop.find(sprintf('[%s][data-group="%s"]', that.selectItemName, group)), |
842 | 853 | $selected = $children.filter(':checked'); |
843 | 854 |
|
844 | | - if (!$selected.length) { |
| 855 | + if (!$selected || !$selected.length) { |
845 | 856 | return; |
846 | 857 | } |
847 | 858 |
|
848 | 859 | html.push('['); |
849 | 860 | html.push(text); |
850 | | - if ($children.length > $selected.length) { |
| 861 | + if (($children && $children.length) > ($selected && $selected.length)) { |
851 | 862 | var list = []; |
852 | 863 | $selected.each(function () { |
853 | 864 | list.push($(this).parent().text()); |
|
857 | 868 | html.push(']'); |
858 | 869 | texts.push(html.join('')); |
859 | 870 | }); |
860 | | - } else if (type === 'label' && this.$selectGroups.length) { |
| 871 | + } else if (type === 'label' && this.$selectGroups && this.$selectGroups.length) { |
861 | 872 | labels = []; |
862 | 873 | this.$selectGroups.each(function () { |
863 | 874 | var html = [], |
|
866 | 877 | $children = that.$drop.find(sprintf('[%s][data-group="%s"]', that.selectItemName, group)), |
867 | 878 | $selected = $children.filter(':checked'); |
868 | 879 |
|
869 | | - if (!$selected.length) { |
| 880 | + if (!$selected || !$selected.length) { |
870 | 881 | return; |
871 | 882 | } |
872 | 883 |
|
873 | 884 | html.push('['); |
874 | 885 | html.push(label); |
875 | | - if ($children.length > $selected.length) { |
| 886 | + if (($children && $children.length) > ($selected && $selected.length)) { |
876 | 887 | var list = []; |
877 | 888 | $selected.each(function () { |
878 | 889 | list.push($(this).attr('label') || ''); |
|
921 | 932 |
|
922 | 933 | setSelects: function (values) { |
923 | 934 | var that = this; |
924 | | - this.$selectItems.prop('checked', false); |
925 | | - this.$disableItems.prop('checked', false); |
926 | | - $.each(values, function (i, value) { |
927 | | - that.$selectItems.filter(sprintf('[value="%s"]', value)).prop('checked', true); |
928 | | - that.$disableItems.filter(sprintf('[value="%s"]', value)).prop('checked', true); |
929 | | - }); |
930 | | - this.$selectAll.prop('checked', this.$selectItems.length === |
931 | | - this.$selectItems.filter(':checked').length + this.$disableItems.filter(':checked').length); |
932 | | - |
933 | | - $.each(that.$selectGroups, function (i, val) { |
934 | | - var group = $(val).parent().attr('data-group'), |
935 | | - $children = that.$selectItems.filter('[data-group="' + group + '"]'); |
936 | | - $(val).prop('checked', $children.length && |
937 | | - $children.length === $children.filter(':checked').length); |
938 | | - }); |
| 935 | + if (this.$selectItems && this.$disableItems) { |
| 936 | + this.$selectItems.prop('checked', false); |
| 937 | + this.$disableItems.prop('checked', false); |
| 938 | + $.each(values, function (i, value) { |
| 939 | + var selectedItemElm = that.$selectItems.filter(sprintf('[value="%s"]', value)); |
| 940 | + var disabledItemElm = that.$disableItems.filter(sprintf('[value="%s"]', value)); |
| 941 | + if (selectedItemElm) { |
| 942 | + selectedItemElm.prop('checked', true); |
| 943 | + } |
| 944 | + if (disabledItemElm) { |
| 945 | + disabledItemElm.prop('checked', true); |
| 946 | + } |
| 947 | + }); |
| 948 | + if (this.$selectItems && this.$selectAll) { |
| 949 | + this.$selectAll.prop('checked', this.$selectItems.length === |
| 950 | + this.$selectItems.filter(':checked').length + this.$disableItems.filter(':checked').length); |
| 951 | + } |
939 | 952 |
|
| 953 | + $.each(that.$selectGroups, function (i, val) { |
| 954 | + var group = $(val).parent().attr('data-group'), |
| 955 | + $children = that.$selectItems.filter('[data-group="' + group + '"]'); |
| 956 | + $(val).prop('checked', $children.length && |
| 957 | + $children.length === $children.filter(':checked').length); |
| 958 | + }); |
| 959 | + } |
940 | 960 | this.update(false); |
941 | 961 | }, |
942 | 962 |
|
|
962 | 982 | }, |
963 | 983 |
|
964 | 984 | checkAll: function () { |
965 | | - this.$selectItems.prop('checked', true); |
966 | | - this.$selectGroups.prop('checked', true); |
967 | | - this.$selectAll.prop('checked', true); |
| 985 | + this.$selectItems && this.$selectItems.prop('checked', true); |
| 986 | + this.$selectGroups && this.$selectGroups.prop('checked', true); |
| 987 | + this.$selectAll && this.$selectAll.prop('checked', true); |
968 | 988 | this.update(); |
969 | 989 | this.options.onCheckAll(); |
970 | 990 | }, |
971 | 991 |
|
972 | 992 | uncheckAll: function () { |
973 | | - this.$selectItems.prop('checked', false); |
974 | | - this.$selectGroups.prop('checked', false); |
975 | | - this.$selectAll.prop('checked', false); |
| 993 | + this.$selectItems && this.$selectItems.prop('checked', false); |
| 994 | + this.$selectGroups && this.$selectGroups.prop('checked', false); |
| 995 | + this.$selectAll && this.$selectAll.prop('checked', false); |
976 | 996 | this.update(); |
977 | 997 | this.options.onUncheckAll(); |
978 | 998 | }, |
979 | 999 |
|
980 | 1000 | focus: function () { |
981 | | - this.$choice.focus(); |
| 1001 | + this.$choice && this.$choice.focus(); |
982 | 1002 | this.options.onFocus(); |
983 | 1003 | }, |
984 | 1004 |
|
985 | 1005 | blur: function () { |
986 | | - this.$choice.blur(); |
| 1006 | + this.$choice && this.$choice.blur(); |
987 | 1007 | this.options.onBlur(); |
988 | 1008 | }, |
989 | 1009 |
|
|
996 | 1016 | text = $.trim(this.$searchInput.val()).toLowerCase(); |
997 | 1017 |
|
998 | 1018 | if (text.length === 0) { |
999 | | - this.$selectAll.parent().show(); |
1000 | | - this.$selectItems.parent().show(); |
1001 | | - this.$disableItems.parent().show(); |
1002 | | - this.$selectGroups.parent().show(); |
| 1019 | + this.$selectAll && this.$selectAll.parent().show(); |
| 1020 | + this.$selectItems && this.$selectItems.parent().show(); |
| 1021 | + this.$disableItems && this.$disableItems.parent().show(); |
| 1022 | + this.$selectGroups && this.$selectGroups.parent().show(); |
1003 | 1023 | this.$noResults.hide(); |
1004 | 1024 | } else { |
1005 | 1025 | this.$selectItems.each(function () { |
1006 | 1026 | var $parent = $(this).parent(); |
1007 | 1027 | $parent[removeDiacritics($parent.text().toLowerCase()).indexOf(removeDiacritics(text)) < 0 ? 'hide' : 'show'](); |
1008 | 1028 | }); |
1009 | | - this.$disableItems.parent().hide(); |
| 1029 | + this.$disableItems && this.$disableItems.parent().hide(); |
1010 | 1030 | this.$selectGroups.each(function () { |
1011 | 1031 | var $parent = $(this).parent(); |
1012 | 1032 | var group = $parent.attr('data-group'), |
|
1015 | 1035 | }); |
1016 | 1036 |
|
1017 | 1037 | //Check if no matches found |
1018 | | - if (this.$selectItems.parent().filter(':visible').length) { |
| 1038 | + if (this.$selectItems && this.$selectItems.parent().filter(':visible').length) { |
1019 | 1039 | this.$selectAll.parent().show(); |
1020 | 1040 | this.$noResults.hide(); |
1021 | 1041 | } else { |
1022 | | - this.$selectAll.parent().hide(); |
| 1042 | + this.$selectAll && this.$selectAll.parent().hide(); |
1023 | 1043 | this.$noResults.show(); |
1024 | 1044 | } |
1025 | 1045 | } |
|
0 commit comments