Skip to content

Instantly share code, notes, and snippets.

@everdimension
everdimension / on_scroll_visible.js
Created August 14, 2015 12:57
Add event listener to the `scroll` event to check if the element is fully visible within the window viewport and do something when it is. Use debounce function to not abuse the cpu.
function debounce (fn, wait) {
var timeout;
return function() {
var cntx = this;
var args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function() {
timeout = null;
@everdimension
everdimension / es6_classes.js
Last active March 5, 2020 07:59
Explanation of ecmascript 6 (ES2015) classes, class inheritance, super calls and static methods. Compiled online on babel site: https://goo.gl/vfdkpC
class BaseClass {
constructor(name, age) {
this.name = name;
this.age = age;
}
static getDescription() {
return "this function can be called without instantiating BaseClass";
}
getData() {
return ["base data is:", this.name, this.age].join(' ');
@everdimension
everdimension / formdata.appenObject.js
Created October 26, 2015 18:01
Append nested object to FormData. The object can be just one level deep. For anything more complex it is recommended to rethink the way you send such data to the server.
(function() {
'use strict';
if (window.FormData) {
FormData.prototype.appendObject = function (obj, namespace) {
// EXAMPLE:
// var person = { name: 'some name', age: 87 };
// var fd = new FormData();
// fd.appenObject(obj, 'person');
@everdimension
everdimension / parse_search.js
Created December 3, 2015 14:59
A function to parse search string params into javascript key-value object.
// sample data for trying
var paramsString = "?lunch=sandwich&dinner=stirfry";
console.log(parseParams(paramsString));
//
// main function
//
function parseParams(str) {
return str
.replace(/(^\?)/, '')
@everdimension
everdimension / outside_click.js
Last active March 11, 2023 15:20
Listener for clicks outside the element, for example, for closing menus on outside click.
var el = document.getElementById('el');
document.addEventListener('click', outsideEvtListener);
function outsideEvtListener(evt) {
if (evt.target === el || el.contains(evt.target)) {
return;
}
// code handling outside click
@everdimension
everdimension / redux-api_failing_test.js
Last active January 28, 2016 15:49
Test code demonstrating that two stores created using redux-api fail to listen to the same action.
// DrawerApi.js
// ** code from demo ommitted **
//
// AnotherApi.js
import Api from '../src/redux-apis';
export default class AnotherApi extends Api {
constructor(state = { drawerIsOpened: false }) {
super(state);
@everdimension
everdimension / svg-base-fix.js
Last active February 15, 2016 12:27
An angular directive making svg's work when <base href="/path"> tag is present.
angular.module('app.directives.svgBaseFix', [])
.directive('svgBaseFix', function($rootScope) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var attr = 'xlinkHref';
var initialUrl = attrs[attr];
var parsingNode = document.createElement('a');
class Store {
constructor(reducer, initialState) {
this.reducer = reducer;
this.state = this.reducer(initialState, {});
console.log('set state', this.state);
this.listeners = [];
}
getState() {
return this.state;
@everdimension
everdimension / accessibility_outline.js
Created November 11, 2016 22:38
A simple trick (subject to improvement) to hide outline until the user starts interacting with the site with the keyboard.
/* global document */
import { KEY_CODES } from './data/constants';
export default function initTabSpy() {
document.body.classList.add('isUsingPointer');
document.addEventListener('keydown', (evt) => {
if (evt.which === KEY_CODES.TAB) { // or may be any keydown?
document.body.classList.remove('isUsingPointer');
}
});
class MyForm extends React.Component {
constructor() {
super();
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event) {
event.preventDefault();
const data = new FormData(event.target);