imce_set_app.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. (function($) {
  13. var appFields = {}, appWindow = (top.appiFrm||window).opener || parent;
  14. // Execute when imce loads.
  15. imce.hooks.load.push(function(win) {
  16. var index = location.href.lastIndexOf('app=');
  17. if (index == -1) return;
  18. var data = decodeURIComponent(location.href.substr(index + 4)).split('|');
  19. var arr, prop, str, func, appName = data.shift();
  20. // Extract fields
  21. for (var i = 0, len = data.length; i < len; i++) {
  22. str = data[i];
  23. if (!str.length) continue;
  24. if (str.indexOf('&') != -1) str = str.split('&')[0];
  25. arr = str.split('@');
  26. if (arr.length > 1) {
  27. prop = arr.shift();
  28. appFields[prop] = arr.join('@');
  29. }
  30. }
  31. // Run custom onload function if available
  32. if (appFields.imceload && (func = isFunc(appFields.imceload))) {
  33. func(win);
  34. delete appFields.imceload;
  35. }
  36. // Set custom sendto function. appFinish is the default.
  37. var sendtoFunc = appFields.url ? appFinish : false;
  38. //check sendto@funcName syntax in URL
  39. if (appFields.sendto && (func = isFunc(appFields.sendto))) {
  40. sendtoFunc = func;
  41. delete appFields.sendto;
  42. }
  43. // Check old method windowname+ImceFinish.
  44. else if (win.name && (func = isFunc(win.name +'ImceFinish'))) {
  45. sendtoFunc = func;
  46. }
  47. // Highlight file
  48. if (appFields.url) {
  49. // Support multiple url fields url@field1,field2..
  50. if (appFields.url.indexOf(',') > -1) {
  51. var arr = appFields.url.split(',');
  52. for (var i in arr) {
  53. if ($('#'+ arr[i], appWindow.document).length) {
  54. appFields.url = arr[i];
  55. break;
  56. }
  57. }
  58. }
  59. var filename = $('#'+ appFields.url, appWindow.document).val() || '';
  60. imce.highlight(filename.substr(filename.lastIndexOf('/')+1));
  61. }
  62. // Set send to
  63. sendtoFunc && imce.setSendTo(Drupal.t('Insert file'), sendtoFunc);
  64. });
  65. // Default sendTo function
  66. var appFinish = function(file, win) {
  67. var $doc = $(appWindow.document);
  68. for (var i in appFields) {
  69. $doc.find('#'+ appFields[i]).val(file[i]);
  70. }
  71. if (appFields.url) {
  72. try{
  73. $doc.find('#'+ appFields.url).blur().change().focus();
  74. }catch(e){
  75. try{
  76. $doc.find('#'+ appFields.url).trigger('onblur').trigger('onchange').trigger('onfocus');//inline events for IE
  77. }catch(e){}
  78. }
  79. }
  80. appWindow.focus();
  81. win.close();
  82. };
  83. // Checks if a string is a function name in the given scope.
  84. // Returns function reference. Supports x.y.z notation.
  85. var isFunc = function(str, scope) {
  86. var obj = scope || appWindow;
  87. var parts = str.split('.'), len = parts.length;
  88. for (var i = 0; i < len && (obj = obj[parts[i]]); i++);
  89. return obj && i == len && (typeof obj == 'function' || typeof obj != 'string' && !obj.nodeName && obj.constructor != Array && /^[\s[]?function/.test(obj.toString())) ? obj : false;
  90. }
  91. })(jQuery);