[Slashhack-cvs] slashhack/resources/js/jslib/xul RDFTree.js,NONE,1.1 appRoutines.js,NONE,1.1 commonD
Brought to you by:
fletch
|
From: Dave F. <fl...@us...> - 2004-10-21 03:49:27
|
Update of /cvsroot/slashhack/slashhack/resources/js/jslib/xul In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4907/xul Added Files: RDFTree.js appRoutines.js commonDialog.js commonFilePicker.js commonWindow.js Log Message: Task #106454 - include jslib. --- NEW FILE: commonWindow.js --- /*** -*- Mode: Javascript; tab-width: 2; The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is Mozdev Group, Inc. code. The Initial Developer of the Original Code is Pete Collins. Portions created by Mozdev Group, Inc. are Copyright (C) 2003 Mozdev Group, Inc. All Rights Reserved. Contributor(s): Pete Collins <pe...@mo...)> (original author) Henrik Gemal <http://gemal.dk> ***/ /****************** Globals **********************/ if(typeof(JS_LIB_LOADED)=='boolean') { const JS_COMMONWINDOW_FILE = "commonWindow.js"; const JS_COMMONWINDOW_LOADED = true; const JS_TOP_LEFT = 0; const JS_TOP_CENTER = 1; const JS_TOP_RIGHT = 2; const JS_MIDDLE_LEFT = 3; const JS_MIDDLE_CENTER = 4; const JS_MIDDLE_RIGHT = 5; const JS_BOTTOM_LEFT = 6; const JS_BOTTOM_CENTER = 7; const JS_BOTTOM_RIGHT = 8; const JS_UNINSTALL_URL = JS_LIB_PATH + "install/uninstall.xul"; /****************** Common Dialog Object Class *********************/ function CommonWindow(aURL, aWidth, aHeight) { if (typeof(aWidth)=="number") this.mWidth=aWidth; if (typeof(aHeight)=="number") this.mHeight=aHeight; if (typeof(aURL)=="string") this.mURL=aURL; return; } // constructor CommonWindow.prototype = { mWidth : 0, mHeight : 0, mURL : null, mPosition : JS_TOP_LEFT, mX : 0, mY : 0, set width (aWidth) { this.mWidth = aWidth; }, get width () { return this.mWidth; }, set height (aHeight) { this.mHeight = aHeight; }, get height () { return this.mHeight; }, set position (aPos) { var x,y; switch (aPos) { case JS_MIDDLE_CENTER: this.mPosition=aPos; x = Math.round(this.mWidth/2); y = Math.round(this.mHeight/2); var sY = window.screen.height/2; var sX = window.screen.width/2; jslibPrint("width: "+this.mWidth); jslibPrint("height: "+this.mHeight); jslibPrint("x: "+x+"y: "+y); jslibPrint("sX: "+sX+"sY: "+sY); this.mY = sY-y; this.mX = sX-x; jslibPrint("mX: "+this.mX); jslibPrint("mY: "+this.mY); break; default: this.mPosition=JS_TOP_LEFT; jslibDebug("not implemented yet setting to JS_TOP_LEFT for now"); } }, get position () { var rv=""; switch (this.mPosition) { case JS_TOP_LEFT: rv="JS_TOP_LEFT"; break; case JS_TOP_CENTER: rv="JS_TOP_CENTER"; break; case JS_TOP_RIGHT: rv="JS_TOP_RIGHT"; break; case JS_MIDDLE_LEFT: rv="JS_MIDDLE_LEFT"; break; case JS_MIDDLE_CENTER: rv="JS_MIDDLE_CENTER"; break; case JS_MIDDLE_RIGHT: rv="JS_MIDDLE_RIGHT"; break; case JS_BOTTOM_LEFT: rv="JS_BOTTOM_LEFT"; break; case JS_BOTTOM_CENTER: rv="JS_BOTTOM_CENTER"; break; case JS_BOTTOM_RIGHT: rv="JS_BOTTOM_RIGHT"; break; } return rv; }, set url (aURL) { this.mURL = aURL; }, get url () { return this.mURL; }, openFullScreen : function () { if (!this.mURL) return; var h = window.screen.height; var w = window.screen.width; var win_prefs = "chrome,dialog=no,width="+w+ ",height="+h+",screenX=0,screenY=0"; window.openDialog(this.mURL, "_blank", win_prefs); }, openWebTop : function () { if (!this.mURL) return; var h = window.screen.height; var w = window.screen.width; var win_prefs = "chrome,popup,scrollbars=yes,width="+w+ ",height="+h+",screenX=0,screenY=0"; jslibDebug(this.mURL+"_blank"+win_prefs); window.openDialog(this.mURL, "_blank", win_prefs); }, openUninstallWindow : function (aPackage, aCallback) { if (!aPackage) { jslibDebug("Please provide a package name to uninstall") return; } var win_prefs = "chrome,dialog,dependent=no,resize=yes,screenX="+this.mX+ ",screenY="+this.mY+ ",width="+this.mWidth+",height="+this.mHeight; window.openDialog(JS_UNINSTALL_URL, "_blank", win_prefs, aPackage, aCallback); }, openAbout : function () { if (!this.mURL) return; var h = this.mHeight; var w = this.mWidth; var win_prefs = "chrome,dialog,modal,dependent=no,resize=no,width="+w+ ",height="+h+",screenX="+this.mX+",screenY="+this.mY; window.openDialog(this.mURL, "_blank", win_prefs); }, openSplash : function () { jslibPrint("open splash . . . "); if (!this.mURL) return; var h = this.mHeight; var w = this.mWidth; var popup="popup,"; if (/Mac/g.test(window.navigator.platform)) popup=""; var win_prefs = "chrome,dialog=no,titlebar=no,"+popup+"width="+w+ ",height="+h+",screenX="+this.mX+",screenY="+this.mY; window.openDialog(this.mURL, "_blank", win_prefs); }, open : function () { if (!this.mURL) return; var win_prefs = "chrome,dialog=no,dependent=no,resize=yes,screenX="+this.mX+",screenY="+this.mY+ ",width="+this.mWidth+",height="+this.mHeight; window.openDialog(this.mURL, "_blank", win_prefs); }, openDialog : function () { if (!this.mURL) return; var win_prefs = "chrome,dialog,dependent=yes,resize=yes,screenX="+this.mX+",screenY="+this.mY+ ",width="+this.mWidth+",height="+this.mHeight; window.openDialog(this.mURL, "_blank", win_prefs); }, openModalDialog : function () { if (!this.mURL) return; var win_prefs = "chrome,dialog,dependent=no,modal,resize=yes,screenX="+this.mX+",screenY="+this.mY+ ",width="+this.mWidth+",height="+this.mHeight; window.openDialog(this.mURL, "_blank", win_prefs); }, get openWindows () { var wm = jslibGetService("@mozilla.org/appshell/window-mediator;1", "nsIWindowMediator"); jslibDebug(wm); var enumerator = wm.getEnumerator(null); var winArray = new Array(); while (enumerator.hasMoreElements()) { var domWindow = enumerator.getNext(); winArray.push(domWindow); } return winArray; }, /********************* help ***************************** * void getter help * * Returns the methods in this object * * return values on success and failure * aStr The methods in this object * * useage: * <string> = obj.help; ****************************************************/ get help() { const help = "\n\nFunction and Attribute List:\n" + "\n"; return help; } } jslibDebug('*** load: '+JS_COMMONWINDOW_FILE+' OK'); } // END BLOCK JS_LIB_LOADED CHECK // If jslib base library is not loaded, dump this error. else { dump("JS_BASE library not loaded:\n" + " \tTo load use: chrome://slashhack/content/resources/js/jslib/jslib.js\n" + " \tThen: include('chrome://slashhack/content/resources/js/jslib/xul/commonWindow.js');\n\n"); }; // END FileSystem Class --- NEW FILE: RDFTree.js --- /*** -*- Mode: Javascript; tab-width: 2; The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is Urban Rage Software. The Initial Developer of the Original Code is Urban Rage Software. Portions created by Urban Rage Software are Copyright (C) 2000-2001 Urban Rage Software. All Rights Reserved. Contributor(s): Eric Plaster, Becki Sanford RDFTree API RDFTree.js Restrictions: This class is intended to make it easier to control a tree that has rdf datasources. To use this class, you will need to follow a few simple rules: (1) Your tree must have an 'id'. This is needed to find the tree and change it's attributes. (2) You must supply an 'id' for your 'treecol'. (3) Your 'treecell' under your 'treehead' must have the same 'id' as your 'treecol'+"Header". (i.e. your treecol has an id="linksColumn" and your treehead->treerow->treecell has an id="linksColumnHeader") (4) Must be an rdf tree. (5) Only supports single selection trees at this time. Function List // Constructor RDFTree(aID) // creates the Tree object and gets the tree from the ID (get) tree // tree object addDataSource(aRDF_URL_Path) // adds a datasource to the tree. Must be a valid url path (i.e. file:///tmp/bob.rdf) rebuild() // Rebuilds the tree (resets it. You need to do this when you delete an entry in an rdf file) (get) selected // returns the currently selected index. (set) selected(which) // set the selected index. (get) count // returns the number of items in the tree (get) treeitems // returns an array of treeitem elements (get) selectedID // returns the 'id' of the selected item (get) selection // returns an array of selected treeitems (get) selectCount // returns the number of selected treeitems clearSelection(which) // clears item selections; if which = null, all selections are cleared getRowIndexOf(which) // gets the index of the given treeitem 'id' doSort(column) // sorts the given column (see Notes) refreshSort() // sorts to the previous settings (see Notes) sortColumn(column, sortKey[, direction]) // sorts the given column 'id' either 'ascending' or 'descending' sortToPreviousSettings() // sorts to the previous settings. // internal members. sort(column, Key, direction) // sorts the given column 'id' either 'ascending' or 'descending' updateSortIndicator(column, direction) // Updates the sort indicator Instructions: Warning: these API's are not for religious types Notes: (1) The doSort() and refreshSort() functions require the <treecell> to contain an observes attribute that points to the <treecol> that defines the resource attribute. The following is a skeleton code snippet: <tree> <template/> <treecolgroup> <treecol id="fooName" resource="http://home.netscape.com/NC-rdf#fooName"/> </treecolgroup> <treehead> <treerow> <treecell value="Name" observes="fooName" class="sortDirectionIndicator" onclick="gShell.treeUtils.doSort('fooName')"/> </treerow> </treehead> </tree> ************/ /****************** Globals **********************/ const JSLIB_TREE = "tree.js"; const JSTREE_OK = true; const JSLIB_TREE_SORT_PROGID = '@mozilla.org/xul/xul-sort-service;1'; /****************** Globals **********************/ /****************** File Object Class *********************/ function RDFTree(aID) { if(aID) this.mID= aID; } // constructor RDFTree.prototype = { mID : null, mTree : null, get tree() { if (this.mTree) return this.mTree; var tree = document.getElementById(this.mID); if(!tree) { throw("Unable to get tree. ID: "+this.mID); } this.mTree = tree; return tree; }, addDataSource : function(aRDF_URL_Path) { var tree = this.tree; var ds = Components.classes['@mozilla.org/rdf/rdf-service;1'].getService().QueryInterface(Components.interfaces.nsIRDFService).GetDataSource(aRDF_URL_Path); tree.database.AddDataSource(ds); tree.builder.rebuild(); }, removeDataSource : function(aRDF_URL_Path) { var tree = this.tree; var ds = Components.classes['@mozilla.org/rdf/rdf-service;1'].getService().QueryInterface(Components.interfaces.nsIRDFService).GetDataSource(aRDF_URL_Path); tree.database.RemoveDataSource(ds); tree.builder.rebuild(); }, focus : function() { this.tree.focus(); }, blur : function() { this.tree.blur(); }, rebuild : function() { this.tree.builder.rebuild(); }, get selected() { if (this.tree.treeBoxObject.selection == null) return -1; return (this.tree.treeBoxObject.selection.currentIndex); }, get selectCount() { if (this.tree.treeBoxObject.selection == null) return 0; return (this.tree.treeBoxObject.selection.count); }, set selected(index) { // Don't attempt to make any selections on an empty tree if (this.tree.treeBoxObject.view == null) return; this.tree.treeBoxObject.selection.select(index); // Give the tree focus (this will also allow the user to immediately // use the up/dopwn arrows -- bonus points!) this.tree.focus(); }, get count() { if (this.tree.treeBoxObject.view == null) return 0; return (this.tree.treeBoxObject.view.rowCount); }, get treeitems() { return (this.tree.treeBoxObject.view); }, get selectedID() { // dump("--[ selectedID ]-- selectCount="+ this.selectCount +" selected="+ this.selected +"\n"); if (this.selectCount == 0 || this.selected == -1) return null; var element = this.tree.contentView.getItemAtIndex( this.selected ); return element.getAttribute('id'); }, get selection() { // NOTES: // (1) This returns an nsITreeSelection element. Use getSelectedIDs() // to get a list of IDs. return this.tree.treeBoxObject.selection; }, get getSelectedIDs() { var selectionArray = new Array(); // Get the current selection var selection = this.selection; var rangeCount = selection.getRangeCount(); // The rangeCount represents the number of different groups of selections // (i.e. there may be gaps in the selection) for (var range = 0; range < rangeCount; ++range) { var min = {}, max = {}; selection.getRangeAt(range, min, max); for (var index = min.value; index <= max.value; ++index) { var item = this.tree.contentView.getItemAtIndex(index); selectionArray.push( item.getAttribute('id') ); } } return selectionArray; }, clearSelection : function(index) { if (index != null) { this.tree.treeBoxObject.selection.clearRange( index,index ); } else { this.tree.treeBoxObject.selection.clearSelection(); } }, getRowIndexOf : function(which) { var element = document.getElementById(which); var index = this.tree.contentView.getIndexOfItem( element ); return index; }, //------------------------------------------------------------------------ // Column sort utilities //------------------------------------------------------------------------ doSort : function(column) { var tree = this.tree; var node = document.getElementById(column); if (!node) return false; var sortKey = node.getAttribute('resource'); if (!sortKey) return false; var sortDirection = "ascending"; if ( node.getAttribute('sortDirection') == "ascending" ) sortDirection = "descending"; else sortDirection = "ascending"; this.updateSortIndicator(column, sortDirection); this.sort(column, sortKey, sortDirection); // Save the current sort settings for use by SortToPreviousSettings() tree.setAttribute('sortColumn',column); tree.setAttribute('sortKey',sortKey); tree.setAttribute('sortDirection',sortDirection); node.setAttribute('sortDirection',sortDirection); return true; }, //------------------------------------------------------------------------ refreshSort : function() { // Retrieve the last sorted state from the tree element var tree = this.tree; var column = tree.getAttribute('sortColumn'); var node = document.getElementById(column); var sortKey = node.getAttribute('resource'); var sortDirection = node.getAttribute('sortDirection'); this.sort(column, sortKey, sortDirection); // Force the button states to be updated by giving the tree focus // (this will also allow the user to immediately use the up/down // arrows --- bonus points!) tree.focus(); }, //------------------------------------------------------------------------ sortColumn : function(column, sortKey, direction) { var tree = this.tree; var node = document.getElementById(column); if (!node) return false; // If a specific direction passed in as a parameter, use it; otherwise // get the last sort direction from the tree node (defaulting to // "ascending" if none available) var sortDirection; if (direction != null) { sortDirection = direction; } else { sortDirection = "ascending"; if ( node.getAttribute('sortDirection') == "ascending" ) sortDirection = "descending"; else sortDirection = "ascending"; } this.updateSortIndicator(column, sortDirection); this.sort(column, sortKey, sortDirection); // Save the current sort settings for use by SortToPreviousSettings() tree.setAttribute('sortColumn',column); tree.setAttribute('sortKey',sortKey); tree.setAttribute('sortDirection',sortDirection); node.setAttribute('sortDirection',sortDirection); this.rebuild(); return true; }, //------------------------------------------------------------------------ sort : function(column, key, direction) { var xulSortService = Components.classes[JSLIB_TREE_SORT_PROGID].getService(); xulSortService = xulSortService.QueryInterface(Components.interfaces.nsIXULSortService); if (!xulSortService) return (false); var node = document.getElementById(column); if ( node ) { try { xulSortService.Sort(node, key, direction); } catch (ex) {} } }, //------------------------------------------------------------------------ sortToPreviousSettings : function() { var tree = this.tree; // Retrieve the last sorted state from the tree element var column = tree.getAttribute('sortColumn'); var sortKey = tree.getAttribute('sortKey'); var sortDirection = tree.getAttribute('sortDirection'); // Provide default sort data if none was d if ( !column || !sortKey ) { return; } if ( !sortDirection ) sortDirection = 'ascending'; this.updateSortIndicator(column,sortDirection); this.sort(column, sortKey, sortDirection); }, //------------------------------------------------------------------------ // Sets the column header sort icon based on the requested // column and direction. // // Notes: // (1) This function relies on the first part of the // <treecell id> matching the <treecol id>. The treecell // id must have a "Header" suffix. // (2) By changing the "sortDirection" attribute, a different // CSS style will be used, thus changing the icon based on // the "sortDirection" parameter. //------------------------------------------------------------------------ updateSortIndicator : function(column,sortDirection) { // set the sort indicator on the column we are sorted by if (column) { var sortedColumn = document.getElementById(column); if (sortedColumn) { sortedColumn.setAttribute("sortDirection",sortDirection); // remove the sort indicator from all the columns // except the one we are sorted by var currCol = sortedColumn.parentNode.firstChild; while (currCol) { if (currCol != sortedColumn && currCol.localName == "treecol") { currCol.removeAttribute("sortDirection"); } currCol = currCol.nextSibling; } } } } }; --- NEW FILE: appRoutines.js --- /*** -*- Mode: Javascript; tab-width: 2; The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is Mozdev Group, Inc. code. The Initial Developer of the Original Code is Pete Collins. Portions created by Mozdev Group, Inc. are Copyright (C) 2003 Mozdev Group, Inc. All Rights Reserved. Contributor(s): Pete Collins <pe...@mo...)> (original author) ***/ /****************** Globals **********************/ if(typeof(JS_LIB_LOADED)=='boolean') { const JS_APPROUTINES_FILE = "appRoutines.js"; const JS_APPROUTINES_LOADED = true; /****************** Common Application Routines *********************/ /**************************** QUIT **********************************/ function quit() { try { var windowManager = C.classes['@mozilla.org/appshell/window-mediator;1'] .getService(C.interfaces.nsIWindowMediator); var enumerator = windowManager.getEnumerator(null); // we are only closing dom windows for now // var appShell = C.classes['@mozilla.org/appshell/appShellService;1'].getService(); // appShell = appShell.QueryInterface(C.interfaces.nsIAppShellService); while (enumerator.hasMoreElements()) { var domWindow = enumerator.getNext(); if (("tryToClose" in domWindow) && !domWindow.tryToClose()) return false; domWindow.close(); } // we are only closing dom windows for now // appShell.quit(C.interfaces.nsIAppShellService.eAttemptQuit); } catch (e) { jslibPrint(e); } return true; } /**************************** QUIT **********************************/ jslibDebug('*** load: '+JS_APPROUTINES_FILE+' OK'); } // END BLOCK JS_LIB_LOADED CHECK // If jslib base library is not loaded, dump this error. else { dump("JS_BASE library not loaded:\n" + " \tTo load use: chrome://slashhack/content/resources/js/jslib/jslib.js\n" + " \tThen: include('chrome://slashhack/content/resources/js/jslib/xul/appRoutines.js');\n\n"); }; // END FileSystem Class --- NEW FILE: commonDialog.js --- /*** -*- Mode: Javascript; tab-width: 2; The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is Urban Rage Software code. The Initial Developer of the Original Code is Eric Plaster. Portions created by Urban Rage Software are Copyright (C) 2000 Urban Rage Software. All Rights Reserved. Contributor(s): Eric Plaster <pl...@ur...)> (original author) ***/ // // The idea behind these globals, is that it's easier to catch a spelling mistake // from a javascript error, rather then just a silent bug. For example if you typed 'bob' // for the type, everything would work, except the dialog wouldn't have an image. // /****************** Globals **********************/ /**** User defines (to make it easier for the user) ****/ const JS_CD_MESSAGE = "message"; const JS_CD_ALERT = "alert"; const JS_CD_ERROR = "error"; const JS_CD_QUESTION = "question"; const JS_CD_YESNO = "yesno"; const JS_CD_YES = "yes"; const JS_CD_NO = "no"; const JS_CD_OKCANCEL = "okcancel"; const JS_CD_OK = "ok"; const JS_CD_CANCEL = "cancel"; const JS_CD_RIGHT = "right"; const JS_CD_LEFT = "left"; const JS_CD_CENTER = "center"; /**** Internal consts ****/ const JS_CD_BTN_OK = "OK"; const JS_CD_BTN_CANCEL = "Cancel"; const JS_CD_BTN_YES = "Yes"; const JS_CD_BTN_NO = "No"; const JS_CD_FILE = "commonDialog.js"; /****************** Globals **********************/ if(typeof(JS_LIB_LOADED)=='boolean') { /****************** Common Dialog Object Class *********************/ function CommonDialog(aType, aButtontype, aButtonAlign) { return this.initDialog(aType, aButtontype, aButtonAlign); } // constructor CommonDialog.prototype = { mDialogArgs : null, mDone : false, /********************* INIT Dialog ************************ * void initDialog * * INTERNAL MEMBER FUNCTION * This initializes the object and sets the type, buttonType * and buttonAlign for the object (if passed in). * * param name and discription * aType A dialog type. * aButtonType A "pre-built" set of buttons * aButtonAlign A button alignment * * return values on success and failure * NA * * useage: * this.initDialog(type, buttontype, buttonalign); ****************************************************/ initDialog : function(aType, aButtonType, aButtonAlign) { this.mDialogArgs = new Object; if(aType) { this.type = aType; } if(aButtonType) { this.buttonType = aButtonType; } if(aButtonAlign) { this.mDialogArgs.buttonalign = aButtonAlign; } // set the left most button as the default button. this.buttonDefault = 0; }, /********************* Type ************************ * void getter Type * * Returns a valid dialog type (if set), null otherwise. * Types can be: JS_CD_MESSAGE, JS_CD_ALERT, JS_CD_ERROR * or JS_CD_QUESTION * * return values on success and failure * A valid type or null if unset. * * useage: * <type> = obj.type; * ****************************************************/ get type() { return this.mDialogArgs.type; }, /********************* Type ************************ * void setter Type * * Sets the type of dialog. Types can be: * JS_CD_MESSAGE, JS_CD_ALERT, JS_CD_ERROR or JS_CD_QUESTION * * This really only changes the icon, but may change other attributes * in the future. * * param name and discription * aType A dialog type. * * return values on success and failure * NA * useage: * obj.type = <type> ****************************************************/ set type(aType) { if(!aType) { jslibError(null, "Missing argument\n", "NS_ERROR_XPC_NOT_ENOUGH_ARGS", JS_CD_FILE+": set type"); throw Components.results.NS_ERROR_XPC_NOT_ENOUGH_ARGS; } this.mDialogArgs.type = aType; }, /********************* buttonType ************************ * void setter buttonType * * Sets the buttons to one of the "pre-built" buttons: * JS_CD_MESSAGE, JS_CD_ALERT, JS_CD_ERROR or JS_CD_QUESTION. * * param name and discription * aType A "pre-built" set of buttons * * return values on success and failure * NA * useage: * obj.buttonType = <type> ****************************************************/ set buttonType(aType) { if(!aType) { jslibError(null, "Missing argument\n", "NS_ERROR_XPC_NOT_ENOUGH_ARGS", JS_CD_FILE+": set buttonType"); throw Components.results.NS_ERROR_XPC_NOT_ENOUGH_ARGS; } this.mDialogArgs.buttonList = new Array; if(aType == JS_CD_OKCANCEL) { this.mDialogArgs.buttonList[0] = JS_CD_BTN_OK; this.mDialogArgs.buttonList[1] = JS_CD_BTN_CANCEL; } else if(aType == JS_CD_OK) { this.mDialogArgs.buttonList[0] = JS_CD_BTN_OK; } else if(aType == JS_CD_CANCEL) { this.mDialogArgs.buttonList[0] = JS_CD_BTN_CANCEL; } else if(aType == JS_CD_YESNO) { this.mDialogArgs.buttonList[0] = JS_CD_BTN_YES; this.mDialogArgs.buttonList[1] = JS_CD_BTN_NO; } else if(aType == JS_CD_YES) { this.mDialogArgs.buttonList[0] = JS_CD_BTN_YES; } else if(aType == JS_CD_NO) { this.mDialogArgs.buttonList[0] = JS_CD_BTN_NO; } }, /********************* defaultButton ************************ * void getter defaultButton * * Returns the number coresponding to the default button. * Buttons are numbered from left to right starting with zero. * * return values on success and failure * aNumber The number coresponding to the default button * * useage: * <number> = obj.defaultButton; ****************************************************/ get defaultButton() { return this.mDialogArgs.defaultButton; }, /********************* defaultButton ************************ * void setter defaultButton * * Sets he number coresponding to the default button. Buttons * are numbered from left to right starting with zero. * * param name and discription * aNum A number coresponding to the default button. * * return values on success and failure * NA * * useage: * obj.buttonType = <type> ****************************************************/ set defaultButton(aNum) { if(!aNum) { jslibError(null, "Missing argument\n", "NS_ERROR_XPC_NOT_ENOUGH_ARGS", JS_CD_FILE+": set defaultButton"); throw Components.results.NS_ERROR_XPC_NOT_ENOUGH_ARGS; } this.mDialogArgs.defaultButton = aNum; }, /********************* message ************************ * void getter message * * Returns the current message that is displayed to the user. * * return values on success and failure * aMessage The current message. * * useage: * <string> = obj.message; ****************************************************/ get message() { return this.mDialogArgs.message; }, /********************* message ************************ * void setter message * * Sets the current message. * * param name and discription * aMessage A string to set the message to. * * return values on success and failure * NA * * useage: * obj.message = <string> ****************************************************/ set message(aMessage) { if(!aMessage) { jslibError(null, "Missing argument\n", "NS_ERROR_XPC_NOT_ENOUGH_ARGS", JS_CD_FILE+": set message"); throw Components.results.NS_ERROR_XPC_NOT_ENOUGH_ARGS; } this.mDialogArgs.message = aMessage; }, /********************* buttonAlign ************************ * void getter buttonAlign * * Returns the current alignment: JS_CD_LEFT, JS_CD_CENTER, * JS_CD_RIGHT. * * return values on success and failure * aAlign Current button alignment * * useage: * <alignment> = obj.buttonAlign; ****************************************************/ get buttonAlign() { return this.mDialogArgs.buttonalign; }, /********************* buttonAlign ************************ * void setter buttonAlign * * Sets the current alignment: JS_CD_LEFT, JS_CD_CENTER, * JS_CD_RIGHT. * * param name and discription * aAlign A button alignment * * return values on success and failure * NA * * useage: * obj.buttonAlign = <alignment> ****************************************************/ set buttonAlign(aAlignment) { if(!aAlignment) { jslibError(null, "Missing argument\n", "NS_ERROR_XPC_NOT_ENOUGH_ARGS", JS_CD_FILE+": set buttonAlign"); throw Components.results.NS_ERROR_XPC_NOT_ENOUGH_ARGS; } this.mDialogArgs.buttonalign = aAlignment; }, /********************* appendButton ************************ * void setter appendButton * * Creates a button and appends it to the right side of the * existing buttons. * * param name and discription * aLabel The label for the button * aDefault (optional) set to "true" if it is the default * button. * * return values on success and failure * NA * * useage: * obj.appendButton(label, true); ****************************************************/ appendButton : function (aLabel, aDefault) { if(!aLabel) { jslibError(null, "Missing argument\n", "NS_ERROR_XPC_NOT_ENOUGH_ARGS", JS_CD_FILE+": set appendButton"); throw Components.results.NS_ERROR_XPC_NOT_ENOUGH_ARGS; } if(!this.mDialogArgs.buttonList) { this.mDialogArgs.buttonList = new Array; } this.mDialogArgs.buttonList.push(aLabel); if(aDefault) { this.defaultButton = this.mDialogArgs.buttonList.length -1; } }, /********************* title ************************ * void getter title * * Returns the current dialog title. * * return values on success and failure * aTitle Current dialog title. * * useage: * <string> = obj.title; ****************************************************/ get title() { return this.mDialogArgs.title; }, /********************* title ************************ * void setter buttonAlign * * Sets the current dialog title. * * param name and discription * aTitle Sets the current dialog title. * * return values on success and failure * NA * * useage: * obj.title = <string> ****************************************************/ set title(aTitle) { if(!aTitle) { jslibError(null, "Missing argument\n", "NS_ERROR_XPC_NOT_ENOUGH_ARGS", JS_CD_FILE+": set appendButton"); throw Components.results.NS_ERROR_XPC_NOT_ENOUGH_ARGS; } this.mDialogArgs.title = aTitle; }, /********************* show ************************ * void show * * Creates the dialog and waits for a button push. * * param name and discription * NA * * return values on success and failure * aNumber Returns the button that was clicked. * * useage: * <number> = obj.show(); ****************************************************/ show : function () { window.openDialog("chrome://slashhack/content/resources/js/jslib/xul/content/cdialog.xul", "_blank","chrome,resizeable=no,modal,titlebar,close", this.mDialogArgs); this.mDone = true; return this.mDialogArgs.buttonHit; }, /********************* result ***************************** * void getter result * * Returns an object with two members: object.id and * object.text. "id" is the number ot the button that * was pushed and "label" was the buttons label. * * return values on success and failure * aObj An object with the button that was pushed and * the label of that button. * * useage: * var rv = obj.result; * var which = rv.id; * var text = rv.text; ****************************************************/ get result() { if (this.mDone) { var rv = new Object; rv.id = this.mDialogArgs.buttonHit; rv.text = this.mDialogArgs.buttonHitText; return rv; } else { return null; } }, /********************* help ***************************** * void getter help * * Returns the methods in this object * * return values on success and failure * aStr The methods in this object * * useage: * <string> = obj.help(); ****************************************************/ help : function() { const help = "\n\nFunction and Attribute List:\n" + "\n" + " initDialog(aType, aButtonType, aButtonAlign);\n" + " type = aType;\n" + " buttonType = aButtonType;\n" + " defaultButton = aNum;\n" + " message = aMessage;\n" + " buttonAlign = aAlignment;\n" + " appendButton(aLabel, aDefault);\n" + " title = aTitle;\n" + " show();\n" + " result;\n" return help; } } } // END BLOCK JS_LIB_LOADED CHECK // If jslib base library is not loaded, dump this error. else { dump("JS_BASE library not loaded:\n" + " \tTo load use: chrome://slashhack/content/resources/js/jslib/jslib.js\n" + " \tThen: include('chrome://slashhack/content/resources/js/jslib/xul/commonDialog.js');\n\n"); }; // END FileSystem Class --- NEW FILE: commonFilePicker.js --- /*** -*- Mode: Javascript; tab-width: 2; The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is Mozdev Group, Inc. code. The Initial Developer of the Original Code is Pete Collins. Portions created by Mozdev Group, Inc. are Copyright (C) 2003 Mozdev Group, Inc. All Rights Reserved. Contributor(s): Pete Collins <pe...@mo...> (original author) ***/ /****************** Globals **********************/ if (typeof(JS_LIB_LOADED)=='boolean') { /************* INCLUDE DIR *****************/ if (typeof(JS_DIR_LOADED)!='boolean') include(jslib_dir); /************* INCLUDE DIR *****************/ const JS_COMMONFILEPICKER_FILE = "commonFilePicker.js"; const JS_COMMONFILEPICKER_LOADED = true; const JS_CFP_CID = "@mozilla.org/filepicker;1"; const JS_I_CFP = "nsIFilePicker"; /****************** Common File Picker Functions *********************/ function getFilePicker () { return jslibCreateInstance(JS_CFP_CID, JS_I_CFP); } /********************************************** * * * Both arguments are optional * * * * aDir * * can be either directory path string * * eg: "/tmp" * * or and nsIFile object * * * * aNewFileName * * a new file name string * * eg: "myfile.xul" * * * **********************************************/ function saveAsXUL (aDir, aNewFileName) { var fp = getFilePicker(); fp.init(window, "SaveAsXULFile", 1); fp.appendFilters(C.interfaces.nsIFilePicker.filterXUL); fp.defaultString= aNewFileName ? aNewFileName : "new_file.xul"; if (aDir) { if (typeof(aDir)=="object" && aDir.toString().match("nsIFile")) { if (!aDir.exists()) { jslibDebug("Dir: "+aDir.path+" doesn't exist"); return null; } if (!aDir.isDirectory()) { jslibDebug("Dir: "+aDir.path+" is not a directory"); return null; } fp.displayDirectory = aDir; } if (typeof(aDir)=="string") { jslibPrint("here"); var d = new Dir(aDir); if (!d.exists()) { jslibDebug("Dir: "+d.path+" doesn't exist"); return null; } fp.displayDirectory = d.nsIFile; } } fp.show(); return (fp.file.path.length > 0 ? fp.file.path : null); } jslibDebug('*** load: '+JS_COMMONFILEPICKER_FILE+' OK'); } // END BLOCK JS_LIB_LOADED CHECK // If jslib base library is not loaded, dump this error. else { dump("JS_BASE library not loaded:\n" + " \tTo load use: chrome://slashhack/content/resources/js/jslib/jslib.js\n" + " \tThen: include(jslib_filepicker);\n\n"); }; // END FileSystem Class |