mcimagemanager.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. * $Id: mcimagemanager.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.mcImageManager = {
  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 : 'image,media'
  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 && /mcimagemanager\.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', scrollbars : 'yes'}, 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. view : function(s) {
  77. this.openWin({page : 'view.html', width : 800, height : 500}, s);
  78. },
  79. createDir : function(s) {
  80. this.openWin({page : 'createdir.html', width : 450, height : 280}, s);
  81. },
  82. openWin : function(f, a) {
  83. var t = this, w, v;
  84. t.windowArgs = a = t.extend({}, t.settings, a);
  85. f = t.extend({
  86. x : -1,
  87. y : -1,
  88. width : 800,
  89. height : 500,
  90. inline : 1
  91. }, f);
  92. if (f.x == -1)
  93. f.x = parseInt(screen.width / 2.0) - (f.width / 2.0);
  94. if (f.y == -1)
  95. f.y = parseInt(screen.height / 2.0) - (f.height / 2.0);
  96. if (f.page)
  97. f.url = t.baseURL + '/../index.php?type=im&page=' + f.page;
  98. if (a.session_id)
  99. f.url += '&sessionid=' + a.session_id;
  100. if (a.custom_data)
  101. f.url += '&custom_data=' + escape(a.custom_data);
  102. if (t.relaxedDomain)
  103. f.url += '&domain=' + escape(t.relaxedDomain);
  104. // Open in specified frame
  105. if (a.target_frame) {
  106. if (v = frames[a.target_frame])
  107. v.document.location = f.url;
  108. if (v = document.getElementById(a.target_frame))
  109. v.src = f.url;
  110. return;
  111. }
  112. // Use TinyMCE window API
  113. if (window.tinymce && tinyMCE.activeEditor)
  114. return tinyMCE.activeEditor.windowManager.open(f, a);
  115. // Use jQuery WindowManager
  116. if (window.jQuery && jQuery.WindowManager)
  117. return jQuery.WindowManager.open(f, a);
  118. // Use native dialogs
  119. w = window.open(f.url, 'mcImageManagerWin', 'left=' + f.x +
  120. ',top=' + f.y + ',width=' + f.width + ',height=' +
  121. f.height + ',scrollbars=' + (f.scrollbars ? 'yes' : 'no') +
  122. ',resizable=' + (f.resizable ? 'yes' : 'no') +
  123. ',statusbar=' + (f.statusbar ? 'yes' : 'no')
  124. );
  125. try {
  126. w.focus();
  127. } catch (ex) {
  128. // Ignore
  129. }
  130. },
  131. each : function(o, f, s) {
  132. var n, l;
  133. if (o) {
  134. s = s || o;
  135. if (o.length !== undefined) {
  136. for (n = 0, l = o.length; n < l; n++)
  137. f.call(s, o[n], n, o);
  138. } else {
  139. for (n in o) {
  140. if (o.hasOwnProperty(n))
  141. f.call(s, o[n], n, o);
  142. }
  143. }
  144. }
  145. },
  146. extend : function() {
  147. var k, a = arguments, t = a[0], i, v;
  148. for (i = 1; i < a.length; i++) {
  149. if (v = a[i]) {
  150. for (k in v)
  151. t[k] = v[k];
  152. }
  153. }
  154. return t;
  155. },
  156. // Legacy functions
  157. open : function(fn, en, url, cb, s) {
  158. var t = this, el;
  159. s = s || {};
  160. // Use input value if it was found
  161. if (!s.url && document.forms[fn] && (el = document.forms[fn].elements[en.split(',')[0]]))
  162. s.url = el.value;
  163. if (!cb) {
  164. s.oninsert = function(o) {
  165. var e, i, v, f = o.focusedFile;
  166. v = en.replace(/\s+/g, '').split(',');
  167. for (i = 0; i < v.length; i++) {
  168. if (e = document.forms[fn][v[i]])
  169. e.value = f.url;
  170. }
  171. };
  172. } else {
  173. if (typeof(cb) == 'string')
  174. cb = window[cb];
  175. s.oninsert = function(o) {
  176. cb(o.focusedFile.url, o);
  177. };
  178. }
  179. t.browse(s);
  180. },
  181. filebrowserCallBack : function(fn, u, ty, w, ask) {
  182. var t = mcImageManager, i, hl, fo, s = {};
  183. // Is filemanager included, ask it first
  184. if (window.mcFileManager && !ask) {
  185. hl = mcFileManager.settings.handle;
  186. hl = hl.split(',');
  187. for (i = 0; i < hl.length; i++) {
  188. if (ty == hl[i])
  189. fo = 1;
  190. }
  191. if (fo && mcFileManager.filebrowserCallBack(fn, u, ty, w, 1))
  192. return;
  193. }
  194. // Grab imagemanager prefixed options
  195. t.each(tinyMCE.activeEditor ? tinyMCE.activeEditor.settings : tinyMCE.settings, function(v, k) {
  196. if (k.indexOf('imagemanager_') === 0)
  197. s[k.substring(13)] = v;
  198. });
  199. t.browse(t.extend(s, {
  200. url : w.document.forms[0][fn].value,
  201. relative_urls : 0,
  202. oninsert : function(o) {
  203. var f, u, na;
  204. f = w.document.forms[0];
  205. u = o.focusedFile.url;
  206. inf = o.focusedFile.custom;
  207. // Let TinyMCE convert the URLs
  208. if (typeof(TinyMCE_convertURL) != "undefined")
  209. u = TinyMCE_convertURL(u, null, true);
  210. else if (tinyMCE.convertURL)
  211. u = tinyMCE.convertURL(u, null, true);
  212. else
  213. u = tinyMCE.activeEditor.convertURL(u, null, true);
  214. f[fn].value = u;
  215. // Set alt and title info
  216. if (inf.custom && inf.custom.description) {
  217. na = ['alt', 'title', 'linktitle'];
  218. for (i = 0; i < na.length; i++) {
  219. if (f.elements[na[i]])
  220. f.elements[na[i]].value = inf.custom.description;
  221. }
  222. }
  223. // Try fire onchange
  224. try {
  225. f[fn].onchange();
  226. } catch (e) {
  227. // Ignore
  228. }
  229. w = null; // IE leak
  230. }
  231. }));
  232. return true;
  233. }
  234. };
  235. mcImageManager.setup();
  236. mcImageManager.relaxDomain();
  237. })();