ActiveState Community

Find ko namespace from dialogs

Posted by dafi on 2008-05-04 06:38
OS: All / Any

I would understand what is the correct way to access to 'ko' namespace from dialogs for example to use ko.dialogs or ko.mru (and so on).

I've found a solution but IMHO isn't good.

My first approach was to access to 'opener' attribute as shown below

opener.ko.dialogs.yesNoCancel(...)
opener.ko.mru.addFromACTextbox(...)

This worked fine but when a dialog opens a new dialog the code must call opener twice

opener.opener.ko.dialogs.yesNoCancel(...)
opener.opener.ko.mru.addFromACTextbox(...)

So I've created a little function iterating to opener.
Using the function the dialog doens't need to now how many opener it has.

function findKomodo(parentWindow) {
    if (parentWindow == null || typeof(parentWindow) == "undefined") {
        parentWindow = window;
    }
    while (parentWindow && !("ko" in parentWindow)) {
        parentWindow = parentWindow.opener;
    }
    return parentWindow.ko;
}

Is this correct? Does exist a better way?
thanks in advance

davide

ToddW | Sun, 2008-05-04 08:23

Hi Dafi,

Your approach is pretty close to what Komodo is already doing in places.

When you include Komodo js files from a xul dialog, it will create a new Komodo namespace specifically for that dialog/window.

If you need access to the main window Komodo namespace, you can use the utility functions provided by the "windowManager" library:

<script src="chrome://komodo/content/library/windowManager.js"        type="application/x-javascript;version=1.7"/>
<script src="chrome://komodo/content/library/windowManager.js"        type="application/x-javascript;version=1.7"/>

Then in your dialog js code, you can make calls to the main namespace like this:

var mainWindow = ko.windowManager.getMainWindow();
var mainNS = mainWindow.ko;
mainNS.xyz;
var mainWindow = ko.windowManager.getMainWindow();
var mainNS = mainWindow.ko;
mainNS.xyz;

Cheers,
Todd

dafi | Sun, 2008-05-04 08:37

Sounds better than my findKomodo

thanks

IMHO this can be posted on wiki
--
dafi
Enhance KomodoEdit with MoreKomodo

shanec | Mon, 2008-05-05 10:04

Just import any script you need into your dialog. If you need dialogs,

<script src="chrome://komodo/content/library/dialogs.js"        type="application/x-javascript;version=1.7"/>
<script src="chrome://komodo/content/library/mru.js"        type="application/x-javascript;version=1.7"/>
<script src="chrome://komodo/content/library/dialogs.js"        type="application/x-javascript;version=1.7"/>
<script src="chrome://komodo/content/library/mru.js"        type="application/x-javascript;version=1.7"/>

You probably need to include a few other files as well, look at some of the dialogs in the content/dialogs directory for examples. Often you must include at least chrome://xtk/content/xtk.js and library/logging.js.

Using the main window's namespace is necessary in some instances, and can be a convenience.

dafi | Sat, 2008-05-10 07:57

I've tried both options and definitively including scripts is my preferred one.

I prefer script inclusion because it doesn't require existing code modification, I can continue to write ko.xxx.yyy instead of ko.windowManager.getMainWindow().ko.xxx.yyy

thanks for your help, this makes code clearer.

--
davide