Skip to content

Instantly share code, notes, and snippets.

View lmartins's full-sized avatar

Luis Martins lmartins

  • Goodwin Media, Multiweb
  • Portugal
View GitHub Profile
@lmartins
lmartins / Decorator.coffee
Created March 30, 2014 14:00
Decorator Pattern with CoffeeScript
class Book
constructor: (@author, @title, @numCopies) ->
display: ->
console.log """Book
\t Author #{@author}
\t Title #{@title}
\t Copies #{@numCopies}
"""
@lmartins
lmartins / GetClickPosition.coffee
Created April 11, 2014 11:58
Get Click Position
getClickPosition = (e) ->
parentPosition = getPosition e.currentTarget
xPos = e.clientX - parentPosition.x
yPos = e.clientY - parentPosition.y
# console.log "X:#{xPos} Y:#{yPos}"
getPosition = (element) ->
xPos = 0
yPos = 0
while (element)
@lmartins
lmartins / DOM - Class Utilities.coffee
Created April 11, 2014 14:22
Extend JS native class manipulation to add multiple CSS classes
DOMTokenList::addClasses = (input) ->
classValues = input.split ' '
for className in classValues
this.add className
return
# usage
# element.classList.addClasses 'xpto1 xpto2'
DOMTokenList::removeClasses = (input) ->
@lmartins
lmartins / Debounce.coffee
Created April 11, 2014 15:06
Debounce function execution in CoffeeScript
# Based on:
# http://unscriptable.com/2009/03/20/debouncing-javascript-methods/
debounce = (func, threshold, execAsap) ->
timeout = null
(args...) ->
obj = this
delayed = ->
func.apply(obj, args) unless execAsap
timeout = null
@lmartins
lmartins / forLoops.coffee
Created April 22, 2014 15:43
CoffeeScript For loops
# Reference: http://discontinuously.com/2012/05/iteration-in-coffeescript/
for element in arr
console.log element
# code here
# Expose the index in an Array Comprehension
for element, index in arr
console.log element
# code here
'use strict';
var gulp = require('gulp'),
gutil = require('gulp-util'),
sass = require('gulp-sass'),
prefix = require('gulp-autoprefixer'),
coffee = require('gulp-coffee'),
coffeelint = require('gulp-coffeelint'),
component = require('gulp-component'),
componentcoffee = require('component-coffee'),
# Swappable Mixins in CoffeeScript
# ================================
# Many thanks to Hashmal, who wrote this to start.
# https://gist.github.com/803816/aceed8fc57188c3a19ce2eccdb25acb64f2be94e
# Usage
# -----
# class Derp extends Mixin
@lmartins
lmartins / viewport.coffee
Created May 19, 2014 13:48
Check if an element is in Viewport
isInViewport = (el) ->
rect = el.getBoundingClientRect()
html = document.documentElement
return(
rect.top >= 0 and
rect.left >= 0 and
rect.bottom <= (window.innerHeight or html.clientHeight) and
rect.right <= (window.innerWidth or html.clientWidth)
)
@lmartins
lmartins / main.coffee
Created May 19, 2014 13:52
Passing objects to addEventListener. Here’s a super awesome trick I had no idea about until someone pointed out you could do this. addEventListener can take an object as a second argument that will look for a method called handleEvent and call it! No need for binding “this” so it will pass around the context correctly, the context is the object …
object =
init: ->
btnEl = document.querySelector 'button'
btnEl.addEventListener 'click', this
btnEl.addEventListener 'touchstart', this
handleEvent: (e) ->
switch e.type
when 'click'
@action(e.type)
@lmartins
lmartins / main.js
Created May 31, 2014 16:40
Get Elements by Data-Attribute
// Define a getElementsByAttribute function. It
// takes an attribute name string and an optional
// matching value. It calls walk_the_DOM, passing it a
// function that looks for an attribute name in the
// node. The matching nodes are accumulated in a
// results array.
var getElementsByAttribute = function (att, value) {
var results = [];
walkTheDom(document.body, function(node){