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