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 / 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 / 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 / validationErrorMacro.php
Last active August 29, 2019 06:42
Laravel macro for returning validation errors to front end using Form class
<?php
/**
* Bootstrap macros in Service Provider.
*
* @return void
*/
public function boot()
{
// Build form error macro to easily set inline error labels.
Form::macro('errorMessage', function($field) {
@adamcrampton
adamcrampton / setElementValueFromLocalStorage.js
Created August 29, 2019 02:31
Set a jQuery element's value using a specified key in local storage
// Fetch storage item if it exists and set element value
function updateFieldValueFromStorage(elementId, storageKey) {
let storage = window.localStorage;
if (storage.getItem(storageKey)) {
($('#' + elementId).val(storage.getItem(storageKey)));
}
}
@adamcrampton
adamcrampton / setElementValueInLocalStorage.js
Last active August 29, 2019 02:27
Sets a jQuery input element value in local storage
// Set field value for element if it has a value
function setFieldValueInStorage(elementId, storageKey) {
if (window.localStorage) {
let storage = window.localStorage;
// If no value, remove item. Otherwise store.
if ($('#' + elementId).val()) {
storage.setItem(storageKey, $('#' + elementId).val());
} else {
storage.removeItem(storageKey);
@adamcrampton
adamcrampton / toTitleCase.js
Created August 29, 2019 02:19
Convert JavsScript string to Title Case
function toTitleCase(string) {
return string.replace(
/\w\S*/g,
function(text) {
return text.charAt(0).toUpperCase() + text.substr(1).toLowerCase();
}
);
}
@adamcrampton
adamcrampton / jQueryBuildOption.js
Created August 29, 2019 02:17
Dynamically build jQuery select options
function buildOption(value, text, selected) {
return $("<option/>", {
value: value,
text: text,
selected: selected
});
}
// Usage:
// $('#myselect').append(buildOption(key, value, selected));
@adamcrampton
adamcrampton / setHashKeys.php
Last active August 22, 2019 03:13
Loops through a nested array and adds a hash key for each data set
<?php
/**
* Generates a hash for each nested array in the data set.
* Sets itself as the parent key - useful for comparisons.
*
* @param array $dataArray
* @param array $preserveFields Fields to return but not include in hash
* @return array $updatedArray
*/
private function setHashKeys(array $dataArray, array $preserveFields = [])
@adamcrampton
adamcrampton / set_laravel_permissions.sh
Last active August 14, 2019 00:18
Simple bash script for setting ownership and permissions for a Laravel project
echo Enter project directory name
read dirname
echo Setting permissions for $dirname...
sudo chown -R $USER:www-data /var/www/$dirname/storage
sudo chown -R $USER:www-data /var/www/$dirname/bootstrap/cache
chmod -R 775 /var/www/$dirname/storage
chmod -R 775 /var/www/$dirname/bootstrap/cache
@adamcrampton
adamcrampton / getImageMimeType.php
Last active July 11, 2019 23:23
Returns image Mime type
<?php
/**
* @param string $imageUrl
* @return string|bool
*/
public function getImageMimeType($imageUrl) {
$mimeTypes = [
IMAGETYPE_GIF => "image/gif",
IMAGETYPE_JPEG => "image/jpg",