ui.bootstrap.pagination for list, cleanup
[angular-drzb] / app / js / directives.js
index c4bed57..20b1127 100644 (file)
@@ -70,6 +70,7 @@ directive('myLabelInput', function() {
 '      <select id="'+m+'" ng-model="'+m+'"';
                        ;
                        if ( attrs.ngOptions  ) html += ' ng-options="' + attrs.ngOptions + '"';
+                       if ( attrs.ngChange )    html += ' ng-change="' + attrs.ngChange + '"';
                        html += '></select>'+
 '    </div>'+
 '  </div>';
@@ -126,4 +127,68 @@ directive('myLabelInput', function() {
                }
        };
 }])
+.directive('myPagination', function() { // modified ui.bootstrap.pagination
+  return {
+    restrict: 'E',
+    scope: {
+      numPages: '=',
+      currentPage: '=',
+      maxSize: '=',
+      onSelectPage: '&'
+    },
+    templateUrl: 'template/pagination/pagination.html',
+    replace: true,
+    link: function(scope) {
+      scope.$watch('numPages + currentPage + maxSize', function() {
+        scope.pages = [];
+
+        //set the default maxSize to numPages
+        var maxSize = ( scope.maxSize && scope.maxSize < scope.numPages ) ? scope.maxSize : scope.numPages;
+        var startPage = scope.currentPage - Math.floor(maxSize/2);
+        
+        //adjust the startPage within boundary
+        if(startPage < 1) {
+            startPage = 1;
+        }
+        if ((startPage + maxSize - 1) > scope.numPages) {
+            startPage = startPage - ((startPage + maxSize - 1) - scope.numPages );
+        }
+
+        for(var i=0; i < maxSize && i < scope.numPages ;i++) {
+          scope.pages.push(startPage + i);
+        }
+        if ( scope.currentPage > scope.numPages ) {
+          scope.selectPage(scope.numPages);
+        }
+      });
+      scope.noPrevious = function() {
+        return scope.currentPage === 1;
+      };
+      scope.noNext = function() {
+        return scope.currentPage === scope.numPages;
+      };
+      scope.isActive = function(page) {
+        return scope.currentPage === page;
+      };
+
+      scope.selectPage = function(page) {
+        if ( ! scope.isActive(page) ) {
+          scope.currentPage = page;
+          scope.onSelectPage({ page: page });
+        }
+      };
+
+      scope.selectPrevious = function() {
+        if ( !scope.noPrevious() ) {
+          scope.selectPage(scope.currentPage-1);
+        }
+      };
+      scope.selectNext = function() {
+        if ( !scope.noNext() ) {
+          scope.selectPage(scope.currentPage+1);
+        }
+      };
+    }
+  };
+})
 ;