Skip to content

Instantly share code, notes, and snippets.

View miguel-leon's full-sized avatar

Miguel Leon miguel-leon

View GitHub Profile
@miguel-leon
miguel-leon / arrayBufferToBase64.js
Created December 7, 2016 14:20
Convert ArrayBuffer to base64 string
function arrayBufferToBase64(buffer) {
return btoa(String.fromCharCode.apply(String, new Uint8Array(buffer)));
}
@miguel-leon
miguel-leon / PDFPrinter.js
Created December 8, 2016 15:07
Send a pdf embedded to print
function printPdf(url) {
var frame = document.createElement('iframe');
frame.setAttribute('style', 'display:none;');
frame.setAttribute('src', url);
var element = document.body.appendChild(frame);
element.addEventListener('load', function () {
element.contentWindow.print();
document.addEventListener('focus', focusEventListener, true);
});
function focusEventListener() {
@miguel-leon
miguel-leon / storage-utils.js
Last active December 13, 2016 15:53
Storage utils for local storage
angular.module('storage.utils', [])
.factory('StorageUnit', ['$localStorage', function ($localStorage) {
function StorageUnit(storageKey) {
this.storageKey = storageKey;
this.content = $localStorage[storageKey];
}
StorageUnit.prototype.get = function () {
return this.content;
@miguel-leon
miguel-leon / Object.fromSchema.js
Created December 20, 2016 19:17
Create object from schema
if (!Object.fromSchema) {
Object.defineProperty(Object, 'fromSchema', {
enumerable: false,
value: function fromSchema(schema) {
var result = {};
Object.keys(schema).forEach(function (key) {
value = schema[key];
if (value && value.constructor === Function) {
result[key] = value();
}
@miguel-leon
miguel-leon / template-cache-watch.js
Created February 5, 2017 05:59
angular.js decorator for $templateCache to add callbacks for when templates become registered
angular.module('$template-cache-watch', [])
.decorator('$templateCache', function ($delegate) {
var decorated = Object.create($delegate);
var callbacks = Object.create(null);
decorated.watch = function (key, callback) {
if (!callbacks[key]) {
callbacks[key] = [];
@miguel-leon
miguel-leon / ng-attribute-constructor.js
Created February 5, 2017 06:07
Sort of decorator so that the Attribute class constructor function is obtainable
angular.module('ng-attribute', [])
// Horrible hack for obtaining directive class `Attribute` constructor function.
// There is a nuisance in AngularJS in that the `constructor` property was not retained in the prototype.
.directive('attributesDecorator', function () {
// constructor extracted from angular.js
function Attributes(element, attributesToCopy) {
if (attributesToCopy) {
var keys = Object.keys(attributesToCopy);
var i, l, key;
@miguel-leon
miguel-leon / services-roles.js
Created February 5, 2017 06:14
Generalization of some "roles" for services regarding rest resources to allow reuse of code.
angular.module('services-roles', [])
.factory('HelperRole_CommonResourceGetter', function (params, errorHandler) {
/**
* @constructor
* @param {string} name of the resource
* @param {Resource} Resource
* @param {Function.<Array|boolean>} actionArgsMapper
*/
@miguel-leon
miguel-leon / util-functions.js
Last active March 29, 2017 21:31
Few JS functions
/**
* Extends `obj` with properties created as `key: value` for every pair of two consecutive arguments
* @param {Object} obj
* @return {Object} obj
*/
function extendWithArgs(obj) {
for (var i = 2; i < arguments.length; i += 2) {
obj[arguments[i-1]] = arguments[i];
}
return obj;
@miguel-leon
miguel-leon / Array.prototype.groupBy.js
Last active June 19, 2017 00:35
group by for arrays
/**
* @method Array.prototype.groupBy(group)
* Creates a dictionary grouping by an attribute or if `group` is a function, what it returns.
* @param {function|string} group - attribute name or function(item, index, array): string
* @returns {object.<array>}
*/
if (!Array.prototype.groupBy) {
Object.defineProperty(Array.prototype, 'groupBy', {
enumerable: false,
value: function groupBy(group) {
@miguel-leon
miguel-leon / angular1.5-configurator.js
Created June 19, 2017 00:41
Routes configurator for angular 1.5
angular.module('configurators', ['ngRoute', 'ngMaterial'])
.config(['$provide', '$routeProvider', 'Resolver', function ($provide, $routeProvider, Resolver) {
var modules = {};
$provide.constant('RouteConfigurationProvider', function (module, options) {
if (arguments.length === 1) {
if (angular.isObject(module)) {
options = module;