Skip to content

Instantly share code, notes, and snippets.

View warrendodsworth's full-sized avatar

Warren Dodsworth warrendodsworth

View GitHub Profile
@warrendodsworth
warrendodsworth / lastKnownTab.js
Last active October 13, 2015 23:48
Recovering the last known Tab (Bootstrap Tabs) on returning to the page using localStorage, can use session storage too
// Bootstrap Tab - Return to Last Known on page refresh or back/forward movement.
// Using data- attrs to identify tab groups to enable use multiple times on a page
$( '[data-tab-group]' ).each( function ( i, o ) {
var tabGroup = $( o );
var lastKnownTabId = tabGroup.data( 'tab-group' ); // Tab Group Id, from the data- attr
var lastKnownTab = localStorage.getItem( lastKnownTabId );
//Set last known tab back on script load - specific to this Tab Group Id
tabGroup.find( ' a[href="#' + lastKnownTab + '"]' ).tab( 'show' );
@warrendodsworth
warrendodsworth / validationSummary.js
Last active October 13, 2015 23:47
Validation Summary from Web Api 2 BadRequest response- Format
// Read and Format Error messages
// Using custom ajax request
// You may need to get to modelState differently depending on how you make your ajax call
// Attached to the jQuery namespace using $.summary = function
$.summary = function ( res ) {
var errors = [], validationSummary;
// eg - res.data.modelState
@warrendodsworth
warrendodsworth / queryString.js
Created October 13, 2015 23:49
Javascript Query string reader - attached to jQuery namespace
//Query string reader function attacehd to jQuery namespace
$.qs = function ( name ) {
name = name.replace( /[\[]/, "\\[" ).replace( /[\]]/, "\\]" );
var regex = new RegExp( "[\\?&]" + name + "=([^&#]*)" );
var results = regex.exec( location.search );
return results == null ? '' : decodeURIComponent( results[1].replace( /\+/g, " " ) );
@warrendodsworth
warrendodsworth / cookie.js
Created October 14, 2015 00:15
Google Analytics Cookie Reading Snippet - attached to jQuery namespace
@warrendodsworth
warrendodsworth / dovnload.v2.js
Last active January 27, 2016 03:25
Download specific images off a webpage using JQuery
//Incomplete
$('div a.grid-image').map(function (i, link){
$('body').append('<canvas/>');
var canvas = $('canvas')[i];
var ctx = canvas.getContext('2d');
var img = new Image();
img.crossOrigin = 'anonymous';
img.src = '...';
img.onload = function(){
@warrendodsworth
warrendodsworth / render_razor_view_to_string
Created June 22, 2017 23:38 — forked from grishin/render_razor_view_to_string
How to render razor view to string
protected string RenderRazorViewToString(string viewName, object model)
{
ViewData.Model = model;
using (var sw = new StringWriter()) {
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
return sw.GetStringBuilder().ToString();
}
<script>
// to dynamically set base url by reading the virtual servers path from the url
// doesn't work perfectly, causes double load as the first set of script calls fire to the base / url without the vd, then the second set go to the vd url (runtime.js etc)
let p = location.pathname.split('/').filter(x => x);
let baseHref = '/';
if (!location.origin.includes('localhost:4200')) {
baseHref += p[0] + '/' + p[1] + '/' + p[2] + '/';
}
console.log('baseHref', baseHref);
@warrendodsworth
warrendodsworth / luxon-date-adapter.ts
Last active April 1, 2020 21:12 — forked from maburdi94/luxon-date-adapter.ts
Angular Date Adapter for Luxon - for Angular 9
import { Inject, Injectable, InjectionToken, LOCALE_ID, Optional } from '@angular/core';
import { DateAdapter } from '@angular/material/core';
import { DateTime, Info } from 'luxon';
export const DATE_LOCALE = new InjectionToken<string>('DATE_LOCALE');
/** Provider for MAT_DATE_LOCALE injection token. */
export const DATE_LOCALE_PROVIDER = { provide: DATE_LOCALE, useExisting: LOCALE_ID };
const ISO_8601_REGEX = /^\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|(?:(?:\+|-)\d{2}:\d{2}))?)?$/;