From e8e94e9427af012d39679376070f862c762759d5 Mon Sep 17 00:00:00 2001 From: erickson Date: Wed, 29 Oct 2008 16:32:20 +0000 Subject: [PATCH] back-porting util class git-svn-id: svn://svn.open-ils.org/ILS/branches/rel_1_4@10972 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/web/js/dojo/openils/Util.js | 82 ++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 Open-ILS/web/js/dojo/openils/Util.js diff --git a/Open-ILS/web/js/dojo/openils/Util.js b/Open-ILS/web/js/dojo/openils/Util.js new file mode 100644 index 0000000000..4ce9cffa71 --- /dev/null +++ b/Open-ILS/web/js/dojo/openils/Util.js @@ -0,0 +1,82 @@ +/* --------------------------------------------------------------------------- + * Copyright (C) 2008 Georgia Public Library Service + * Bill Erickson + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * --------------------------------------------------------------------------- + */ + + +/** + * General purpose, static utility functions + */ + +if(!dojo._hasResource["openils.Util"]) { + dojo._hasResource["openils.Util"] = true; + dojo.provide("openils.Util"); + dojo.require('openils.Event'); + dojo.declare('openils.Util', null, {}); + + + /** + * Wrapper for dojo.addOnLoad that verifies a valid login session is active + * before adding the function to the onload set + */ + openils.Util.addOnLoad = function(func, noSes) { + if(func) { + if(!noSes) { + dojo.require('openils.User'); + if(!openils.User.authtoken) + return; + } + console.log("adding onload " + func.name); + dojo.addOnLoad(func); + } + }; + + /** + * Returns true if the provided array contains the specified value + */ + openils.Util.arrayContains = function(arr, val) { + for(var i = 0; arr && i < arr.length; i++) { + if(arr[i] == val) + return true; + } + return false; + }; + + /** + * Parses opensrf response objects to see if they contain + * data and/or an ILS event. This only calls request.recv() + * once, so in a streaming context, it's necessary to loop on + * this method. + * @param r The OpenSRF Request object + * @param eventOK If true, any found events will be returned as responses. + * If false, they will be treated as error conditions and their content will + * be alerted if openils.Util.alertEvent is set to true. Also, if eventOk is + * false, the response content will be null when an event is encountered. + */ + openils.Util.alertEvent = true; + openils.Util.readResponse = function(r, eventOk) { + var msg = r.recv(); + if(msg == null) return msg; + var val = msg.content(); + if(e = openils.Event.parse(val)) { + if(eventOk) return e; + console.log(e.toString()); + if(openils.Util.alertEvent) + alert(e); + return null; + } + return val; + }; + +} -- 2.11.0