Skip to content

Instantly share code, notes, and snippets.

View Alex1990's full-sized avatar
🐢
Get Things Done!

Alex Chao Alex1990

🐢
Get Things Done!
View GitHub Profile
@Alex1990
Alex1990 / dev-set.js
Created May 10, 2016 21:22
A set function works in development environment.
/**
* A set function works in development environment.
*/
function set(vari, value) {
if (ENV === 'dev') {
vari = value;
}
}
@Alex1990
Alex1990 / pre-commit-lint.json
Last active July 11, 2016 10:30
Run eslint before `git commit` with npm scripts and `pre-commit` package.
{
"scripts": {
"lint-legacy": "files=$(git diff --cached --name-only | grep -v '.jsx$' | grep '.js$' | tr '\\n' ' '); if [[ -n \"$files\" ]]; then eslint --config ./.eslintrc.legacy.json --quiet $files || (git reset HEAD; exit 1); else exit 0; fi",
"lint-base": "files=$(git diff --cached --name-only | grep '.jsx$' | tr '\\n' ' '); if [[ -n \"$files\" ]]; then eslint --config ./.eslintrc.base.json --quiet $files || (git reset HEAD; exit 1); else exit 0; fi",
"lint": "npm run lint-legacy && npm run lint-base",
"lint-all": "(eslint --config ./.eslintrc.base.json --ext .jsx --quiet ./) && (eslint --config ./.eslintrc.legacy.json --ext .js --quiet ./)",
"build": "gulp build"
},
"pre-commit": [
"lint"
@Alex1990
Alex1990 / scrollable.js
Last active July 19, 2019 02:35
Stop propagation for scroll/mousewheel
/**
* Stop propagation behavior for scroll of the element but body.
* Compatibility: IE9+
* Ref:
* - http://stackoverflow.com/questions/5802467/prevent-scrolling-of-parent-element#answer-16324762
* - https://developer.mozilla.org/en-US/docs/Web/Events/wheel
*/
;(function ($) {
$.fn.scrollable = function () {
this.on('wheel', function (event) {
@Alex1990
Alex1990 / post-merge
Created March 21, 2016 06:52 — forked from sindresorhus/post-merge
git hook to run a command after `git pull` if a specified file was changed. In this example it's used to run `npm install` if package.json changed and `bower install` if `bower.json` changed. Run `chmod +x post-merge` to make it executable then put it into `.git/hooks/`.
#/usr/bin/env bash
# MIT © Sindre Sorhus - sindresorhus.com
# git hook to run a command after `git pull` if a specified file was changed
# Run `chmod +x post-merge` to make it executable then put it into `.git/hooks/`.
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
check_run() {
echo "$changed_files" | grep --quiet "$1" && eval "$2"
@Alex1990
Alex1990 / closestDescendant.js
Last active February 17, 2016 08:42
An jQuery function to get the closest descendant element which matches the specified selector.
/**
* closestDescendant
* An jQuery function to get the closest descendant element which matches the specified selector.
* The search strategy is breadth-first.
*/
;(function () {
$.fn.closestDescendant = function (selector) {
var $elem = $();
this.each(function () {
/**
* Transfrom the keys of an object by an filter object, then return a new object.
* ToDo: support deep transforming.
*/
function transKeys(src, filter) {
var dest = {};
var keys = Object.keys(filter);
for (var p in src) {
if (src.hasOwnProperty(p)) {
/**
* Add `:tabbable` selector. (From jQuer UI)
* focusable: https://gist.github.com/Alex1990/f086a23bca98672486ca
*/
$.extend($.expr[':'], {
tabbable: function(element) {
var tabIndex = $(element).attr('tabindex');
var hasTabIndex = tabIndex != null;
return (!hasTabIndex || tabIndex > 0) && $.expr[':'].focusable(element);
}
/**
* Add `:focusable` selector. (From jQuery UI)
*/
$.extend($.expr[':'], {
focusable: function(element) {
var hasTabIndex = $.attr(element, 'tabindex') != null;
var nodeName = element.nodeName.toLowerCase();
return (/^input|textarea|select|button$/.test(nodeName)
? !element.disabled
: ('a' === nodeName
@Alex1990
Alex1990 / safeActiveElement.js
Last active December 20, 2019 09:54
Get the current active element safely.
/**
* Get the current active element safely.
* Ref: https://github.com/jquery/jquery-ui/blob/2b84531ae9331f60e4d739fabca6d78abde89ae1/ui/safe-active-element.js
*/
function safeActiveElement(doc) {
doc = doc || document;
var activeElement;
// Support: IE 9 only
// IE9 throws an "Unspecified error" accessing document.activeElement from an <iframe>
@Alex1990
Alex1990 / scrollbarWidth.js
Last active November 19, 2015 08:59
Get the scrollbar width.
/**
* Get the scorllbar's width.
* Note: This script only works on the default zoom level.
* Ref: https://github.com/twbs/bootstrap/blob/94b4076dd2efba9af71f0b18d4ee4b163aa9e0dd/js/modal.js#L277-L284
*/
function scrollbarWidth() {
var scrollDiv = document.createElement('div');
scrollDiv.style.cssText = 'position:absolute;left:-9999px;width:50px;height:50px;overflow:scroll;';
document.body.appendChild(scrollDiv);
var width = scrollDiv.offsetWidth - scrollDiv.clientWidth;