]> git.evergreen-ils.org Git - Evergreen.git/blob - Evergreen/staff_client/chrome/content/evergreen/patron/patron_search.js
oops
[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                         p.retrieve_button.disabled = true; 
118                         try {
119                                 /* Get selection */
120                                 var patrons = get_list_from_tree_selection( p.search_results.paged_tree.tree );
121                                 /* Clear Current Patron */
122                                 p._patron = fake_patron();
123                                 render_fm( p.w.document, { 'au' : p._patron } );
124                                 /* Get patron and render status */
125                                 retrieve_patron_by_id( 
126                                         patrons[ patrons.length - 1 ].getAttribute('record_id'),
127                                         gen_patron_select_async_function( ++patron_select_async_count )
128                                 );
129                         } catch(E) {
130                                 sdump('D_ERROR_EXPECTED',js2JSON(E) + '\n');
131                         }
132                 }
133         );
134         p.search_results.register_flesh_patron_function(
135                 function (treeitem) {
136                         sdump('D_PATRON_SEARCH','Firing flesh_patron_function\n');
137                         var record_id = treeitem.getAttribute('record_id'); 
138                         retrieve_patron_by_id( 
139                                 record_id, 
140                                 function (request) {
141                                         sdump('D_PATRON_SEARCH','flesh_patron callback\n');
142                                         try {
143                                                 var patron = request.getResultObject();
144                                                 sdump('D_PATRON_SEARCH','patron = ' + js2JSON( patron ) + '\n');
145                                                 try {
146                                                         p.search_results.map_patron_to_cols( patron, treeitem );
147                                                 } catch(E) {
148                                                         sdump('D_ERROR','map in flesh_patron callback\n' + E+ '\n');
149                                                 }
150                                         } catch(E) {
151                                                 sdump('D_ERROR','flesh_patron callback\n' + E+ '\n');
152                                         }
153                                         sdump('D_PATRON_SEARCH','leaving flesh_patron callback\n');
154                                 }
155                         );
156                 }
157         );
158         p.search_results.register_context_builder(
159                 function (ev) {
160                         sdump('D_PATRON_DISPLAY','Firing context_builder\n');
161                         empty_widget(p.search_results.paged_tree.popup);
162                         var search_results = get_list_from_tree_selection( p.search_results.paged_tree.tree );
163                         var menuitem;
164
165                         /*** PATRON DISPLAY ***/
166                         menuitem = p.search_results.paged_tree.w.document.createElement('menuitem');
167                         p.search_results.paged_tree.popup.appendChild( menuitem );
168                         menuitem.setAttribute('label',getString('patron.context_display'));
169                         menuitem.addEventListener(
170                                 'command',
171                                 function (ev) {
172                                         sdump('D_PATRON_DISPLAY','Firing renew context\n');
173                                         for (var i = 0; i < search_results.length; i++) {
174                                                 spawn_patron_display(
175                                                         p.w.app_shell,'new_tab','main_tabbox',
176                                                         {
177                                                                 'patron' : retrieve_patron_by_id(
178                                                                         search_results[i].getAttribute('record_id')
179                                                                 )
180                                                         }
181                                                 );
182                                         }
183                                 },
184                                 false
185                         );
186                 }
187         );
188 }
189
190 function patron_search(crazy_search_hash) {
191         sdump('D_PATRON_SEARCH',arg_dump(arguments));
192         sdump('D_TRACE_ENTER',arg_dump(arguments));
193         var result = [];
194         try {
195                 result = user_request(
196                         'open-ils.actor',
197                         'open-ils.actor.patron.search.advanced',
198                         [ G.auth_ses[0], crazy_search_hash ]
199                 )[0];
200                 sdump('D_PATRON_SEARCH','result.length = ' + result.length + '\n');
201         } catch(E) {
202                 handle_error(E);
203         }
204         sdump('D_TRACE_EXIT',arg_dump(arguments));
205         return result;
206 }
207