PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/example_xinha_files/xinha/plugins/MootoolsFileManager/MootoolsFileManager.FileManager.js

http://imgmap.googlecode.com/
JavaScript | 397 lines | 312 code | 57 blank | 28 comment | 53 complexity | 6de77ffc9cd3551002f0a71274afb13e MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, MIT, GPL-2.0, LGPL-2.1
  1. /**
  2. = Mootools File Manager =
  3. == File Manager ==
  4. The functions in this file extend the MootoolsFileManager plugin with support
  5. for managing files (inserting a link to a file). This file is loaded automatically.
  6. * @author $Author$
  7. * @version $Id$
  8. * @package MootoolsFileManager
  9. */
  10. // Open a "files" mode of the plugin to allow to select a file to
  11. // create a link to.
  12. MootoolsFileManager.prototype.OpenFileManager = function(link)
  13. {
  14. var editor = this.editor;
  15. var outparam = null;
  16. var self = this;
  17. if (typeof link == "undefined")
  18. {
  19. link = this.editor.getParentElement();
  20. if (link)
  21. {
  22. if (/^img$/i.test(link.tagName))
  23. link = link.parentNode;
  24. if (!/^a$/i.test(link.tagName))
  25. link = null;
  26. }
  27. }
  28. // If the link wasn't provided, and no link is currently in focus,
  29. // make one from the selection.
  30. if (!link)
  31. {
  32. var sel = editor.getSelection();
  33. var range = editor.createRange(sel);
  34. var compare = 0;
  35. if (Xinha.is_ie)
  36. {
  37. if ( sel.type == "Control" )
  38. {
  39. compare = range.length;
  40. }
  41. else
  42. {
  43. compare = range.compareEndPoints("StartToEnd", range);
  44. }
  45. }
  46. else
  47. {
  48. compare = range.compareBoundaryPoints(range.START_TO_END, range);
  49. }
  50. if (compare == 0)
  51. {
  52. alert(Xinha._lc("You must select some text before making a new link.", 'MootoolsFileManager'));
  53. return;
  54. }
  55. outparam = {
  56. f_href : '',
  57. f_title : '',
  58. f_target : '',
  59. f_type: '',
  60. baseHref: editor.config.baseHref
  61. };
  62. }
  63. else
  64. {
  65. outparam = {
  66. f_href : Xinha.is_ie ? link.href : link.getAttribute("href"),
  67. f_title : link.title,
  68. f_target : link.target,
  69. f_type : link.type ? link.type : '',
  70. baseHref: editor.config.baseHref
  71. };
  72. }
  73. this.current_link = link;
  74. this.current_attributes = outparam;
  75. if(!this.FileManagerWidget)
  76. {
  77. this.FileManagerWidget = new FileManager({
  78. url: this.editor.config.MootoolsFileManager.backend,
  79. assetBasePath: Xinha.getPluginDir('MootoolsFileManager')+'/mootools-filemanager/Assets',
  80. language: _editor_lang,
  81. selectable: true,
  82. upload: this.phpcfg.files_allow_upload,
  83. destroy: this.phpcfg.files_allow_delete,
  84. createFolders: this.phpcfg.files_allow_create_dir,
  85. rename: this.phpcfg.files_allow_move,
  86. move_or_copy: this.phpcfg.files_allow_move,
  87. download: this.phpcfg.files_allow_download,
  88. propagateData: Object.merge({'__function': 'file-manager'}, this.editor.config.MootoolsFileManager.backend_data),
  89. propagateType: 'POST',
  90. uploadAuthData: Object.merge({'__function': 'file-manager'}, this.editor.config.MootoolsFileManager.backend_data),
  91. onComplete: function(path, file, mgr) { self.FileManagerReturn(path,file); },
  92. onHide: function() { if(this.swf && this.swf.box) this.swf.box.style.display = 'none'; },
  93. onShow: function() { if(this.swf && this.swf.box) this.swf.box.style.display = ''; },
  94. onDetails: function(details)
  95. {
  96. this.info.adopt(self.FileManagerAttributes(details));
  97. return true;
  98. },
  99. onHidePreview: function()
  100. {
  101. document.id(self.FileManagerAttributes().table).dispose();
  102. return true;
  103. },
  104. showDirGallery: false,
  105. keyboardNavigation: false,
  106. listType: this.phpcfg.files_list_type,
  107. listPaginationSize: this.phpcfg.files_pagination_size,
  108. listMaxSuggestedDirSizeForThumbnails: this.phpcfg.files_list_mode_over,
  109. directory: this.phpcfg.files_list_start_in
  110. });
  111. }
  112. if(Xinha.is_ie) this.current_selection = this.editor.saveSelection();
  113. if(link)
  114. {
  115. var src = Xinha.is_ie ? link.href : link.getAttribute("href");
  116. if(!src.match(/^(([a-z]+:)|\/)/i))
  117. {
  118. src = self.editor.config.baseHref.replace(/\/[^\/]*$/, '') + '/' + src;
  119. if(src.match(/^[a-z]+:/i) && !self.phpcfg.files_url.match(/^[a-z]:/i))
  120. {
  121. src = src.replace(/^[a-z]+:(\/\/?)[^/]*/i, '');
  122. }
  123. }
  124. // Get exploded path without the base url
  125. var path = src.replace(self.phpcfg.files_url+'/', '').split('/');
  126. // Pull off the file
  127. var base = path.pop();
  128. // Join the path back togethor (no base url, trailing slash if the path has any length)
  129. path = path.length ? (path.join('/') + '/') : '';
  130. // feed to widget
  131. this.FileManagerWidget.show(null, path, base);
  132. }
  133. else
  134. {
  135. this.FileManagerWidget.show();
  136. }
  137. };
  138. // Take the values from the file selection and make it (or update) a link
  139. MootoolsFileManager.prototype.FileManagerReturn = function(path, file)
  140. {
  141. var editor = this.editor;
  142. var a = this.current_link;
  143. var param = this.FileManagerAttributes();
  144. param.f_href = path;
  145. if(Xinha.is_ie) this.editor.restoreSelection(this.current_selection);
  146. if (!a)
  147. {
  148. try
  149. {
  150. editor._doc.execCommand("createlink", false, param.f_href);
  151. a = editor.getParentElement();
  152. var sel = editor.getSelection();
  153. var range = editor.createRange(sel);
  154. if (!Xinha.is_ie)
  155. {
  156. a = range.startContainer;
  157. if (!/^a$/i.test(a.tagName))
  158. {
  159. a = a.nextSibling;
  160. if (a == null)
  161. {
  162. a = range.startContainer.parentNode;
  163. }
  164. }
  165. }
  166. } catch(e) {}
  167. }
  168. else
  169. {
  170. var href = param.f_href.trim();
  171. editor.selectNodeContents(a);
  172. if (href == "")
  173. {
  174. editor._doc.execCommand("unlink", false, null);
  175. editor.updateToolbar();
  176. return false;
  177. }
  178. else
  179. {
  180. a.href = href;
  181. }
  182. }
  183. if (!(a && /^a$/i.test(a.tagName)))
  184. {
  185. return false;
  186. }
  187. a.type = param.f_type.trim();
  188. a.target = param.f_target.trim();
  189. a.title = param.f_title.trim();
  190. editor.selectNodeContents(a);
  191. editor.updateToolbar();
  192. };
  193. /** Return a DOM fragment which has all the fields needed to set the
  194. * attributes for a link given a structure of initial values.
  195. *
  196. * OR return a structure of values taken from the currently table.
  197. */
  198. MootoolsFileManager.prototype.FileManagerAttributes = function (details)
  199. {
  200. var self = this;
  201. self._LastFileDetails = details;
  202. function f(name)
  203. {
  204. var e = self._FileManagerAttributesTable.getElementsByTagName('input');
  205. for(var i = 0; i < e.length; i++)
  206. {
  207. if(e[i].name == name) return e[i];
  208. }
  209. var e = self._FileManagerAttributesTable.getElementsByTagName('select');
  210. for(var i = 0; i < e.length; i++)
  211. {
  212. if(e[i].name == name) return e[i];
  213. }
  214. return null;
  215. }
  216. function s(name, value)
  217. {
  218. for(var i = 0; i < f(name).options.length; i++)
  219. {
  220. if(f(name).options[i].value == value)
  221. {
  222. // f(name).options[i].selected = true;
  223. f(name).selectedIndex = i;
  224. return true;
  225. }
  226. }
  227. return false;
  228. }
  229. if(!this._FileManagerAttributesTable)
  230. {
  231. this._FileManagerAttributesTable = (function() {
  232. var div = document.createElement('div');
  233. var h2 = document.createElement('h2');
  234. h2.appendChild(document.createTextNode('Link Attributes'));
  235. div.appendChild(h2);
  236. var table = document.createElement('table');
  237. div.appendChild(table);
  238. table.className = 'filemanager-extended-options';
  239. var tbody = table.appendChild(document.createElement('tbody'));
  240. { // Title
  241. var tr = tbody.appendChild(document.createElement('tr'));
  242. var th = tr.appendChild(document.createElement('th'));
  243. var label = th.appendChild(document.createTextNode('Title:'));
  244. var td = tr.appendChild(document.createElement('td'));
  245. var input = td.appendChild(document.createElement('input'));
  246. td.colSpan = 6;
  247. input.name = 'f_title';
  248. input.type = 'text';
  249. th.className = td.className = 'filemanager-f_title';
  250. }
  251. { // Content Type
  252. var tr = tbody.appendChild(document.createElement('tr'));
  253. var th = tr.appendChild(document.createElement('th'));
  254. var label = th.appendChild(document.createTextNode('Type:'));
  255. var td = tr.appendChild(document.createElement('td'));
  256. var input = td.appendChild(document.createElement('input'));
  257. td.colSpan = 6;
  258. input.name = 'f_type';
  259. input.type = 'text';
  260. th.className = td.className = 'filemanager-f_type';
  261. }
  262. { // Target
  263. var tr = tbody.appendChild(document.createElement('tr'));
  264. { // Target
  265. var th = tr.appendChild(document.createElement('th'));
  266. var label = th.appendChild(document.createTextNode('Open In:'));
  267. var td = tr.appendChild(document.createElement('td'));
  268. td.colSpan = 2;
  269. var input = td.appendChild(document.createElement('select'));
  270. input.name = 'f_target';
  271. input.options[0] = new Option('');
  272. input.options[1] = new Option('New Window', '_blank');
  273. input.options[2] = new Option('Top Frame', '_top');
  274. input.options[3] = new Option('Other Frame:', '');
  275. Xinha._addEvent(input, 'change', function()
  276. {
  277. if(f('f_target').selectedIndex == 3)
  278. {
  279. f('f_otherTarget').style.visibility = 'visible';
  280. }
  281. else
  282. {
  283. f('f_otherTarget').style.visibility = 'hidden';
  284. }
  285. });
  286. var input = td.appendChild(document.createElement('input'));
  287. input.name = 'f_otherTarget';
  288. input.size = 7;
  289. input.type = 'text';
  290. input.style.visibility = 'hidden';
  291. th.className = td.className = 'filemanager-f_target';
  292. }
  293. }
  294. return div;
  295. })();
  296. }
  297. if(this.current_attributes)
  298. {
  299. f('f_title').value = this.current_attributes.f_title;
  300. f('f_type').value = this.current_attributes.f_type;
  301. if(this.current_attributes.f_target)
  302. {
  303. if(!s('f_target', this.current_attributes.f_target))
  304. {
  305. f('f_target').selectedIndex = 3;
  306. f('f_otherTarget').value = this.current_attributes.f_target;
  307. }
  308. else
  309. {
  310. f('f_otherTarget').value = '';
  311. }
  312. }
  313. this.current_attributes = null;
  314. }
  315. // If no details were supplied, we return the current ones
  316. if(!details)
  317. {
  318. var details = {
  319. f_title: f('f_title').value,
  320. f_type: f('f_type').value,
  321. f_target: f('f_target').selectedIndex < 3 ? f('f_target').options[f('f_target').selectedIndex].value : f('f_otherTarget').value,
  322. table: this._FileManagerAttributesTable
  323. }
  324. return details;
  325. }
  326. // If details were supplied, we set the appropriate ones.
  327. if(details.mime) f('f_type').value = details.mime;
  328. f('f_target').style.visibility = ''; // Ensure that the select hasn't been hidden by an overlay and not put back
  329. if(f('f_target').selectedIndex == 3)
  330. {
  331. f('f_otherTarget').style.visibility = 'visible';
  332. }
  333. else
  334. {
  335. f('f_otherTarget').style.visibility = 'hidden';
  336. }
  337. return this._FileManagerAttributesTable;
  338. };