]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/Util.js
give readresponse the knowledge to look for failure events within a list result
[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      * Given a HTML select object, returns the currently selected value
58      */
59     openils.Util.selectorValue = function(sel) {
60         if(!sel) return null;
61         var idx = sel.selectedIndex;
62         if(idx < 0) return null;
63         var o = sel.options[idx];
64         var v = o.value; 
65         if(v == null) v = o.innerHTML;
66         return v;
67     }
68
69     /**
70      * Returns the character code of the provided (or current window) event
71      */
72     openils.Util.getCharCode = function(evt) {
73         evt = (evt) ? evt : ((window.event) ? event : null); 
74         if(evt) {
75             return (evt.charCode ? evt.charCode : 
76                 ((evt.which) ? evt.which : evt.keyCode ));
77         } else { return -1; }
78     }
79
80
81     /**
82      * Registers a handler for when the Enter key is pressed while 
83      * the provided DOM node has focus.
84      */
85     openils.Util.registerEnterHandler = function(domNode, func) {
86             if(!(domNode && func)) return;
87             domNode.onkeydown = function(evt) {
88             var code = openils.Util.getCharCode(evt);
89             if(code == 13 || code == 3) 
90                 func();
91         }
92         }
93
94
95     /**
96      * Parses opensrf response objects to see if they contain 
97      * data and/or an ILS event.  This only calls request.recv()
98      * once, so in a streaming context, it's necessary to loop on
99      * this method. 
100      * @param r The OpenSRF Request object
101      * @param eventOK If true, any found events will be returned as responses.  
102      * If false, they will be treated as error conditions and their content will
103      * be alerted if openils.Util.alertEvent is set to true.  Also, if eventOk is
104      * false, the response content will be null when an event is encountered.
105      */
106     openils.Util.alertEvent = true;
107     openils.Util.readResponse = function(r, eventOk, isList) {
108         var msg = r.recv();
109         if(msg == null) return msg;
110         var val = msg.content();
111         var testval = val;
112         if(isList && dojo.isArray(val))
113             testval = val[0];
114         if(e = openils.Event.parse(testval)) {
115             if(eventOk) return e;
116             console.log(e.toString());
117             if(openils.Util.alertEvent)
118                 alert(e);
119             return null;
120         }
121         return val;
122     };
123
124
125     /**
126      * Given a DOM node, adds the provided class to the node 
127      */
128     openils.Util.addCSSClass = function(node, cls) {
129         if(!(node && cls)) return; 
130         var className = node.className;
131
132         if(!className) {
133             node.className = cls;
134             return;
135         }
136
137         var classList = className.split(/\s+/);
138         var newName = '';
139             
140         for (var i = 0; i < classList.length; i++) {
141             if(classList[i] == cls) return;
142             if(classList[i] != null)
143                 newName += classList[i] + " ";
144         }
145
146         newName += cls;
147         node.className = newName;
148     },
149
150     /**
151      * Given a DOM node, removes the provided class from the CSS class 
152      * name list.
153      */
154     openils.Util.removeCSSClass = function(node, cls) {
155         if(!(node && cls && node.className)) return;
156         var classList = node.className.split(/\s+/);
157         var className = '';
158         for(var i = 0; i < classList.length; i++) {
159             if (classList[i] != cls) {
160                 if(i == 0)
161                     className = classList[i];
162                 else 
163                     className += ' ' + classList[i];
164             }
165         }
166         node.className = className;
167     }
168
169 }