support status */ protected array $mimeTypeSupportMap = []; protected ?array $defaultProviders = null; protected ?string $userId; private Coordinator $bootstrapCoordinator; /** * Hash map (without value) of loaded bootstrap providers * @psalm-var array */ private array $loadedBootstrapProviders = []; private IServerContainer $container; private IBinaryFinder $binaryFinder; private IMagickSupport $imagickSupport; private bool $enablePreviews; public function __construct( IConfig $config, IRootFolder $rootFolder, IAppData $appData, IEventDispatcher $eventDispatcher, GeneratorHelper $helper, ?string $userId, Coordinator $bootstrapCoordinator, IServerContainer $container, IBinaryFinder $binaryFinder, IMagickSupport $imagickSupport, ) { $this->config = $config; $this->rootFolder = $rootFolder; $this->appData = $appData; $this->eventDispatcher = $eventDispatcher; $this->helper = $helper; $this->userId = $userId; $this->bootstrapCoordinator = $bootstrapCoordinator; $this->container = $container; $this->binaryFinder = $binaryFinder; $this->imagickSupport = $imagickSupport; $this->enablePreviews = $config->getSystemValueBool('enable_previews', true); } /** * In order to improve lazy loading a closure can be registered which will be * called in case preview providers are actually requested * * $callable has to return an instance of \OCP\Preview\IProvider or \OCP\Preview\IProviderV2 * * @param string $mimeTypeRegex Regex with the mime types that are supported by this provider * @param \Closure $callable * @return void */ public function registerProvider($mimeTypeRegex, \Closure $callable): void { if (!$this->enablePreviews) { return; } if (!isset($this->providers[$mimeTypeRegex])) { $this->providers[$mimeTypeRegex] = []; } $this->providers[$mimeTypeRegex][] = $callable; $this->providerListDirty = true; } /** * Get all providers */ public function getProviders(): array { if (!$this->enablePreviews) { return []; } $this->registerCoreProviders(); $this->registerBootstrapProviders(); if ($this->providerListDirty) { $keys = array_map('strlen', array_keys($this->providers)); array_multisort($keys, SORT_DESC, $this->providers); $this->providerListDirty = false; } return $this->providers; } /** * Does the manager have any providers */ public function hasProviders(): bool { $this->registerCoreProviders(); return !empty($this->providers); } private function getGenerator(): Generator { if ($this->generator === null) { $this->generator = new Generator( $this->config, $this, $this->appData, new GeneratorHelper( $this->rootFolder, $this->config ), $this->eventDispatcher ); } return $this->generator; } /** * Returns a preview of a file * * The cache is searched first and if nothing usable was found then a preview is * generated by one of the providers * * @param File $file * @param int $width * @param int $height * @param bool $crop * @param string $mode * @param string $mimeType * @return ISimpleFile * @throws NotFoundException * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) * @since 11.0.0 - \InvalidArgumentException was added in 12.0.0 */ public function getPreview(File $file, $width = -1, $height = -1, $crop = false, $mode = IPreview::MODE_FILL, $mimeType = null) { $this->throwIfPreviewsDisabled(); $previewConcurrency = $this->getGenerator()->getNumConcurrentPreviews('preview_concurrency_all'); $sem = Generator::guardWithSemaphore(Generator::SEMAPHORE_ID_ALL, $previewConcurrency); try { $preview = $this->getGenerator()->getPreview($file, $width, $height, $crop, $mode, $mimeType); } finally { Generator::unguardWithSemaphore($sem); } return $preview; } /** * Generates previews of a file * * @param File $file * @param array $specifications * @param string $mimeType * @return ISimpleFile the last preview that was generated * @throws NotFoundException * @throws \InvalidArgumentException if the preview would be invalid (in case the original image is invalid) * @since 19.0.0 */ public function generatePreviews(File $file, array $specifications, $mimeType = null) { $this->throwIfPreviewsDisabled(); return $this->getGenerator()->generatePreviews($file, $specifications, $mimeType); } /** * returns true if the passed mime type is supported * * @param string $mimeType * @return boolean */ public function isMimeSupported($mimeType = '*') { if (!$this->enablePreviews) { return false; } if (isset($this->mimeTypeSupportMap[$mimeType])) { return $this->mimeTypeSupportMap[$mimeType]; } $this->registerCoreProviders(); $this->registerBootstrapProviders(); $providerMimeTypes = array_keys($this->providers); foreach ($providerMimeTypes as $supportedMimeType) { if (preg_match($supportedMimeType, $mimeType)) { $this->mimeTypeSupportMap[$mimeType] = true; return true; } } $this->mimeTypeSupportMap[$mimeType] = false; return false; } /** * Check if a preview can be generated for a file */ public function isAvailable(\OCP\Files\FileInfo $file): bool { if (!$this->enablePreviews) { return false; } $this->registerCoreProviders(); if (!$this->isMimeSupported($file->getMimetype())) { return false; } $mount = $file->getMountPoint(); if ($mount and !$mount->getOption('previews', true)) { return false; } foreach ($this->providers as $supportedMimeType => $providers) { if (preg_match($supportedMimeType, $file->getMimetype())) { foreach ($providers as $providerClosure) { $provider = $this->helper->getProvider($providerClosure); if (!($provider instanceof IProviderV2)) { continue; } if ($provider->isAvailable($file)) { return true; } } } } return false; } /** * List of enabled default providers * * The following providers are enabled by default: * - OC\Preview\PNG * - OC\Preview\JPEG * - OC\Preview\GIF * - OC\Preview\BMP * - OC\Preview\XBitmap * - OC\Preview\MarkDown * - OC\Preview\MP3 * - OC\Preview\TXT * * The following providers are disabled by default due to performance or privacy concerns: * - OC\Preview\Font * - OC\Preview\HEIC * - OC\Preview\Illustrator * - OC\Preview\Movie * - OC\Preview\MSOfficeDoc * - OC\Preview\MSOffice2003 * - OC\Preview\MSOffice2007 * - OC\Preview\OpenDocument * - OC\Preview\PDF * - OC\Preview\Photoshop * - OC\Preview\Postscript * - OC\Preview\StarOffice * - OC\Preview\SVG * - OC\Preview\TIFF * * @return array */ protected function getEnabledDefaultProvider() { if ($this->defaultProviders !== null) { return $this->defaultProviders; } $imageProviders = [ Preview\PNG::class, Preview\JPEG::class, Preview\GIF::class, Preview\BMP::class, Preview\XBitmap::class, Preview\Krita::class, Preview\WebP::class, ]; $this->defaultProviders = $this->config->getSystemValue('enabledPreviewProviders', array_merge([ Preview\MarkDown::class, Preview\MP3::class, Preview\TXT::class, Preview\OpenDocument::class, ], $imageProviders)); if (in_array(Preview\Image::class, $this->defaultProviders)) { $this->defaultProviders = array_merge($this->defaultProviders, $imageProviders); } $this->defaultProviders = array_unique($this->defaultProviders); return $this->defaultProviders; } /** * Register the default providers (if enabled) * * @param string $class * @param string $mimeType */ protected function registerCoreProvider($class, $mimeType, $options = []) { if (in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { $this->registerProvider($mimeType, function () use ($class, $options) { return new $class($options); }); } } /** * Register the default providers (if enabled) */ protected function registerCoreProviders() { if ($this->registeredCoreProviders) { return; } $this->registeredCoreProviders = true; $this->registerCoreProvider(Preview\TXT::class, '/text\/plain/'); $this->registerCoreProvider(Preview\MarkDown::class, '/text\/(x-)?markdown/'); $this->registerCoreProvider(Preview\PNG::class, '/image\/png/'); $this->registerCoreProvider(Preview\JPEG::class, '/image\/jpeg/'); $this->registerCoreProvider(Preview\GIF::class, '/image\/gif/'); $this->registerCoreProvider(Preview\BMP::class, '/image\/bmp/'); $this->registerCoreProvider(Preview\XBitmap::class, '/image\/x-xbitmap/'); $this->registerCoreProvider(Preview\WebP::class, '/image\/webp/'); $this->registerCoreProvider(Preview\Krita::class, '/application\/x-krita/'); $this->registerCoreProvider(Preview\MP3::class, '/audio\/mpeg$/'); $this->registerCoreProvider(Preview\OpenDocument::class, '/application\/vnd.oasis.opendocument.*/'); $this->registerCoreProvider(Preview\Imaginary::class, Preview\Imaginary::supportedMimeTypes()); $this->registerCoreProvider(Preview\ImaginaryPDF::class, Preview\ImaginaryPDF::supportedMimeTypes()); // SVG and Bitmap require imagick if ($this->imagickSupport->hasExtension()) { $imagickProviders = [ 'SVG' => ['mimetype' => '/image\/svg\+xml/', 'class' => Preview\SVG::class], 'TIFF' => ['mimetype' => '/image\/tiff/', 'class' => Preview\TIFF::class], 'PDF' => ['mimetype' => '/application\/pdf/', 'class' => Preview\PDF::class], 'AI' => ['mimetype' => '/application\/illustrator/', 'class' => Preview\Illustrator::class], 'PSD' => ['mimetype' => '/application\/x-photoshop/', 'class' => Preview\Photoshop::class], 'EPS' => ['mimetype' => '/application\/postscript/', 'class' => Preview\Postscript::class], 'TTF' => ['mimetype' => '/application\/(?:font-sfnt|x-font$)/', 'class' => Preview\Font::class], 'HEIC' => ['mimetype' => '/image\/(x-)?hei(f|c)/', 'class' => Preview\HEIC::class], 'TGA' => ['mimetype' => '/image\/(x-)?t(ar)?ga/', 'class' => Preview\TGA::class], 'SGI' => ['mimetype' => '/image\/(x-)?sgi/', 'class' => Preview\SGI::class], ]; foreach ($imagickProviders as $queryFormat => $provider) { $class = $provider['class']; if (!in_array(trim($class, '\\'), $this->getEnabledDefaultProvider())) { continue; } if ($this->imagickSupport->supportsFormat($queryFormat)) { $this->registerCoreProvider($class, $provider['mimetype']); } } } $this->registerCoreProvidersOffice(); // Video requires avconv or ffmpeg if (in_array(Preview\Movie::cla
<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>jQuery UI Autocomplete - Combobox</title>
	<link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
	<script src="../../jquery-1.9.1.js"></script>
	<script src="../../ui/jquery.ui.core.js"></script>
	<script src="../../ui/jquery.ui.widget.js"></script>
	<script src="../../ui/jquery.ui.button.js"></script>
	<script src="../../ui/jquery.ui.position.js"></script>
	<script src="../../ui/jquery.ui.menu.js"></script>
	<script src="../../ui/jquery.ui.autocomplete.js"></script>
	<script src="../../ui/jquery.ui.tooltip.js"></script>
	<link rel="stylesheet" href="../demos.css">
	<style>
	.custom-combobox {
		position: relative;
		display: inline-block;
	}
	.custom-combobox-toggle {
		position: absolute;
		top: 0;
		bottom: 0;
		margin-left: -1px;
		padding: 0;
		/* support: IE7 */
		*height: 1.7em;
		*top: 0.1em;
	}
	.custom-combobox-input {
		margin: 0;
		padding: 0.3em;
	}
	</style>
	<script>
	(function( $ ) {
		$.widget( "custom.combobox", {
			_create: function() {
				this.wrapper = $( "<span>" )
					.addClass( "custom-combobox" )
					.insertAfter( this.element );

				this.element.hide();
				this._createAutocomplete();
				this._createShowAllButton();
			},

			_createAutocomplete: function() {
				var selected = this.element.children( ":selected" ),
					value = selected.val() ? selected.text() : "";

				this.input = $( "<input>" )
					.appendTo( this.wrapper )
					.val( value )
					.attr( "title", "" )
					.addClass( "custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
					.autocomplete({
						delay: 0,
						minLength: 0,
						source: $.proxy( this, "_source" )
					})
					.tooltip({
						tooltipClass: "ui-state-highlight"
					});

				this._on( this.input, {
					autocompleteselect: function( event, ui ) {
						ui.item.option.selected = true;
						this._trigger( "select", event, {
							item: ui.item.option
						});
					},

					autocompletechange: "_removeIfInvalid"
				});
			},

			_createShowAllButton: function() {
				var input = this.input,
					wasOpen = false;

				$( "<a>" )
					.attr( "tabIndex", -1 )
					.attr( "title", "Show All Items" )
					.tooltip()
					.appendTo( this.wrapper )
					.button({
						icons: {
							primary: "ui-icon-triangle-1-s"
						},
						text: false
					})
					.removeClass( "ui-corner-all" )
					.addClass( "custom-combobox-toggle ui-corner-right" )
					.mousedown(function() {
						wasOpen = input.autocomplete( "widget" ).is( ":visible" );
					})
					.click(function() {
						input.focus();

						// Close if already visible
						if ( wasOpen ) {
							return;
						}

						// Pass empty string as value to search for, displaying all results
						input.autocomplete( "search", "" );
					});
			},

			_source: function( request, response ) {
				var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
				response( this.element.children( "option" ).map(function() {
					var text = $( this ).text();
					if ( this.value && ( !request.term || matcher.test(text) ) )
						return {
							label: text,
							value: text,
							option: this
						};
				}) );
			},

			_removeIfInvalid: function( event, ui ) {

				// Selected an item, nothing to do
				if ( ui.item ) {
					return;
				}

				// Search for a match (case-insensitive)
				var value = this.input.val(),
					valueLowerCase = value.toLowerCase(),
					valid = false;
				this.element.children( "option" ).each(function() {
					if ( $( this ).text().toLowerCase() === valueLowerCase ) {
						this.selected = valid = true;
						return false;
					}
				});

				// Found a match, nothing to do
				if ( valid ) {
					return;
				}

				// Remove invalid value
				this.input
					.val( "" )
					.attr( "title", value + " didn't match any item" )
					.tooltip( "open" );
				this.element.val( "" );
				this._delay(function() {
					this.input.tooltip( "close" ).attr( "title", "" );
				}, 2500 );
				this.input.autocomplete( "instance" ).term = "";
			},

			_destroy: function() {
				this.wrapper.remove();
				this.element.show();
			}
		});
	})( jQuery );

	$(function() {
		$( "#combobox" ).combobox();
		$( "#toggle" ).click(function() {
			$( "#combobox" ).toggle();
		});
	});
	</script>
</head>
<body>

<div class="ui-widget">
	<label>Your preferred programming language: </label>
	<select id="combobox">
		<option value="">Select one...</option>
		<option value="ActionScript">ActionScript</option>
		<option value="AppleScript">AppleScript</option>
		<option value="Asp">Asp</option>
		<option value="BASIC">BASIC</option>
		<option value="C">C</option>
		<option value="C++">C++</option>
		<option value="Clojure">Clojure</option>
		<option value="COBOL">COBOL</option>
		<option value="ColdFusion">ColdFusion</option>
		<option value="Erlang">Erlang</option>
		<option value="Fortran">Fortran</option>
		<option value="Groovy">Groovy</option>
		<option value="Haskell">Haskell</option>
		<option value="Java">Java</option>
		<option value="JavaScript">JavaScript</option>
		<option value="Lisp">Lisp</option>
		<option value="Perl">Perl</option>
		<option value="PHP">PHP</option>
		<option value="Python">Python</option>
		<option value="Ruby">Ruby</option>
		<option value="Scala">Scala</option>
		<option value="Scheme">Scheme</option>
	</select>
</div>
<button id="toggle">Show underlying select</button>

<div class="demo-description">
<p>A custom widget built by composition of Autocomplete and Button. You can either type something into the field to get filtered suggestions based on your input, or use the button to get the full list of selections.</p>
<p>The input is read from an existing select-element for progressive enhancement, passed to Autocomplete with a customized source-option.</p>
<p>This is not a supported or even complete widget. Its purely for demoing what autocomplete can do with a bit of customization. <a href="http://www.learningjquery.com/2010/06/a-jquery-ui-combobox-under-the-hood">For a detailed explanation of how the widget works, check out this Learning jQuery article.</a></p>
</div>
</body>
</html>