Advertisement
matoni555

jQuery search

Jan 20th, 2022
2,371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 7.00 KB | None | 0 0
  1. var defaultDiacriticsRemovalMap = [
  2.     {'base':'a', 'letters':/[\u0061\u24D0\uFF41\u1E9A\u00E0\u00E1\u00E2\u1EA7\u1EA5\u1EAB\u1EA9\u00E3\u0101\u0103\u1EB1\u1EAF\u1EB5\u1EB3\u0227\u01E1\u00E4\u01DF\u1EA3\u00E5\u01FB\u01CE\u0201\u0203\u1EA1\u1EAD\u1EB7\u1E01\u0105\u2C65\u0250]/g},
  3.     {'base':'aa','letters':/[\uA733]/g},
  4.     {'base':'ae','letters':/[\u00E6\u01FD\u01E3]/g},
  5.     {'base':'ao','letters':/[\uA735]/g},
  6.     {'base':'au','letters':/[\uA737]/g},
  7.     {'base':'av','letters':/[\uA739\uA73B]/g},
  8.     {'base':'ay','letters':/[\uA73D]/g},
  9.     {'base':'b', 'letters':/[\u0062\u24D1\uFF42\u1E03\u1E05\u1E07\u0180\u0183\u0253]/g},
  10.     {'base':'c', 'letters':/[\u0063\u24D2\uFF43\u0107\u0109\u010B\u010D\u00E7\u1E09\u0188\u023C\uA73F\u2184]/g},
  11.     {'base':'d', 'letters':/[\u0064\u24D3\uFF44\u1E0B\u010F\u1E0D\u1E11\u1E13\u1E0F\u0111\u018C\u0256\u0257\uA77A]/g},
  12.     {'base':'dz','letters':/[\u01F3\u01C6]/g},
  13.     {'base':'e', 'letters':/[\u0065\u24D4\uFF45\u00E8\u00E9\u00EA\u1EC1\u1EBF\u1EC5\u1EC3\u1EBD\u0113\u1E15\u1E17\u0115\u0117\u00EB\u1EBB\u011B\u0205\u0207\u1EB9\u1EC7\u0229\u1E1D\u0119\u1E19\u1E1B\u0247\u025B\u01DD]/g},
  14.     {'base':'f', 'letters':/[\u0066\u24D5\uFF46\u1E1F\u0192\uA77C]/g},
  15.     {'base':'g', 'letters':/[\u0067\u24D6\uFF47\u01F5\u011D\u1E21\u011F\u0121\u01E7\u0123\u01E5\u0260\uA7A1\u1D79\uA77F]/g},
  16.     {'base':'h', 'letters':/[\u0068\u24D7\uFF48\u0125\u1E23\u1E27\u021F\u1E25\u1E29\u1E2B\u1E96\u0127\u2C68\u2C76\u0265]/g},
  17.     {'base':'hv','letters':/[\u0195]/g},
  18.     {'base':'i', 'letters':/[\u0069\u24D8\uFF49\u00EC\u00ED\u00EE\u0129\u012B\u012D\u00EF\u1E2F\u1EC9\u01D0\u0209\u020B\u1ECB\u012F\u1E2D\u0268\u0131]/g},
  19.     {'base':'j', 'letters':/[\u006A\u24D9\uFF4A\u0135\u01F0\u0249]/g},
  20.     {'base':'k', 'letters':/[\u006B\u24DA\uFF4B\u1E31\u01E9\u1E33\u0137\u1E35\u0199\u2C6A\uA741\uA743\uA745\uA7A3]/g},
  21.     {'base':'l', 'letters':/[\u006C\u24DB\uFF4C\u0140\u013A\u013E\u1E37\u1E39\u013C\u1E3D\u1E3B\u017F\u0142\u019A\u026B\u2C61\uA749\uA781\uA747]/g},
  22.     {'base':'lj','letters':/[\u01C9]/g},
  23.     {'base':'m', 'letters':/[\u006D\u24DC\uFF4D\u1E3F\u1E41\u1E43\u0271\u026F]/g},
  24.     {'base':'n', 'letters':/[\u006E\u24DD\uFF4E\u01F9\u0144\u00F1\u1E45\u0148\u1E47\u0146\u1E4B\u1E49\u019E\u0272\u0149\uA791\uA7A5]/g},
  25.     {'base':'nj','letters':/[\u01CC]/g},
  26.     {'base':'o', 'letters':/[\u006F\u24DE\uFF4F\u00F2\u00F3\u00F4\u1ED3\u1ED1\u1ED7\u1ED5\u00F5\u1E4D\u022D\u1E4F\u014D\u1E51\u1E53\u014F\u022F\u0231\u00F6\u022B\u1ECF\u0151\u01D2\u020D\u020F\u01A1\u1EDD\u1EDB\u1EE1\u1EDF\u1EE3\u1ECD\u1ED9\u01EB\u01ED\u00F8\u01FF\u0254\uA74B\uA74D\u0275]/g},
  27.     {'base':'oi','letters':/[\u01A3]/g},
  28.     {'base':'ou','letters':/[\u0223]/g},
  29.     {'base':'oo','letters':/[\uA74F]/g},
  30.     {'base':'p','letters':/[\u0070\u24DF\uFF50\u1E55\u1E57\u01A5\u1D7D\uA751\uA753\uA755]/g},
  31.     {'base':'q','letters':/[\u0071\u24E0\uFF51\u024B\uA757\uA759]/g},
  32.     {'base':'r','letters':/[\u0072\u24E1\uFF52\u0155\u1E59\u0159\u0211\u0213\u1E5B\u1E5D\u0157\u1E5F\u024D\u027D\uA75B\uA7A7\uA783]/g},
  33.     {'base':'s','letters':/[\u0073\u24E2\uFF53\u00DF\u015B\u1E65\u015D\u1E61\u0161\u1E67\u1E63\u1E69\u0219\u015F\u023F\uA7A9\uA785\u1E9B]/g},
  34.     {'base':'t','letters':/[\u0074\u24E3\uFF54\u1E6B\u1E97\u0165\u1E6D\u021B\u0163\u1E71\u1E6F\u0167\u01AD\u0288\u2C66\uA787]/g},
  35.     {'base':'tz','letters':/[\uA729]/g},
  36.     {'base':'u','letters':/[\u0075\u24E4\uFF55\u00F9\u00FA\u00FB\u0169\u1E79\u016B\u1E7B\u016D\u00FC\u01DC\u01D8\u01D6\u01DA\u1EE7\u016F\u0171\u01D4\u0215\u0217\u01B0\u1EEB\u1EE9\u1EEF\u1EED\u1EF1\u1EE5\u1E73\u0173\u1E77\u1E75\u0289]/g},
  37.     {'base':'v','letters':/[\u0076\u24E5\uFF56\u1E7D\u1E7F\u028B\uA75F\u028C]/g},
  38.     {'base':'vy','letters':/[\uA761]/g},
  39.     {'base':'w','letters':/[\u0077\u24E6\uFF57\u1E81\u1E83\u0175\u1E87\u1E85\u1E98\u1E89\u2C73]/g},
  40.     {'base':'x','letters':/[\u0078\u24E7\uFF58\u1E8B\u1E8D]/g},
  41.     {'base':'y','letters':/[\u0079\u24E8\uFF59\u1EF3\u00FD\u0177\u1EF9\u0233\u1E8F\u00FF\u1EF7\u1E99\u1EF5\u01B4\u024F\u1EFF]/g},
  42.     {'base':'z','letters':/[\u007A\u24E9\uFF5A\u017A\u1E91\u017C\u017E\u1E93\u1E95\u01B6\u0225\u0240\u2C6C\uA763]/g}
  43. ];
  44.  
  45. function removeDiacritics(str) {
  46.  
  47.     for (var i = 0; i < defaultDiacriticsRemovalMap.length; i++) {
  48.         str = str.replace(defaultDiacriticsRemovalMap[i].letters, defaultDiacriticsRemovalMap[i].base);
  49.     }
  50.  
  51.     return str;
  52.  
  53. }
  54.  
  55.  
  56. var $repertoire = $('.js-repertoire');
  57.  
  58.     if ($repertoire.length) {
  59.  
  60.         var $repertoireSearchForm = $repertoire.find('.js-repertoire-search-form');
  61.         var $repertoireSearchFormInput = $repertoireSearchForm.find('.js-repertoire-search-form-input');
  62.         var $repertoireLinkCancel = $repertoire.find('.js-repertoire-search-link-cancel');
  63.  
  64.         var $repertoireLetters = $repertoire.find('.js-repertoire-letter');
  65.         var $repertoireArtistsItems = $repertoireLetters.find('.js-repertoire-letter-artists-item');
  66.  
  67.         var repertoireColumnsCount;
  68.         var setRepertoireColumns = function () {
  69.  
  70.             if (window.matchMedia && window.matchMedia('(max-width: 700px)').matches) {
  71.                 repertoireColumnsCount = 2;
  72.             } else if (window.matchMedia && window.matchMedia('(max-width: 1000px)').matches) {
  73.                 repertoireColumnsCount = 3;
  74.             } else {
  75.                 repertoireColumnsCount = 4
  76.             }
  77.  
  78.         };
  79.  
  80.         $(window).on('resize', function () {
  81.             waitForFinalEvent(setRepertoireColumns, 300, 'repertoirecolumns');
  82.         });
  83.  
  84.         setRepertoireColumns();
  85.  
  86.         $repertoireSearchFormInput.keyup(function () {
  87.  
  88.             var val = $(this).val().toLowerCase();
  89.  
  90.             $repertoireArtistsItems.removeClass('repertoire-letter-artists-item--hidden');
  91.             $repertoireArtistsItems.removeClass('repertoire-letter-artists-item--visible');
  92.  
  93.             $repertoireLetters.removeClass('repertoire-letter--hidden');
  94.             $repertoireLetters.removeClass('repertoire-letter--visible');
  95.  
  96.             if (val.length) {
  97.  
  98.                 $repertoireLinkCancel.show();
  99.                 $repertoire.addClass('repertoire--search-active');
  100.  
  101.                 $repertoireLetters.each(function () {
  102.  
  103.                     var $letterItem = $(this);
  104.                     var $artistsItems = $letterItem.find('.js-repertoire-letter-artists-item');
  105.                     var visibleCount = 0;
  106.  
  107.                     $artistsItems.each(function (index) {
  108.  
  109.                         var $item = $(this);
  110.                         var content = $item.data('content').toLowerCase();
  111.                         var contentWithoutDiacritic = removeDiacritics(content);
  112.  
  113.                         $item.attr('data-index', '');
  114.  
  115.                         if (content.indexOf(val) === -1 && contentWithoutDiacritic.indexOf(val)) {
  116.                             $item.addClass('repertoire-letter-artists-item--hidden');
  117.                         } else {
  118.  
  119.                             ++visibleCount;
  120.                             $item.addClass('repertoire-letter-artists-item--visible');
  121.  
  122.                             if (visibleCount > 1) {
  123.                                 $item.attr('data-index', visibleCount % repertoireColumnsCount);
  124.                             }
  125.  
  126.                         }
  127.  
  128.                     });
  129.  
  130.                     if (visibleCount == 0) {
  131.                         $letterItem.addClass('repertoire-letter--hidden');
  132.                     } else {
  133.                         $letterItem.addClass('repertoire-letter--visible');
  134.                     }
  135.  
  136.                 });
  137.  
  138.             } else {
  139.                 $repertoireLinkCancel.hide();
  140.                 $repertoire.removeClass('repertoire--search-active');
  141.             }
  142.  
  143.         });
  144.  
  145.         $repertoireLinkCancel.click(function () {
  146.             $repertoireSearchFormInput.val('');
  147.             $repertoireSearchFormInput.trigger('keyup');
  148.             return false;
  149.         });
  150.  
  151.         $repertoireSearchForm.submit(function () {
  152.             return false;
  153.         });
  154.  
  155.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement