<input type="text" name="urlField" id="urlField"><input type="button" value="Browse" onclick="openFileBrowser()">Now let's go through the integration methods and define the openFileBrowser function that opens IMCE and makes it fill our url field on file selection.
When IMCE is opened using an url that contains
app=applicationName|fileProperty1@FieldId1|fileProperty2@FieldId2|...
the specified fields are filled with the specified properties of the selected file.
Avaliable file properties are:
In our CASE, we should open IMCE using this URL: /?q=imce&app=myApp|url@urlField which contains our application name and our url field id
function openFileBrowser() {
window.open('/?q=imce&app=myApp|url@urlField', '', 'width=760,height=560,resizable=1');
}That's all we need. Leave the rest to IMCE. It will automatically create an operation tab named "Send to myApp" that sends the file url to our url field. Clicking the files in preview do the same thing as well.
- What if we had another field for another file property e.g, Size: <input type="text" id="file-size"> ?
- We should have opened imce using this URL: /?q=imce&app=myApp|url@urlField|size@file-size
&app=myApp|imceload@myOnloadFunc, IMCE looks for myOnloadFunc in the parent window and executes it with the window parameter referring to IMCE window.
function myOnloadFunc (win) {//any method of imce is available through win.imce
win.imce.setSendTo('Give it to myApplication baby', myFileHandler);//you should also define myFileHandler
}&app=myApp|sendto@myFileHandler, IMCE calls myFileHandler function of the parent window with file and window parameters.
function myFileHandler (file, win) {
$('#urlFieldId').val(file.url);//insert file url into the url field
win.close();//close IMCE
}IMCE introduces two javascript hooks for custom usage. One of them is hookImceFinish function and the other is global hookImceUrl variable. In order to use these hooks one must open IMCE browser by specifying a proper window name with window.open method. That window name plus hook names will be the custom function and the custom URL.
Here is a sample code for opening IMCE with window.open:
window.open('imce/browse', 'windowName', 'width=640, height=480');
For this instance of imce there should be a previously declared function named as windowNameImceFinish and optionally a variable named as windowNameImceUrl. If the function exists, IMCE will call it when the user clicks "add" link. windowNameImceUrl will be used for highlighting if it matches any url in the file list.
This function is called with five parameters:
function windowNameImceFinish(path, w, h, s, imceWin) {
imceWin.close();
alert('selected file path='+ path +', width='+ w +', height='+ h +', filesize='+s);
}
This optional global variable is used for highlighting of the matching url in the file list. In other words, if it's value matches any of the paths in the list, that file is highlighted.
One may change the value of this variable just before opening the IMCE pop-up.
windowNameImceUrl = '/files/images/img.gif';
You can have a look at plain textarea demo in demo page to see the API in action.