11Template . ionList . helpers ( {
2- classes : function ( ) {
3- var classes = [ 'list' ] ;
4-
5- if ( this . class ) {
6- var customClasses = this . class . split ( ' ' ) ;
7- _ ( customClasses ) . each ( function ( customClass ) {
8- classes . push ( customClass ) ;
9- } ) ;
10- }
2+ classes : function ( ) {
3+ var classes = [ 'list' ] ;
114
12- if ( this . rightEdit ) {
13- classes . push ( 'list-right-editing' ) ;
14- }
5+ if ( this . class ) {
6+ var customClasses = this . class . split ( ' ' ) ;
7+ _ ( customClasses ) . each ( function ( customClass ) {
8+ classes . push ( customClass ) ;
9+ } ) ;
10+ }
1511
16- if ( this . leftEdit ) {
17- classes . push ( 'list-left -editing' ) ;
18- }
12+ if ( this . rightEdit ) {
13+ classes . push ( 'list-right -editing' ) ;
14+ }
1915
20- return classes . join ( ' ' ) ;
16+ if ( this . leftEdit ) {
17+ classes . push ( 'list-left-editing' ) ;
2118 }
19+
20+ return classes . join ( ' ' ) ;
21+ }
2222} ) ;
2323
2424
2525Template . ionList . rendered = function ( ) {
26+
27+ if ( this . data && this . data . ionSortable ) {
28+ Session . set ( "ionSortable" , true ) ;
2629 var list = this . $ ( '.list' ) [ 0 ] ;
2730 new Slip ( list ) ;
28- //TODO: ensure not creating a slip list if unnecessary
29- } ;
31+ }
32+
33+ } ;
34+
35+
36+ Template . ionList . events ( {
37+ 'click .item-delete' : function ( e , template ) {
38+ e . preventDefault ( ) ;
3039
40+ target = $ ( e . target ) . closest ( '.item' ) . get ( 0 ) ;
3141
32- Template . ionList . events ( {
33- 'slip:swipe .list' : function ( e , template ) {
34- e . preventDefault ( ) ;
35- } ,
36- 'slip:beforereorder .list' : function ( e , template ) {
37- if ( e . originalEvent . target . className . indexOf ( 'instant' ) == - 1 ) {
38- e . preventDefault ( ) ;
42+ targetData = Blaze . getData ( target . getElementsByClassName ( 'item-content' ) [ 0 ] ) . _id || undefined ;
43+
44+ template . data . ionSortable . find ( { } ) . forEach ( function ( item , i ) {
45+ if ( item . _id === targetData ) {
46+ template . data . ionSortable . _collection . remove ( {
47+ _id : item . _id
48+ } , function ( error , result ) { } ) ;
3949 }
40- } ,
41- 'slip:beforeswipe .list' : function ( e , template ) {
42- e . preventDefault ( ) ;
43- } ,
44- 'slip:beforewait .list' : function ( e , template ) {
45- e . preventDefault ( ) ;
46- } ,
47- 'slip:afterswipe .list' : function ( e , template ) {
48- e . preventDefault ( ) ;
49- } ,
50- 'slip:reorder .list' : function ( e , template ) {
51- spliceIndex = e . originalEvent . detail . spliceIndex
52- originalIndex = e . originalEvent . detail . originalIndex
53-
54- if ( spliceIndex != originalIndex ) {
55-
56- template . data . collection . find ( { } , {
57- sort : {
58- order : 1
59- }
60- } ) . forEach ( function ( item , i ) {
61- //Pause the Observers while we update the order of each item.
62- template . data . collection . _collection . pauseObservers ( )
50+ } ) ;
51+ } ,
52+ 'slip:swipe .list, slip:beforeswipe .list, slip:beforewait .list, slip:afterswipe .list' : function ( e , template ) {
53+ e . preventDefault ( ) ;
54+ } ,
55+ 'slip:beforereorder .list' : function ( e , template ) {
56+ if ( e . originalEvent . target . className . indexOf ( 'instant' ) == - 1 ) {
57+ e . preventDefault ( ) ;
58+ }
59+ } ,
60+ 'slip:reorder .list' : function ( e , template ) {
61+ spliceIndex = e . originalEvent . detail . spliceIndex
62+ originalIndex = e . originalEvent . detail . originalIndex
63+
64+ if ( spliceIndex != originalIndex ) {
65+
66+ template . data . ionSortable . find ( { } , {
67+ sort : {
68+ order : 1
69+ }
70+ } ) . forEach ( function ( item , i ) {
71+ template . data . ionSortable . _collection . pauseObservers ( )
6372 if ( item . _id == Blaze . getData ( e . target . getElementsByClassName ( 'item-content' ) [ 0 ] ) . _id ) {
64- temp = template . data . collection . update ( {
65- _id : item . _id
66- } , {
67- $set : {
68- order : spliceIndex
69- }
70- } )
71- } else {
72- if ( spliceIndex > originalIndex ) {
73- newOrder = ( ( spliceIndex >= i ) && ( originalIndex < i ) ) ? ( i - 1 ) : i
74- } else if ( spliceIndex == '0' ) {
75- newOrder = ( originalIndex > i ) ? ( i + 1 ) : i
76- } else {
77- newOrder = ( ( spliceIndex <= i ) && ( originalIndex > i ) ) ? ( i + 1 ) : i
73+ temp = template . data . ionSortable . update ( {
74+ _id : item . _id
75+ } , {
76+ $set : {
77+ order : spliceIndex
7878 }
79+ } )
80+ } else {
81+ if ( spliceIndex > originalIndex ) {
82+ newOrder = ( ( spliceIndex >= i ) && ( originalIndex < i ) ) ? ( i - 1 ) : i
83+ } else if ( spliceIndex == '0' ) {
84+ newOrder = ( originalIndex > i ) ? ( i + 1 ) : i
85+ } else {
86+ newOrder = ( ( spliceIndex <= i ) && ( originalIndex > i ) ) ? ( i + 1 ) : i
87+ }
7988
80- temp = template . data . collection . update ( {
81- _id : item . _id
82- } , {
83- $set : {
84- order : newOrder
85- }
86- } )
89+ temp = template . data . ionSortable . update ( {
90+ _id : item . _id
91+ } , {
92+ $set : {
93+ order : newOrder
94+ }
95+ } )
8796 }
88- template . data . collection . _collection . resumeObservers ( )
89- } )
97+ template . data . ionSortable . _collection . resumeObservers ( )
98+ } )
9099
91- }
92- } ,
100+ }
101+ }
93102
94103} ) ;
0 commit comments