From: erickson Date: Thu, 16 Oct 2008 21:54:38 +0000 (+0000) Subject: somewhere to put general stuff that has no better place to live. added onload wrappe... X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=5e83c12cf0ed886fccb9f9ebfed623dc210b2d1e;p=Evergreen.git somewhere to put general stuff that has no better place to live. added onload wrapper to prevent calls when there is no valid authtoken. added method to extract responses and check for events git-svn-id: svn://svn.open-ils.org/ILS/branches/acq-experiment@10854 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- 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..d5e0c442b4 --- /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.extractResponse = 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; + }; + +}