Skip to content

Instantly share code, notes, and snippets.

View zaichaopan's full-sized avatar
🎯
Focusing

zaichaopan

🎯
Focusing
View GitHub Profile
@zaichaopan
zaichaopan / try-catch.ts
Created May 14, 2025 23:51 — forked from t3dotgg/try-catch.ts
Theo's preferred way of handling try/catch in TypeScript
// Types for the result object with discriminated union
type Success<T> = {
data: T;
error: null;
};
type Failure<E> = {
data: null;
error: E;
};
@zaichaopan
zaichaopan / inactivity.js
Created March 14, 2021 17:57 — forked from gerard-kanters/inactivity.js
Inactivity timeout javascript
<script type="text/javascript">
function idleTimer() {
var t;
//window.onload = resetTimer;
window.onmousemove = resetTimer; // catches mouse movements
window.onmousedown = resetTimer; // catches mouse movements
window.onclick = resetTimer; // catches mouse clicks
window.onscroll = resetTimer; // catches scrolling
window.onkeypress = resetTimer; //catches keyboard actions
@zaichaopan
zaichaopan / modal.blade.php
Created May 29, 2019 01:39 — forked from laracasts/modal.blade.php
Modals with Zero JavaScript
<div id="{{ $name }}" class="overlay">
<a href="#" class="cancel"></a>
<div class="modal">
{{ $slot }}
<a href="#" class="close">&times;</a>
</div>
</div>
@zaichaopan
zaichaopan / cookie.js
Created April 5, 2019 16:03 — forked from eugene-ilyin/cookie.js
Work with cookies in native Javascript
<template>
<div v-click-outside="close">
<label v-if="label" class="form-label" @click="open">{{ label }}:</label>
<div class="relative">
<div ref="input" @click="open" @focus="open" @keydown.down.prevent="open" @keydown.up.prevent="open" :class="{ focus: show, error: error }" class="form-input pr-6" :tabindex="show ? -1 : 0">
<slot v-if="value"></slot>
<div v-else-if="data.length === 0" class="text-grey-600">No options found</div>
<div v-else class="text-grey-600">Click to choose…</div>
</div>
<button type="button" @click.stop="clear" v-if="value" tabindex="-1" class="absolute p-2 inline-block text-grey-500 hover:text-amber-700" style="right: 4px; top: 8px;">
@zaichaopan
zaichaopan / AppServiceProvider.php
Created November 19, 2018 06:24 — forked from reinink/AppServiceProvider.php
Eloquent addSubSelect()
<?php
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot()
@zaichaopan
zaichaopan / User.php
Created November 19, 2018 06:24 — forked from reinink/User.php
Add "computed attributes" support to Eloquent models
<?php
namespace App;
use App\Scopes\WithSelects;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
@zaichaopan
zaichaopan / AppServiceProvider.php
Created November 19, 2018 06:22 — forked from reinink/AppServiceProvider.php
Multi-page Vue App in Laravel
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\Factory as ViewFactory;
class AppServiceProvider extends ServiceProvider
{
@zaichaopan
zaichaopan / VueForm.vue
Created November 19, 2018 06:13 — forked from calebporzio/VueForm.vue
A Vue component to include Laravel's CSRF Token within form tag
// Usage:
// <vue-form method="PUT">
// <input type="text" name="email">
// <input type="submit">
// </vue-form>
<template>
<form :method="method.toUpperCase() == 'GET' ? 'GET' : 'POST'">
<input-hidden :value="csrfToken" name="_token"/>
@zaichaopan
zaichaopan / InputHidden.vue
Created November 19, 2018 06:12 — forked from calebporzio/InputHidden.vue
A Vue component for turning json into hidden input elements
// Usage:
// <input-hidden name="users" :value="[{name: 'Caleb'}, {name: 'Daniel'}]"/>
// (value can be a: string, integer, array, object)
//
// will render:
// <input class="hidden" type="text" name="users[0][name]" value="Caleb">
// <input class="hidden" type="text" name="users[1][name]" value="Daniel">
<script>