Writting Plugins in Javascript(使用Javascript编写插件)

Writting myBase plugins in Javascript is simple. Just write source code using a text editor (e.g. Windows Notepad), and save it as a *.js file within the ./plugins subfolder under myBase setup folder, and then re-launch myBase for new plugins to be registered and shown in menus. Following are the sample code snippets.

编写myBase插件并不复杂。只需要使用文本编辑器(如:写字板)编写源码,保存成.js文件并存在安装目录的./plugins下,然后关闭并重新启动myBase软件即可,新的插件会出现在相应的菜单中。以下提供了一些简单的示例源码。

//DEMO #1: Display the About info;
//显示“关于”信息
alert(about());
//DEMO #2: add a file as attachment in the currently info item;
//把一个文件加入当前信息条目的附件
var nyf=new CNyfDb(-1);
var sFn=platform.getOpenFileName({sTitle: 'Add a file', sFilter: 'Text files(*.txt);;All files(*.*)'});
if(sFn){
    var nBytes=nyf.createFile(plugin.getCurInfoItem()+'/'+'newfile.txt', sFn);
    if(nBytes>=0){
        plugin.refreshDocViews(-1);
        alert('File added.');
    }
}
//DEMO #3: Load a specified .nyf database, traverse the outline tree and show the item titles with indentation;
var sFn=platform.getOpenFileName({sTitle: 'Open file', sFilter: 'nyf files(*.nyf);;all files(*.*)'});
var nyf=new CNyfDb(sFn, false), sTxt='';
nyf.traverseOutline('/Organizer/data/', false, function(sPath, iLevel){
    var sHint=nyf.getFolderHint(sPath);
    if(sHint=='') sHint='new item ...'; else if(sHint==undefined) sHint='secured item ...';
    var sIndent=''; while(iLevel-->0) sIndent+='\t';
    sTxt+=(sIndent+sHint+'\n');
});
alert(sTxt);
//DEMO #4: Use the currently working .nyf database, and display the current item's HTML content;
//显示当前节点的html源码
var nyf=new CNyfDb(-1);
var f=new CLocalFile(plugin.getCurInfoItem(), '_~_~_notes.html');
var sHtml=nyf.loadText(f);
alert(sHtml);
var sTxt=platform.parseFile(sHtml);
alert(sTxt);

The word 'JavaScript' may bring to mind features such as event handlers (like onclick), DOM objects, window.open, and getElementById. But within myBase Javascript APIs, there're no such interfaces at all. myBase exposes a set of application specific classes, objects and functions, such as the 'plugin' and 'platform' objects. In addition, myBase exposes several other classes, such as 'CNyfDb', 'CLocalFile', 'CLocalDir', and 'CAppWord' for manipulating .nyf databases and local files, and exchanging data with Microsoft Office.

通常JavaScript意味着一些诸如事件处理(按键时),DOM组件,window.open或者getElementById的功能,但myBase API并不提供这类接口。myBase API只提供一些与应用相关的类,组件和接口,例如“插件”和“平台”接口。另外,myBase API亦提供了'CNyfDb', 'CLocalFile', 'CLocalDir'和'CAppWord'类,通过这些类,可以实现和myBase数据库引擎的通信、操作本地文件以及和Microsoft Office交换数据。