From bfba044b5f2acc4d83f0d995a03f421ef9d826b7 Mon Sep 17 00:00:00 2001 From: Mark Spencer Date: Sat, 1 Apr 2006 08:49:54 +0000 Subject: Flesh out the remainder of the manager + http changes and create a sample application to partially demonstrate the capability of manager over http. git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@16850 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- static-http/astman.js | 256 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 256 insertions(+) create mode 100644 static-http/astman.js (limited to 'static-http/astman.js') diff --git a/static-http/astman.js b/static-http/astman.js new file mode 100644 index 000000000..1a6927263 --- /dev/null +++ b/static-http/astman.js @@ -0,0 +1,256 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Javascript routines or accessing manager routines over HTTP. + * + * Copyright (C) 1999 - 2006, Digium, Inc. + * + * Mark Spencer + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + * + */ + + +function Astman() { + var me = this; + var channels = new Array; + var lastselect; + var selecttarget; + this.setURL = function(url) { + this.url = url; + }; + this.setEventCallback = function(callback) { + this.eventcallback = callback; + }; + this.setDebug = function(debug) { + this.debug = debug; + }; + this.clickChannel = function(ev) { + var target = ev.target; + // XXX This is icky, we statically use astmanEngine to call the callback XXX + if (me.selecttarget) + me.restoreTarget(me.selecttarget); + while(!target.id || !target.id.length) + target=target.parentNode; + me.selecttarget = target.id; + target.className = "chanlistselected"; + me.chancallback(target.id); + }; + this.restoreTarget = function(targetname) { + var other; + target = $(targetname); + if (!target) + return; + if (target.previousSibling) { + other = target.previousSibling.previousSibling.className; + } else if (target.nextSibling) { + other = target.nextSibling.nextSibling.className; + } + if (other) { + if (other == "chanlisteven") + target.className = "chanlistodd"; + else + target.className = "chanlisteven"; + } else + target.className = "chanlistodd"; + }; + this.channelUpdate = function(msg, channame) { + var fields = new Array("callerid", "calleridname", "context", "extension", "priority", "account", "state", "link", "uniqueid" ); + + if (!channame || !channame.length) + channame = msg.headers['channel']; + + if (!channels[channame]) + channels[channame] = new Array(); + + if (msg.headers.event) { + if (msg.headers.event == "Hangup") { + delete channels[channame]; + } else if (msg.headers.event == "Link") { + var chan1 = msg.headers.channel1; + var chan2 = msg.headers.channel2; + if (chan1 && channels[chan1]) + channels[chan1].link = chan2; + if (chan2 && channels[chan2]) + channels[chan2].link = chan1; + } else if (msg.headers.event == "Unlink") { + var chan1 = msg.headers.channel1; + var chan2 = msg.headers.channel2; + if (chan1 && channels[chan1]) + delete channels[chan1].link; + if (chan2 && channels[chan2]) + delete channels[chan2].link; + } else if (msg.headers.event == "Rename") { + var oldname = msg.headers.oldname; + var newname = msg.headers.newname; + if (oldname && channels[oldname]) { + channels[newname] = channels[oldname]; + delete channels[oldname]; + } + } else { + channels[channame]['channel'] = channame; + for (x=0;xChannelStateCallerLocationLink"; + count=0; + for (x in channels) { + if (channels[x].channel) { + if (count % 2) + cclass = "chanlistodd"; + else + cclass = "chanlisteven"; + if (me.selecttarget && (me.selecttarget == x)) + cclass = "chanlistselected"; + count++; + s = s + "\t"; + s = s + "" + channels[x].channel + ""; + if (channels[x].state) + s = s + "" + channels[x].state + ""; + else + s = s + "unknown"; + if (channels[x].calleridname && channels[x].callerid && channels[x].calleridname != "") { + cid = channels[x].calleridname.escapeHTML() + " <" + channels[x].callerid.escapeHTML() + ">"; + } else if (channels[x].calleridname && (channels[x].calleridname != "")) { + cid = channels[x].calleridname.escapeHTML(); + } else if (channels[x].callerid) { + cid = channels[x].callerid.escapeHTML(); + } else { + cid = "Unknown"; + } + s = s + "" + cid + ""; + if (channels[x].extension) { + s = s + "" + channels[x].extension + "@" + channels[x].context + ":" + channels[x].priority + ""; + } else { + s = s + "None"; + } + if (channels[x].link) { + s = s + "" + channels[x].link + ""; + } else { + s = s + "None"; + } + s = s + "\n"; + found++; + } + } + if (!found) + s += "No active channels\n"; + s += "\n"; + return s; + }; + this.parseResponse = function(t, callback) { + var msgs = new Array(); + var inmsg = 0; + var msgnum = 0; + var x,y; + var s = t.responseText; + var allheaders = s.split('\r\n'); + if (me.debug) + me.debug.value = "\n"; + for (x=0;x