Skip to content

Instantly share code, notes, and snippets.

@odytrice
Created August 2, 2016 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odytrice/252dfc3c684a63abc860949f7ec8f0b7 to your computer and use it in GitHub Desktop.
Save odytrice/252dfc3c684a63abc860949f7ec8f0b7 to your computer and use it in GitHub Desktop.
Angular Typescript Directives and Utilities
module App.Directives {
var Directive = function (): ng.IDirective {
return {
restrict: "A",
scope: {
location: "@navigate"
},
link: function (scope: any, el, attrs) {
el.click(function () {
document.location.href = scope.location;
});
}
};
};
App.module.directive("navigate", Directive);
}
module App.Directives {
var Directive = function ($parse: ng.IParseService): ng.IDirective {
return {
restrict: "A",
link: function (scope, elm, attrs: any) {
elm.bind('change', function () {
$parse(attrs.fileInput).assign(scope, elm[0].files);
scope.$apply();
});
}
};
}
Directive.$inject = ['$parse'];
App.module.directive("fileInput", Directive);
}
module App.Directives {
class PageCtrl {
page: PagedList<any>;
static $inject = ["$scope"];
constructor($scope) {
this.page = $scope.page;
}
next() {
this.page.NextPage();
}
prev() {
this.page.previousPage();
}
goto(index: number) {
this.page.Goto(index + 1);
}
isCurrentPage(item) {
return this.page.CurrentPage() == item + 1;
}
links() {
var page = this.page;
var arr = [];
if (page && page.Pages() > 0) {
var curr = page.CurrentPage();
var count = page.Pages();
var k = 4;
//Get Index Bounds
var kl = curr - k;
var kr = curr + k;
//Handle Left Overflow
if (kl < 0) {
kr = kr + (kl * -1)
kl = 0;
}
//Handle Right Overflow
if (kr > count) {
kl = kl - (kr - count);
kr = count;
}
//Handle Recursive Left Overflow
if (kl < 0) {
kl = 0
}
//Plot Range
for (var x = kl; x < kr; x++) {
arr.push(x);
}
}
return arr;
}
}
var Directive = function (): ng.IDirective {
return {
restrict: "EA",
templateUrl: "template/paging/pager.html",
scope: {
page: "="
},
controller: PageCtrl,
controllerAs: "model"
};
};
App.module.directive("odPager", Directive);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment