├── README.md ├── composer.json ├── license.md └── src ├── Config └── config.php ├── Http └── Middleware │ └── LoadingMiddleware.php └── ServiceProvider.php /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Version on Packagist][ico-version]][link-packagist] 2 | [![Total Downloads][ico-downloads]][link-downloads] 3 | 4 | # Laravel Loading Indicator 5 | 6 | ## Introduction ## 7 | 8 | Laravel package to add loading indicator to pages while page is loading. Loading indicator is automatically removed after page has fully loaded. 9 | 10 | Behind the scene, middleware is used to inject needed HTML/CSS/JS before `` tag to show and hide the loading indicator accordingly. 11 | 12 | Loading indicators are via to awesome [CSSPIN](https://github.com/webkul/csspin) project. 13 | 14 | ## Requirements ## 15 | 16 | - PHP >= 7 17 | - Laravel 5 18 | 19 | ## Installation ## 20 | 21 | Install via composer 22 | 23 | ``` 24 | composer require sarfraznawaz2005/loading 25 | ``` 26 | 27 | For Laravel < 5.5: 28 | 29 | Add Service Provider to `config/app.php` in `providers` section 30 | 31 | ```php 32 | Sarfraznawaz2005\Loading\ServiceProvider::class, 33 | ``` 34 | 35 | --- 36 | 37 | Publish package's config file by running below command: 38 | 39 | ```bash 40 | $ php artisan vendor:publish --provider="Sarfraznawaz2005\Loading\ServiceProvider" 41 | ``` 42 | It should publish `config/loading.php` config file. 43 | 44 | ## Usage ## 45 | 46 | Simply add to `\Sarfraznawaz2005\Loading\Http\Middleware\LoadingMiddleware::class` to `app/Http/Kernel.php` either in global middleware section or route section. 47 | 48 | Add in global section if you want loading indicator on all pages automatically or add it to route middleware section if you want to add indictor to certain pages only via `middleware` method in routes. 49 | 50 | Visit any page and you should see loading indicator at middle of the page while page is loading. 51 | 52 | Check config file for options. 53 | 54 | 55 | ## Credits 56 | 57 | - [Sarfraz Ahmed][link-author] 58 | - [All Contributors][link-contributors] 59 | 60 | ## License 61 | 62 | Please see the [license file](license.md) for more information. 63 | 64 | [ico-version]: https://img.shields.io/packagist/v/sarfraznawaz2005/loading.svg?style=flat-square 65 | [ico-downloads]: https://img.shields.io/packagist/dt/sarfraznawaz2005/loading.svg?style=flat-square 66 | 67 | [link-packagist]: https://packagist.org/packages/sarfraznawaz2005/loading 68 | [link-downloads]: https://packagist.org/packages/sarfraznawaz2005/loading 69 | [link-author]: https://github.com/sarfraznawaz2005 70 | [link-contributors]: https://github.com/sarfraznawaz2005/loading/graphs/contributors 71 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sarfraznawaz2005/loading", 3 | "keywords": [ 4 | "laravel", 5 | "loader", 6 | "loading", 7 | "indicator" 8 | ], 9 | "description": "Laravel package to add loading indicator to pages while page is loading.", 10 | "type": "library", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Sarfraz Ahmed", 15 | "email": "sarfraznawaz2005@gmail.com" 16 | } 17 | ], 18 | "require": { 19 | "php": ">=7.0", 20 | "illuminate/support": "~5|~6|~7|~8|~9|~10" 21 | }, 22 | "autoload": { 23 | "psr-4": { 24 | "Sarfraznawaz2005\\Loading\\": "src/" 25 | } 26 | }, 27 | "extra": { 28 | "laravel": { 29 | "providers": [ 30 | "Sarfraznawaz2005\\Loading\\ServiceProvider" 31 | ] 32 | } 33 | }, 34 | "minimum-stability": "stable" 35 | } 36 | -------------------------------------------------------------------------------- /license.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Sarfraz Ahmed 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Config/config.php: -------------------------------------------------------------------------------- 1 | env('ENABLE_LOADING', true), 7 | 8 | // specify loading icon: 9 | // cp-round, cp-pinwheel, cp-balls, cp-bubble, cp-flip, cp-hue, cp-skeleton, cp-eclipse, cp-boxes, cp-morph, cp-heart, cp-meter 10 | 'icon' => 'cp-pinwheel', 11 | 12 | // hide loading indicator on DOM ready or complete window load. Use either of "window", "dom" 13 | 'hide_event' => 'window', 14 | 15 | // loading indicator background overlay options 16 | 'show_overlay' => true, 17 | 'overlay_background_color' => '#fff', 18 | 'overlay_opacity' => 0.7, 19 | 20 | // any custom css you want to override loading indicator styles with 21 | 'custom_css' => '', 22 | 23 | ]; 24 | -------------------------------------------------------------------------------- /src/Http/Middleware/LoadingMiddleware.php: -------------------------------------------------------------------------------- 1 | addContent($request, $response); 24 | } 25 | 26 | return $response; 27 | } 28 | 29 | /** 30 | * @param Request $request 31 | * @param $response 32 | */ 33 | protected function addContent($request, $response) 34 | { 35 | $icon = config('loading.icon', 'cp-skeleton'); 36 | $showOverlay = config('loading.show_overlay', true) ? 'block' : 'none'; 37 | $overlayOpacity = config('loading.overlay_opacity', 0.3); 38 | $overlayBackground = config('loading.overlay_background_color', '#000'); 39 | $clodeLoadingCode = config('loading.hide_event') === 'window' ? 'window.onload = hidePHPLoadingIndicator;' : 'document.addEventListener("DOMContentLoaded", hidePHPLoadingIndicator);'; 40 | $customCSS = config('loading.custom_css', ''); 41 | 42 | // do nothing in case of console 43 | if (app()->runningInConsole()) { 44 | return; 45 | } 46 | 47 | // do nothing in case of ajax request 48 | if ($request->ajax() || $request->expectsJson()) { 49 | return; 50 | } 51 | 52 | // do nothing in case of method which is not GET 53 | if (!$request->isMethod('get')) { 54 | return; 55 | } 56 | 57 | // do nothing in case of binary content 58 | if ($response instanceof BinaryFileResponse) { 59 | return; 60 | } 61 | 62 | $html = <<< LOADING 63 | 94 | 95 |
96 |
97 |
98 | 99 | 107 | LOADING; 108 | 109 | $content = $response->getContent(); 110 | 111 | $bodyPosition = strripos($content, ''); 112 | 113 | if (false !== $bodyPosition) { 114 | $html = "\n\n" . $this->compress($html) . "\n\n\n"; 115 | $content = substr($content, 0, $bodyPosition) . $html . substr($content, $bodyPosition); 116 | } 117 | 118 | $response->setContent($content); 119 | } 120 | 121 | private function compress($html) 122 | { 123 | ini_set('pcre.recursion_limit', '16777'); 124 | @ini_set('zlib.output_compression', 'On'); 125 | 126 | $regEx = '%# Collapse whitespace everywhere but in blacklisted elements. 127 | (?> # Match all whitespans other than single space. 128 | [^\S ]\s* # Either one [\t\r\n\f\v] and zero or more ws, 129 | | \s{2,} # or two or more consecutive-any-whitespace. 130 | ) # Note: The remaining regex consumes no text at all... 131 | (?= # Ensure we are not in a blacklist tag. 132 | [^<]*+ # Either zero or more non-"<" {normal*} 133 | (?: # Begin {(special normal*)*} construct 134 | < # or a < starting a non-blacklist tag. 135 | (?!/?(?:textarea|pre)\b) 136 | [^<]*+ # more non-"<" {normal*} 137 | )*+ # Finish "unrolling-the-loop" 138 | (?: # Begin alternation group. 139 | < # Either a blacklist start tag. 140 | (?>textarea|pre)\b 141 | | \z # or end of file. 142 | ) # End alternation group. 143 | ) # If we made it here, we are not in a blacklist tag. 144 | %Six'; 145 | 146 | $compressed = preg_replace($regEx, ' ', $html); 147 | 148 | if ($compressed !== null) { 149 | $html = $compressed; 150 | } 151 | 152 | return trim($html); 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /src/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 17 | $this->publishes([ 18 | __DIR__ . '/Config/config.php' => config_path('loading.php'), 19 | ], 'loading.config'); 20 | } 21 | } 22 | 23 | /** 24 | * Register package services. 25 | * 26 | * @return void 27 | */ 28 | public function register() 29 | { 30 | $this->mergeConfigFrom(__DIR__ . '/Config/config.php', 'loading'); 31 | } 32 | } 33 | --------------------------------------------------------------------------------