Skip to content

Instantly share code, notes, and snippets.

View adamcrampton's full-sized avatar
💪
Crushing it

Adam Crampton adamcrampton

💪
Crushing it
  • Sydney, Australia
View GitHub Profile
@adamcrampton
adamcrampton / sidebarToggleActions.js
Created September 12, 2019 02:32
jQuery - Toggle locking the page position and grey out background when sidebar drawer opens
// This is for use with AdminLTE, but the same stuff can be applied to other drawer plugins.
$(document).ready(function() {
// Lock the page when drawer is open.
$('[data-toggle="control-sidebar"]').on('expanded.controlsidebar', sidebarOpenActions);
// Unlock.
$('[data-toggle="control-sidebar"]').on('collapsed.controlsidebar', sidebarCloseActions);
// Allow Esc to close the drawer.
$(document).keyup(function(e) {
@adamcrampton
adamcrampton / fake_progress_bar.html
Created September 18, 2019 03:58
Progress bar that increments itself via a loop
<div id="progress-bar"><div style="width:0;"></div>
<script>
let progressBar = document.querySelector("#progress-bar div")
let items = [];
for (let index = 0; index <= 100; index++) {
items.push(index);
}
@adamcrampton
adamcrampton / mysql_import.sh
Created September 23, 2019 03:42
MySQL command line import
#!/bin/bash
# Argument is path + filename of .sql file dump.
mysql database_name -u user_name -p --host=127.0.0.1 --port=33060 < $1
@adamcrampton
adamcrampton / ttlArray.php
Created September 30, 2019 02:28
Handy array useful for configuring cache TTLs etc
<?php
/*
|--------------------------------------------------------------------------
| Cache TTL
|--------------------------------------------------------------------------
|
| Set commonly used TTL values.
|
*/
return [
@adamcrampton
adamcrampton / jqueryAjaxPostBoilerplate.js
Last active March 13, 2020 05:10
Boilerplate for jQuery AJAX POST request
const endpoint = 'https://api.some.endpoint.test';
const formId = 'this-form-id-value';
const data = new FormData(document.getElementById('formId'));
$.ajax({
async: true,
contentType: false,
processData: false,
url: endpoint,
method: 'POST',
@adamcrampton
adamcrampton / ajaxFullPageSpinner.html
Created May 7, 2020 23:59
Full page CSS spinner useful for pages with AJAX
<div class="loading hidden"><i class="fa fa-spin fa-circle-o-notch" style="font-size: 48px;"></i></div>
<script type="text/javascript">
const formData = new FormData();
formData.append('key', 'value');
$.ajax({
async: true,
contentType: false,
processData: false,
@adamcrampton
adamcrampton / JsonCheck.php
Created May 26, 2020 06:50
Determine if PHP string is valid JSON
<?php
/**
* Checks if string is valid JSON.
*
* @param string $string
* @return bool
*/
public function jsonCheck($string)
{
@adamcrampton
adamcrampton / youTubeDurationParser.php
Created June 22, 2020 23:01
Parse YouTube duration string to seconds integer
<?php
/**
* Match pattern PT#H#M#S (leading PT, hours, minutes, seconds) and return seconds value.
*
* @param string $string
* @return int
*/
private static function convertDuration($string)
{
// Comprehensive regex that should match any value.
@adamcrampton
adamcrampton / loading.html
Created August 11, 2020 22:32
Loading overlay with spinner
<style>
#overlay {
background: #000;
color: #fff;
position: fixed;
height: 100%;
width: 100%;
z-index: 5000;
top: 0;
left: 0;
@adamcrampton
adamcrampton / Handler.php
Last active November 4, 2023 07:55
Custom error handling redirects for Laravel (App\Exceptions\Handler)
<?php
namespace App\Exceptions;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Throwable;
class Handler extends ExceptionHandler