mcfilemanager.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /**
  2. * $Id: mcfilemanager.js 550 2008-11-03 12:12:24Z spocke $
  3. *
  4. * @author Moxiecode
  5. * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved.
  6. */
  7. (function() {
  8. window.mcFileManager = {
  9. settings : {
  10. document_base_url : '',
  11. relative_urls : false,
  12. remove_script_host : false,
  13. use_url_path : true,
  14. remember_last_path : false,
  15. target_elements : '',
  16. target_form : '',
  17. handle : 'file'
  18. },
  19. setup : function() {
  20. var t = this, o, d = document, cp = [];
  21. // Find document_base_url
  22. o = d.location.href;
  23. if (o.indexOf('?') != -1)
  24. o = o.substring(0, o.indexOf('?'));
  25. o = o.substring(0, o.lastIndexOf('/') + 1);
  26. t.settings.default_base_url = unescape(o);
  27. // Find script base URL
  28. function get(nl) {
  29. var i, n;
  30. for (i=0; i<nl.length; i++) {
  31. n = nl[i];
  32. cp.push(n);
  33. if (n.src && /mcfilemanager\.js/g.test(n.src))
  34. return n.src.substring(0, n.src.lastIndexOf('/'));
  35. }
  36. };
  37. o = d.documentElement;
  38. if (o && (o = get(o.getElementsByTagName('script'))))
  39. return t.baseURL = o;
  40. o = d.getElementsByTagName('script');
  41. if (o && (o = get(o)))
  42. return t.baseURL = o;
  43. o = d.getElementsByTagName('head')[0];
  44. if (o && (o = get(o.getElementsByTagName('script'))))
  45. return t.baseURL = o;
  46. },
  47. relaxDomain : function() {
  48. var t = this, p = /(http|https):\/\/([^\/:]+)\/?/.exec(t.baseURL);
  49. // Relax domain
  50. if (p && p[2] != document.location.hostname)
  51. document.domain = t.relaxedDomain = p[2].replace(/.*\.(.+\..+)$/, '$1');
  52. },
  53. init : function(s) {
  54. this.extend(this.settings, s);
  55. },
  56. browse : function(s) {
  57. var t = this;
  58. s = s || {};
  59. if (s.fields) {
  60. s.oninsert = function(o) {
  61. t.each(s.fields.replace(/\s+/g, '').split(/,/), function(v) {
  62. var n;
  63. if (n = document.getElementById(v))
  64. n.value = o.focusedFile.url;
  65. });
  66. };
  67. }
  68. this.openWin({page : 'index.html'}, s);
  69. },
  70. edit : function(s) {
  71. this.openWin({page : 'edit.html', width : 800, height : 500}, s);
  72. },
  73. upload : function(s) {
  74. this.openWin({page : 'upload.html', width : 550, height : 350}, s);
  75. },
  76. createDoc : function(s) {
  77. this.openWin({page : 'createdoc.html', width : 450, height : 280}, s);
  78. },
  79. createDir : function(s) {
  80. this.openWin({page : 'createdir.html', width : 450, height : 280}, s);
  81. },
  82. createZip : function(s) {
  83. this.openWin({page : 'createzip.html', width : 450, height : 280}, s);
  84. },
  85. openWin : function(f, a) {
  86. var t = this, v, w;
  87. t.windowArgs = a = t.extend({}, t.settings, a);
  88. f = t.extend({
  89. x : -1,
  90. y : -1,
  91. width : 800,
  92. height : 500,
  93. inline : 1
  94. }, f);
  95. if (f.x == -1)
  96. f.x = parseInt(screen.width / 2.0) - (f.width / 2.0);
  97. if (f.y == -1)
  98. f.y = parseInt(screen.height / 2.0) - (f.height / 2.0);
  99. if (f.page)
  100. f.url = t.baseURL + '/../index.php?type=fm&page=' + f.page;
  101. if (a.session_id)
  102. f.url += '&sessionid=' + a.session_id;
  103. if (a.custom_data)
  104. f.url += '&custom_data=' + escape(a.custom_data);
  105. if (t.relaxedDomain)
  106. f.url += '&domain=' + escape(t.relaxedDomain);
  107. // Open in specified frame
  108. if (a.target_frame) {
  109. if (v = frames[a.target_frame])
  110. v.document.location = f.url;
  111. if (v = document.getElementById(a.target_frame))
  112. v.src = f.url;
  113. return;
  114. }
  115. // Use TinyMCE window API
  116. if (window.tinymce && tinyMCE.activeEditor)
  117. return tinyMCE.activeEditor.windowManager.open(f, a);
  118. // Use jQuery WindowManager
  119. if (window.jQuery && jQuery.WindowManager)
  120. return jQuery.WindowManager.open(f, a);
  121. // Use native dialogs
  122. w = window.open(f.url, 'mcFileManagerWin', 'left=' + f.x +
  123. ',top=' + f.y + ',width=' + f.width + ',height=' +
  124. f.height + ',scrollbars=' + (f.scrollbars ? 'yes' : 'no') +
  125. ',resizable=' + (f.resizable ? 'yes' : 'no') +
  126. ',statusbar=' + (f.statusbar ? 'yes' : 'no')
  127. );
  128. try {
  129. w.focus();
  130. } catch (ex) {
  131. // Ignore
  132. }
  133. },
  134. each : function(o, f, s) {
  135. var n, l;
  136. if (o) {
  137. s = s || o;
  138. if (o.length !== undefined) {
  139. for (n = 0, l = o.length; n < l; n++)
  140. f.call(s, o[n], n, o);
  141. } else {
  142. for (n in o) {
  143. if (o.hasOwnProperty(n))
  144. f.call(s, o[n], n, o);
  145. }
  146. }
  147. }
  148. },
  149. extend : function() {
  150. var k, a = arguments, t = a[0], i, v;
  151. for (i = 1; i < a.length; i++) {
  152. if (v = a[i]) {
  153. for (k in v)
  154. t[k] = v[k];
  155. }
  156. }
  157. return t;
  158. },
  159. // Legacy functions
  160. open : function(fn, en, url, cb, s) {
  161. var t = this, el;
  162. s = s || {};
  163. // Use input value if it was found
  164. if (!s.url && document.forms[fn] && (el = document.forms[fn].elements[en.split(',')[0]]))
  165. s.url = el.value;
  166. if (!cb) {
  167. s.oninsert = function(o) {
  168. var e, i, v, f = o.focusedFile;
  169. v = en.replace(/\s+/g, '').split(',');
  170. for (i = 0; i < v.length; i++) {
  171. if (e = document.forms[fn][v[i]])
  172. e.value = f.url;
  173. }
  174. };
  175. } else {
  176. if (typeof(cb) == 'string')
  177. cb = window[cb];
  178. s.oninsert = function(o) {
  179. cb(o.focusedFile.url, o);
  180. };
  181. }
  182. t.browse(s);
  183. },
  184. filebrowserCallBack : function(fn, u, ty, w, ask) {
  185. var t = mcFileManager, i, hl, fo, s = {};
  186. // Is imagemanager included, ask it first
  187. if (window.mcImageManager && !ask) {
  188. hl = mcImageManager.settings.handle;
  189. hl = hl.split(',');
  190. for (i = 0; i < hl.length; i++) {
  191. if (ty == hl[i])
  192. fo = 1;
  193. }
  194. if (fo && mcImageManager.filebrowserCallBack(fn, u, ty, w, 1))
  195. return;
  196. }
  197. // Grab filemanager prefixed options
  198. t.each(tinyMCE.activeEditor ? tinyMCE.activeEditor.settings : tinyMCE.settings, function(v, k) {
  199. if (k.indexOf('filemanager_') === 0)
  200. s[k.substring(12)] = v;
  201. });
  202. t.browse(t.extend(s, {
  203. url : w.document.forms[0][fn].value,
  204. relative_urls : 0,
  205. oninsert : function(o) {
  206. var f, u, na;
  207. f = w.document.forms[0];
  208. u = o.focusedFile.url;
  209. inf = o.focusedFile.custom;
  210. // Let TinyMCE convert the URLs
  211. if (typeof(TinyMCE_convertURL) != "undefined")
  212. u = TinyMCE_convertURL(u, null, true);
  213. else if (tinyMCE.convertURL)
  214. u = tinyMCE.convertURL(u, null, true);
  215. else
  216. u = tinyMCE.activeEditor.convertURL(u, null, true);
  217. f[fn].value = u;
  218. // Set alt and title info
  219. if (inf.custom && inf.custom.description) {
  220. na = ['alt', 'title', 'linktitle'];
  221. for (i = 0; i < na.length; i++) {
  222. if (f.elements[na[i]])
  223. f.elements[na[i]].value = inf.custom.description;
  224. }
  225. }
  226. // Try fire onchange
  227. try {
  228. f[fn].onchange();
  229. } catch (e) {
  230. // Ignore
  231. }
  232. w = null; // IE leak
  233. }
  234. }));
  235. return true;
  236. }
  237. };
  238. mcFileManager.setup();
  239. mcFileManager.relaxDomain();
  240. })();