@@ -34,7 +34,8 @@ define(function (require, exports, module) {
3434 "use strict" ;
3535
3636 // Load dependent modules
37- var DocumentManager = require ( "document/DocumentManager" ) ,
37+ var AppInit = require ( "utils/AppInit" ) ,
38+ DocumentManager = require ( "document/DocumentManager" ) ,
3839 MainViewManager = require ( "view/MainViewManager" ) ,
3940 CommandManager = require ( "command/CommandManager" ) ,
4041 Commands = require ( "command/Commands" ) ,
@@ -69,6 +70,12 @@ define(function (require, exports, module) {
6970 var _classProviders = [ ] ;
7071
7172
73+ /**
74+ * #working-set-list-container
75+ * @type {jQuery }
76+ */
77+ var $workingFilesContainer ;
78+
7279 /**
7380 * Constants for event.which values
7481 * @enum {number}
@@ -97,6 +104,12 @@ define(function (require, exports, module) {
97104 BELOWVIEW = "belowview" ,
98105 ABOVEVIEW = "aboveview" ;
99106
107+ /**
108+ * Drag an item has to move 3px before dragging starts
109+ * @constant
110+ */
111+ var _DRAG_MOVE_DETECTION_START = 3 ;
112+
100113 /**
101114 * Refreshes all Pane View List Views
102115 */
@@ -129,7 +142,6 @@ define(function (require, exports, module) {
129142 */
130143 function _updateListItemSelection ( listItem , selectedFile ) {
131144 var shouldBeSelected = ( selectedFile && $ ( listItem ) . data ( _FILE_KEY ) . fullPath === selectedFile . fullPath ) ;
132-
133145 ViewUtils . toggleClass ( $ ( listItem ) , "selected" , shouldBeSelected ) ;
134146 }
135147
@@ -263,19 +275,43 @@ define(function (require, exports, module) {
263275 dragged = false ,
264276 startPageY = e . pageY ,
265277 lastPageY = startPageY ,
266- itemHeight = $el . height ( ) ,
278+ lastHit = { where : NOMANSLAND } ,
267279 tryClosing = $ ( e . target ) . hasClass ( "can-close" ) ,
268- offset = $el . offset ( ) ,
269- $copy = $el . clone ( ) ,
270- $ghost = $ ( "<div class='open-files-container wsv-drag-ghost' style='overflow: hidden; display: inline-block;'>" ) . append ( $ ( "<ul>" ) . append ( $copy ) . css ( "padding" , "0" ) ) ,
271- sourceView = _viewFromEl ( $el ) ,
272280 currentFile = MainViewManager . getCurrentlyViewedFile ( ) ,
273281 activePaneId = MainViewManager . getActivePaneId ( ) ,
274282 activeView = _views [ activePaneId ] ,
275- draggingCurrentFile = ( $el . hasClass ( "selected" ) && sourceView . paneId === activePaneId ) ,
276- startingIndex = MainViewManager . findInWorkingSet ( sourceView . paneId , sourceFile . fullPath ) ,
283+ sourceView = _viewFromEl ( $el ) ,
277284 currentView = sourceView ,
278- lastHit = { where : NOMANSLAND } ;
285+ startingIndex = $el . index ( ) ,
286+ itemHeight ,
287+ offset ,
288+ $copy ,
289+ $ghost ,
290+ draggingCurrentFile ;
291+
292+ function initDragging ( ) {
293+ itemHeight = $el . height ( ) ;
294+ offset = $el . offset ( ) ;
295+ $copy = $el . clone ( ) ;
296+ $ghost = $ ( "<div class='open-files-container wsv-drag-ghost' style='overflow: hidden; display: inline-block;'>" ) . append ( $ ( "<ul>" ) . append ( $copy ) . css ( "padding" , "0" ) ) ;
297+ draggingCurrentFile = ( $el . hasClass ( "selected" ) && sourceView . paneId === activePaneId ) ;
298+
299+ // setup our ghost element as position absolute
300+ // so we can put it wherever we want to while dragging
301+ if ( draggingCurrentFile && _hasSelectionFocus ( ) ) {
302+ $ghost . addClass ( "dragging-current-file" ) ;
303+ }
304+
305+ $ghost . css ( {
306+ top : offset . top ,
307+ left : offset . left ,
308+ width : $el . width ( ) + 8
309+ } ) ;
310+
311+ // this will give the element the appearence that it's ghosted if the user
312+ // drags the element out of the view and goes off into no mans land
313+ $ghost . appendTo ( $ ( "body" ) ) ;
314+ }
279315
280316 // Switches the view context to match the hit context
281317 function updateContext ( hit ) {
@@ -294,7 +330,6 @@ define(function (require, exports, module) {
294330 hasScroller = false ,
295331 onTopScroller = false ,
296332 onBottomScroller = false ,
297- $workingFilesContainer = $ ( "#working-set-list-container" ) ,
298333 $container ,
299334 $hit ,
300335 $item ,
@@ -564,7 +599,7 @@ define(function (require, exports, module) {
564599 // The drag function
565600 function drag ( e ) {
566601 if ( ! dragged ) {
567-
602+ initDragging ( ) ;
568603 // sort redraw and scroll shadows
569604 // cause problems during drag so disable them
570605 _suppressSortRedrawForAllViews ( true ) ;
@@ -576,7 +611,7 @@ define(function (require, exports, module) {
576611 _deactivateAllViews ( true ) ;
577612
578613 // add a "dragging" class to the outer container
579- $ ( "#working-set-list-container" ) . addClass ( "dragging" ) ;
614+ $workingFilesContainer . addClass ( "dragging" ) ;
580615
581616 // add a class to the element we're dragging if
582617 // it's the currently selected file so that we
@@ -654,13 +689,15 @@ define(function (require, exports, module) {
654689 }
655690 }
656691
657- // move the drag affordance
658- $ghost . css ( "top" , $ghost . offset ( ) . top + ( e . pageY - lastPageY ) ) ;
659-
692+ // Reposition the drag affordance if we've started dragging
693+ if ( $ghost ) {
694+ $ghost . css ( "top" , $ghost . offset ( ) . top + ( e . pageY - lastPageY ) ) ;
695+ }
696+
660697 // if we have't started dragging yet then we wait until
661698 // the mouse has moved 3 pixels before we start dragging
662699 // to avoid the item moving when clicked or double clicked
663- if ( dragged || Math . abs ( e . pageY - startPageY ) > 3 ) {
700+ if ( dragged || Math . abs ( e . pageY - startPageY ) > _DRAG_MOVE_DETECTION_START ) {
664701 drag ( e ) ;
665702 }
666703
@@ -681,19 +718,21 @@ define(function (require, exports, module) {
681718
682719 // Close down the drag operation
683720 function preDropCleanup ( ) {
684- $ ( "#working-set-list-container" ) . removeClass ( "dragging" ) ;
685- $ ( "#working-set-list-container .drag-show-as-selected" ) . removeClass ( "drag-show-as-selected" ) ;
686- endScroll ( $el ) ;
687- // re-activate the views (adds the "active" class to the view that was previously active)
688- _deactivateAllViews ( false ) ;
689- // turn scroll wheel back on
690721 window . onmousewheel = window . document . onmousewheel = null ;
691722 $ ( window ) . off ( ".wsvdragging" ) ;
692- $ghost . remove ( ) ;
693- $el . css ( "opacity" , "" ) ;
723+ if ( dragged ) {
724+ $workingFilesContainer . removeClass ( "dragging" ) ;
725+ $workingFilesContainer . find ( ".drag-show-as-selected" ) . removeClass ( "drag-show-as-selected" ) ;
726+ endScroll ( $el ) ;
727+ // re-activate the views (adds the "active" class to the view that was previously active)
728+ _deactivateAllViews ( false ) ;
729+ // turn scroll wheel back on
730+ $ghost . remove ( ) ;
731+ $el . css ( "opacity" , "" ) ;
694732
695- if ( dragged && $el . next ( ) . length === 0 ) {
696- scrollCurrentViewToBottom ( ) ;
733+ if ( $el . next ( ) . length === 0 ) {
734+ scrollCurrentViewToBottom ( ) ;
735+ }
697736 }
698737 }
699738
@@ -800,21 +839,7 @@ define(function (require, exports, module) {
800839 return ;
801840 }
802841
803- // setup our ghost element as position absolute
804- // so we can put it wherever we want to while dragging
805- if ( draggingCurrentFile && _hasSelectionFocus ( ) ) {
806- $ghost . addClass ( "dragging-current-file" ) ;
807- }
808842
809- $ghost . css ( {
810- top : offset . top ,
811- left : offset . left ,
812- width : $el . width ( ) + 8
813- } ) ;
814-
815- // this will give the element the appearence that it's ghosted if the user
816- // drags the element out of the view and goes off into no mans land
817- $ghost . appendTo ( $ ( "body" ) ) ;
818843
819844 e . stopPropagation ( ) ;
820845 } ) ;
@@ -1435,6 +1460,11 @@ define(function (require, exports, module) {
14351460 refresh ( true ) ;
14361461 }
14371462
1463+ AppInit . htmlReady ( function ( ) {
1464+ $workingFilesContainer = $ ( "#working-set-list-container" ) ;
1465+ } ) ;
1466+
1467+
14381468 // Public API
14391469 exports . createWorkingSetViewForPane = createWorkingSetViewForPane ;
14401470 exports . refresh = refresh ;
0 commit comments