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