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
gulp = require 'gulp'
gutil = require 'gulp-util'
sass = require 'gulp-sass'
prefix = require 'gulp-autoprefixer'
coffee = require 'gulp-coffee'
coffeelint = require 'gulp-coffeelint'
concat = require 'gulp-concat'
plumber = require 'gulp-plumber'
changed = require 'gulp-changed'
uglify = require 'gulp-uglify'
@lmartins
lmartins / pad.coffee
Created February 22, 2014 10:16
Add leading zeros to a number if applicable
pad = (num) ->
('0'+num).slice(-2)
console.log pad(8)
console.log pad(12)
@lmartins
lmartins / application.coffee
Created February 25, 2014 12:45
Load application javascript code based on the current view/controller http://viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution
# http://viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution
SITENAME =
common:
init: ->
# application-wide code
users:
init: ->
@lmartins
lmartins / Module.coffee
Created March 7, 2014 20:01
Module Pattern in CoffeeScript
Module = (->
_privateMethod = ->
console.log "Some text from the private method, called from the public method"
publicMethod = ->
console.log "something from the public method"
_privateMethod()
# métodos públicos que são expostos através do módulo
@lmartins
lmartins / protoObject.coffee
Last active August 29, 2015 13:57
Create Object passing it's prototype
# Creates a new object to be associated with the same prototype from the object
# passed in the constructor function
if typeof Object.create isnt 'function'
Object.create = (o) ->
F = ->
F:: = o
return new F()
newObject = Object.create(originalObject)
@lmartins
lmartins / SelfInvokingFunction.coffee
Last active August 29, 2015 13:57
Quick Self-invokinf functions in Coffeescript
#If you need to declare a self calling function in CoffeeScript, it's super easy, just do it:
message = "but stay safe"
do ->
message = "take chances"
alert message
alert message
# Outputs "takes chances" then "but stay safe"
# This gets super useful when you need to freeze a variable, like when in a for loop and using setTimeout, just pass in the variable:
@lmartins
lmartins / ExtendObjects.coffee
Last active August 29, 2015 13:57
Extend Objects with CoffeeScript
extend = (target) ->
return unless arguments[1]
for property in arguments
sourceProp = property
for prop of sourceProp
if not target[prop] and sourceProp.hasOwnProperty(prop)
target[prop] = sourceProp[prop]
return
@lmartins
lmartins / Mixin.coffee
Last active August 29, 2015 13:57
Mixin Pattern in CoffeeScript
# MIXIN PATTERN IN COFFEESCRIPT
mixin = (target, source, methods...) ->
for method in methods
target[method] = source[method].bind(source)
return
# usage:
mixin toggle, toolbar.items[0], 'toggleActiveState'
@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) ->