Skip to content

Instantly share code, notes, and snippets.

@neilbo
neilbo / disable-space.js
Created September 3, 2014 11:00
disable space bar angular directive
app.directive('ngSpace', function() {
return function(scope, element, attrs) {
element.bind("keydown keypress", function(event) {
if(event.which === 32) {
scope.$apply(function(){
scope.$eval(attrs.ngSpace);
});
event.preventDefault();
}
@neilbo
neilbo / ngEnter.js
Last active August 29, 2015 14:06 — forked from EpokK/ngEnter.js
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();
}
@neilbo
neilbo / creditcardmask.js
Created September 10, 2014 03:59
Mask Credit Card xxx-xxxx-xxxx-4567
app.filter('creditcard', function(){
return function(cc, maskChar, sep){
if(!sep)
sep = '-';
var maskedNum = Array(cc.length - 3).join(maskChar) + cc.substr(cc.length - 4, 4);
maskedNum = maskedNum.substring(0, 4) + sep +
maskedNum.substring(4, 8) + sep +
maskedNum.substring(8, 12) + sep +
maskedNum.substring(12, 16);
/**
* Custom submit directive that will only submit when all the validation has passed
* for all the fields. This extends on the ng-submit directive provided by AngularJS.
*
* This directive will also remove the 'pristine' flag from all the fields when
* hitting submit, allowing the form to display no errors until the submit button
* is clicked/enter is pressed.
*
* The variable 'app' is the instance of a module.
* E.g. var app = angular.module('my-app', []);
@neilbo
neilbo / capitalise-directive.js
Created September 22, 2014 13:49
Angular Comes with upper and lower case directives, so here's a capitalise directive
angular.module('myApp').
filter('capitalise', function() {
return function(input) {
return (!!input) ? input.replace(/([^\W_]+[^\s-]*) */g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}) : '';
};
});
@neilbo
neilbo / nomatch.css
Created September 22, 2014 14:04
Show an error when inputs match
.help-block {
display: none;
}
.has-error .help-block {
display: block;
}
@neilbo
neilbo / errors-onblur.css
Created September 22, 2014 14:16
Show error on blur, and use myFormSubmitted
.form-group .help-block {
display: none;
}
.form-group.has-error .help-block {
display: block;
}
@neilbo
neilbo / no-fraction-currency.js
Created October 8, 2014 13:20
remove decimals in currency
app.filter('noFractionCurrency',
[ '$filter', '$locale', function(filter, locale) {
var currencyFilter = filter('currency');
var formats = locale.NUMBER_FORMATS;
return function(amount, currencySymbol) {
var value = currencyFilter(amount, currencySymbol);
var sep = value.indexOf(formats.DECIMAL_SEP);
console.log(amount, value);
if(amount >= 0) {
return value.substring(0, sep);
@neilbo
neilbo / animateWhenScrollView
Created July 14, 2017 04:58
animate element when in scroll view
var inView = false;
function isScrolledIntoView(elementID) {
// This checks if the element is in view
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elemTop = $(elementID).offset().top;
var elementBottom = elemTop + $(elementID).height();
@neilbo
neilbo / currency-custom.ts
Created July 14, 2017 05:01
Typescript Angular 2 Pipe for Custom Currency
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'currencyCustom'
})
export class CurrencyCustom implements PipeTransform {
/**
* @param value
*/