/* Methods and properties added to facilitate creation of external linked tools.
 * This assumes that WeavePath.js has already been loaded. */
/* "use strict"; */

weave.WeavePath.prototype.probe_keyset = weave.path("defaultProbeKeySet");
weave.WeavePath.prototype.selection_keyset = weave.path("defaultSelectionKeySet");
weave.WeavePath.prototype.subset_filter = weave.path("defaultSubsetKeyFilter");

weave.WeavePath._qkeys_to_numeric = {};
weave.WeavePath._numeric_to_qkeys = {};
weave.WeavePath._numeric_key_idx = 0;
weave.WeavePath._keyIdPrefix = "WeaveQKey";

weave.WeavePath.qkeyToIndex = function(key)
{
    var key_str = JSON.stringify([key.keyType.toString(), key.localName.toString()]);

    if (this._qkeys_to_numeric[key_str] == undefined)
    {
        var idx = weave.WeavePath._numeric_key_idx;

        this._numeric_to_qkeys[idx] = key;
        this._qkeys_to_numeric[key_str] = idx;
        
        weave.WeavePath._numeric_key_idx = idx + 1;
    }
    return this._qkeys_to_numeric[key_str];
};

weave.WeavePath.prototype.qkeyToIndex = function (key) { return weave.WeavePath.qkeyToIndex(key); };

weave.WeavePath.indexToQKey = function (index)
{
    return this._numeric_to_qkeys[index];
};

weave.WeavePath.prototype.indexToQKey = function (index) { return weave.WeavePath.indexToQKey(index); };

weave.WeavePath.qkeyToString = function(key)
{
    return this._keyIdPrefix + this.qkeyToIndex(key);
};

weave.WeavePath.prototype.qkeyToString = function (key) { return weave.WeavePath.qkeyToString(key); };

weave.WeavePath.stringToQKey = function(s) 
{
    idx = s.substr(this._keyIdPrefix.length);
    return this.indexToQKey(idx);
};

weave.WeavePath.prototype.stringToQKey = function (s) { return weave.WeavePath.stringToQKey(s); }

/**
 * Creates a new property of the specified type, and binds the appropriate callback.
 * @param name The name of the new property or previously existing property to access.
 * @param type The session state object type of the new property.
 * @param callback A callback taking no arguments to be called when the newly created property changes.
 * @return The current WeavePath object.
 */
weave.WeavePath.prototype.newProperty = function(name, type, callback)
{
    this.push(name).request(type).addCallback(callback, true);
    return this;
};

weave.WeavePath.prototype.getKeys = function(/* [relpath] */)
{
    var raw_keys = this.weave.evaluateExpression(this._path.concat(this._A(arguments, 1)), "this.keys", this._vars);
    var length = raw_keys.length;
    var result = new Array(length);
    for (var idx = 0; idx < length; idx++)
    {
        result[idx] = this.qkeyToString(raw_keys[idx]);
    }
    return result;
};

weave.WeavePath.prototype.setKeys = function(/* [relpath], keyStringArray */)
{
    var args = this._A(arguments, 2);
    if (this._assertParams('setKeys', args))
    {
        var keyStringArray = args.pop();
        var length = keyStringArray.length;
        var keyObjectArray = new Array(length);

        for (var idx = 0; idx < length; idx++)
        {
            keyObjectArray[idx] = this.stringToQKey(keyStringArray[idx]);
        }
        this.push(args).vars({setKeysArgs: keyObjectArray}).exec('replaceKeys(setKeysArgs)');
        return this;
    };
    return this;
};
weave.WeavePath.prototype.filterKeys = function (/* [relpath], keyStringArray */)
{
    var args = this._A(arguments, 2);
    if (this._assertParams('filterKeys', args))
    {
        var keyStringArray = args.pop();
        var keyObjects = keyStringArray.map(this.stringToQKey, this);
        var resultArray = this.push(args).libs("weave.api.WeaveAPI").vars({containsKeysArgs: keyObjects}).getValue(
            'WeaveAPI.QKeyManager.convertToQKeys(containsKeysArgs).filter(function(d) { return containsKey(d); })'
        );
        return resultArray.map(this.qkeyToString, this);
    }
}
weave.WeavePath.prototype.retrieveColumns = function(/* [relpath], columnNameArray */)
{
    var args = this._A(arguments, 2);
    if (this._assertParams('retrieveColumns', args))
    {
        var columnNameArray = args.pop();

        var results = this.push(args).libs("weave.utils.ColumnUtils").vars({retrieveColumnsArgs: columnNameArray}).getValue(
            'ColumnUtils.joinColumns(retrieveColumnsArgs.map(function(name){ return getObject(name); }, this),  null, true)'
        );
        /* Convert the keys to strings */
        results[0] = results[0].map(this.qkeyToString, this);
        return results;
    }
}
@@ -770,31 +770,20 @@
 r idx = 
-weave.WeavePa
 th
+is
 ._numeri
@@ -901,31 +901,20 @@
         
-weave.WeavePa
 th
+is
 ._numeri
@@ -1031,32 +1031,8 @@
 ex =
- function (key) %7B return
  wea
@@ -1055,24 +1055,38 @@
 yToIndex
-(key); %7D
+.bind(weave.WeavePath)
 ;%0A%0Aweave
@@ -1218,34 +1218,8 @@
 ey =
- function (index) %7B return
  wea
@@ -1246,18 +1246,30 @@
 QKey
-(index); %7D
+.bind(weave.WeavePath)
 ;%0A%0Aw
@@ -1416,32 +1416,8 @@
 ng =
- function (key) %7B return
  wea
@@ -1445,16 +1445,30 @@
 ring
-(key); %7D
+.bind(weave.WeavePath)
 ;%0A%0Aw
@@ -1640,30 +1640,8 @@
 ey =
- function (s) %7B return
  wea
@@ -1669,14 +1669,31 @@
 QKey
-(s); %7D
+.bind(weave.WeavePath);
 %0A%0A/*

making the code more consistent.