Temporary fix for autocomplete with materializeCSS

This commit is contained in:
Kasper Rynning-Tønnesen
2017-02-15 12:21:12 +01:00
parent 9d4bb0b68b
commit db1c25c2f3
6 changed files with 181 additions and 11 deletions

View File

@@ -1 +1 @@
!function(){function e(e){var t,a=e.substring(1).split("&"),n={};for(var o in a)t=a[o].split("="),2==t.length&&(n[t[0]]=t[1]);return n}window.addEventListener("load",function(){var t,a,n,o=e(window.location.hash),i=window.location.protocol+"//"+window.location.hostname+"/o_callback";if(o.spotify)t="b934ecdd173648f5bcd38738af529d58",a="token",n="playlist-read-private playlist-read-collaborative user-read-private playlist-modify-public",state=o.nonce,window.location.href="https://accounts.spotify.com/authorize?client_id="+t+"&scope="+n+"&show_dialog=false&response_type="+a+"&redirect_uri="+i+"&state="+state;else if(o.youtube)t="944988770273-butsmlr1aotlsskk8lmgvh0etqqekigf.apps.googleusercontent.com",a="token",n="https://www.googleapis.com/auth/youtube",state=o.nonce,window.location.href="https://accounts.google.com/o/oauth2/v2/auth?client_id="+t+"&response_type="+a+"&state="+state+"&redirect_uri="+i+"&scope="+n;else{var s=e(window.location.hash);window.opener.callback(s)}})}();
!function(){function e(e){var t,a=e.substring(1).split("&"),n={};for(var i in a)t=a[i].split("="),2==t.length&&(n[t[0]]=t[1]);return n}window.addEventListener("load",function(){var t,a,n,i=e(window.location.hash),o=window.location.protocol+"//"+window.location.hostname+"/o_callback";if(i.spotify)t="b934ecdd173648f5bcd38738af529d58",a="token",n="playlist-read-private playlist-read-collaborative user-read-private playlist-modify-public",state=i.nonce,window.location.href="https://accounts.spotify.com/authorize?client_id="+t+"&scope="+n+"&show_dialog=false&response_type="+a+"&redirect_uri="+o+"&state="+state;else if(i.youtube)t="944988770273-butsmlr1aotlsskk8lmgvh0etqqekigf.apps.googleusercontent.com",a="token",n="https://www.googleapis.com/auth/youtube",state=i.nonce,window.location.href="https://accounts.google.com/o/oauth2/v2/auth?client_id="+t+"&response_type="+a+"&state="+state+"&redirect_uri="+o+"&scope="+n;else{var s=e(window.location.hash);window.opener.callback(s)}})}();

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -136,7 +136,7 @@ var Frontpage = {
limit: 5, // The max amount of results that can be shown at once. Default: Infinity.
});
$(".autocomplete").off('keydown.autocomplete');
//$(".autocomplete").off('keydown.autocomplete');
document.getElementById("preloader").style.display = "none";
//Materialize.fadeInImage('#channels');

View File

