Sh4wn
New Member
Posts: 14
Joined: Jul 2006
|
O.P. Problem with class
I have this code:
FileSender.js
code: /******************************************************************
* Script made by Lucas van dijk
*
* This script sends a specific file on command
*******************************************************************/
// Global vars
var prefs = new Prefs('prefs');
var valid_files = new Array();
function OnEvent_Initialize(MessengerStart)
{
// Nothing here @ the moment
}
function OnEvent_Uninitialize(MessengerExit)
{
}
function trim(string)
{
// Remove whitesc
while(string.charAt(0) == ' ')
{
string = string.substring(1);
}
while(string.charAt(string.length - 1) == ' ')
{
string = string.substr(0, string.length - 1);
}
return string;
}
function OnEvent_ChatWndReceiveMessage(wnd, user, message, kind)
{
if(message.charAt(0) == "!")
{
var command = trim(message.substring(1, message.indexOf(" ")));
var param = trim(message.substring(message.indexOf(" ")));
Debug.Trace("Command: " + command + ", param: " + param);
switch(command)
{
case "send":
var valid = false;
for(var i = 0; i < valid_files.length; i++)
{
if(param == valid_files[i].getName())
{
wnd.SendFile(valid_files[i].getPath());
valid = true;
break;
}
}
if(!valid)
{
var message;
message = "Het opgegeven bestand is niet geldig.\nJe kan kiezen uit de volgende bestanden:\n\n";
for(var i = 0; i < valid_files.length; i++)
{
message += (i+1) + ". " + valid_files[i].getName() + "\n";
}
wnd.SendMessage(message);
}
break;
}
}
}
function OnEvent_ChatWndSendMessage(chatWnd,message)
{
switch(message)
{
case "/filescfg":
OpenCfgWindow();
break;
}
return '';
}
function OnGetScriptMenu(Location)
{
return "<ScriptMenu><MenuEntry Id='MenuFileCfg'>Configure File Sender</MenuEntry></ScriptMenu>";
}
function OnEvent_MenuClicked(id, Location, wnd)
{
if(id == "MenuFileCfg")
{
OpenCfgWindow();
}
}
function OnWndFileCfgEvent_CtrlClicked(wnd, ctrl_id)
{
switch(ctrl_id)
{
case "btnClose":
wnd.Close(0);
break;
}
}
function OpenCfgWindow()
{
var wnd = MsgPlus.CreateWnd("interface.xml", "WndFileCfg");
clearListView(wnd, "lstFiles");
var all_files = prefs.getAsArray(Messenger.MyEmail + "/files");
var i = 0;
for(x in all_files)
{
ListViewAddRow(wnd, "lstFiles", [all_files[x]['name'], all_files[x]['path']], true);
valid_files[i] = new File(all_files[x]['name'], all_files[x]['path']);
i++;
}
}
function clearListView(wnd, id)
{
var xxx = parseInt(wnd.LstView_GetCount(id));
for(var i = 0; i < xxx; i++)
{
wnd.LstView_RemoveItem(id, 0);
}
}
function ListViewAddRow(window, id, row, set)
{
var pos = window.LstView_AddItem(id,row[0]);
for(var i = 1; i < row.length; i++)
{
window.LstView_SetItemText(id, pos, i, row[i]);
}
window.LstView_SetSelectedState(id, pos, 1);
}
prefs.class.js
code: /*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <darktempler@gmail.com> wrote this file. As long as you retain this notice you
* can do whatever you want with my code. If we meet some day, and you think
* this code is worth it, you can buy me a beer in return - Matt Labrum (-dt-)
* ----------------------------------------------------------------------------
*/
function Prefs(filename)
{
this.file = new ActiveXObject("Scripting.FileSystemObject");
this.filename = MsgPlus.ScriptFilesPath + '/' + filename + '.xml';
this.xml = new ActiveXObject("Microsoft.XMLDOM");
this.FirstTime = false;
//
// Functions
//
this.createFile = function()
{
Debug.Trace("Creating XML Prefs file..");
this.FirstTime = true;
var topNode = this.xml.createElement('prefs');
this.xml.appendChild(topNode);
this.save();
Debug.Trace("Done.");
}
this.loadXML = function()
{
Debug.Trace("Loading XML Prefs file..");
this.xml.load(this.filename);
Debug.Trace("Done.");
}
this.save = function()
{
this.xml.save(this.filename);
}
this.set = function(path, data)
{
path = path.split('/');
if(typeof(data) == "boolean")
{
if(data == true)
{
data = 1;
}
else
{
data = 0;
}
}
var lastNode = this.xml.getElementsByTagName('prefs')[0];
for(var i=0;i<path.length;i++)
{
var node = lastNode.selectNodes('pref[@name="'+path[i]+'"]');
if(node.length != 0)
{
lastNode = node[0];
}
else
{
var node = this.xml.createElement('pref');
node.setAttribute('name',path[i]);
lastNode.appendChild(node);
lastNode = node;
}
}
var text = this.xml.createTextNode(data);
var textNodeF = this.getFirstTextNode(lastNode);
if(textNodeF)
{
lastNode.replaceChild(text,textNodeF);
}
else
{
lastNode.appendChild(text);
}
}
this.get = function(path)
{
var xpath = 'prefs/' + this.pathToXpath(path);
var element = this.xml.selectNodes(xpath);
if(element.length != 0)
{
var text = this.getFirstTextNode(element[0]);
if(text)
{
return text.text;
}
}
return -1;
}
this.getAsArray = function(path)
{
var xpath = 'prefs/' + this.pathToXpath(path) + '/*';
var returns = {};
var elements = this.xml.selectNodes(xpath);
if(elements.length==0)
{
Debug.Trace('fat zero');
return returns;
}
var array = {};
for(var i=0;i<elements.length;i++)
{
if(elements[i].nodeType == 1)
{
array = this.getAsArrayAddNode(elements[i],array);
}
}
return array;
}
this.getAsArrayAddNode = function(node,array)
{
if(node.childNodes.length==1)
{
array[node.getAttribute("name")] = this.getFirstTextNode(node).text;
}
else if(node.childNodes.length==0)
{
array[node.getAttribute("name")] = '';
}
else
{
//more than one child...
array[node.getAttribute("name")] = {};
for(var i = 0; i <node.childNodes.length; i++)
{
if(node.childNodes[i].nodeType == 1)
array[node.getAttribute("name")] = this.getAsArrayAddNode(node.childNodes[i],array[node.getAttribute("name")]);
}
}
return array;
}
this.pathToXPath = function(path)
{
var parts = path.split('/');
var xpath = '';
for(var i=0;i<parts.length;i++)
{
xpath += "pref[@name='"+parts[i]+"']/";
}
return xpath.substring(0,xpath.length-1);
}
this.getFirstTextNode = function(element)
{
if(element.hasChildNodes)
{
for(var i=0;i<element.childNodes.length;i++)
{
if(element.childNodes[i].nodeType == 3)
{
return element.childNodes[i];
}
}
}
return false;
}
if(!this.file.FileExists(this.filename))
{
this.createFile();
}
else
{
this.loadXML();
}
}
But the script debugger gives an error @ line 8:
code: var prefs = new Prefs('prefs');
quote: 'Prefs' is undefined.
Why am I getting this error?
My Prefs class is defined in prefs.class.js..
This post was edited on 07-18-2006 at 11:29 AM by Sh4wn.
|
|