Skip to content

Instantly share code, notes, and snippets.

View Jabher's full-sized avatar

Vsevolod Rodionov Jabher

  • St. Petersburg, Russia
View GitHub Profile
@Jabher
Jabher / angular resource cacher
Created April 5, 2013 11:18
Transparent caching function for AngularJS resources. Accepts factory name(string), path mask and original routes hash as arguments. Supports callbacks in functions.
function createRestFactory(name, path, r){
rest.factory(name, function($resource, $cacheFactory){
var c=$cacheFactory(name);
var res = $resource(path, {}, r);
return (function(resource, routes, cache) {
var o={};
angular.forEach(routes, function(value, key) {
o[key]=(function(r) {
var request=r;
return function(attrs) {
@Jabher
Jabher / main2.js
Last active December 16, 2015 06:19
almost-universal ajaxifier
var link_processor = function () {
var link_selector = 'a[href^="/"], a[href^="' + document.location.origin + '"]';
$('body').on('click', link_selector, function () {
var target, all_of_type, target_path;
target = $(this);
if (target.attr('href')) {
target_path = target.attr('href').replace(document.location.origin, '');
var chunks = target_path.split('.');
if ((chunks.length == 1) || (chunks.length > 1 && (chunks[chunks.length - 1] == "htm") && (chunks[chunks.length - 1] == "html"))) {
@Jabher
Jabher / cache.js
Last active December 16, 2015 11:39
Caching factory; usage syntax: cache = new CacheFactory(Math.random) cache(1)> 0.808444140246138; cache(2)> 0.9461546286474913; cache(1)> 0.808444140246138;
CacheFactory = (function () {
function compareThings() {
var names = [
arguments[0]["name"].toLowerCase(),
arguments[1]["name"].toLowerCase()
];
if (names[0] < names[1]) {
return -1;
}
if (names[0] > names[1]) {
var brightnessProperty = {
get: function () {
var canvas = document.createElement('canvas');
canvas.width = 1;
canvas.height = 1;
var context = canvas.getContext('2d');
context.drawImage(this, 0, 0, 1, 1);
var data = context.getImageData(0, 0, 1, 1).data;
return Math.floor((data[0] + data[1] + data[2]) / 3)
},
@Jabher
Jabher / application.js
Created August 30, 2013 13:12
rarjpeg detector
(function (rarDetected) {
function detectRar(blob, callback) {
var reader = new FileReader;
reader.readAsBinaryString(blob);
reader.onload = function () {
callback(reader.result.indexOf('Rar!') != -1)
}
}
function getBlob(url, callback) {
@Jabher
Jabher / uploader.js
Last active December 22, 2015 10:49
iframe ajax uploader
$('body').on('submit', 'form.remote', function (e) {
e.preventDefault();
var form = this;
form.xhrSend(function (responseData) {
form.reset();
window.images.add(responseData.src)
});
return false
});
directive('preload-css-images', function () {
var image_regexp = /url\(['"]?\S*['"]?\)/;
for (var i = 0; i < document.styleSheets.length; i++) {
var styleSheet = document.styleSheets[i];
var rules = styleSheet.cssRules || styleSheet.rules;
if (rules && rules.length)
for (var j = 0; j < rules.length; j++) {
var image_urls = image_regexp.exec(rules[j].cssText);
if (image_urls) for (var k = 0; k < image_urls.length; k++) {
document.createElement('img').src = image_urls[k]
directive('preload-css-images', function(){
var image_regexp = /url\(['"]?\S*['"]?\)/;
for (var i = 0; i < document.styleSheets.length; i++) {
var styleSheet = document.styleSheets[i];
var rules = styleSheet.cssRules || styleSheet.rules;
if (rules && rules.length)
for (var j = 0; j < rules.length; j++) {
var ruleText = rules[j].cssText;
if (ruleText){
var image_urls = image_regexp.exec(ruleText);
@Jabher
Jabher / gist:10445952
Created April 11, 2014 07:12
traceur require by default
require('traceur').require.makeDefault(function(filename){
return filename.endsWith('traceur.js');
});
module.exports = require('./index.traceur.js');
function lcs(x, y) {
var symbols = {},
r = 0, p = 0, p1, L = 0, idx,
m = x.length, n = y.length,
S = new Array(m < n ? n : m);
p1 = popsym(0);
for (i = 0; i < m; i++) {
p = (r === p) ? p1 : popsym(i);
p1 = popsym(i + 1);
idx = (p > p1) ? (i++, p1) : p;