Skip to content

Instantly share code, notes, and snippets.

View EtienneLem's full-sized avatar

Etienne Lemay EtienneLem

View GitHub Profile
@EtienneLem
EtienneLem / ajax_worker.js
Created June 10, 2013 19:40
Ajax Web Worker
var handleRequest = function(data) {
postMessage(data)
}
addEventListener('message', function(e) {
var xhr = new XMLHttpRequest
xhr.open('GET', e.data)
xhr.onreadystatechange = function(e) {
if (xhr.readyState === 4 && xhr.status === 200) {
handleRequest(xhr.responseText)
@EtienneLem
EtienneLem / .zshrc
Last active March 11, 2021 16:30
Homebrew, CocoaPods & rbenv ARM64 / X86_64 configuration
# Homebrew
case $(uname -m) in
"arm64")
export PATH="/opt/homebrew/bin:$PATH"
export RBENV_ROOT="/opt/homebrew/opt/rbenv"
;;
"x86_64")
export PATH="/usr/local/bin:$PATH"
export RBENV_ROOT="~/.rbenv"
;;
@EtienneLem
EtienneLem / Rakefile
Created October 18, 2012 15:27
Sinatra (javascript|stylesheet)_include_tag helper w/ Rakefile tasks
namespace :assets do
# `bundle exec rake assets:compile`
# * Compile stylesheets and javascripts
desc 'compile assets'
task :compile => [:compile_css, :compile_js] do
end
# `bundle exec rake assets:compile_css`
# IN => /app/assets/stylesheets/styles.styl
# OUT => /public/css/styles-<version>.min.css
@EtienneLem
EtienneLem / 1 - helper.rb
Created September 12, 2013 18:14
Responsive images container (Bonus: The content flow stays the same whether the image is loaded or not (or broken))
def image_container(path, width, height)
%(<div class="image-container" style="max-width: #{width}px">
<div style="padding-top: #{(height.to_f / width.to_f) * 100}%">
#{image_tag(path)}
</div>
</div>).html_safe
end
# Note: If you want the image to scale up, remove the `max-width`
@EtienneLem
EtienneLem / 1 - add_at_index.js
Created September 3, 2013 19:17
$('div').add_at_index(content, index) adds an element at a given index.
// Works with Zepto/jQuery
$.fn.add_at_index = function(content, index) {
if (index < 0) { index = 0 }
this.each(function() {
var $this = $(this)
var $child = $this.children().eq(index)
if ($child.length) { return $child.before(content) }
$this.append(content)
@EtienneLem
EtienneLem / 1 - one_for_all.js
Created September 3, 2013 19:03
$('a').one('click', callback) is executed at most once *per element*. $('a').one_for_all('click', callback) is executed at most once… *for all elements*.
// Works with Zepto/jQuery
$.fn.one_for_all = function(type, callback) {
var $all = $(this)
$all.on(type, function(e) {
$all.off(type)
callback(e)
})
}
@EtienneLem
EtienneLem / application_helper.rb
Created June 19, 2013 17:35
The best way to code bookmarlets (with Rails)
def javascript_content(file_name)
Rails.application.assets[file_name].source.html_safe
end
@EtienneLem
EtienneLem / application.erb
Last active December 17, 2015 06:59
Modular DOMReady example (Rails)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Modular DOMReady</title>
</head>
<body>
<header></header>
<section role="main">
@EtienneLem
EtienneLem / app.coffee
Last active December 16, 2015 09:59
Custom Deferrer. It doesn’t support errors, because, you know, fix them… You shouldn’t code errors to begin with.
doThis = (deferrer) ->
# do some JS nifty things
deferrer.done('foo', 'bar')
doThat = (deferrer) ->
# Have callbacks/timeouts/async? Sure!
setTimeout ->
deferrer.done('foozle', 'barzle')
, 5000
@EtienneLem
EtienneLem / staticmap.rb
Created April 1, 2013 20:24
Static Google Maps Image
require 'uri'
address = '335 Rue Saint-Joseph Est, Quebec City, QC'
marker_color = '4fc27d'
args = {
markers: "color:0x#{marker_color}|#{address}",
size: '640x330',
zoom: 14,
language: 'en',