Acq: several interface improvements
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / picklist / view.js
1 dojo.require('dojo.date.stamp');
2 dojo.require('dojo.date.locale');
3 dojo.require('openils.User');
4 dojo.require('openils.Util');
5 dojo.require('openils.CGI');
6 dojo.require('dijit.layout.ContentPane');
7
8 var plist;
9 var plOffset = 0;
10 var plLimit = 20;
11 var liTable;
12
13
14 function load() {
15     liTable = new AcqLiTable();
16     liTable.isPL = plId;
17     liTable.enableActionsDropdownOptions("pl");
18
19     fieldmapper.standardRequest(
20         ['open-ils.acq', 'open-ils.acq.picklist.retrieve.authoritative'],
21         {   async: true,
22             params: [openils.User.authtoken, plId, 
23                 {flesh_lineitem_count:true, flesh_owner:true}],
24             oncomplete: function(r) {
25                 plist = openils.Util.readResponse(r);
26                 liTable.testOrderIdentPerms(
27                     plist.org_unit(),
28                     function() { drawPl(plist) }
29                 );
30             }
31         }
32     );
33
34     /* if we got here from the search/invoice page with a focused LI,
35      * return to the previous page with the same LI focused */
36     var cgi = new openils.CGI();
37     var source = cgi.param('source');
38     var focus_li = cgi.param('focus_li');
39     if (source && focus_li) {
40         dojo.forEach(
41             ['search', 'invoice'], // perhaps a wee bit too loose
42             function(srcType) {
43                 if (source.match(new RegExp(srcType))) {
44                     openils.Util.show('acq-pl-return-to-' + srcType);
45                     var newCgi = new openils.CGI({url : source});
46                     newCgi.param('focus_li', cgi.param('focus_li'));
47                     dojo.byId('acq-pl-return-to-' + srcType + '-button').onclick = function() {
48                         location.href = newCgi.url();
49                     }
50                 }
51             }
52         );
53     }
54 }
55
56 function drawPl() {
57     
58     dojo.byId("oils-acq-picklist-name").innerHTML = plist.name();
59     dojo.byId("oils-acq-picklist-attr-owner").innerHTML = plist.owner().usrname();
60     dojo.byId("oils-acq-picklist-attr-count").innerHTML = plist.entry_count();
61
62     dojo.byId("oils-acq-picklist-attr-cdate").innerHTML =
63          dojo.date.locale.format(
64             dojo.date.stamp.fromISOString(plist.create_time()), 
65             {selector:'date'}
66         );
67
68     dojo.byId("oils-acq-picklist-attr-edate").innerHTML = 
69          dojo.date.locale.format(
70             dojo.date.stamp.fromISOString(plist.edit_time()), 
71             {selector:'date'}
72         );
73
74     loadLIs();
75 }
76
77 function loadLIs() {
78     liTable.reset();
79
80     if(plist.entry_count() > (plOffset + plLimit)) {
81         liTable.setNext(
82             function() { 
83                 plOffset += plLimit;
84                 loadLIs();
85             }
86         );
87     } else {
88         liTable.setNext(null);
89     }
90
91     if(plOffset > 0) {
92         liTable.setPrev(
93             function() { 
94                 plOffset -= plLimit;
95                 loadLIs();
96             }
97         );
98     } else {
99         liTable.setPrev(null);
100     }
101
102
103     fieldmapper.standardRequest(
104         ['open-ils.acq', 'open-ils.acq.lineitem.picklist.retrieve'],
105         {   async: true,
106             params: [openils.User.authtoken, plId, 
107                 {flesh_notes:true, flesh_cancel_reason:true, flesh_attrs:true, clear_marc:true, offset:plOffset, limit:plLimit}],
108             onresponse: function(r) {
109                 var li = openils.Util.readResponse(r);
110                 if (li) { /* Not every response is an LI (for some reason) */
111                     liTable.addLineitem(li);
112                     liTable.show('list');
113                 }
114             }, 
115             oncomplete : function() {
116                     liTable.show('list');
117             }
118         }
119     );
120 }
121
122 openils.Util.addOnLoad(load);
123
124