testing commit, please ignore
[kcls-web.git] / js / ui / default / acq / picklist / user_request.js
1 dojo.require('openils.Util');
2 dojo.require('openils.User');
3 dojo.require('openils.widget.AutoGrid');
4 dojo.require('fieldmapper.OrgUtils');
5 dojo.require('openils.widget.OrgUnitFilteringSelect');
6 dojo.require('openils.widget.EditPane');
7 dojo.require("dijit.layout.StackContainer");
8 dojo.require('openils.PermaCrud');
9 dojo.requireLocalization("openils.acq", "acq");
10
11 var contextOrg;
12 var aur_obj;
13 var localeStrings = dojo.i18n.getLocalization('openils.acq', 'acq');
14
15 function setup() {
16     if(reqId) {
17         drawRequest();
18     } else {
19         drawList();
20     }
21 }
22
23 function drawRequest() {
24     var pcrud = new openils.PermaCrud({ authtoken : openils.User.authtoken });
25     aur_obj = pcrud.retrieve('aur',reqId);
26
27     // hide the grid and the context selector
28     dijit.byId('stackContainer').forward();
29
30     // toggle the View Picklist/Add to Picklist button label
31     if (aur_obj.lineitem()) {
32         openils.Util.hide( 'add_to_picklist' );
33         openils.Util.show( 'view_picklist' );
34     } else {
35         openils.Util.hide( 'view_picklist' );
36         openils.Util.show( 'add_to_picklist' );
37     }
38
39     // draw a detail page for a particular request
40     var div = document.getElementById('detail_content_pane');
41     while (div.lastChild) { div.removeChild( div.lastChild ); }
42     var pane = new openils.widget.EditPane({ 
43         fmObject : aur_obj,
44         readOnly : true
45     });
46     pane.domNode = div;
47     pane.hideActionButtons = true;
48     pane.startup();
49
50     // including ability to add request to a picklist
51     // and to "reject" it (aka apply a cancel reason)
52
53     dojo.byId("acq-ur-cancel-reason").innerHTML = '';
54     var widget = new openils.widget.AutoFieldWidget({
55         "fmField": "cancel_reason",
56         "fmClass": "aur",
57         "parentNode": dojo.byId("acq-ur-cancel-reason"),
58         "orgLimitPerms": ["CREATE_PURCHASE_REQUEST"],
59         "forceSync": true
60     });
61
62     widget.build(
63         function(w, ww) {
64             acqUrCancelReasonSubmit.onClick = function() {
65                 if (w.attr("value")) {
66                     if (confirm( localeStrings.UR_CANCEL_CONFIRM )) {
67                         fieldmapper.standardRequest(
68                             [ 'open-ils.acq', 'open-ils.acq.user_request.cancel.batch' ],
69                             {   async: true,
70                                 params: [openils.User.authtoken, [reqId], w.attr("value")],
71                                 oncomplete: function(r) {
72                                     location.href = location.href; // kludge to reload the interface
73                                 }
74                             }
75                         );
76                     }
77                 }
78             };
79         }
80     );
81 }
82
83 function fooPicklist() {
84     if (aur_obj.lineitem()) {
85         viewPicklist();
86     } else {
87         addToPicklist();
88     }
89 }
90
91 function viewPicklist() {
92     var lineitem = fieldmapper.standardRequest(
93         [ 'open-ils.acq', 'open-ils.acq.lineitem.retrieve.authoritative' ],
94         {
95             params: [openils.User.authtoken, aur_obj.lineitem()]
96         }
97     );
98     location.href = oilsBasePath + "/acq/picklist/view/" + lineitem.picklist();
99 }
100
101 function fooPicklist() {
102     if (aur_obj.lineitem()) {
103         viewPicklist();
104     } else {
105         addToPicklist();
106     }
107 }
108
109 function viewPicklist() {
110     var lineitem = fieldmapper.standardRequest(
111         [ 'open-ils.acq', 'open-ils.acq.lineitem.retrieve' ],
112         {
113             params: [openils.User.authtoken, aur_obj.lineitem()]
114         }
115     );
116     location.href = oilsBasePath + "/acq/picklist/view/" + lineitem.picklist();
117 }
118
119 function addToPicklist() {
120     // reqId, from detail view
121     location.href = oilsBasePath + "/acq/picklist/brief_record?ur=" + reqId + "&prepop=" + encodeURIComponent(js2JSON({
122         "1": aur_obj.title() || aur_obj.article_title() || aur_obj.volume(),
123         "2": aur_obj.author(),
124         "5": aur_obj.isxn(),
125         "9": aur_obj.publisher(),
126         "10": aur_obj.pubdate()
127     }));
128 }
129
130 function setNoHold() {
131     // reqId, from detail view
132     fieldmapper.standardRequest(
133         [ 'open-ils.acq', 'open-ils.acq.user_request.set_no_hold.batch' ],
134         {   async: true,
135             params: [openils.User.authtoken, [reqId]],
136             oncomplete: function(r) {
137                 location.href = location.href; // kludge to reload the interface
138             }
139         }
140     );
141 }
142
143 // format the title data as id:title
144 function getTitle(idx, item) {
145     if(item) {
146         return this.grid.store.getValue(item, 'id') + ':' + 
147         this.grid.store.getValue(item, 'title');
148     }
149     return '';
150 }
151
152 // turn id:title into a url
153 function formatTitle(value) {
154     if(value) {
155         var parts = value.split(/:/);
156         return '<a href="' + oilsBasePath + 
157             '/acq/picklist/user_request/' + parts[0] + '">' + parts[1] + '</a>';
158     }
159 }
160
161 function drawList() {
162     buildGrid();
163
164     var connect = function() {
165         dojo.connect(contextOrgSelector, 'onChange',
166             function() {
167                 contextOrg = this.attr('value');
168                 rGrid.resetStore();
169                 buildGrid();
170             }
171         );
172     };
173
174     new openils.User().buildPermOrgSelector(
175         'CREATE_PICKLIST', contextOrgSelector, null, connect);
176 }
177
178 function buildGrid() {
179
180     if(contextOrg == null)
181         contextOrg = openils.User.user.ws_ou();
182
183     rGrid.loadAll(
184         {   order_by : {aur : 'request_date'},
185             join : 'au' 
186         },
187         {
188             cancel_reason : null,
189             '+au' : {
190                 home_ou : fieldmapper.aou.descendantNodeList(contextOrg).map(
191                     function(item) { return item.id(); })
192             }
193         }
194     );
195 }
196
197 openils.Util.addOnLoad(setup);
198
199