@@ -41,51 +41,101 @@ export default class IMEX {
4141 }
4242
4343 storage_import ( ) {
44+ var filters = new Array ( ) ;
45+ this . data . forEach ( function ( e ) {
46+ if ( e . importable ) {
47+ filters . push ( {
48+ name : e . dialogOptions . name ,
49+ extensions : e . dialogOptions . extensions
50+ } ) ;
51+ }
52+ } ) ;
53+ filters . push ( { name : 'Any File' , extensions : [ '*' ] } ) ;
54+ var _this = this ;
4455
56+ dialog . showOpenDialog ( {
57+ buttonLabel : 'Import' ,
58+ properties : [ 'openFile' ] ,
59+ filters : filters
60+ } , function ( filenames ) {
61+ var filename = filenames [ 0 ] ;
62+ if ( Util . isset ( filename , 'string' ) ) {
63+ var temp = path . extname ( filename ) . replace ( '.' , '' ) . toLowerCase ( ) ;
64+ switch ( temp ) {
65+ case 'csv' :
66+ _this . _impcsv ( filename ) ;
67+ return true ;
68+ break ;
69+ default :
70+ return false ;
71+ }
72+ }
73+ } ) ;
4574 }
4675
4776 // internal methods
4877
4978 _expcsv ( ) {
5079 var storage = this . store ;
51- var proto = this . data . export . csv ;
80+ var config = this . data . filter ( function ( e ) {
81+ if ( e . name === 'csv' ) {
82+ return true ;
83+ } else {
84+ return false ;
85+ }
86+ } ) [ 0 ] ;
5287 var files = storage . retrieveFiles ( ) ;
5388 var csv_string = '' ;
5489 if ( files . length > 0 ) {
55- proto . labels . forEach ( function ( element , index , array ) {
90+ config . labels . forEach ( function ( element , index , array ) {
5691 csv_string += element ;
5792 if ( index < array . length - 1 ) {
58- csv_string += proto . divider . field ;
93+ csv_string += config . divider . field ;
5994 } else {
60- csv_string += proto . divider . line ;
95+ csv_string += config . divider . line ;
6196 }
6297 } )
6398 files . forEach ( function ( element , index , array ) {
6499 var snippet = new Snippet ( JSON . parse ( storage . retrieveFile ( element . replace ( '.json' , '' ) ) ) ) ;
65- proto . order . forEach ( function ( element , index , array ) {
66- if ( element === 'content' ) {
67- csv_string += '"' + snippet . _get ( element ) . split ( '\n' ) . join ( '[LF]' ) . split ( '\r' ) . join ( '[CR]' ) . split ( ';' ) . join ( '[SC]' ) + '"' ;
68- } else if ( element === 'tags' ) {
69- csv_string += '"' + JSON . stringify ( snippet . _get ( element ) ) + '"' ;
70- } else {
71- csv_string += '"' + snippet . _get ( element ) + '"' ;
100+ config . order . forEach ( function ( e , i , a ) {
101+ var insert = snippet . _get ( e ) ;
102+ if ( config . convert . length > 0 ) {
103+ config . convert . filter ( function ( item ) {
104+ if ( item . name === e ) {
105+ return true ;
106+ } else {
107+ return false ;
108+ }
109+ } ) . forEach ( function ( conversion ) {
110+ if ( conversion . type === 'replace' ) {
111+ insert = insert . split ( conversion . search ) . join ( conversion . replace ) ;
112+ } else if ( conversion . type === 'json' ) {
113+ insert = JSON . stringify ( insert ) ;
114+ }
115+ } ) ;
72116 }
117+ csv_string += insert ;
73118
74- if ( index < array . length - 1 ) {
75- csv_string += proto . divider . field ;
119+ if ( i < a . length - 1 ) {
120+ csv_string += config . divider . field ;
76121 } else {
77- csv_string += proto . divider . line ;
122+ csv_string += config . divider . line ;
78123 }
79124 } ) ;
80125 } ) ;
81126
127+ var filters = new Array ( ) ;
128+ this . data . forEach ( function ( e ) {
129+ filters . push ( {
130+ name : e . dialogOptions . name ,
131+ extensions : e . dialogOptions . extensions
132+ } ) ;
133+ } ) ;
134+ filters . push ( { name : 'Any File' , extensions : [ '*' ] } ) ;
135+
82136 dialog . showSaveDialog ( {
83- title : 'Choose the location for your export' ,
84137 buttonLabel : 'Export' ,
85- filters : [
86- { name : 'CSV File' , extensions : [ 'csv' ] } ,
87- { name : 'Any File' , extensions : [ '*' ] }
88- ]
138+ filters : filters
89139 } , function ( filename ) {
90140 if ( Util . isset ( filename , 'string' ) ) {
91141 fs . writeFileSync ( filename , csv_string ) ;
@@ -103,4 +153,58 @@ export default class IMEX {
103153 return false ;
104154 }
105155 }
156+
157+ _impcsv ( filename ) {
158+ var storage = this . store ;
159+ var config = this . data . filter ( function ( e ) {
160+ if ( e . name === 'csv' ) {
161+ return true ;
162+ } else {
163+ return false ;
164+ }
165+ } ) [ 0 ] ;
166+
167+ var count = 0 ;
168+ var file = fs . readFileSync ( filename ) . toString ( ) ;
169+ file . split ( config . divider . line ) . forEach ( function ( e , i , a ) {
170+ var temp = ( e !== '' && e !== ' ' ) ;
171+ config . labels . forEach ( function ( label ) {
172+ if ( e . indexOf ( label ) > - 1 ) {
173+ temp = false ;
174+ }
175+ } ) ;
176+ if ( temp ) {
177+ var snippet = new Snippet ( ) ;
178+ e . split ( config . divider . field ) . forEach ( function ( field , index ) {
179+ var type = config . order [ index ] ;
180+ var insert = field ;
181+ if ( config . convert . length > 0 ) {
182+ config . convert . filter ( function ( item ) {
183+ if ( item . name === type ) {
184+ return true ;
185+ } else {
186+ return false ;
187+ }
188+ } ) . forEach ( function ( conversion ) {
189+ if ( conversion . type === 'replace' ) {
190+ insert = insert . split ( conversion . replace ) . join ( conversion . search ) ;
191+ } else if ( conversion . type === 'json' ) {
192+ insert = JSON . parse ( insert ) ;
193+ }
194+ } ) ;
195+ }
196+ snippet . _set ( type , insert ) ;
197+ } ) ;
198+ storage . store ( snippet ) ;
199+ count ++ ;
200+ }
201+ } ) ;
202+ if ( count < 1 ) {
203+ atom . notifications . addWarning ( 'No snippets have been imported.' , null )
204+ } else if ( count == 1 ) {
205+ atom . notifications . addSuccess ( 'One snippet has been imported.' , null ) ;
206+ } else {
207+ atom . notifications . addSuccess ( count + ' snippets have been imported.' , null ) ;
208+ }
209+ }
106210}
0 commit comments