Skip to content

Instantly share code, notes, and snippets.

View martindrapeau's full-sized avatar

Martin Drapeau martindrapeau

View GitHub Profile
function loadImage(url, callback) {
var img = document.createElement("img");
img.src = url;
if (callback) {
img.onreadystatechange = callback;
img.onload = callback;
}
}
$(document).ready(function() {
getActivities('992572', gotCategory);
});
function gotCategory(data) {
console.log(data);
var tbody = document.getElementById('tb_activity');
@martindrapeau
martindrapeau / sql2json.js
Created January 18, 2016 15:53
CSVJSON.com - SQL to JSON function. Can be used in Node.js to run large data sets.
(function() {
/**
*
* CSVJSON.sql2json(sql)
*
* Converts SQL to JSON. Returns an object. Detects CREATE TABLE and INSERT INTO
* statements to extract table header and rows. Use JSON.stringify to conver to a string.
*
* Dependencies:
* - underscore (http://underscorejs.org/)
@martindrapeau
martindrapeau / gist:561616cf5b5ed9fc58a6
Created August 29, 2014 03:01
Rename files replacing non ASCII by _
find /path/to/files -type f -print0 | \
perl -n0e '$new = $_; if($new =~ s/[^[:ascii:]]/_/g) {
print("Renaming $_ to $new\n"); rename($_, $new);
}'
// Subclass the Backbone model to keep the model and its state (errors)
// sent back by the server. Creates a new errorModel property holding
// a Backbone model with errors, mapped to the model attributes.
Backbone.ModelWithState = Backbone.Model.extend({
errorModel: undefined,
constructor: function(attributes, options) {
Backbone.Model.apply(this, arguments);
options || (options = {});
this.errorModel = options.errorModel || new Backbone.Model();
},
@martindrapeau
martindrapeau / Backgrid.ClientSideFilterWithPickFilter
Created March 27, 2014 16:50
Extend Backgrid ClientSideFilter with a pick filter, to pre-filter data set. Useful to combine text search with a select, checkboxes or radio buttons filters.
// Subclass Backgrid Filter to pass an extra 'pick' filter
Backgrid.ClientSideFilterWithPickFilter = Backgrid.Extension.ClientSideFilter.extend({
pickFilter: null,
setPickFilter: function (attrs) {
this.pickFilter = attrs;
this.search();
return this;
},
makeMatcher: function (query) {
@martindrapeau
martindrapeau / telerik-patch.js
Last active January 1, 2016 18:19
Patches Telerik Tooltip control in IE10 which fails with Javascript error
/*
* Patches Telerik Tooltip control in IE10 which fails with this Javascript error:
*
* SCRIPT5007: Unable to get property 'documentElement' of undefined or null reference
* Telerik.Web.UI.WebResource.axd, line 151 character 2
*
* Solution is to redefine the bugged function and replace a.document.documentElement
* with (a.document || a.ownerDocument).documentElement, thus avoiding a paid upgrade.
*
* To use, include this script after Telerik's.
@martindrapeau
martindrapeau / jquery.cach-inputs.js
Created December 27, 2013 02:36
jQuery plugin to cache HTML inputs using local storage. Restores them upon next page load.
// Cache user inputs into HTML5 local storage. Restore them upon next page load.
// Usage:
// $('body').CacheInputs();
//
// Will store stringified JSON in local storage, against the key you specify
// or 'cache-inputs' if you omit it. See settings below.
//
// Author: Martin Drapeau
//
// Greatly inspired from Johnathan Schnittger
@martindrapeau
martindrapeau / gist:1467822
Created December 12, 2011 15:23
Planbox My Itemsin All Initiatives
$.post('https://www.planbox.com/api/get_products', function(data) {
var products = data.content;
var product_ids = [];
var product_id2name = {};
for (var i=0; i<products.length; i++) {
var product = products[i];
product_id2name[product.id] = product.name;
product_ids.push(product.id);
}
$.post('https://www.planbox.com/api/get_stories',
@martindrapeau
martindrapeau / gist:1438680
Created December 6, 2011 15:57
Planbox Velocity for the Current Iteration
$.post('https://www.planbox.com/api/get_iterations',
{product_id:1234, timeframe:"current"},
function(object) {
var iterations = object.content;
var iteration = iterations[0];
console.log('Your velocity for iteration '+iteration.name+' averaged on the past '+iteration.velocity.iteration_ids.length+' iterations:');
var record = iteration.velocity.data[0];
console.log(' Estimate-hours Velocity = '+Math.round(record.estimate_done));
console.log(' Points Velocity = '+Math.round(record.points_done));
console.log(' Business Value Velocity = '+Math.round(record.value_done));