(Created page with 'Writing plugins for CKFinder might be fun when you understand how to create them. Below you'll find a short instruction how to do it. == Creating a plugin == You can create you…') |
(Template contents proof-read and formatted) |
||
Line 1: | Line 1: | ||
− | Writing plugins for CKFinder might be fun when you understand how to create them. Below you | + | __TOC__ |
− | + | {{#CUSTOMTITLE:Writing JavaScript Plugins}} | |
− | + | Writing plugins for CKFinder might be fun when you understand how to create them. Below you will find some basic guidelines on how to do it. | |
+ | == Creating a Plugin == | ||
You can create your own plugin in a four simple steps. | You can create your own plugin in a four simple steps. | ||
− | ==== Step1: Create | + | ==== Step1: Create the Plugin Folder ==== |
− | + | Create a directory for your plugin inside of the <code>plugins</code> directory (by default CKFinder comes with three plugins built-in: <code>dummy</code>, <code>fileeditor</code>, <code>imagreresize</code>). Let us use <code>myplugin</code> as the plugin name and use the same name for our new folder. | |
− | Create a directory for your plugin inside of the | ||
− | |||
− | |||
− | Inside | + | ==== Step2: Create the Plugin File ==== |
+ | Inside your plugin folder (<code>myplugin</code>) create an empty file named <code>plugin.js</code>. | ||
− | ==== Step3: Add | + | ==== Step3: Add Plugin Definition ==== |
+ | Plugins are registered with the <code>[http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinder.html#.addPlugin CKFinder.addPlugin]</code> function. | ||
− | + | <source lang="javascript"> | |
− | |||
− | <source> | ||
CKFinder.addPlugin( 'myplugin', function( api ) { | CKFinder.addPlugin( 'myplugin', function( api ) { | ||
var toolId = api.addToolPanel( '<h3>My tool</h3><p>Sample content.</p>' ); | var toolId = api.addToolPanel( '<h3>My tool</h3><p>Sample content.</p>' ); | ||
Line 24: | Line 22: | ||
</source> | </source> | ||
− | (Paste the code above to plugin.js and save the file.) | + | (Paste the code above to <code>plugin.js</code> and save the file.) |
− | The plugin definition might be a function (where [http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinderAPI.html CKFinderAPI] is passed as the first argument) or an object. See [http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinder.html#.addPlugin addPlugin documentation] for more details. | + | The plugin definition might be a function (where <code>[http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinderAPI.html CKFinderAPI]</code> is passed as the first argument) or an object. See [http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinder.html#.addPlugin <code>addPlugin</code> function documentation] for more details. |
− | ==== Step4: Enable | + | ==== Step4: Enable the Plugin ==== |
+ | To enable a plugin, use the <code>[http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinder.config.html#.extraPlugins extraPlugins]</code> configuration option in the <code>config.js</code> file: | ||
− | + | <source lang="javascript"> | |
− | |||
− | <source> | ||
CKFinder.customConfig = function( config ) | CKFinder.customConfig = function( config ) | ||
{ | { | ||
Line 39: | Line 36: | ||
</source> | </source> | ||
− | == Creating a | + | == Creating a Plugin — the Shorter Way == |
− | Sometimes (usually when developing a new plugin) it is more suitable to have the plugin definition in a single place, where | + | Sometimes (usually when developing a new plugin) it is more suitable to have the plugin definition in a single place, where CKFinder is created, instead of having it in a separate file (for example to avoid having to clear the browser cache). |
==== Example ==== | ==== Example ==== | ||
− | <source> | + | <source lang="javascript"> |
CKFinder.addPlugin( 'myplugin', function( api ) { | CKFinder.addPlugin( 'myplugin', function( api ) { | ||
api.addFileContextMenuOption( { label : 'My plugin', command : "myplugincommand" } , function( api, file ) | api.addFileContextMenuOption( { label : 'My plugin', command : "myplugincommand" } , function( api, file ) | ||
Line 56: | Line 53: | ||
var finder = new CKFinder(); | var finder = new CKFinder(); | ||
finder.basePath = '/ckfinder/'; | finder.basePath = '/ckfinder/'; | ||
− | // Specify extraPlugins here, no need to change config.js | + | // Specify extraPlugins here, no need to change the config.js file. |
finder.extraPlugins = 'myplugin'; | finder.extraPlugins = 'myplugin'; | ||
finder.create(); | finder.create(); | ||
</source> | </source> | ||
− | See the | + | See the "Public API" sample (<code>_samples/public_api.html</code>) for an example of such approach. |
− | == Localizing a | + | == Localizing a Plugin == |
+ | In the example above we hardcoded some strings (e.g. "Enter a number") that ideally should be moved to a separate file in order to allow translating them into other languages. | ||
− | + | ==== Specify Available Languages ==== | |
+ | Suppose we want to translate our plugin into English, French and Spanish. We will use the <code>lang</code> property to list all available languages. | ||
− | + | <source lang="javascript"> | |
− | |||
− | |||
− | |||
− | |||
CKFinder.addPlugin( 'pluginname', { | CKFinder.addPlugin( 'pluginname', { | ||
// Available languages | // Available languages | ||
Line 85: | Line 80: | ||
</source> | </source> | ||
− | ==== Replace | + | ==== Replace Hardcoded Strings with Variables ==== |
+ | Replace each hardcoded string with a <code>api.lang.[''your plugin name''].[''translation key'']</code> variable. | ||
− | + | ==== Create Language Files ==== | |
+ | Add a <code>lang</code> subfolder inside the plugin folder and create three files: <code>en.js</code>, <code>es.js</code>, and <code>fr.js</code>. | ||
− | |||
− | |||
− | |||
In each file paste the following: | In each file paste the following: | ||
− | <source> | + | <source lang="javascript"> |
// setPluginLang( [Plugin name], [Language code], [Translations] ); | // setPluginLang( [Plugin name], [Language code], [Translations] ); | ||
CKFinder.setPluginLang( 'pluginname', 'en', | CKFinder.setPluginLang( 'pluginname', 'en', | ||
Line 106: | Line 100: | ||
}); | }); | ||
</source> | </source> | ||
− | |||
− | + | After the file is created, you can use the <code>api.lang.pluginname.translationKey</code> variable in the plugin definition instead of the "Application is now ready to use" string. | |
− | + | See the <code>dummy</code> plugin (<code>plugins/dummy</code>) distributed with CKFinder for an example. | |
+ | == Useful Resources == | ||
+ | The following resources might prove useful when creating custom CKFinder plugins: | ||
* [http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinderAPI.html#addFileContextMenuOption Adding file context menu item] | * [http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinderAPI.html#addFileContextMenuOption Adding file context menu item] | ||
* [http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinderAPI.html#addFolderContextMenuOption Adding folder context menu item] | * [http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinderAPI.html#addFolderContextMenuOption Adding folder context menu item] | ||
Line 117: | Line 112: | ||
* [http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinderAPI.html#getSelectedFolder Getting selected folder] | * [http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinderAPI.html#getSelectedFolder Getting selected folder] | ||
* [http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinder.dataTypes.Connector.html#sendCommand Calling the server connector] | * [http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinder.dataTypes.Connector.html#sendCommand Calling the server connector] | ||
− | * [http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinder.dialog.html#.add Creating a dialog] | + | * [http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinder.dialog.html#.add Creating a dialog window] |
− | * [http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinderAPI.html#addToolPanel Adding tool box] (on the left side of the application) | + | * [http://docs.cksource.com/ckfinder_2.x_api/symbols/CKFinderAPI.html#addToolPanel Adding a tool box] (on the left side of the application) |
* [http://docs.cksource.com/ckfinder_2.x_api/ Full API documentation] | * [http://docs.cksource.com/ckfinder_2.x_api/ Full API documentation] | ||
− | |||
− |
Latest revision as of 11:37, 1 April 2011
Contents
Writing plugins for CKFinder might be fun when you understand how to create them. Below you will find some basic guidelines on how to do it.
Creating a Plugin
You can create your own plugin in a four simple steps.
Step1: Create the Plugin Folder
Create a directory for your plugin inside of the plugins
directory (by default CKFinder comes with three plugins built-in: dummy
, fileeditor
, imagreresize
). Let us use myplugin
as the plugin name and use the same name for our new folder.
Step2: Create the Plugin File
Inside your plugin folder (myplugin
) create an empty file named plugin.js
.
Step3: Add Plugin Definition
Plugins are registered with the CKFinder.addPlugin
function.
CKFinder.addPlugin( 'myplugin', function( api ) { var toolId = api.addToolPanel( '<h3>My tool</h3><p>Sample content.</p>' ); api.showTool( toolId ); } );
(Paste the code above to plugin.js
and save the file.)
The plugin definition might be a function (where CKFinderAPI
is passed as the first argument) or an object. See addPlugin
function documentation for more details.
Step4: Enable the Plugin
To enable a plugin, use the extraPlugins
configuration option in the config.js
file:
CKFinder.customConfig = function( config ) { config.extraPlugins = 'myplugin'; };
Creating a Plugin — the Shorter Way
Sometimes (usually when developing a new plugin) it is more suitable to have the plugin definition in a single place, where CKFinder is created, instead of having it in a separate file (for example to avoid having to clear the browser cache).
Example
CKFinder.addPlugin( 'myplugin', function( api ) { api.addFileContextMenuOption( { label : 'My plugin', command : "myplugincommand" } , function( api, file ) { api.openInputDialog( "Enter a number: ", "0", function( value ) { api.openMsgDialog( "You have entered: " + value ); } ); }); }); var finder = new CKFinder(); finder.basePath = '/ckfinder/'; // Specify extraPlugins here, no need to change the config.js file. finder.extraPlugins = 'myplugin'; finder.create();
See the "Public API" sample (_samples/public_api.html
) for an example of such approach.
Localizing a Plugin
In the example above we hardcoded some strings (e.g. "Enter a number") that ideally should be moved to a separate file in order to allow translating them into other languages.
Specify Available Languages
Suppose we want to translate our plugin into English, French and Spanish. We will use the lang
property to list all available languages.
CKFinder.addPlugin( 'pluginname', { // Available languages lang : [ 'en', 'es', 'fr' ], // Plugin definition appReady : function( api ) { // ... api.openMsgDialog( api.lang.pluginname.translationKey ); } });
Replace Hardcoded Strings with Variables
Replace each hardcoded string with a api.lang.[your plugin name].[translation key]
variable.
Create Language Files
Add a lang
subfolder inside the plugin folder and create three files: en.js
, es.js
, and fr.js
.
In each file paste the following:
// setPluginLang( [Plugin name], [Language code], [Translations] ); CKFinder.setPluginLang( 'pluginname', 'en', { pluginname : { translationKey : 'Application is now ready to use', anotherKey : 'Another message' // ... } });
After the file is created, you can use the api.lang.pluginname.translationKey
variable in the plugin definition instead of the "Application is now ready to use" string.
See the dummy
plugin (plugins/dummy
) distributed with CKFinder for an example.
Useful Resources
The following resources might prove useful when creating custom CKFinder plugins: