Skip to content

Instantly share code, notes, and snippets.

@MeTe-30
MeTe-30 / Resize & Optimize Image
Created December 6, 2016 08:28
Resize & Optimize Image, create Thumbnail | Javascript
// UseCases:
// function CB(dataUrl, newSize) { console.info(newSize.w, newSize.h); };
// To set specific width: resizeImage(url, {w: 200}, CB);
// To set specific height: resizeImage(url, {h: 200}, CB);
// To rediuce size by percent: resizeImage(url, {r: .5}, CB);
// To change export quality add {q: .5} (default & max is 1)
@MeTe-30
MeTe-30 / Convert image to Base64, dataUrl
Created December 6, 2016 08:01
Convert image to Base64, dataUrl by Canvas & BLOB | Javscript
function toDataUrl(url, callback) {
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function () {
var reader = new FileReader();
reader.onloadend = function () {
callback(reader.result);
}
reader.readAsDataURL(xhr.response);
@MeTe-30
MeTe-30 / Convert Persian & English digits together
Created December 6, 2016 07:53
Convert Persian & English digits together | Javascript
String.prototype.toPersianDigits = function () {
var id = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
return this.replace(/[0-9]/g, function (w) {
return id[+w];
});
};
String.prototype.toEnglishDigits = function () {
var id = { '۰': '0', '۱': '1', '۲': '2', '۳': '3', '۴': '4', '۵': '5', '۶': '6', '۷': '7', '۸': '8', '۹': '9' };
@MeTe-30
MeTe-30 / Money-Price formatting directive
Created November 28, 2016 11:50
Money-Price formatting directive without affecting on ngModel (:Integer) | AngularJs
// My answer to question http://stackoverflow.com/questions/38062482/money-formatting-directive-in-angular/38112095#38112095
app.directive('price', [function () {
return {
require: 'ngModel',
link: function (scope, element, attrs, ngModel) {
attrs.$set('ngTrim', "false");
var formatter = function(str, isNum) {
@MeTe-30
MeTe-30 / Directives for useful events
Created November 28, 2016 11:15
ngEnter, ngEsc, ngTab, ngResize, ngDragenter, ngMouse, ngDragdiff | AngularJs
app.directive('ngEnter', function() {
return function(scope, element, attrs) {
element.bind("keydown keypress", function(event) {
if (event.which === 13) {
scope.$apply(function() {
scope.$eval(attrs.ngEnter);
});
event.preventDefault();
}
});
@MeTe-30
MeTe-30 / Angular template handlers
Created November 28, 2016 10:24
Better use of $compile and $sce to render html contents | AngularJs
// for html strings that contain expressiona or other angular directives
app.directive('template', ['$compile', function($compile) {
return function(scope, element, attrs) {
attrs.$observe("template", function(_newVal) {
scope.$applyAsync(function() {
element.replaceWith($compile(_newVal)(scope));
});
});
};
@MeTe-30
MeTe-30 / ng-validation
Last active November 28, 2016 10:06
Add custom validation to ngModel (useful in forms) | AngularJs
app.directive('ngValidation', function () {
return {
require: 'ngModel',
scope: {
ngValidation: '='
},
link: function (scope, element, attrs, ngModel) {
scope.$watch('ngValidation', function (newVal) {
if (!(newVal instanceof Object)) return;