]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/acq/common/vlagent.js
ACQ+Vandelay detect existing queue with same name
[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
88             var qSelector = self.getDijit('existing_queue');
89             var matchSetSelector = self.getDijit('match_set');
90             var foundMatch = false;
91
92             if (val) {
93
94                 // if the user entered the name of an existing queue, update the 
95                 // queue selector to match the value (and clear the text input 
96                 // via qselector onchange)
97                 qSelector.store.fetch({
98                     query:{name:val},
99                     onComplete:function(items) {
100                         if(items.length == 0) return;
101                         var item = items[0];
102                         qSelector.attr('value', item.id);
103                         foundMatch = true;
104                     }
105                 });
106             }
107
108             if (!foundMatch) {
109                 self.getDijit('match_set').attr('disabled', false);
110                 dojo.disconnect(qSelector._onchange);
111                 qSelector.attr('value', '');
112                 qSelector._onchange = dojo.connect(qSelector, 'onChange', qSelChange);
113             }
114         }
115
116         if (widg.key == 'existing_queue') {
117             var qSelector = self.getDijit('existing_queue');
118             qSelector._onchange = dojo.connect(qSelector, 'onChange', qSelChange);
119         } else if(widg.key == 'queue_name') {
120             var qInput = self.getDijit('queue_name');
121             qInput._onchange = dojo.connect(qInput, 'onChange', qInputChange);
122         }
123     }
124
125     this.getDijit = function(key) {
126         return this.widgets.filter(function(w) {return (w.key == key)})[0].dijit;
127     }
128
129     this.values = function() {
130         var values = {};
131         dojo.forEach(this.widgets,
132             function(widg) {
133                 values[widg.key] = widg.dijit.attr('value');
134             }
135         );
136         return values;
137     }
138
139     this.handleResponse = function(resp, oncomplete) {
140         if(!resp) return;
141         var res = {}
142
143         console.log('vandelay import returned : ' + js2JSON(resp));
144
145         // update the display counts
146         dojo.byId('acq_vl:li-processed').innerHTML = resp.li;
147         dojo.byId('acq_vl:vqbr-processed').innerHTML = resp.vqbr;
148         dojo.byId('acq_vl:bibs-processed').innerHTML = resp.bibs;
149         dojo.byId('acq_vl:lid-processed').innerHTML = resp.lid;
150         dojo.byId('acq_vl:debits-processed').innerHTML = resp.debits_accrued;
151         dojo.byId('acq_vl:copies-processed').innerHTML = resp.copies;
152
153         if (resp.complete) {
154
155             if(resp.picklist) {
156                 res.picklist_url = oilsBasePath + '/acq/picklist/view/' + resp.picklist.id();
157             } 
158
159             if(resp.purchase_order) {
160                 res.po_url = oilsBasePath + '/acq/po/view/' + resp.purchase_order.id();
161             }
162
163             if (resp.queue) {
164                 res.queue_url = oilsBasePath + '/vandelay/vandelay?qtype=bib&qid=' + resp.queue.id();
165             }
166
167             if (oncomplete) 
168                 oncomplete(resp, res);
169
170             return res;
171         }
172
173         return false; // not yet complete
174     }
175 }