comparison sites/all/modules/imce/js/imce_set_app.js @ 0:ff03f76ab3fe

initial version
author danieleb <danielebarchiesi@me.com>
date Wed, 21 Aug 2013 18:51:11 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ff03f76ab3fe
1 /*
2 * IMCE Integration by URL
3 * Ex-1: http://example.com/imce?app=XEditor|url@urlFieldId|width@widthFieldId|height@heightFieldId
4 * Creates "Insert file" operation tab, which fills the specified fields with url, width, height properties
5 * of the selected file in the parent window
6 * Ex-2: http://example.com/imce?app=XEditor|sendto@functionName
7 * "Insert file" operation calls parent window's functionName(file, imceWindow)
8 * Ex-3: http://example.com/imce?app=XEditor|imceload@functionName
9 * Parent window's functionName(imceWindow) is called as soon as IMCE UI is ready. Send to operation
10 * needs to be set manually. See imce.setSendTo() method in imce.js
11 */
12
13 (function($) {
14
15 var appFields = {}, appWindow = (top.appiFrm||window).opener || parent;
16
17 // Execute when imce loads.
18 imce.hooks.load.push(function(win) {
19 var index = location.href.lastIndexOf('app=');
20 if (index == -1) return;
21 var data = decodeURIComponent(location.href.substr(index + 4)).split('|');
22 var arr, prop, str, func, appName = data.shift();
23 // Extract fields
24 for (var i = 0, len = data.length; i < len; i++) {
25 str = data[i];
26 if (!str.length) continue;
27 if (str.indexOf('&') != -1) str = str.split('&')[0];
28 arr = str.split('@');
29 if (arr.length > 1) {
30 prop = arr.shift();
31 appFields[prop] = arr.join('@');
32 }
33 }
34 // Run custom onload function if available
35 if (appFields.imceload && (func = isFunc(appFields.imceload))) {
36 func(win);
37 delete appFields.imceload;
38 }
39 // Set custom sendto function. appFinish is the default.
40 var sendtoFunc = appFields.url ? appFinish : false;
41 //check sendto@funcName syntax in URL
42 if (appFields.sendto && (func = isFunc(appFields.sendto))) {
43 sendtoFunc = func;
44 delete appFields.sendto;
45 }
46 // Check old method windowname+ImceFinish.
47 else if (win.name && (func = isFunc(win.name +'ImceFinish'))) {
48 sendtoFunc = func;
49 }
50 // Highlight file
51 if (appFields.url) {
52 // Support multiple url fields url@field1,field2..
53 if (appFields.url.indexOf(',') > -1) {
54 var arr = appFields.url.split(',');
55 for (var i in arr) {
56 if ($('#'+ arr[i], appWindow.document).size()) {
57 appFields.url = arr[i];
58 break;
59 }
60 }
61 }
62 var filename = $('#'+ appFields.url, appWindow.document).val() || '';
63 imce.highlight(filename.substr(filename.lastIndexOf('/')+1));
64 }
65 // Set send to
66 sendtoFunc && imce.setSendTo(Drupal.t('Insert file'), sendtoFunc);
67 });
68
69 // Default sendTo function
70 var appFinish = function(file, win) {
71 var $doc = $(appWindow.document);
72 for (var i in appFields) {
73 $doc.find('#'+ appFields[i]).val(file[i]);
74 }
75 if (appFields.url) {
76 try{
77 $doc.find('#'+ appFields.url).blur().change().focus();
78 }catch(e){
79 try{
80 $doc.find('#'+ appFields.url).trigger('onblur').trigger('onchange').trigger('onfocus');//inline events for IE
81 }catch(e){}
82 }
83 }
84 appWindow.focus();
85 win.close();
86 };
87
88 // Checks if a string is a function name in the given scope.
89 // Returns function reference. Supports x.y.z notation.
90 var isFunc = function(str, scope) {
91 var obj = scope || appWindow;
92 var parts = str.split('.'), len = parts.length;
93 for (var i = 0; i < len && (obj = obj[parts[i]]); i++);
94 return obj && i == len && (typeof obj == 'function' || typeof obj != 'string' && !obj.nodeName && obj.constructor != Array && /^[\s[]?function/.test(obj.toString())) ? obj : false;
95 }
96
97 })(jQuery);