]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/Util.js
Merging acq-experiment to trunk, since rel_1_4 has been branched.
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / Util.js
1 /* ---------------------------------------------------------------------------
2  * Copyright (C) 2008  Georgia Public Library Service
3  * Bill Erickson <erickson@esilibrary.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * ---------------------------------------------------------------------------
15  */
16
17
18 /**
19  * General purpose, static utility functions
20  */
21
22 if(!dojo._hasResource["openils.Util"]) {
23     dojo._hasResource["openils.Util"] = true;
24     dojo.provide("openils.Util");
25     dojo.require('openils.Event');
26     dojo.declare('openils.Util', null, {});
27
28
29     /**
30      * Wrapper for dojo.addOnLoad that verifies a valid login session is active
31      * before adding the function to the onload set
32      */
33     openils.Util.addOnLoad = function(func, noSes) {
34         if(func) {
35             if(!noSes) {
36                 dojo.require('openils.User');
37                 if(!openils.User.authtoken) 
38                     return;
39             }
40             console.log("adding onload " + func.name);
41             dojo.addOnLoad(func);
42         }
43     };
44
45     /**
46      * Returns true if the provided array contains the specified value
47      */
48     openils.Util.arrayContains = function(arr, val) {
49         for(var i = 0; arr && i < arr.length; i++) {
50             if(arr[i] == val)
51                 return true;
52         }
53         return false;
54     };
55
56     /**
57      * Parses opensrf response objects to see if they contain 
58      * data and/or an ILS event.  This only calls request.recv()
59      * once, so in a streaming context, it's necessary to loop on
60      * this method. 
61      * @param r The OpenSRF Request object
62      * @param eventOK If true, any found events will be returned as responses.  
63      * If false, they will be treated as error conditions and their content will
64      * be alerted if openils.Util.alertEvent is set to true.  Also, if eventOk is
65      * false, the response content will be null when an event is encountered.
66      */
67     openils.Util.alertEvent = true;
68     openils.Util.extractResponse = function(r, eventOk) {
69         var msg = r.recv();
70         if(msg == null) return msg;
71         var val = msg.content();
72         if(e = openils.Event.parse(val)) {
73             if(eventOk) return e;
74             console.log(e.toString());
75             if(openils.Util.alertEvent)
76                 alert(e);
77             return null;
78         }
79         return val;
80     };
81
82 }