Skip to content

Instantly share code, notes, and snippets.

@houmanka
Last active August 29, 2015 14:17
Show Gist options
  • Save houmanka/d1caa713111c0fb22ee8 to your computer and use it in GitHub Desktop.
Save houmanka/d1caa713111c0fb22ee8 to your computer and use it in GitHub Desktop.
Angular Lib
app.directive('sameAs', function () {
return {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
ctrl.$parsers.unshift(function (viewValue) {
if (viewValue === scope.user[attrs.sameAs]) {
ctrl.$setValidity('sameAs', true);
return viewValue;
} else {
ctrl.$setValidity('sameAs', false);
return undefined;
}
});
}
};
});
app.directive("scroll", function ($window) {
return function(scope, element, attrs) {
angular.element($window).bind("scroll", function() {
if (this.pageYOffset >= parseInt(attrs.scroll)) {
scope.boolChangeClass = true;
} else {
scope.boolChangeClass = false;
}
scope.$apply();
});
};
});
app.directive('ngReallyClick', [function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', function() {
var message = attrs.ngReallyMessage;
if (message && confirm(message)) {
scope.$apply(attrs.ngReallyClick);
}
});
}
}
}]);
app.directive('siteHeader', function () {
return {
restrict: 'E',
template: '<i class="fa fa-chevron-left pull-left"></i>',
link: function(scope, element) {
element.bind("click", function(){
history.back();
scope.$apply();
});
}
};
});
// http://stackoverflow.com/questions/12295983/set-active-tab-style-with-angularjs
app.directive('classOnActiveLink', [function() {
return {
link: function(scope, element, attrs) {
var anchorLink = element.children()[0].getAttribute('ng-href') || element.children()[0].getAttribute('href');
anchorLink = anchorLink.replace(/#/, '');
scope.$on("$routeChangeSuccess", function (event, current) {
if (current.$$route.originalPath == anchorLink) {
element.addClass(attrs.classOnActiveLink);
}
else {
element.removeClass(attrs.classOnActiveLink);
}
});
}
};
}]);
app.factory('Page', function(){
var title = 'Domain Name';
return {
title: function() { return title; },
setTitle: function(newTitle) { title = newTitle; }
};
});
app.factory('MetaInformation', function() {
var metaDescription = '';
var metaKeywords = '';
return {
metaDescription: function() { return metaDescription; },
metaKeywords: function() { return metaKeywords; },
reset: function() {
metaDescription = '';
metaKeywords = '';
},
setMetaDescription: function(newMetaDescription) {
metaDescription = newMetaDescription;
},
setMetaKeywords: function(newMetaKeywords) {
metaKeywords = newMetaKeywords;
}
};
});
app.factory('basket', function() {
var items = [];
var basketService = {};
basketService.addItem = function(item) {
items.push(item);
};
basketService.removeItem = function(item) {
var index = items.indexOf(item);
items.splice(index, 1);
};
basketService.items = function() {
return items;
};
return basketService;
});
app.factory('serverBuilder', function serverBuilderFactory() {
var built_in = []
built_in['base_url'] = "http://api_dev.domain.com.au:3000/";
built_in['config'] = {
'Accept': "application/app.domain.v1",
'Content-Type': "application/json"
};
return built_in;
});
app.factory('secureServerBuilder', function secureServerBuilderFactory(apiKeyFactory) {
var built_in = [];
api_key = apiKeyFactory.fetchApiKey();
built_in['base_url'] = "http://api_dev.domain.com.au:3000/";
built_in['config'] = {
'Accept': "application/app.domain.v1",
'Content-Type': "application/json",
'Authorization': 'Token token="' + api_key + '"'
};
return built_in;
});
app.filter('html', function ($sce) {
return function (val) {
return $sce.trustAsHtml(val);
};
});
app.filter('cut', function () {
return function (value, wordwise, max, tail) {
if (!value) return '';
max = parseInt(max, 10);
if (!max) return value;
if (value.length <= max) return value;
value = value.substr(0, max);
if (wordwise) {
var lastspace = value.lastIndexOf(' ');
if (lastspace != -1) {
value = value.substr(0, lastspace);
}
}
return value + (tail || ' …');
};
});
app.filter('range', function () {
return function (input, total) {
total = parseInt(total);
for (var i = 0; i < total; i++)
input.push(i);
return input;
};
});
app.filter('min_max_range', function() {
return function(input, min, max) {
min = parseInt(min); //Make string input int
max = parseInt(max);
for (var i=min; i<max; i++)
input.push(i);
return input;
};
});
app.filter('humanizeConstant', function () {
return function (text) {
if (text) {
var string = text.split("_").join(" ").toLowerCase();
string = string.charAt(0).toUpperCase() + string.slice(1);
return string
}
;
};
});
// Usage: in your $scope: $scope.helpers = HmanNamespace.helpers;
// The use it like this $scope.helpers.makeSlug($scope.aString);
var HmanNamespace = HmanNamespace || {};
HmanNamespace.helpers = {
makeSlug: function (string) {
string = string.replace(/ /, '-');
string = angular.lowercase(string);
return string;
},
// Usage: object.sort(sortBy('name'));
sortBy: function (key, reverse) {
var moveSmaller = reverse ? 1 : -1;
var moveLarger = reverse ? -1 : 1;
/**
* @param {*} a
* @param {*} b
* @return {Number}
*/
return function (a, b) {
if (a[key] < b[key]) {
return moveSmaller;
}
if (a[key] > b[key]) {
return moveLarger;
}
return 0;
};
},
//usage: var arrUnique = unique(arr);
unique: function(origArr) {
var newArr = [],
origLen = origArr.length,
found, x, y;
for (x = 0; x < origLen; x++) {
found = undefined;
for (y = 0; y < newArr.length; y++) {
if (origArr[x] === newArr[y]) {
found = true;
break;
}
}
if (!found) {
newArr.push(origArr[x]);
}
}
return newArr;
},
is_kindOf: function(obj) {
return obj.constructor;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment