]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/common/li_table_pager.js
LP2061136 - Stamping 1405 DB upgrade script
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / common / li_table_pager.js
1 function LiTablePager() {
2     var self = this;
3
4     this.init = function(dataLoader, liTable, offset, limit) {
5         this.dataLoader = dataLoader;
6         this.liTable = liTable;
7         this.liTable.isUni = true;
8         this.liTable.pager = this;  /* XXX memory leak waiting to happen? */
9
10         this.displayLimit = limit || 15;
11         this.displayOffset = offset || 0;
12
13         dojo.byId("acq-litpager-controls-prev").onclick =
14             function() { self.go(-1); }
15         dojo.byId("acq-litpager-controls-next").onclick =
16             function() { self.go(1); }
17     };
18
19     this.go = function(n /* generally (-1, 0, 1) */) {
20         if (n) this.displayOffset += n * this.displayLimit;
21
22         this.show();
23         this.dataLoader(this); /* not a normal method, but a callback */
24         this.enableControls(true);
25         this.relabelControls();
26     };
27
28     this.show = function() {
29         this.liTable.reset();
30         this.liTable.show("list");
31     };
32
33     this.enableControls = function(yes) {
34         dojo.byId("acq-litpager-controls-prev").disabled =
35             (!yes) || this.displayOffset < 1;
36         dojo.byId("acq-litpager-controls-next").disabled =
37             (!yes) || (
38                 (typeof(this.total) != "undefined") &&
39                     this.displayOffset + this.displayLimit >= this.total
40             );
41         dojo.attr("acq-litpager-controls", "disabled", String(!yes));
42     }
43
44     this.relabelControls = function() {
45         if (typeof(this.total) != "undefined") {
46             dojo.byId("acq-litpager-controls-total").innerHTML = this.total;
47             openils.Util.show("acq-litpager-controls-total-holder", "inline");
48         } else {
49             openils.Util.hide("acq-litpager-controls-total-holder");
50         }
51
52         if (this.batch_length) {
53             dojo.byId("acq-litpager-controls-batch-start").innerHTML =
54                 this.displayOffset + 1;
55             dojo.byId("acq-litpager-controls-batch-end").innerHTML =
56                 this.displayOffset + this.batch_length;
57             openils.Util.show("acq-litpager-controls-batch-range", "inline");
58         } else {
59             openils.Util.hide("acq-litpager-controls-batch-range");
60         }
61     };
62
63     this.focusLi = function() {
64         var liId = this.liTable.focusLineitem;
65         if (liId && this.liTable.liCache[liId] && dojo.byId('li-title-ref-' + liId))
66             this.liTable.focusLi();
67     };
68
69     /* given a lineitem to focus, this will determine what page in 
70      * the results set the lineitem sits, then fetch that page 
71      * of results.  Returns false if no focus requested.
72      */
73     this.loadFocusLi = function() {
74         var liId = this.liTable.focusLineitem;
75         if (!liId) return false;
76
77         var _this = this;
78         this.getAllLineitemIDs(
79             function(r) {
80                 var allIds = openils.Util.readResponse(r);
81                 var idx = dojo.indexOf(allIds, liId);
82                 // if li not found, result is loading page 1
83
84                 var page = 1;
85                 while ( idx >= (page * _this.displayLimit) ) { 
86                     page++;
87                 }
88
89                 _this.displayOffset = (_this.displayLimit * (page - 1));
90                 _this.dataLoader();
91             }
92         );
93
94         return true;
95     };
96
97     this.getAllLineitemIDs = function(callback) {
98         this.dataLoader({
99             "id_list": true,
100             "atomic": true,
101             "skip_paging": true,
102             "onresponse": null,
103             "oncomplete": callback
104         });
105     };
106
107     this.init.apply(this, arguments);
108 }