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

/IZWebFileManager/resources/FileView.js

http://izwebfilemanager.googlecode.com/
JavaScript | 475 lines | 429 code | 46 blank | 0 comment | 111 complexity | aefb27b96b4f4a841d02fe3e26c42782 MD5 | raw file
  1. FileView = function(ClientID, ControllerID, RegularItemStyle, SelectedItemStyle, EditTextBoxStyle) {
  2. this.ClientID = ClientID;
  3. this.ControllerID = ControllerID;
  4. this._controller = eval('WFM_' + ControllerID);
  5. this.RegularItemStyle = RegularItemStyle;
  6. this.SelectedItemStyle = SelectedItemStyle;
  7. this.EditTextBoxStyle = EditTextBoxStyle;
  8. this.Element = WebForm_GetElementById(this.ClientID);
  9. this.Focuser = WebForm_GetElementById(this.ClientID + '_Focus');
  10. this.Address = WebForm_GetElementById(this.ClientID + '_Address');
  11. this.SelectedItems = new Array();
  12. this.HitInfo = 'FileView';
  13. this.InProcess = false;
  14. var This = this;
  15. this.Element.onselectstart = function(e) {
  16. return false;
  17. }
  18. this.Element.onscroll = function(e) {
  19. eval('WFM_' + ControllerID + '.HideTextBox()');
  20. var top=''+WebForm_GetElementById(ClientID).scrollTop+'px'
  21. var left=''+WebForm_GetElementById(ClientID).scrollLeft+'px'
  22. var tHead = WebForm_GetElementById(ClientID+"_Thead_Name");
  23. if(tHead) {
  24. tHead.style.position = 'relative';
  25. tHead.style.zIndex = 10;
  26. tHead.style.top = top;
  27. tHead.style.left = '-1px';
  28. tHead.style.left = '0px';
  29. }
  30. var tHead = WebForm_GetElementById(ClientID+"_Thead_Size");
  31. if(tHead) {
  32. tHead.style.position = 'relative';
  33. tHead.style.zIndex = 10;
  34. tHead.style.top = top;
  35. tHead.style.left = '-1px';
  36. tHead.style.left = '0px';
  37. }
  38. var tHead = WebForm_GetElementById(ClientID+"_Thead_Type");
  39. if(tHead) {
  40. tHead.style.position = 'relative';
  41. tHead.style.zIndex = 10;
  42. tHead.style.top = top;
  43. tHead.style.left = '-1px';
  44. tHead.style.left = '0px';
  45. }
  46. var tHead = WebForm_GetElementById(ClientID+"_Thead_Modified");
  47. if(tHead) {
  48. tHead.style.position = 'relative';
  49. tHead.style.zIndex = 10;
  50. tHead.style.top = top;
  51. tHead.style.left = '-1px';
  52. tHead.style.left = '0px';
  53. }
  54. }
  55. this.Element.oncontextmenu = function(e) {
  56. if(e == null) var e = event;
  57. eval('WFM_' + ClientID + '.ShowContextMenu(e)');
  58. eval('WFM_' + ClientID + '.HitInfo = \'FileView\'');
  59. return false;
  60. }
  61. if(this.Address) {
  62. this.Address.onkeydown = function(e) {
  63. if(e == null) var e = event;
  64. if(e.keyCode == 13) {
  65. This.Navigate(this.value);
  66. e.cancelBubble = true;
  67. return false;
  68. }
  69. }
  70. }
  71. this.Focuser.onkeydown = function(e) {
  72. if(e == null) var e = event;
  73. if(e.keyCode == 13) {
  74. eval('WFM_' + ControllerID + '.OnExecuteCommand(WFM_' + ClientID + ',\'0:0\')');
  75. e.cancelBubble = true;
  76. return false;
  77. } else if (e.keyCode == 38 || e.keyCode == 37) {
  78. eval('WFM_' + ClientID + '.GoToPreviousItem(e)');
  79. e.cancelBubble = true;
  80. return false;
  81. } else if (e.keyCode == 40 || e.keyCode == 39) {
  82. eval('WFM_' + ClientID + '.GoToNextItem(e)');
  83. e.cancelBubble = true;
  84. return false;
  85. } else if (e.keyCode == 113) {
  86. eval('WFM_' + ControllerID + '.doNotHide = true');
  87. eval('WFM_' + ControllerID + '.OnRename(WFM_' + ClientID + ',\'\')');
  88. e.cancelBubble = true;
  89. return false;
  90. } else if (e.keyCode == 116) {
  91. eval('WFM_' + ControllerID + '.OnRefresh(WFM_' + ClientID + ',\'\')');
  92. e.cancelBubble = true;
  93. return false;
  94. } else if (e.keyCode == 46) {
  95. eval('WFM_' + ControllerID + '.OnSelectedItemsDelete(WFM_' + ClientID + ',\'\')');
  96. e.cancelBubble = true;
  97. return false;
  98. }
  99. return false;
  100. }
  101. }
  102. FileView.prototype.getController = function() {return this._controller;}
  103. FileView.prototype.ShowContextMenu = function(arg) {
  104. if(this.InProcess)
  105. return;
  106. var x = arg.clientX + WebForm_GetScrollX();
  107. var y = arg.clientY + WebForm_GetScrollY();
  108. if(this.HitInfo == 'FileView')
  109. eval(this.ClientID+'_ShowContextMenu(x,y)');
  110. else
  111. eval(this.ClientID+'_ShowSelectedItemsContextMenu(x,y)');
  112. }
  113. FileView.prototype.ShowProgress = function() {
  114. this.InProcess = true;
  115. progImg = WebForm_GetElementById(this.ClientID + '_ProgressImg');
  116. if(progImg)
  117. progImg.style.visibility = 'visible';
  118. this.Element.style.cursor = 'wait';
  119. var rootControl = WebForm_GetElementById(this.Element.rootControl);
  120. if(rootControl)
  121. rootControl.style.cursor = 'wait';
  122. }
  123. FileView.prototype.HidePrgress = function() {
  124. this.InProcess = false;
  125. progImg = WebForm_GetElementById(this.ClientID + '_ProgressImg');
  126. if(progImg)
  127. progImg.style.visibility = 'hidden';
  128. this.Element.style.cursor = 'default';
  129. var rootControl = WebForm_GetElementById(this.Element.rootControl);
  130. if(rootControl)
  131. rootControl.style.cursor = 'default';
  132. }
  133. FileView.prototype.Navigate = function(arg) {
  134. eval('WFM_' + this.ControllerID + '.OnFileViewNavigate(WFM_' + this.ClientID + ',arg)');
  135. }
  136. FileView.prototype.Rename = function(arg) {
  137. eval('WFM_' + this.ControllerID + '.OnRename(WFM_' + this.ClientID + ',arg)');
  138. }
  139. FileView.prototype.SelectedItemsDelete = function(arg) {
  140. eval('WFM_' + this.ControllerID + '.OnSelectedItemsDelete(WFM_' + this.ClientID + ',arg)');
  141. }
  142. FileView.prototype.SetFocus = function() {
  143. this.Focuser.focus();
  144. }
  145. FileView.prototype.GoToPreviousItem = function(e) {
  146. var item = null;
  147. if(this.SelectedItems.length == 0) {
  148. item = WebForm_GetElementById(this.ClientID + '_Item_0');
  149. } else {
  150. var currentSelectedItem = this.SelectedItems[this.SelectedItems.length - 1];
  151. for(i=0;i<this.Items.length;i++) {
  152. if(this.Items[i].id == currentSelectedItem.id) {
  153. try {item = this.Items[i-1];}
  154. catch(e) {}
  155. break;
  156. }
  157. }
  158. }
  159. if(item) {
  160. this.AddSelectedItem(item, true);
  161. this.EnsureVisible(item);
  162. }
  163. }
  164. FileView.prototype.GoToNextItem = function(e) {
  165. var item = null;
  166. if(this.SelectedItems.length == 0) {
  167. item = WebForm_GetElementById(this.ClientID + '_Item_0');
  168. } else {
  169. var currentSelectedItem = this.SelectedItems[this.SelectedItems.length - 1];
  170. for(i=0;i<this.Items.length;i++) {
  171. if(this.Items[i].id == currentSelectedItem.id) {
  172. try {item = this.Items[i+1];}
  173. catch(e) {}
  174. break;
  175. }
  176. }
  177. }
  178. if(item) {
  179. this.AddSelectedItem(item, true);
  180. this.EnsureVisible(item);
  181. }
  182. }
  183. FileView.prototype.EnsureVisible = function(item) {
  184. var posEl = WebForm_GetElementPosition(this.Element);
  185. var posItem = WebForm_GetElementPosition(item);
  186. var topBar = 0;
  187. var tHeadName = WebForm_GetElementById(this.ClientID+"_Thead_Name");
  188. if(tHeadName) { topBar = WebForm_GetElementPosition(tHeadName).height; }
  189. if((posItem.y+posItem.height-this.Element.scrollTop)>(posEl.y+posEl.height-25)) this.Element.scrollTop = (posItem.y+posItem.height)-(posEl.y+posEl.height-25);
  190. if((posItem.y-this.Element.scrollTop)<(posEl.y + topBar)) this.Element.scrollTop = (posItem.y)-(posEl.y + topBar);
  191. }
  192. FileView.prototype.Initialize = function() {
  193. var initScript = WebForm_GetElementById(this.ClientID+'_InitScript').innerHTML;
  194. eval(initScript);
  195. var dir = WebForm_GetElementDir(this.Element);
  196. this.Element.scrollTop = 0;
  197. this.Element.scrollLeft = (dir=='rtl')?this.Element.scrollWidth:0;
  198. }
  199. FileView.prototype.ClearSelectedItems = function() {
  200. for(i=0;i<this.SelectedItems.length;i++) {
  201. WebForm_RemoveClassName(this.SelectedItems[i], this.SelectedItemStyle);
  202. this.SelectedItems[i].Selected = false;
  203. }
  204. this.SelectedItems = new Array();
  205. this.SaveSelectedItemsState();
  206. }
  207. FileView.prototype.AddSelectedItem = function(item, clearBefore) {
  208. if(clearBefore)
  209. this.ClearSelectedItems();
  210. for(i=0;i<this.SelectedItems.length;i++) {
  211. if(this.SelectedItems[i].id == item.id) {
  212. return;
  213. }
  214. }
  215. item.Selected = true;
  216. WebForm_AppendToClassName(item, this.SelectedItemStyle);
  217. this.SelectedItems[this.SelectedItems.length] = item;
  218. this.SaveSelectedItemsState();
  219. }
  220. FileView.prototype.SaveSelectedItemsState = function() {
  221. var selectedItemsEl = document.getElementById(this.ClientID+'_SelectedItems');
  222. var paths = new Array();
  223. for(i=0;i<this.SelectedItems.length;i++) {
  224. paths[paths.length] = this.SelectedItems[i].Path;
  225. }
  226. selectedItemsEl.value = paths.join(":");
  227. }
  228. FileView.prototype.GetDirectory = function() {
  229. var directory = document.getElementById(this.ClientID+'_Directory');
  230. return directory.value;
  231. }
  232. FileView.prototype.GetSort = function() {
  233. var sort = document.getElementById(this.ClientID+'_Sort');
  234. return sort.value;
  235. }
  236. FileView.prototype.SetSort = function(arg) {
  237. var sort = document.getElementById(this.ClientID+'_Sort');
  238. var sortDirection = document.getElementById(this.ClientID+'_SortDirection');
  239. if(sort.value == arg) {
  240. if(sortDirection.value == 'Ascending') sortDirection.value = 'Descending';
  241. else sortDirection.value = 'Ascending';
  242. } else {
  243. sort.value = arg;
  244. sortDirection.value = 'Ascending';
  245. }
  246. }
  247. FileView.prototype.GetShowInGroups = function() {
  248. var showInGroups = document.getElementById(this.ClientID+'_ShowInGroups');
  249. return eval(showInGroups.value);
  250. }
  251. FileView.prototype.SwitchShowInGroups = function() {
  252. var showInGroups = document.getElementById(this.ClientID+'_ShowInGroups');
  253. var _showInGroups = eval(showInGroups.value);
  254. showInGroups.value = ''+!_showInGroups;
  255. }
  256. FileView.prototype.GetView = function() {
  257. var sort = document.getElementById(this.ClientID+'_View');
  258. return sort.value;
  259. }
  260. FileView.prototype.SetView = function(arg) {
  261. var sort = document.getElementById(this.ClientID+'_View');
  262. sort.value = arg;
  263. }
  264. FileView.prototype.InitItem = function(item, path, isDirectory, canBeRenamed, selected, fileType) {
  265. var ControllerID = this.ControllerID;
  266. var ClientID = this.ClientID;
  267. item.OwnerID = ClientID;
  268. item.Path = path;
  269. item.className = this.RegularItemStyle;
  270. item.FileType = fileType;
  271. item.IsDirectory = isDirectory;
  272. item.Selected = selected;
  273. item.CanBeRenamed = canBeRenamed;
  274. item.Name = decodeURIComponent(path);
  275. item.ondblclick = function(e) {
  276. if(e == null) var e = event;
  277. eval('WFM_' + ControllerID + '.OnExecuteCommand(WFM_' + ClientID + ',\'0:0\')');
  278. e.cancelBubble = true;
  279. return false;
  280. }
  281. item.oncontextmenu = function(e) {
  282. if(e == null) var e = event;
  283. if(!this.Selected)
  284. eval('WFM_' + ClientID + '.AddSelectedItem(this, true)');
  285. eval('WFM_' + ClientID + '.HitInfo = \'SelectedItems\'');
  286. return false;
  287. }
  288. var fileViewItem = new FileViewItem(this, item);
  289. }
  290. FileView.prototype.OnMenuItemClick = function(sender, arg) {
  291. var ControllerID = this.ControllerID;
  292. var ClientID = this.ClientID;
  293. eval('WFM_' + ControllerID + '.On'+arg.CommandName+'(WFM_' + ClientID + ',arg.CommandArgument)');
  294. }
  295. FileViewItem = function(owner, element) {
  296. this._owner = owner;
  297. this._element = element;
  298. var mouseDown = this._mouseDown;
  299. var mouseOut = this._mouseOut;
  300. var mouseUp = this._mouseUp;
  301. var mouseMove = this._mouseMove;
  302. var instance = this;
  303. element.onmouseup = function(e) {
  304. e = e || window.event;
  305. mouseUp.call(instance, e);
  306. }
  307. element.onmousemove = function(e) {
  308. e = e || window.event;
  309. mouseMove.call(instance, e);
  310. }
  311. element.onmousedown = function(e) {
  312. e = e || window.event;
  313. mouseDown.call(instance, e);
  314. }
  315. element.onmouseout = function(e) {
  316. e = e || window.event;
  317. mouseOut.call(instance, e);
  318. }
  319. }
  320. FileViewItem.prototype._owner = null;
  321. FileViewItem.prototype._div = null;
  322. FileViewItem.prototype._dropMove = true;
  323. FileViewItem.prototype._node = null;
  324. FileViewItem.prototype._highlight = false;
  325. FileViewItem.prototype._cursor = null;
  326. FileViewItem.prototype.getController = function() {return this._owner.getController();}
  327. FileViewItem.prototype.getFullPath = function() {
  328. var dirPath = decodeURIComponent(this._owner.GetDirectory());
  329. var name = decodeURIComponent(this._element.Path);
  330. var spliter = '/';
  331. if(dirPath.charAt(dirPath.length-1) == '/') spliter = '';
  332. return dirPath + spliter + name;
  333. }
  334. FileViewItem.prototype.highlight = function(bool) {
  335. if(this._highlight == bool) return;
  336. this._highlight = bool;
  337. if(bool) WebForm_AppendToClassName(this._element, this._owner.SelectedItemStyle);
  338. else WebForm_RemoveClassName(this._element, this._owner.SelectedItemStyle);
  339. };
  340. FileViewItem.prototype.setCursor = function(cursor){
  341. if(this._cursor == cursor) return;
  342. this._cursor = cursor;
  343. this._element.style.cursor = cursor;
  344. var name = WebForm_GetElementById(this._element.id+'_Name');
  345. if(name) name.style.cursor = cursor;
  346. };
  347. FileViewItem.prototype.isSelected = function() {return this._element.Selected;};
  348. FileViewItem.prototype.select = function(bool) {this._owner.AddSelectedItem(this._element, bool);};
  349. FileViewItem.prototype.canDrop = function() {
  350. return this._element.IsDirectory && !this.isSelected();
  351. };
  352. FileViewItem.prototype._mouseMove = function(ev) {
  353. if(ev.preventDefault) ev.preventDefault();
  354. ev.returnValue = false;
  355. if(this._pendDragDrop) {
  356. this._moveCounter++;
  357. if(this._moveCounter>2) {
  358. this._pendDragDrop = false;
  359. this._pendSelect = false;
  360. this.getController().startDragDrop(this._owner);
  361. }
  362. }
  363. if(this.getController().isDragging()) {
  364. this.onDragInTarget(ev);
  365. }
  366. return false;
  367. };
  368. FileViewItem.prototype._mouseOut = function(ev) {
  369. if(ev.preventDefault) ev.preventDefault();
  370. ev.returnValue = false;
  371. if(this.getController().isDragging()) {
  372. this.onDragLeaveTarget();
  373. }
  374. return false;
  375. };
  376. FileViewItem.prototype._mouseDown = function(ev) {
  377. if(ev.preventDefault) ev.preventDefault();
  378. ev.returnValue = false;
  379. this._pendDragDrop = true;
  380. this._moveCounter = 0;
  381. if(this.isSelected()) this._pendSelect = true;
  382. else this.select(!ev.ctrlKey && !ev.shiftKey);
  383. return false;
  384. };
  385. FileViewItem.prototype._mouseUp = function(ev) {
  386. if(ev.preventDefault) ev.preventDefault();
  387. ev.returnValue = false;
  388. var pendSelect = this._pendSelect;
  389. this._pendDragDrop = false;
  390. this._pendSelect = false;
  391. if(this.getController().isDragging()) {
  392. this.onDrop();
  393. }
  394. else if(pendSelect) {
  395. this.select(!ev.ctrlKey && !ev.shiftKey)
  396. }
  397. return false;
  398. };
  399. FileViewItem.prototype.onDragLeaveTarget = function() {
  400. this.getController()._dropTarget = null;
  401. if(this.canDrop()){
  402. this.highlight(false);
  403. }
  404. this.setCursor("default");
  405. };
  406. FileViewItem.prototype.onDragInTarget = function(ev) {
  407. if(this.canDrop()){
  408. this._dropMove = !ev.ctrlKey && !ev.shiftKey;
  409. this.getController()._dropTarget = this;
  410. this.highlight(true);
  411. if(this._dropMove)
  412. this.setCursor(this.getController()._dropMoveCursor);
  413. else
  414. this.setCursor(this.getController()._dropCopyCursor);
  415. }
  416. else {
  417. this.setCursor(this.getController()._dropNotAllowedCursor);
  418. }
  419. };
  420. FileViewItem.prototype.onDrop = function (){
  421. if(this.canDrop()) {
  422. this.getController().drop(this, this._dropMove);
  423. this.highlight(false);
  424. }
  425. else {
  426. this.getController().stopDragDrop(this);
  427. }
  428. this.setCursor("default");
  429. };