Skip to content

Instantly share code, notes, and snippets.

View dalgard's full-sized avatar

Kristian Dalgård dalgard

  • Various Productions
  • Aarhus, Denmark
View GitHub Profile
@dalgard
dalgard / custom-fields.js
Last active September 22, 2017 07:08
(Meteor) Customizing field layout in useraccounts:semantic-ui with the use of aldeed:template-extension
// Adding some simplistic fields
AccountsTemplates.addFields([
{
_id: "address",
type: "text",
// Options object with custom properties for my layout
options: {
// Put a divider before this field
dividerBefore: true
@dalgard
dalgard / _checkbox_method.scss
Last active February 16, 2016 21:45
Sass mixin to help put 'the checkbox hack' (which I rather consider a 'technique') to good use. This version requires a media mixin, e.g. from Neat
@mixin toggled($selector, $media...) {
#{unquote($selector)}:checked ~ & {
@if length($media) > 0 {
@include media($media...) {
@content;
}
}
@else {
@content;
}
@dalgard
dalgard / extendDeep.js
Created April 26, 2014 15:24
Method for deep (recursive) extension of a given object with the properties of passed-in object(s) with support for standards-compliant getters and setters
function extendDeep(target) {
// Run through rest parameters
Array.prototype.slice.call(arguments, 1).forEach(function (source) {
if (typeof target === "object") {
if (typeof source === "object") {
// If source is an array, only copy enumerable properties
var keys = (Array.isArray(source) ? Object.keys(source) : Object.getOwnPropertyNames(source));
// Iterate over keys
keys.forEach(function (key) {
@dalgard
dalgard / _icons.scss
Created March 17, 2014 11:10
Use placeholder selectors for icons
// Use embedded fonts
@font-face {
font-family: 'icons';
font-weight: normal;
font-style: normal;
src:
url(data:application/x-font-ttf;charset=utf-8;base64,...) format('truetype'),
url(data:application/font-woff;charset=utf-8;base64,...) format('woff');
}
@dalgard
dalgard / Class.js
Last active August 29, 2015 13:56
Extensible constructor pattern (ES5 version) inspired by Backbone.js – NOTE: Using `this.super` multiple times in the inheritance chain creates a problem since it results in a loop. There hasn't yet been an elegant solution to this, but a proper `super` keyword will be supported in ES6. In the meantime, coupling with the super's name can be avoi…
function Class(properties) {
// This base constructor can be left empty, but a nice boilerplate might look like this
if (properties) {
// Add properties to the instance
Object.getOwnPropertyNames(properties).forEach(function (property_name) {
var descriptor = Object.getOwnPropertyDescriptor(properties, property_name);
Object.defineProperty(this, property_name, descriptor);
}, this);
}
}
@dalgard
dalgard / promiseBoilerplate.js
Last active August 29, 2015 13:56
Module/function boilerplate using promises
/*
Module boilerplate (jQuery)
- Pass in a callback OR a promise
- Always returns a promise
*/
function getStuff(arg, d) {
var callback = d, // Save possible callback
is_promise = d && d.resolve; // Check whether d is a promise
@dalgard
dalgard / _link_states.scss
Last active January 26, 2017 09:09
A Sass mixin for quickly setting link states – test it on http://sassmeister.com/ (see more examples in the comments)
@dalgard
dalgard / createOptions.js
Last active December 31, 2015 18:49
Sync some form controls with an object (http://codepen.io/dalgard/full/CkbKw)
/*
This trick is a lazy way of two-way sync'ing some form controls with an options object.
Actually, the "sync'ing" consists in getting the values when needed and updating the fields
when changing something. It has its limits, but may work quite nicely.
Depends on: jQuery/Zepto
The code below works for checkboxes (boolean), text input (string), number input (number),
select lists (string) and json in textarea (object)
@dalgard
dalgard / matchesSelector.js
Last active February 20, 2018 22:47
Cross-browser wrapper for element.matchesSelector
function matchesSelector(dom_element, selector) {
var matchesSelector = dom_element.matches || dom_element.matchesSelector || dom_element.webkitMatchesSelector || dom_element.mozMatchesSelector || dom_element.msM atchesSelector || dom_element.oMatchesSelector;
return matchesSelector.call(dom_element, selector);
}
@dalgard
dalgard / queryAll.js
Created December 6, 2013 01:55
Efficient wrapper for document.querySelectorAll
// Always return an array of DOM elements
function queryAll(selector) {
var id_sel = selector.match(/^#([\w-]*)$/),
class_sel = !id_sel && selector.match(/^\.([\w-]+)$/),
tag_sel = !class_sel && selector.match(/^[\w-]+$/);
if (id_sel) {
var elem = document.getElementById(id_sel[1]);
return (elem ? [elem] : []); // Always return an array
}