editor_plugin_src.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /**
  2. * $Id: editor_plugin_src.js 42 2006-08-08 14:32:24Z spocke $
  3. *
  4. * @author Moxiecode
  5. * @copyright Copyright © 2004-2007, 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'}, 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. var mcImageManagerPlugin = {
  238. getInfo : function() {
  239. return {
  240. longname : 'MCImageManager PHP',
  241. author : 'Moxiecode Systems AB',
  242. authorurl : 'http://tinymce.moxiecode.com',
  243. infourl : 'http://tinymce.moxiecode.com/plugins_filemanager.php',
  244. version : "3.1"
  245. };
  246. },
  247. convertURL : function(u) {
  248. // 2.x
  249. if (window.TinyMCE_convertURL)
  250. return TinyMCE_convertURL(u, null, true);
  251. // 2.x
  252. if (tinyMCE.convertURL)
  253. return tinyMCE.convertURL(u, null, true);
  254. // 3.x
  255. return tinyMCE.activeEditor.convertURL(u, null, true);
  256. },
  257. replace : function(t, d, e) {
  258. var i, r;
  259. function get(d, s) {
  260. for (i=0, r=d, s=s.split('.'); i<s.length; i++)
  261. r = r[s[i]];
  262. return r;
  263. };
  264. // Replace variables
  265. t = '' + t.replace(/\{\$([^\}]+)\}/g, function(a, b) {
  266. var l = b.split('|'), v = get(d, l[0]);
  267. // Default encoding
  268. if (l.length == 1 && e && e.xmlEncode)
  269. v = e.xmlEncode(v);
  270. // Execute encoders
  271. for (i=1; i<l.length; i++)
  272. v = e[l[i]](v, d, b);
  273. return v;
  274. });
  275. // Execute functions
  276. t = t.replace(/\{\=([\w]+)([^\}]+)\}/g, function(a, b, c) {
  277. return get(e, b)(d, b, c);
  278. });
  279. return t;
  280. }
  281. };
  282. // Setup TinyMCE 3.x plugin
  283. if (window.tinymce) {
  284. tinymce.create('tinymce.plugins.ImageManagerPlugin', {
  285. init : function(ed, url) {
  286. var t = this;
  287. t.editor = ed;
  288. t.url = url;
  289. mcImageManager.baseURL = url + '/js';
  290. ed.settings.file_browser_callback = mcImageManager.filebrowserCallBack;
  291. mcImageManager.settings.handle = ed.getParam('imagemanager_handle', mcImageManager.settings.handle);
  292. ed.onInit.add(function() {
  293. if (ed && ed.plugins.contextmenu) {
  294. ed.plugins.contextmenu.onContextMenu.add(function(th, m, e) {
  295. var el = ed.selection.getNode();
  296. if (el && el.nodeName == 'IMG') {
  297. // Imagemanager
  298. m.addSeparator();
  299. sm = m.addMenu({title : 'ImageManager'});
  300. sm.add({
  301. title : 'imagemanager_replaceimage_desc',
  302. icon_src : url + '/pages/im/img/insertimage.gif',
  303. onclick : function() {
  304. mcImageManager.browse({
  305. path : ed.getParam("imagemanager_path"),
  306. rootpath : ed.getParam("imagemanager_rootpath"),
  307. remember_last_path : ed.getParam("imagemanager_remember_last_path"),
  308. custom_data : ed.getParam("imagemanager_custom_data"),
  309. insert_filter : ed.getParam("imagemanager_insert_filter"),
  310. oninsert : function(o) {
  311. ed.dom.setAttribs(el, {width : '', height : '', style : {width : '', height : ''}});
  312. ed.dom.setAttrib(el, 'src', o.focusedFile.url);
  313. }
  314. });
  315. }
  316. });
  317. sm.add({
  318. title : 'imagemanager_editimage_desc',
  319. icon_src : url + '/pages/im/img/editimage.gif',
  320. onclick : function() {
  321. mcImageManager.edit({
  322. insert_filter : ed.getParam("imagemanager_insert_filter"),
  323. url : ed.documentBaseURI.toAbsolute(ed.dom.getAttrib(el, 'src', ed.dom.getAttrib(el, 'src'))),
  324. onsave : function(o) {
  325. ed.dom.setAttribs(el, {width : '', height : '', style : {width : '', height : ''}});
  326. ed.dom.setAttrib(el, 'src', o.url);
  327. }
  328. });
  329. }
  330. });
  331. }
  332. });
  333. }
  334. });
  335. ed.addCommand('mceInsertImage', function(u, v) {
  336. var s = {};
  337. // Grab imagemanager prefixed options
  338. tinymce.each(tinyMCE.activeEditor.settings, function(v, k) {
  339. if (k.indexOf('imagemanager_') === 0)
  340. s[k.substring(13)] = v;
  341. });
  342. mcImageManager.browse(tinymce.extend(s, {
  343. oninsert : function(o) {
  344. var ci = o.focusedFile.custom;
  345. if (!ci.thumbnail_url) {
  346. ci.thumbnail_url = url;
  347. ci.twidth = ci.width;
  348. ci.theight = ci.height;
  349. }
  350. ed.execCommand('mceInsertContent', false, mcImageManagerPlugin.replace(
  351. ed.getParam('imagemanager_insert_template', '<img src="{$url}" width="{$custom.width}" height="{$custom.height}" />'),
  352. o.focusedFile,
  353. {
  354. urlencode : function(v) {
  355. return escape(v);
  356. },
  357. xmlEncode : function(v) {
  358. return tinymce.DOM.encode(v);
  359. }
  360. }
  361. ));
  362. }
  363. }, v));
  364. });
  365. tinymce.ScriptLoader.load(url + '/language/index.php?type=im&format=tinymce_3_x&group=tinymce&prefix=imagemanager_');
  366. },
  367. getInfo : function() {
  368. return mcImageManagerPlugin.getInfo();
  369. },
  370. createControl: function(n, cm) {
  371. var t = this, c, ed = t.editor, v;
  372. switch (n) {
  373. case 'insertimage':
  374. v = ed.getParam('imagemanager_insert_template');
  375. if (v instanceof Array) {
  376. c = cm.createMenuButton('insertimage', {
  377. title : 'imagemanager_insertimage_desc',
  378. image : t.url + '/pages/im/img/insertimage.gif',
  379. icons : false
  380. });
  381. c.onRenderMenu.add(function(c, m) {
  382. tinymce.each(v, function(v) {
  383. m.add({title : v.title, onclick : function() {
  384. ed.execCommand('mceInsertImage', false, v);
  385. }});
  386. });
  387. });
  388. } else {
  389. c = cm.createButton('insertimage', {
  390. title : 'imagemanager_insertimage_desc',
  391. image : t.url + '/pages/im/img/insertimage.gif',
  392. onclick : function() {
  393. ed.execCommand('mceInsertImage', false, {template : v});
  394. }
  395. });
  396. }
  397. return c;
  398. }
  399. return null;
  400. }
  401. });
  402. tinymce.PluginManager.add('imagemanager', tinymce.plugins.ImageManagerPlugin);
  403. }
  404. // Setup TinyMCE 2.x plugin
  405. if (window.TinyMCE_Engine) {
  406. var TinyMCE_ImageManagerPlugin = {
  407. setup : function() {
  408. var b = (window.realTinyMCE || tinyMCE).baseURL;
  409. mcImageManager.baseURL = b + '/plugins/imagemanager/js';
  410. document.write('<script type="text/javascript" src="' + b + '/plugins/imagemanager/language/index.php?type=im&format=tinymce&group=tinymce&prefix=imagemanager_"></script>');
  411. mcImageManager.baseURL = '/mcmanager_php/js';
  412. },
  413. initInstance : function(ed) {
  414. ed.settings.file_browser_callback = 'mcImageManager.filebrowserCallBack';
  415. mcImageManager.settings.handle = tinyMCE.getParam('imagemanager_handle', mcImageManager.settings.handle);
  416. },
  417. getControlHTML : function(cn) {
  418. switch (cn) {
  419. case "insertimage":
  420. return tinyMCE.getButtonHTML(cn, 'lang_imagemanager_insertimage_desc', '{$pluginurl}/pages/im/img/insertimage.gif', 'mceInsertImage', false);
  421. }
  422. return "";
  423. },
  424. getInfo : function() {
  425. return mcImageManagerPlugin.getInfo();
  426. },
  427. execCommand : function(id, el, cmd, ui, v) {
  428. var ed = tinyMCE.getInstanceById(id);
  429. if (cmd == 'mceInsertImage') {
  430. mcImageManager.browse(tinyMCE.extend({
  431. path : tinyMCE.getParam("imagemanager_path"),
  432. rootpath : tinyMCE.getParam("imagemanager_rootpath"),
  433. remember_last_path : tinyMCE.getParam("imagemanager_remember_last_path"),
  434. custom_data : tinyMCE.getParam("imagemanager_custom_data"),
  435. insert_filter : tinyMCE.getParam("imagemanager_insert_filter"),
  436. oninsert : function(o) {
  437. var ci = o.focusedFile.custom;
  438. if (!ci.thumbnail_url) {
  439. ci.thumbnail_url = url;
  440. ci.twidth = ci.width;
  441. ci.theight = ci.height;
  442. }
  443. ed.execCommand('mceInsertContent', false, mcImageManagerPlugin.replace(
  444. tinyMCE.getParam('imagemanager_insert_template', '<a href="{$url}" rel="lightbox"><img src="{$custom.thumbnail_url}" width="{$custom.twidth}" height="{$custom.theight}" /></a>'),
  445. o.focusedFile,
  446. {
  447. urlencode : function(v) {
  448. return escape(v);
  449. },
  450. xmlEncode : function(v) {
  451. return tinyMCE.xmlEncode(v);
  452. }
  453. }
  454. ));
  455. }
  456. }, v));
  457. return true;
  458. }
  459. return false;
  460. }
  461. };
  462. TinyMCE_ImageManagerPlugin.setup();
  463. tinyMCE.addPlugin('imagemanager', TinyMCE_ImageManagerPlugin);
  464. }
  465. })();