]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/common/vlagent.js
ACQ+Vandelay limit upload queue selector to ACQ queues
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / common / vlagent.js
1 dojo.require('openils.widget.AutoFieldWidget');
2
3 function VLAgent(args) {
4     args = args || {};
5     for (var key in args) { 
6         this[key] = args[key]; 
7     }
8
9     this.widgets = [  
10         {key : 'import_no_match'},
11         {key : 'auto_overlay_exact'},
12         {key : 'auto_overlay_1match'},
13         {key : 'auto_overlay_best_match'},
14         {key : 'match_quality_ratio'},
15         {key : 'queue_name'},
16         {key : 'match_set', cls : 'vms'},
17         {key : 'bib_source', cls : 'cbs'},
18         {key : 'merge_profile', cls : 'vmp'},
19         {key : 'fall_through_merge_profile', cls : 'vmp'},
20         {key : 'existing_queue', cls : 'vbq'}
21     ];
22     
23     this.loaded = false;
24
25     this.init = function() {
26         var self = this;
27
28         dojo.forEach(this.widgets,
29             function(widg) {
30                 if (widg.cls) { // selectors
31
32                     new openils.widget.AutoFieldWidget({
33                         fmClass : widg.cls,
34                         selfReference : true,
35                         orgLimitPerms : [self.limitPerm || 'CREATE_PURCHASE_ORDER'],
36                         parentNode : dojo.byId('acq_vl:' + widg.key),
37                         searchFilter : (widg.cls == 'vbq') ? {queue_type : 'acq'} : null
38                     }).build(function(dijit) { 
39                         widg.dijit = dijit; 
40                         self.attachOnChange(widg);
41                     }); 
42
43                 } else { // bools
44                     widg.dijit = dijit.byId('acq_vl:' + widg.key);
45                     self.attachOnChange(widg);
46                 }
47             }
48         );
49         
50         // loaded != all widgets are done rendering,
51         // only that init() has been called.
52         this.loaded = true;
53     }
54
55     this.attachOnChange = function(widg) {
56         var self = this;
57         var qInputChange;
58
59         var qSelChange = function(val) {
60             // user selected a queue from the selector;  clear the text input 
61             // and set the item import profile already defined for the queue
62
63             var qInput = self.getDijit('queue_name');
64             var matchSetSelector = self.getDijit('match_set');
65             var qSelector = self.getDijit('existing_queue');
66
67             if(val) {
68                 qSelector.store.fetch({
69                     query : {id : val+''},
70                     onComplete : function(items) {
71                         matchSetSelector.attr('value', items[0].match_set[0] || '');
72                         matchSetSelector.attr('disabled', true);
73                     }
74                 });
75             } else {
76                 matchSetSelector.attr('value', '');
77                 matchSetSelector.attr('disabled', false);
78             }
79
80             // detach and reattach to avoid onchange firing while when we clear
81             dojo.disconnect(qInput._onchange);
82             qInput.attr('value', '');
83             qInput._onchange = dojo.connect(qInput, 'onChange', qInputChange);
84         }
85
86         qInputChange = function(val) {
87             // TODO: user may enter the name of an existing queue.  
88             // Detect this and disable/update match_set accordingly
89             self.getDijit('match_set').attr('disabled', false);
90             var qSelector = self.getDijit('existing_queue');
91             dojo.disconnect(qSelector._onchange);
92             qSelector.attr('value', '');
93             qSelector._onchange = dojo.connect(qSelector, 'onChange', qSelChange);
94         }
95
96         if (widg.key == 'existing_queue') {
97             var qSelector = self.getDijit('existing_queue');
98             qSelector._onchange = dojo.connect(qSelector, 'onChange', qSelChange);
99         } else if(widg.key == 'queue_name') {
100             var qInput = self.getDijit('queue_name');
101             qInput._onchange = dojo.connect(qInput, 'onChange', qInputChange);
102         }
103     }
104
105     this.getDijit = function(key) {
106         return this.widgets.filter( 
107             function(w) {return (w.key == key)}
108         )[0].dijit;
109     }
110
111     this.values = function() {
112         var values = {};
113         dojo.forEach(this.widgets,
114             function(widg) {
115                 values[widg.key] = widg.dijit.attr('value');
116             }
117         );
118         return values;
119     }
120
121     this.handleResponse = function(resp, oncomplete) {
122         if(!resp) return;
123         var res = {}
124
125         console.log('vandelay import returned : ' + js2JSON(resp));
126
127         // update the display counts
128         dojo.byId('acq_vl:li-processed').innerHTML = resp.li;
129         dojo.byId('acq_vl:vqbr-processed').innerHTML = resp.vqbr;
130         dojo.byId('acq_vl:bibs-processed').innerHTML = resp.bibs;
131         dojo.byId('acq_vl:lid-processed').innerHTML = resp.lid;
132         dojo.byId('acq_vl:debits-processed').innerHTML = resp.debits_accrued;
133         dojo.byId('acq_vl:copies-processed').innerHTML = resp.copies;
134
135         if (resp.complete) {
136
137             if(resp.picklist) {
138                 res.picklist_url = oilsBasePath + '/acq/picklist/view/' + resp.picklist.id();
139             } 
140
141             if(resp.purchase_order) {
142                 res.po_url = oilsBasePath + '/acq/po/view/' + resp.purchase_order.id();
143             }
144
145             if (resp.queue) {
146                 res.queue_url = oilsBasePath + '/vandelay/vandelay?qtype=bib&qid=' + resp.queue.id();
147             }
148
149             if (oncomplete) 
150                 oncomplete(resp, res);
151
152             return res;
153         }
154
155         return false; // not yet complete
156     }
157 }