@@ -129,6 +129,176 @@ $().ready(function(){
} catch(error){
Helper.log("Error with fetching GitHub commit info");
}
/*******
MaterializeCSS fix for autocomplete
******/
$.fn.autocomplete = function (options) {
// Defaults
var defaults = {
data: {},
limit: Infinity,
onAutocomplete: null
};
options = $.extend(defaults, options);
return this.each(function() {
var $input = $(this);
var data = options.data,
count = 0,
activeIndex = -1,
oldVal,
$inputDiv = $input.closest('.input-field'); // Div to append on
// Check if data isn't empty
if (!$.isEmptyObject(data)) {
var $autocomplete = $('<ul class="autocomplete-content dropdown-content"></ul>');
var $oldAutocomplete;
// Append autocomplete element.
// Prevent double structure init.
if ($inputDiv.length) {
$oldAutocomplete = $inputDiv.children('.autocomplete-content.dropdown-content').first();
if (!$oldAutocomplete.length) {
$inputDiv.append($autocomplete); // Set ul in body
}
} else {
$oldAutocomplete = $input.next('.autocomplete-content.dropdown-content');
if (!$oldAutocomplete.length) {
$input.after($autocomplete);
}
}
if ($oldAutocomplete.length) {
$autocomplete = $oldAutocomplete;
}
// Highlight partial match.
var highlight = function(string, $el) {
var img = $el.find('img');
var matchStart = $el.text().toLowerCase().indexOf("" + string.toLowerCase() + ""),
matchEnd = matchStart + string.length - 1,
beforeMatch = $el.text().slice(0, matchStart),
matchText = $el.text().slice(matchStart, matchEnd + 1),
afterMatch = $el.text().slice(matchEnd + 1);
$el.html("<span>" + beforeMatch + "<span class='highlight'>" + matchText + "</span>" + afterMatch + "</span>");
if (img.length) {
$el.prepend(img);
}
};
// Reset current element position
var resetCurrentElement = function() {
activeIndex = -1;
$autocomplete.find('.active').removeClass('active');
}
// Perform search
$input.off('keyup.autocomplete').on('keyup.autocomplete', function (e) {
// Reset count.
count = 0;
// Don't capture enter or arrow key usage.
if (e.which === 13 ||
e.which === 38 ||
e.which === 40) {
return;
}
var val = $input.val().toLowerCase();
// Check if the input isn't empty
if (oldVal !== val) {
$autocomplete.empty();
resetCurrentElement();
if (val !== '') {
for(var key in data) {
if (data.hasOwnProperty(key) &&
key.toLowerCase().indexOf(val) !== -1 &&
key.toLowerCase() !== val) {
// Break if past limit
if (count >= options.limit) {
break;
}
var autocompleteOption = $('<li></li>');
if (!!data[key]) {
autocompleteOption.append('<img src="'+ data[key] +'" class="right circle"><span>'+ key +'</span>');
} else {
autocompleteOption.append('<span>'+ key +'</span>');
}
$autocomplete.append(autocompleteOption);
highlight(val, autocompleteOption);
count++;
}
}
}
}
// Update oldVal
oldVal = val;
});
$input.off('keydown.autocomplete').on('keydown.autocomplete', function (e) {
// Arrow keys and enter key usage
var keyCode = e.which,
liElement,
numItems = $autocomplete.children('li').length,
$active = $autocomplete.children('.active').first();
// select element on Enter
if (keyCode === 13 && activeIndex >= 0) {
liElement = $autocomplete.children('li').eq(activeIndex);
if (liElement.length) {
liElement.click();
e.preventDefault();
}
return;
}
// Capture up and down key
if ( keyCode === 38 || keyCode === 40 ) {
e.preventDefault();
if (keyCode === 38 &&
activeIndex > 0) {
activeIndex--;
}
if (keyCode === 40 &&
activeIndex < (numItems - 1)) {
activeIndex++;
}
console.log(activeIndex);
$active.removeClass('active');
if (activeIndex >= 0) {
$autocomplete.children('li').eq(activeIndex).addClass('active');
}
}
});
// Set input value
$autocomplete.on('click', 'li', function () {
var text = $(this).text().trim();
$input.val(text);
$input.trigger('change');
$autocomplete.empty();
resetCurrentElement();
// Handle onAutocomplete callback.
if (typeof(options.onAutocomplete) === "function") {
options.onAutocomplete.call(this, text);
}
});
}
});
};
});
@@ -855,7 +1025,7 @@ $(document).on("submit", ".channel-finder", function(e){
Frontpage.to_channel($(".room-namer").val());
return false;
});
/*
$(document).off("keyup", "keyup.autocomplete", function(e){
if(e.keyCode == 13){
e.preventDefault();
@@ -870,7 +1040,7 @@ $(document).off("keydown", "keydown.autocomplete", function(e){
console.log(e.keyCode);
console.log($(this).val());
}
});
});*/
$(document).on("submit", ".channel-finder-mobile", function(e){
e.preventDefault();