2
2
* You can obtain one at http://mozilla.org/MPL/2.0/. */
3
3
4
4
const siteUtil = require ( '../../../js/state/siteUtil' )
5
+ const bookmarkFoldersState = require ( '../state/bookmarkFoldersState' )
5
6
6
7
const isFolderNameValid = ( title ) => {
7
8
return ( title != null && title !== 0 ) && title . trim ( ) . length > 0
8
9
}
9
10
10
- /**
11
- * Obtains an array of folders
12
- */
13
- const getFolders = ( sites , folderId , parentId , labelPrefix ) => {
14
- parentId = parentId || 0
15
- let folders = [ ]
16
- const results = sites
17
- . filter ( site => site . get ( 'parentFolderId' , 0 ) === parentId )
18
- . toList ( )
19
- . sort ( siteUtil . siteSort )
20
-
21
- const resultSize = results . size
22
- for ( let i = 0 ; i < resultSize ; i ++ ) {
23
- const site = results . get ( i )
24
- if ( site . get ( 'folderId' ) === folderId ) {
25
- continue
26
- }
27
-
28
- const label = ( labelPrefix || '' ) + site . get ( 'title' )
29
- folders . push ( {
30
- folderId : site . get ( 'folderId' ) ,
31
- parentFolderId : site . get ( 'parentFolderId' ) ,
32
- label
33
- } )
34
- const subSites = getFolders ( sites , folderId , site . get ( 'folderId' ) , ( label || '' ) + ' / ' )
35
- folders = folders . concat ( subSites )
36
- }
37
-
38
- return folders
39
- }
40
-
41
11
const getNextFolderIdItem = ( sites ) =>
42
12
sites . max ( ( siteA , siteB ) => {
43
13
const folderIdA = siteA . get ( 'folderId' )
@@ -63,8 +33,53 @@ const getNextFolderId = (sites) => {
63
33
return ( maxIdItem ? ( maxIdItem . get ( 'folderId' ) || 0 ) : 0 ) + 1
64
34
}
65
35
36
+ const getNextFolderName = ( folders , name ) => {
37
+ if ( ! folders ) {
38
+ return name
39
+ }
40
+ const site = folders . find ( ( site ) => site . get ( 'customTitle' ) === name )
41
+ if ( ! site ) {
42
+ return name
43
+ }
44
+ const filenameFormat = / ( .* ) \( ( \d + ) \) /
45
+ let result = filenameFormat . exec ( name )
46
+ if ( ! result ) {
47
+ return getNextFolderName ( folders , name + ' (1)' )
48
+ }
49
+
50
+ const nextNum = parseInt ( result [ 2 ] ) + 1
51
+ return getNextFolderName ( folders , result [ 1 ] + ' (' + nextNum + ')' )
52
+ }
53
+
54
+ const getFoldersWithoutKey = ( state , folderKey , parentId = 0 , labelPrefix = '' ) => {
55
+ let folders = [ ]
56
+ const results = bookmarkFoldersState . getFolders ( state )
57
+ . filter ( site => site . get ( 'parentFolderId' , 0 ) === parentId )
58
+ . toList ( )
59
+ . sort ( siteUtil . siteSort )
60
+
61
+ const resultSize = results . size
62
+ for ( let i = 0 ; i < resultSize ; i ++ ) {
63
+ const folder = results . get ( i )
64
+ if ( folder . get ( 'folderId' ) === folderKey ) {
65
+ continue
66
+ }
67
+
68
+ const label = labelPrefix + folder . get ( 'title' )
69
+ folders . push ( {
70
+ folderId : folder . get ( 'folderId' ) ,
71
+ label
72
+ } )
73
+ const subSites = getFoldersWithoutKey ( state , folderKey , folder . get ( 'folderId' ) , ( label || '' ) + ' / ' )
74
+ folders = folders . concat ( subSites )
75
+ }
76
+
77
+ return folders
78
+ }
79
+
66
80
module . exports = {
67
81
isFolderNameValid,
68
- getFolders,
69
- getNextFolderId
82
+ getNextFolderId,
83
+ getNextFolderName,
84
+ getFoldersWithoutKey
70
85
}
0 commit comments