Skip to content

Instantly share code, notes, and snippets.

function keypress(n) {
var keyboardEvent = new KeyboardEvent('keydown', {bubbles:true});
Object.defineProperty(keyboardEvent, 'charCode', {get:function(){return this.charCodeVal;}});
Object.defineProperty(keyboardEvent, 'keyCode', {get: function(){return this.charCodeVal;}});
keyboardEvent.charCodeVal = n;
document.body.dispatchEvent(keyboardEvent);
}
@ruiwen
ruiwen / sq-content.d.coffee
Created August 11, 2015 10:01
AngularJs directive for dynamically adjusting an element's height to fill the vertical height of the page
angular.module('utils')
.directive('sq-content', [
'$log'
'$window'
($log, $window) ->
restrict: 'A'
link: (scope, elem, attrs) ->
# Calculates and sets the height of element to fully take up
# the rest of the vertical height in a page
# Mainly used for activating overflow: scroll for md-content
@ruiwen
ruiwen / UtilMixIn.py
Created May 9, 2011 19:45
UtilMixIn with JSON renderer for Django models
# Utility classes
class UtilMixIn():
def __json_render_field(self, f, only_fields=[], mapped_fields={}):
field = self._meta.get_field_by_name(f)[0]
if isinstance(field, models.fields.DateTimeField):
return time.mktime(getattr(self, f).timetuple())
elif isinstance(field, models.fields.related.ForeignKey):
@ruiwen
ruiwen / utils
Created August 28, 2011 13:59
Utility methods for Django
# Various utility functions
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden
from django.template import RequestContext, Context, loader
from django.conf import settings
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import authenticate, login as django_login
from django.db import models
import json
@ruiwen
ruiwen / console.log shim
Created November 6, 2011 13:21
Silence window.console.log on demand
/**
*
* Neuters console.log, and makes its output dependent on the global boolean DEBUG
*
**/
window.DEBUG = false;
window.console._log = window.console.log; // Make sure we have a pointer to the original function
window.console.log = function(str) {
@ruiwen
ruiwen / setup_init.sh
Created February 8, 2012 13:13
Server setup for Django / uWSGI / nginx
#!/bin/bash
# Check if we're root
if [ 0 -ne `id -u` ]; then
id -u
echo "This script needs to be run as root. Exiting."
exit 1
fi
# Add uwsgi and nginx repositories
@ruiwen
ruiwen / django-piston POSTPUT
Created February 13, 2012 15:38
Enable django-piston to POST/PUT models with ForeignKeys
def flatten_dict(self, dct):
out = {}
for field in dct.keys():
fk = self.model._meta.get_field(field)
if isinstance(fk, ForeignKey):
# Determine the target model of the ForeignKey
Klass = fk.rel.to().__class__
d = { Klass._meta.pk.name : dct[field] }
# Grab the instance
i = Klass.objects.get(**d)
@ruiwen
ruiwen / KeychainItemWrapper.h
Created May 19, 2012 05:13 — forked from dhoerl/KeychainItemWrapper.h
KeychainItemWrapper ARCified - saves NSString, NSArray, NSDictionary
/*
File: KeychainItemWrapper.h
Abstract:
Objective-C wrapper for accessing a single keychain item.
Version: 1.2 - ARCified
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
@ruiwen
ruiwen / enum.js
Last active October 5, 2015 07:04
Enums
function Enum() {
var self = this;
var vals = {};
for(var i=0; i < arguments.length; ++i) {
vals[arguments[i]] = i == 0? 0 : Math.pow(2, i-1)
vals[i] = i == 0? 0 : Math.pow(2, i-1)
Object.defineProperty(self, arguments[i], {
'value': vals[arguments[i]],
})
}
@ruiwen
ruiwen / RCDirectionalPanGestureRecognizer.h
Last active October 6, 2015 10:27
DirectionalPanGestureRecognizer for iOS
//
// RCDirectionalPanRecognizer.h
//
// Created by Ruiwen Chua on 6/23/12.
// Copyright (c) 2012 thoughtmonkeys.
//
// Reference taken from http://stackoverflow.com/a/7149691
#import <UIKit/UIKit.h>