]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.browser/resources/js/jquery.tablesorter.pager.js
(refs #7260) Added a new DerivedProperty to SCLValues
[simantics/platform.git] / bundles / org.simantics.debug.browser / resources / js / jquery.tablesorter.pager.js
1 (function($) {
2         $.extend({
3                 tablesorterPager: new function() {
4                         
5                         function updatePageDisplay(c) {
6                                 var s = $(c.cssPageDisplay,c.container).val((c.page+1) + c.seperator + c.totalPages);   
7                         }
8                         
9                         function setPageSize(table,size) {
10                                 var c = table.config;
11                                 c.size = size;
12                                 c.totalPages = Math.ceil(c.totalRows / c.size);
13                                 c.pagerPositionSet = false;
14                                 moveToPage(table);
15                                 fixPosition(table);
16                         }
17                         
18                         function fixPosition(table) {
19                                 var c = table.config;
20                                 if(!c.pagerPositionSet && c.positionFixed) {
21                                         var c = table.config, o = $(table);
22                                         if(o.offset) {
23                                                 c.container.css({
24                                                         top: o.offset().top + o.height() + 'px',
25                                                         position: 'absolute'
26                                                 });
27                                         }
28                                         c.pagerPositionSet = true;
29                                 }
30                         }
31                         
32                         function moveToFirstPage(table) {
33                                 var c = table.config;
34                                 c.page = 0;
35                                 moveToPage(table);
36                         }
37                         
38                         function moveToLastPage(table) {
39                                 var c = table.config;
40                                 c.page = (c.totalPages-1);
41                                 moveToPage(table);
42                         }
43                         
44                         function moveToNextPage(table) {
45                                 var c = table.config;
46                                 c.page++;
47                                 if(c.page >= (c.totalPages-1)) {
48                                         c.page = (c.totalPages-1);
49                                 }
50                                 moveToPage(table);
51                         }
52                         
53                         function moveToPrevPage(table) {
54                                 var c = table.config;
55                                 c.page--;
56                                 if(c.page <= 0) {
57                                         c.page = 0;
58                                 }
59                                 moveToPage(table);
60                         }
61                                                 
62                         
63                         function moveToPage(table) {
64                                 var c = table.config;
65                                 if(c.page < 0 || c.page > (c.totalPages-1)) {
66                                         c.page = 0;
67                                 }
68                                 
69                                 renderTable(table,c.rowsCopy);
70                         }
71                         
72                         function renderTable(table,rows) {
73                                 
74                                 var c = table.config;
75                                 var l = rows.length;
76                                 var s = (c.page * c.size);
77                                 var e = (s + c.size);
78                                 if(e > rows.length ) {
79                                         e = rows.length;
80                                 }
81                                 
82                                 
83                                 var tableBody = $(table.tBodies[0]);
84                                 
85                                 // clear the table body
86                                 
87                                 $.tablesorter.clearTableBody(table);
88                                 
89                                 for(var i = s; i < e; i++) {
90                                         
91                                         //tableBody.append(rows[i]);
92                                         
93                                         var o = rows[i];
94                                         var l = o.length;
95                                         for(var j=0; j < l; j++) {
96                                                 
97                                                 tableBody[0].appendChild(o[j]);
98
99                                         }
100                                 }
101                                 
102                                 fixPosition(table,tableBody);
103                                 
104                                 $(table).trigger("applyWidgets");
105                                 
106                                 if( c.page >= c.totalPages ) {
107                                 moveToLastPage(table);
108                                 }
109                                 
110                                 updatePageDisplay(c);
111                         }
112                         
113                         this.appender = function(table,rows) {
114                                 
115                                 var c = table.config;
116                                 
117                                 c.rowsCopy = rows;
118                                 c.totalRows = rows.length;
119                                 c.totalPages = Math.ceil(c.totalRows / c.size);
120                                 
121                                 renderTable(table,rows);
122                         };
123                         
124                         this.defaults = {
125                                 size: 10,
126                                 offset: 0,
127                                 page: 0,
128                                 totalRows: 0,
129                                 totalPages: 0,
130                                 container: null,
131                                 cssNext: '.next',
132                                 cssPrev: '.prev',
133                                 cssFirst: '.first',
134                                 cssLast: '.last',
135                                 cssPageDisplay: '.pagedisplay',
136                                 cssPageSize: '.pagesize',
137                                 seperator: "/",
138                                 positionFixed: true,
139                                 appender: this.appender
140                         };
141                         
142                         this.construct = function(settings) {
143                                 
144                                 return this.each(function() {   
145                                         
146                                         config = $.extend(this.config, $.tablesorterPager.defaults, settings);
147                                         
148                                         var table = this, pager = config.container;
149                                 
150                                         $(this).trigger("appendCache");
151                                         
152                                         config.size = parseInt($(".pagesize",pager).val());
153                                         
154                                         $(config.cssFirst,pager).click(function() {
155                                                 moveToFirstPage(table);
156                                                 return false;
157                                         });
158                                         $(config.cssNext,pager).click(function() {
159                                                 moveToNextPage(table);
160                                                 return false;
161                                         });
162                                         $(config.cssPrev,pager).click(function() {
163                                                 moveToPrevPage(table);
164                                                 return false;
165                                         });
166                                         $(config.cssLast,pager).click(function() {
167                                                 moveToLastPage(table);
168                                                 return false;
169                                         });
170                                         $(config.cssPageSize,pager).change(function() {
171                                                 setPageSize(table,parseInt($(this).val()));
172                                                 return false;
173                                         });
174                                 });
175                         };
176                         
177                 }
178         });
179         // extend plugin scope
180         $.fn.extend({
181         tablesorterPager: $.tablesorterPager.construct
182         });
183         
184 })(jQuery);