]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Evergreen/staff_client/chrome/content/evergreen/patron/patron_search.js
user_request is now optionally async'able. Aggressive async of patron select in...
[working/Evergreen.git] / Evergreen / staff_client / chrome / content / evergreen / patron / patron_search.js
1 sdump('D_TRACE','Loading patron_search.js\n');
2
3 var test_variable = false;
4
5 function patron_search_init(p) {
6         sdump('D_PATRON_SEARCH',"TESTING: patron_search.js: " + mw.G['main_test_variable'] + '\n');
7         sdump('D_CONSTRUCTOR',arg_dump(arguments));
8
9         /* gives: p.clamshell, p.right_panel, p.left_panel */
10         patron_search_clamshell_init(p);
11
12         /* gives: p.search_form */
13         patron_search_patron_search_form_init(p);
14
15         /* gives: p.search_results */
16         patron_search_patron_search_results_init(p);
17
18         p.crazy_search = function (crazy_search_hash) {
19                 return patron_search( crazy_search_hash );
20         };
21
22         p.retrieve_button = p.w.document.getElementById('PatronSearch_retrieve_button');
23         p.retrieve_button.addEventListener(
24                 'command',
25                 function (ev) {
26                         spawn_patron_display(
27                                 p.w.app_shell,'new_tab','main_tabbox',
28                                 {
29                                         'patron' : retrieve_patron_by_id(
30                                                 p._patron.id()
31                                         )
32                                 }
33                         );
34                 }
35                 ,false
36         );
37
38         sdump('D_TRACE_EXIT',arg_dump(arguments));
39         return;
40 }
41
42 function patron_search_clamshell_init(p) {
43         p.clamshell = clam_shell_init( { 'w' : p.w, 'node' : p.clamshell_node, 'debug' : p.app } );
44         p.left_panel = p.clamshell.first_deck;
45         p.right_panel = p.clamshell.second_deck;
46 }
47
48 function patron_search_patron_search_form_init(p) {
49         p.search_form = patron_search_form_init( { 'w' : p.w, 'node' : p.patron_search_form_node, 'debug' : p.ap } );
50         p.search_form.register_search_callback(
51                 function (ev) {
52                         sdump('D_PATRON_SEARCH','Submitted: ' + js2JSON(p.search_form.crazy_search_hash) + '\n');
53                         if (p.crazy_search) {
54                                 p.search_results.clear_patrons();
55                                 p.search_results.add_patrons(
56                                         p.crazy_search( p.search_form.crazy_search_hash )
57                                 );
58                                 p.search_results.paged_tree.tree.view.selection.select( 0 );
59                                 p.search_results.paged_tree.tree.focus();
60                         }
61                 }
62         );
63 }
64
65 function patron_search_patron_search_results_init(p) {
66         p.search_results = patron_search_results_init( { 'w' : p.w, 'node' : p.patron_search_results_node, 'popupset_node' : p.popupset_node, 'debug' : p.app } );
67
68         p.redraw_search_results = function() {
69                 p.search_results.clear_search_results();
70                 if (!p._patron.checkouts()) patron_get_checkouts( p._patron );
71                 for (var i = 0; i < p._patron.checkouts().length; i++) {
72                         p.search_results.add_search_results( [ i ] );
73                 }
74         }
75
76         var patron_select_async_count = 0;
77
78         function gen_patron_select_async_function(count) {
79                 return function (request) {
80                         /* Set new patron */
81                         if (count == patron_select_async_count) {
82
83                                 p._patron = request.getResultObject();
84
85                                 patron_get_checkouts( p._patron, function(req) {
86         
87                                         if (count == patron_select_async_count) {
88
89                                                 p._patron.checkouts( req.getResultObject() );
90
91                                                 patron_get_holds( p._patron, function(req) {
92
93                                                         if (count == patron_select_async_count) {
94
95                                                                 p._patron.hold_requests( req.getResultObject() );
96                                                                 
97                                                                 patron_get_bills( p._patron, function(req) {
98
99                                                                         if (count == patron_select_async_count) {
100
101                                                                                 p._patron.bills = req.getResultObject();
102                                                                                 render_fm(p.w.document,{'au':p._patron});
103                                                                                 p.retrieve_button.disabled = false;
104                                                                         }
105                                                                 });
106                                                         }
107                                                 });
108                                         }
109                                 });     
110                         };
111                 };
112         }
113
114         p.search_results.register_patron_select_callback(
115                 function (ev) {
116                         sdump('D_PATRON_SEARCH','Firing patron_select_callback\n');
117                         /* Clear Current Patron */
118                         p.retrieve_button.disabled = true; 
119                         p._patron = fake_patron();
120                         render_fm( p.w.document, { 'au' : p._patron } );
121                         /* Get selection */
122                         var patrons = get_list_from_tree_selection( p.search_results.paged_tree.tree );
123                         /* Get patron and render status */
124                         retrieve_patron_by_id( 
125                                 patrons[ patrons.length - 1 ].getAttribute('record_id'),
126                                 gen_patron_select_async_function( ++patron_select_async_count )
127                         );
128                 }
129         );
130         p.search_results.register_flesh_patron_function(
131                 function (treeitem) {
132                         sdump('D_PATRON_SEARCH','Firing flesh_patron_function\n');
133                         var record_id = treeitem.getAttribute('record_id'); 
134                         retrieve_patron_by_id( 
135                                 record_id, 
136                                 function (request) {
137                                         sdump('D_PATRON_SEARCH','flesh_patron callback\n');
138                                         try {
139                                                 var patron = request.getResultObject();
140                                                 sdump('D_PATRON_SEARCH','patron = ' + js2JSON( patron ) + '\n');
141                                                 try {
142                                                         p.search_results.map_patron_to_cols( patron, treeitem );
143                                                 } catch(E) {
144                                                         sdump('D_ERROR','map in flesh_patron callback\n' + E+ '\n');
145                                                 }
146                                         } catch(E) {
147                                                 sdump('D_ERROR','flesh_patron callback\n' + E+ '\n');
148                                         }
149                                         sdump('D_PATRON_SEARCH','leaving flesh_patron callback\n');
150                                 }
151                         );
152                 }
153         );
154         p.search_results.register_context_builder(
155                 function (ev) {
156                         sdump('D_PATRON_DISPLAY','Firing context_builder\n');
157                         empty_widget(p.search_results.paged_tree.popup);
158                         var search_results = get_list_from_tree_selection( p.search_results.paged_tree.tree );
159                         var menuitem;
160
161                         /*** PATRON DISPLAY ***/
162                         menuitem = p.search_results.paged_tree.w.document.createElement('menuitem');
163                         p.search_results.paged_tree.popup.appendChild( menuitem );
164                         menuitem.setAttribute('label',getString('patron.context_display'));
165                         menuitem.addEventListener(
166                                 'command',
167                                 function (ev) {
168                                         sdump('D_PATRON_DISPLAY','Firing renew context\n');
169                                         for (var i = 0; i < search_results.length; i++) {
170                                                 spawn_patron_display(
171                                                         p.w.app_shell,'new_tab','main_tabbox',
172                                                         {
173                                                                 'patron' : retrieve_patron_by_id(
174                                                                         search_results[i].getAttribute('record_id')
175                                                                 )
176                                                         }
177                                                 );
178                                         }
179                                 },
180                                 false
181                         );
182                 }
183         );
184 }
185
186 function patron_search(crazy_search_hash) {
187         sdump('D_PATRON_SEARCH',arg_dump(arguments));
188         sdump('D_TRACE_ENTER',arg_dump(arguments));
189         var result = [];
190         try {
191                 result = user_request(
192                         'open-ils.actor',
193                         'open-ils.actor.patron.search.advanced',
194                         [ G.auth_ses[0], crazy_search_hash ]
195                 )[0];
196                 sdump('D_PATRON_SEARCH','result.length = ' + result.length + '\n');
197         } catch(E) {
198                 handle_error(E);
199         }
200         sdump('D_TRACE_EXIT',arg_dump(arguments));
201         return result;
202 }
203