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