From 3376908710672f7d13b7c1d6e7628000b1b87094 Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Tue, 29 Sep 2020 19:02:47 -0700 Subject: [PATCH] Add selectize disable-options plugin * Modified to add a `disableField` option --- data/interfaces/default/css/tautulli.css | 4 + .../js/selectize.plugin.disable-options.js | 87 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 data/interfaces/default/js/selectize.plugin.disable-options.js diff --git a/data/interfaces/default/css/tautulli.css b/data/interfaces/default/css/tautulli.css index 61b1af65..ebe7b6d3 100644 --- a/data/interfaces/default/css/tautulli.css +++ b/data/interfaces/default/css/tautulli.css @@ -217,6 +217,10 @@ select.form-control:focus, .selectize-dropdown .optgroup-header { font-weight: bold; } +.selectize-dropdown [data-selectable].option-disabled { + color: #aaa; + cursor: default; +} select.form-control option { color: #555; background-color: #eee; diff --git a/data/interfaces/default/js/selectize.plugin.disable-options.js b/data/interfaces/default/js/selectize.plugin.disable-options.js new file mode 100644 index 00000000..0a5ed8fc --- /dev/null +++ b/data/interfaces/default/js/selectize.plugin.disable-options.js @@ -0,0 +1,87 @@ +/** + * Plugin: "disable_options" (selectize.js) + * Copyright (c) 2013 Mondo Robot & contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this + * file except in compliance with the License. You may obtain a copy of the License at: + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF + * ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + * + * @authors Jake Myers , Vaughn Draughon + */ + + Selectize.define('disable_options', function(options) { + var self = this; + + options = $.extend({ + 'disableField': '', + 'disableOptions': [] + }, options); + + self.refreshOptions = (function() { + var original = self.refreshOptions; + + return function() { + original.apply(this, arguments); + + $.each(options.disableOptions, function(index, option) { + self.$dropdown_content.find('[data-' + options.disableField + '="' + String(option) + '"]').addClass('option-disabled'); + }); + }; + })(); + + self.onOptionSelect = (function() { + var original = self.onOptionSelect; + + return function(e) { + var value, $target, $option; + + if (e.preventDefault) { + e.preventDefault(); + e.stopPropagation(); + } + + $target = $(e.currentTarget); + + if ($target.hasClass('option-disabled')) { + return; + } + return original.apply(this, arguments); + }; + })(); + + self.disabledOptions = function() { + return options.disableOptions; + } + + self.setDisabledOptions = function( values ) { + options.disableOptions = values + } + + self.disableOptions = function( values ) { + if ( ! ( values instanceof Array ) ) { + values = [ values ] + } + values.forEach( function( val ) { + if ( options.disableOptions.indexOf( val ) == -1 ) { + options.disableOptions.push( val ) + } + } ); + } + + self.enableOptions = function( values ) { + if ( ! ( values instanceof Array ) ) { + values = [ values ] + } + values.forEach( function( val ) { + var remove = options.disableOptions.indexOf( val ); + if ( remove + 1 ) { + options.disableOptions.splice( remove, 1 ); + } + } ); + } +}); \ No newline at end of file