Skip to content

Instantly share code, notes, and snippets.

View corpix's full-sized avatar
👹
Fighting demons

corpix corpix

👹
Fighting demons
View GitHub Profile
@corpix
corpix / Resize image to fit box.js
Created September 17, 2011 23:19
Proprotionate size changing(clientside)
function resizeImgOO(el){
function imgRatio(){
return (el.height / el.width);
}
function holderRatio(){
return (el.offsetParent.offsetHeight / el.offsetParent.offsetWidth);
}
function fitToContainer(){
@corpix
corpix / esplit.js
Last active September 27, 2015 14:37
String.esplit function
var str = 'fooooo1#foo1:1,bfooooo1#foo1:1,arrrr2#barr2:2',
slice = [].slice;
String.prototype.esplit = function(){
if(this.length == 0)
return [];
var args = slice.apply(arguments)
, options = {
@corpix
corpix / ajaxForms.js
Created November 15, 2011 23:03
jQuery - Easy ajax forms
var ajaxForms = function(/* TODO translations */){
var self = this;
$('form.ajax').each(function(i, form){
var f = $(form)
, submit = f.find('input[type="submit"]');
if($.fn.html5Uploader && f.hasClass('html5')){
f.find('[type="file"]').html5Uploader();
}
@corpix
corpix / filereader.js
Created November 22, 2011 06:15
HTML5 FileReader
var fileUploads = function(input){
var self = this;
if(input.length > 0 && !window.FileReader){ // TODO failover
$.sticky('Ваш браузер не поддерживает HTML5 FileReader, обновитесь.');
return false;
}
this.input = input;
var parent = input.parent()
@corpix
corpix / bindings.js
Created November 22, 2011 06:16
Мой файл с биндингами для форм и немного сахара
(function(){
var ajaxBindings = function(){
if(window.csrf) {
$('body').bind('ajaxSend', function(ev, xhr, s){
if (s.type == 'POST') {
xhr.setRequestHeader('X-CSRF-Token', csrf);
}
});
}
@corpix
corpix / formdata.js
Created November 23, 2011 13:37
FormData html5Uploader
(function($){
$.fn.html5Uploader = function(){
return this.each(function(){
var $this = $(this);
if($this.is('[type="file"]')){
$this.bind('change', function(){
var files = this.files;
for(var i = 0;i < files.length;i++){
fileHandler.call($this, files[i]);
}
@corpix
corpix / objectPath.js
Created February 28, 2012 04:10
Object paths(Javascript dot notation)
var getObj = {
a: {
foo: 1,
bar: 2
}
};
var objectPath = function(obj, path, value, options){
path = typeof path == 'string' ? path.split('.') : path;
var key = path.shift()
@corpix
corpix / backbone-fetch-multiple.js
Created March 3, 2012 15:07
Backbone fetch multiple
var Utils = {};
/**
* Fetch multiple models(or collections) and execute passed callback
*
* @param {Array} stack - stack of objects
* @param {Function} callback - exec on ready
* @param {Object} ctx - callback context
*
* @returns {Object} context
@corpix
corpix / backbone-router-filter.js
Created March 4, 2012 13:00
Backbone router before,after,leave
(function(Backbone, _) {
var leave;
_.extend(Backbone.Router.prototype, Backbone.Events, {
route : function(route, name, callback) {
var before
, fn = callback
, after;
Backbone.history || (Backbone.history = new Backbone.History);
@corpix
corpix / count-lines.js
Created March 7, 2012 21:05
Count lines with length >= than in a file
// node count-lines.js "somefile.txt" 12
var fs = require('fs')
, length = process.argv.pop()
, filename = process.argv.pop()
, stream
, counter = 0;
stream = fs.createReadStream(filename);