CustomSearchExtensionHandler.jsx
Summary
Demonstrates how to implement a custom node search with a node-handling extension.
See:
function CustomSearchExtensionHandler() {
$.level = 1;
this.requiredContext = "\tNeed to be running in context of Bridge\n";
}
CustomSearchExtensionHandler.prototype.run = function()
{
if(!this.canRun())
{
return false;
}
$.writeln("About to run CustomSearchExtensionHandler");
var root = new SDKNode("CSRoot", true);
root.addNode(new SDKNode("CSFolder1", true));
var lf = new SDKNode("RootLeaf", false);
root.addNode(lf);
var n = new SDKNode("leaf1", false);
var d = root.findChildNode("CSFolder1");
d.addNode(n);
root.addNode(new SDKNode("CSFolder2", true));
n = new SDKNode("leaf2", false);
d = root.findChildNode("CSFolder2");
d.addNode(n);
n = new SDKNode("leaf3", false);
d = root.findChildNode("CSFolder2");
d.addNode(n);
root.getChildNode("CSFolder2").addNode(new SDKNode("CSSubFolder1", true));
n = new SDKNode("leaf4", false);
d = root.findChildNode("CSSubFolder1");
d.addNode(n);
var sys = new SDKSystem("bridge:csNode:");
sys.addRoot(root);
var csHandler = new ExtensionHandler("csHandler");
app.registerExtension(csHandler);
app.registerPrefix( "bridge:csNode:", csHandler);
csHandler.getBridgeUriForPath = function(path)
{
return path;
}
ExtensionHandler.prototype.getThumbsToSearch = function(kids)
{
for(var h = 0;h < kids.length;h++)
{
if(kids[h].isContainer())
{
this.getThumbsToSearch(kids[h].getChildren());
}
csHandler.thumbsToSearch.push(kids[h]);
}
}
csHandler.getBridgeUriForSearch = function(scope, spec)
{
var randomnumber=Math.floor(Math.random()*101);
var d = new Date();
var postfix = d.getSeconds() + randomnumber;
var sNode = new SDKNode("SearchNode" + postfix, true)
sNode.children = new Array();
sys.addSearchNode(sNode);
var sThumb = new Thumbnail("bridge:csNode:/SearchNode" + postfix);
sThumb.core.children.cacheData.status = "bad";
var searchScopes = spec.scopeSpecifiers;
csHandler.thumbsToSearch = new Array();
var scopeNode = sys.getNodeFromPath(scope.uri);
var children;
if(searchScopes[0] == "DoSubFolders")
{
this.getThumbsToSearch(scopeNode.getChildren());
children = csHandler.thumbsToSearch;
}
else
{
children = scopeNode.getChildren();
}
var conditions = spec.conditionList;
if(spec.conjunction == "or")
{
for(var i = 0;i < children.length;i++)
{
var currentNode = children[i];
for(j = 0;j < conditions.length;j++)
{
var currentCon = conditions[j]
var searchField = currentCon.searchField;
var operand = currentCon.operand;
switch(searchField)
{
var thumbFieldValue;
case "CustomString":
thumbFieldValue = currentNode.getCustomString();
break;
case "CustomNumber":
thumbFieldValue = currentNode.getCustomNumber();
break;
case "CustomBool":
thumbFieldValue = currentNode.getCustomBool();
break;
case "name":
thumbFieldValue = currentNode.getName();
break;
}
switch(currentCon.operatorType)
{
case "equals":
if(thumbFieldValue == operand || thumbFieldValue.toString() == operand)
{
addNodeToSearchResult(currentNode);
}
break;
case "lessThanOrEqual":
if(thumbFieldValue <= operand)
{
addNodeToSearchResult(currentNode);
}
break;
case "greaterThanOrEqual":
if(thumbFieldValue >= operand)
{
addNodeToSearchResult(currentNode);
}
break;
case "endsWith":
var s = thumbFieldValue.substr((thumbFieldValue.length - operand.length), operand.length );
if(s == operand)
{
addNodeToSearchResult(currentNode);
}
break;
default:
}
}
}
}
else if(spec.conjunction == "and")
{
alert("SDK: CustomSearchExtensionHandler:\nSample does not show this type of search!");
}
function addNodeToSearchResult(node)
{
var matches = sNode.getChildren();
var addNode = true;
for(var i = 0;i < matches.length;i++)
{
if(matches[i].getPath() == node.getPath())
{
addNode = false;
break;
}
}
if(addNode)
{
sNode.addNode(node);
}
}
return sThumb.uri;
}
var wrapperObject = this;
csHandler.makeModel = function(path)
{
csModel = new ExtensionModel(path);
csModel.path = path;
this.path = path;
wrapperObject.initModel(csModel, sys);
return csModel;
}
var csDescriptions = new Array();
csDescriptions[0] = new InfosetMemberDescription( "CustomString", "String" );
csDescriptions[1] = new InfosetMemberDescription( "CustomNumber", "Number" );
csDescriptions[2] = new InfosetMemberDescription( "CustomDate", "String" );
csDescriptions[3] = new InfosetMemberDescription( "CustomArray", "Array of String" );
csDescriptions[4] = new InfosetMemberDescription( "CustomBool", "Boolean" );
var customInfoset = new Infoset("customInfoset", csDescriptions);
app.registerInfoset(csHandler, customInfoset);
onEvent = function(event)
{
if(event.object instanceof Document)
{
if(event.type == "selectionsChanged")
{
if(event.object.selections.length > 0)
{
var thumb = event.object.selections[0];
if(thumb.csHandler)
{
stringText.text = thumb.csHandler.customInfoset.CustomString;
numbersText.text = thumb.csHandler.customInfoset.CustomNumber;
dateText.text = thumb.csHandler.customInfoset.CustomDate;
arrStringText.text = thumb.csHandler.customInfoset.CustomArray.toString();
boolList.selection = (thumb.csHandler.customInfoset.CustomBool) ? 0 : 1;
customSearchDataPalette.content.visible = true;
}
else
{
customSearchDataPalette.content.visible= false;
}
}
else
{
customSearchDataPalette.content.visible= false;
}
}
}
}
app.eventHandlers.push({handler: onEvent});
var customSearchDataPalette = new TabbedPalette(app.document, "SDK: Custom Search Infoset Data", "SDKCustomInfoPaletteSearch", "script");
var win = customSearchDataPalette.content;
var stringLabel = win.add("statictext", [5, 9, 115, 25], "Custom String");
var stringText = win.add("edittext", [120, 5, 320, 25], "");
stringText.enabled = false;
var numbersLabel = win.add("statictext", [5, 39, 115, 55], "Custom Number");
var numbersText = win.add("edittext", [120, 35, 320, 55], "");
numbersText.enabled = false;
var dateLabel = win.add("statictext", [5, 69, 115, 85], "Custom Date");
var dateText = win.add("edittext", [120, 65, 320, 85], "");
dateText.enabled = false;
var arrLabel = win.add("statictext", [5, 99, 115, 115], "Custom Array");
var arrStringText = win.add("edittext", [120, 95, 320, 115], "");
arrStringText.enabled = false;
var boolLabel = win.add("statictext", [5, 129, 115, 145], "Custom Boolean");
var boolList = win.add("dropdownlist", [120, 125, 320, 145]);
var tItem = boolList.add("item", "True");
tItem.val = true;
var fItem = boolList.add("item", "False");
fItem.val = false;
boolList.enabled = false;
customSearchDataPalette.content.visible = false;
var t = new Thumbnail("bridge:csNode:/CSRoot");
app.favorites.insert(t);
t.refresh();
$.writeln("CustomSearchExtensionHandler initialized successfully");
return true;
}
CustomSearchExtensionHandler.prototype.canRun = function() {
if(BridgeTalk.appName == "bridge") {
for(index in app.extensions)
{
if(app.extensions[index].name == "csHandler")
{
$.writeln("ERROR: Cannont run twice! You must restart Bridge");
return false;
}
}
return true;
}
$.writeln("ERROR:: Cannot run CustomSearchExtensionHandler");
$.writeln(this.requiredContext);
return false;
}
CustomSearchExtensionHandler.prototype.initModel = function(csModel, sys)
{
csModel.getSearchDefinition = function()
{
var searchField = "CustomString";
var opType = "string";
var searchFieldDisplay = "Custom String";
var searchCriteria1 = new SearchCriteria(searchField, opType, searchFieldDisplay);
searchCriteria1.operatorTypesToDisable = ["exists", "equals", "doesNotExist", "doesNotEqual", "less",
"lessThanOrEqual", "greaterThanOrEqual", "contains",
"doesNotContain", "startsWith", "greater"];
searchField = "CustomNumber";
opType = "float";
searchFieldDisplay = "Custom Number";
var searchCriteria2 = new SearchCriteria(searchField, opType, searchFieldDisplay);
searchCriteria2.operatorTypesToDisable = ["exists", "doesNotExist", "doesNotEqual", "less",
"contains", "doesNotContain", "endsWith",
"startsWith", "greater"];
searchField = "name";
opType = "string";
searchFieldDisplay = "Name";
var searchCriteria3 = new SearchCriteria(searchField, opType, searchFieldDisplay);
searchCriteria3.operatorTypesToDisable = ["exists", "doesNotExist", "doesNotEqual", "less",
"lessThanOrEqual", "greaterThanOrEqual", "contains",
"doesNotContain", "endsWith", "startsWith", "greater"];
searchField = "CustomBool";
opType = "boolean";
searchFieldDisplay = "Custom Boolean";
var op1 = new Operand("true", "A true value");
var op2 = new Operand("false", "A false value");
var myOperators = [op1, op2];
var searchCriteria4 = new SearchCriteria(searchField, opType, searchFieldDisplay, myOperators);
searchCriteria4.operatorTypesToDisable = ["exists", "doesNotExist", "doesNotEqual", "less",
"lessThanOrEqual", "greaterThanOrEqual", "contains",
"doesNotContain", "endsWith", "startsWith", "greater"];
var critList = [searchCriteria1, searchCriteria2, searchCriteria3, searchCriteria4];
var ranksArray = [];
var scope1 = new Scope( "DoSubFolders", "SDKSearch: Search subfolders");
var scope2 = new Scope("DoHidden", "SDKSearch: Search hidden files");
var scopesArray = [scope1, scope2];
try
{
var searchDef = new SearchDefinition(critList, 10, ranksArray, scopesArray);
}
catch(error) { alert(error); }
return searchDef;
}
csModel.needAuthentication = function()
{
return false;
}
csModel.authenticate = function()
{
return;
}
csModel.exists = function()
{
return true;
}
csModel.initialize = function()
{
this.privateData.node = sys.getNodeFromPath(this.path);
}
csModel.registerInterest = function(cacheElement)
{
this.privateData.cacheElement = cacheElement;
}
csModel.unregisterInterest = function()
{
this.privateData.cacheElement = undefined;
}
csModel.getParent = function()
{
var parent = this.privateData.node.getParent();
if(parent != undefined)
{
return (sys.getPrefix() + parent.getPath());
}
}
csModel.refreshInfoset = function(infosetName)
{
var infoset = this.privateData.cacheElement[infosetName];
var currentNode = this.privateData.node;
try
{
if(infosetName == "immediate" || infosetName == "all")
{
if(currentNode.getName().substr(0,10) == "SearchNode")
{
infoset.name = "SearchNode";
}
else
{
infoset.name = currentNode.getName();
infoset.isLink = false;
}
infoset.isContainer = currentNode.isContainer();
infoset.cacheData.status = "good";
}
if(infosetName == "item" || infosetName == "all")
{
if(currentNode.getName().substr(0,10) != "SearchNode")
{
if(currentNode.isContainer())
{
infoset.canSearch = true;
}
}
infoset.cacheData.status = "good";
}
if(infosetName == "children" || infosetName == "all")
{
var arr = currentNode.getChildren();
for(var j = 0;j < arr.length;j++)
{
infoset.addChild(sys.getPrefix() + arr[j].getPath());
}
infoset.cacheData.status = "good";
}
if(infosetName == "icon" || infosetName == "all")
{
infoset.bitmap = currentNode.getIcon();
infoset.cacheData.status = "good";
}
if(infosetName == "thumbnail" || infosetName == "all")
{
infoset.thumbnail = currentNode.getThumb();
infoset.cacheData.status = "good";
}
if(infosetName == "quickMetadata" || infosetName == "all")
{
infoset.colorMode = -1;
infoset.hasRawSetting = false;
infoset.hasCrop = false;
infoset.height = 300;
infoset.rating = 0;
infoset.rotation = 90;
infoset.stockPhotoStatus = 0;
infoset.xResolution = 0;
infosetyResolution = 0;
infoset.width = 300;
infoset.cacheData.status = "good";
}
if(infosetName == "itemContent" || infosetName == "all")
{
infoset.canGetPreview = false;
infoset.cacheData.status = "good";
}
if(infosetName == "preview" || infosetName == "all")
{
infoset.preview = currentNode.getPreview();
infoset.cacheData.status = "good";
}
if(infosetName == "customInfoset" || "all")
{
this.privateData.cacheElement.customInfoset.CustomString = this.privateData.node.getCustomString();
this.privateData.cacheElement.customInfoset.CustomNumber = this.privateData.node.getCustomNumber();
this.privateData.cacheElement.customInfoset.CustomDate = this.privateData.node.getCustomDate();
this.privateData.cacheElement.customInfoset.CustomArray = this.privateData.node.getCustomArray();
this.privateData.cacheElement.customInfoset.CustomBool = this.privateData.node.getCustomBool();
this.privateData.cacheElement.customInfoset.cacheData.status = "good";
}
}
catch(error) { alert(error); }
}
csModel.getCacheStatus = function( infosetName)
{
if(typeof this.privateData.cacheElement == "undefined")
{
return "bad";
}
if(typeof this.privateData.cacheElement.infosetName== "undefined")
{
var ce = this.privateData.cacheElement;
var info = ce[infosetName];
var cd = info.cacheData;
var stat = cd.status;
return stat;
}
return this.privateData.cacheElement[infosetName].cacheData.status;
}
csModel.terminate = function()
{
return;
}
}
#include SDKSystem.jsx
#include SDKNode.jsx
if(typeof(CustomSearchExtensionHandler_unitTest) == "undefined") {
new CustomSearchExtensionHandler().run();
}
http://www.adobe.com/devnet/bridge
Documentation generated by
JSDoc on Tue Apr 27 10:21:34 2010