Skip to content

Commit

Permalink
refactor: fix eslint/xo errors in js and a memory leak in rootDirs.js
Browse files Browse the repository at this point in the history
Signed-off-by: miigotu <[email protected]>
  • Loading branch information
miigotu committed Feb 12, 2024
1 parent fceb970 commit 54f49b9
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 168 deletions.
4 changes: 3 additions & 1 deletion sickchill/gui/slick/js/ajaxNotifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ function displayPNotify(type, title, message, id) {
hide: true,
history: true,
shadow: true,
stack: {dir1: 'up', dir2: 'left', firstpos1: 25, firstpos2: 25},
stack: {
dir1: 'up', dir2: 'left', firstpos1: 25, firstpos2: 25,
},
styling: 'fontawesome',
width: '340px',
destroy: true,
Expand Down
98 changes: 48 additions & 50 deletions sickchill/gui/slick/js/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
}, data => {
fileBrowserDialog.empty();
const firstValue = data[0];
let i = 0;
let list = null;
let link = null;
data = $.grep(data, () => i++ !== 0);

const innerData = $.grep(data, (value, index) => index !== 0);

const inputContainer = $('<div class="fileBrowserFieldContainer"></div>');
$('<input type="text" class="form-control input-sm">').val(firstValue.currentPath).on('keypress', event_ => {
Expand All @@ -52,7 +52,7 @@

const listContainer = $('<div class="ui-dialog-scrollable-child">');
list = $('<ul>').appendTo(listContainer.appendTo(fileBrowserDialog));
$.each(data, (i, entry) => {
$.each(innerData, (i, entry) => {
if (entry.isFile && fileTypes && (!entry.isAllowed || fileTypes.includes('images') && !entry.isImage)) { // eslint-disable-line no-mixed-operators
return true;
}
Expand All @@ -70,13 +70,11 @@
} else if (entry.isFile) {
link.prepend('<span class="ui-icon ui-icon-document"></span>');
} else {
link.prepend('<span class="ui-icon ui-icon-folder-collapsed"></span>')
.on('mouseenter', function () {
$('span', this).addClass('ui-icon-folder-open');
})
.on('mouseleave', function () {
$('span', this).removeClass('ui-icon-folder-open');
});
link.prepend('<span class="ui-icon ui-icon-folder-collapsed"></span>').on('mouseenter', function () {
$('span', this).addClass('ui-icon-folder-open');
}).on('mouseleave', function () {
$('span', this).removeClass('ui-icon-folder-open');
});
}

link.appendTo(list);
Expand All @@ -91,20 +89,20 @@
}

$.fn.nFileBrowser = function (callback, options) {
options = $.extend({}, $.Browser.defaults, options);
const newOptions = $.extend({}, $.Browser.defaults, options);

// Make a fileBrowserDialog object if one doesn't exist already
if (fileBrowserDialog) {
// The title may change, even if fileBrowserDialog already exists
fileBrowserDialog.dialog('option', 'title', options.title);
fileBrowserDialog.dialog('option', 'title', newOptions.title);
} else {
// Set up the jquery dialog
fileBrowserDialog = $('<div class="fileBrowserDialog" style="display:none"></div>').appendTo('body').dialog({
dialogClass: 'browserDialog',
classes: {
'ui-dialog': 'ui-dialog-scrollable-by-child',
},
title: options.title,
title: newOptions.title,
position: {my: 'center top', at: 'center top+60', of: window},
minWidth: Math.min($(document).width() - 80, 650),
height: Math.min($(document).height() - 80, $(window).height() - 80),
Expand All @@ -120,7 +118,7 @@
class: 'btn',
click() {
// Store the browsed path to the associated text field
callback(options.includeFiles ? currentBrowserPath : $(this).find('.fileBrowserField').val(), options);
callback(newOptions.includeFiles ? currentBrowserPath : $(this).find('.fileBrowserField').val(), newOptions);
$(this).dialog('close');
},
}, {
Expand All @@ -132,38 +130,38 @@
}]);

// Set up the browser and launch the dialog
let initialDir = '';
if (options.initialDir) {
initialDir = options.initialDir;
let initialDirectory = '';
if (newOptions.initialDirectory) {
initialDirectory = newOptions.initialDirectory;
}

browse(initialDir, options.url, options.includeFiles, options.fileTypes);
browse(initialDirectory, newOptions.url, newOptions.includeFiles, newOptions.fileTypes);
fileBrowserDialog.dialog('open');

return false;
};

$.fn.fileBrowser = function (options) {
options = $.extend({}, $.Browser.defaults, options);
const newOptions = $.extend({}, $.Browser.defaults, options);
// Text field used for the result
options.field = $(this);
newOptions.field = $(this);

if (options.field.autocomplete && options.autocompleteURL) {
if (newOptions.field.autocomplete && newOptions.autocompleteURL) {
let query = '';
options.field.autocomplete({
newOptions.field.autocomplete({
position: {my: 'top', at: 'bottom', collision: 'flipfit'},
source(request, response) {
// Keep track of user submitted search term
query = $.ui.autocomplete.escapeRegex(request.term, options.includeFiles);
query = $.ui.autocomplete.escapeRegex(request.term, newOptions.includeFiles);
$.ajax({
url: options.autocompleteURL,
url: newOptions.autocompleteURL,
data: request,
dataType: 'json',
success(data) {
// Implement a startsWith filter for the results
const matcher = new RegExp('^' + query, 'i');
const a = $.grep(data, item => matcher.test(item));
response(a);
const match = $.grep(data, item => matcher.test(item));
response(match);
},
});
},
Expand All @@ -173,53 +171,53 @@
}).data('ui-autocomplete')._renderItem = function (ul, item) {
// Highlight the matched search term from the item -- note that this is global and will match anywhere
let resultItem = item.label;
const x = new RegExp('(?![^&;]+;)(?!<[^<>]*)(' + query + ')(?![^<>]*>)(?![^&;]+;)', 'gi');
resultItem = resultItem.replace(x, fullMatch => '<b>' + fullMatch + '</b>');
return $('<li></li>')
.data('ui-autocomplete-item', item)
.append('<a class="nowrap">' + resultItem + '</a>')
.appendTo(ul);
const matcher = new RegExp('(?![^&;]+;)(?!<[^<>]*)(' + query + ')(?![^<>]*>)(?![^&;]+;)', 'gi');
resultItem = resultItem.replace(matcher, fullMatch => '<b>' + fullMatch + '</b>');
return $('<li></li>').data('ui-autocomplete-item', item).append('<a class="nowrap">' + resultItem + '</a>').appendTo(ul);
};
}

let path = false;
let callback = false;
let ls = false;
// If the text field is empty and we're given a key then populate it with the last browsed value from localStorage
let hasLocalStorage = false;
// If the text field is empty, and we're given a key then populate it with the last browsed value from localStorage
try {
ls = Boolean(localStorage.getItem);
hasLocalStorage = Boolean(localStorage.getItem);
} catch {}

if (ls && options.key) {
path = localStorage['fileBrowser-' + options.key];
if (hasLocalStorage && newOptions.key) {
path = localStorage['fileBrowser-' + newOptions.key];
}

if (options.key && options.field.val().length === 0 && path) {
options.field.val(path);
if (newOptions.key && newOptions.field.val().length === 0 && path) {
newOptions.field.val(path);
}

callback = function (path, options) {
options.field.val(path);
callback = function (path, newOptions) {
newOptions.field.val(path);

// Use a localStorage to remember for next time -- no ie6/7
if (ls && options.key) {
localStorage['fileBrowser-' + options.key] = path;
if (hasLocalStorage && newOptions.key) {
localStorage['fileBrowser-' + newOptions.key] = path;
}
};

options.field.addClass('fileBrowserField');
if (options.showBrowseButton) {
newOptions.field.addClass('fileBrowserField');
if (newOptions.showBrowseButton) {
// Append the browse button and give it a click behaviour
options.field.after(
newOptions.field.after(
$('<input type="button" value="Browse&hellip;" class="btn btn-inline fileBrowser">').on('click', function () {
const initialDir = options.field.val() || (options.key && path) || '';
const optionsWithInitialDir = $.extend({}, options, {initialDir});
$(this).nFileBrowser(callback, optionsWithInitialDir);
let initialDirectory = newOptions.field.val();
initialDirectory ||= (newOptions.key && path);
initialDirectory ||= '';

const optionsWithInitialDirectory = $.extend({}, newOptions, {initialDirectory});
$(this).nFileBrowser(callback, optionsWithInitialDirectory);
return false;
}),
);
}

return options.field;
return newOptions.field;
};
})(jQuery);
10 changes: 6 additions & 4 deletions sickchill/gui/slick/js/configProviders.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ $(document).ready(function () {
$.fn.makeNewznabProviderString = function () {
const provStrings = [];
for (const id in newznabProviders) {
if (Object.prototype.hasOwnProperty.call(newznabProviders, id)) {
if (Object.hasOwn(newznabProviders, id)) {
provStrings.push(newznabProviders[id][1].join('|'));
}
}
Expand Down Expand Up @@ -289,7 +289,7 @@ $(document).ready(function () {
$.fn.makeTorrentRssProviderString = function () {
const provStrings = [];
for (const id in torrentRssProviders) {
if (Object.prototype.hasOwnProperty.call(torrentRssProviders, id)) {
if (Object.hasOwn(torrentRssProviders, id)) {
provStrings.push(torrentRssProviders[id].join('|'));
}
}
Expand Down Expand Up @@ -323,7 +323,7 @@ $(document).ready(function () {
if (finalArray.length > 0) {
$('<select>').prop('id', 'editAProvider').addClass('form-control input-sm').appendTo('#provider-list');
for (const id in finalArray) {
if (Object.prototype.hasOwnProperty.call(finalArray, id)) {
if (Object.hasOwn(finalArray, id)) {
const provider = finalArray[id];
$('#editAProvider').append($('<option>').prop('value', provider).text($.trim($('#' + provider).text()).replace(/\s\*$/, '').replace(/\s\*\*$/, '')));
}
Expand Down Expand Up @@ -461,7 +461,9 @@ $(document).ready(function () {
const url = $('#torrentrss_url').val();
const cookies = $('#torrentrss_cookies').val();
const titleTAG = $('#torrentrss_titleTAG').val();
const parameters = {name, url, cookies, titleTAG};
const parameters = {
name, url, cookies, titleTAG,
};

// Send to the form with ajax, get a return value
$.getJSON(scRoot + '/config/providers/canAddTorrentRssProvider', parameters, function (data) {
Expand Down
Loading

0 comments on commit 54f49b9

Please sign in to comment.