11import Ember from "ember" ;
2- import _ from 'lodash/lodash' ;
3-
42const { computed } = Ember ;
53
64export default Ember . Component . extend ( {
75 jsTreeActionReceiver : null ,
86
9- /**
10- * Calculate data for file tree
11- */
12- fileTreeData : computed ( 'model.files.[]' , function ( ) {
13- let seq = 0 ;
14- let treeData = this . get ( 'model.files' ) . map ( function ( file ) {
15- let path = file . get ( 'filePath' ) ;
16- let splitPath = path . split ( "/" ) ;
17- let parentPath = splitPath . slice ( 0 , - 1 ) . join ( "/" ) ;
18- let fileName = splitPath [ splitPath . length - 1 ] ;
19- if ( parentPath === "" ) {
20- parentPath = "#" ;
21- }
22- return {
23- id : "node" + seq ++ ,
24- text : fileName ,
25- parent : parentPath ,
26- icon : "glyphicon glyphicon-file light-gray" ,
27- path : path ,
28- leaf : true
29- } ;
30- } ) ;
7+ fileTreeHash : computed ( 'model.files.[]' , function ( ) {
8+ const files = this . get ( 'model.files' ) ;
319
32- let done = false ;
33- do {
34- done = true ;
35- let paths = _ . uniq ( _ . pluck ( treeData , 'text' ) ) ;
36- let parents = _ . uniq ( _ . pluck ( treeData , 'parent' ) ) ;
37- parents . forEach ( function ( parent ) {
38- if ( ! paths . contains ( parent ) && parent !== "#" && treeData . filterBy ( 'path' , parent ) . length === 0 ) {
39- let splitPath = parent . split ( "/" ) ;
40- let parentPath = splitPath . slice ( 0 , - 1 ) . join ( "/" ) ;
41- let fileName = splitPath [ splitPath . length - 1 ] ;
42- if ( parentPath === "" ) {
43- parentPath = "#" ;
44- }
45- treeData . push ( {
46- id : "node" + seq ++ ,
47- text : fileName ,
48- parent : parentPath ,
49- icon : "glyphicon glyphicon-folder-open yellow" ,
50- path : parent
51- } ) ;
52- done = false ;
10+ return files . reduce ( ( accumulator , file ) => {
11+ const path = file . get ( 'filePath' ) ;
12+ const splitPath = path . split ( '/' ) ;
13+ const splitPathZeroBasedLength = splitPath . length - 1 ;
14+
15+ const possiblePaths = splitPath . map ( ( pathPart , index , paths ) => {
16+ const previousPathIsRoot = index - 1 < 0 ;
17+ const pathObj = {
18+ nodeName : pathPart ,
19+ isFile : false
20+ } ;
21+
22+ if ( previousPathIsRoot ) {
23+ pathObj . path = pathPart ;
24+ pathObj . parent = '#' ;
25+ } else {
26+ const previousPath = paths . slice ( 0 , index ) . join ( '/' ) ;
27+ pathObj . path = `${ previousPath } /${ pathPart } ` ;
28+ pathObj . parent = previousPath ;
29+ }
30+
31+ if ( index === splitPathZeroBasedLength ) {
32+ pathObj . isFile = true ;
5333 }
34+
35+ return pathObj ;
5436 } ) ;
55- } while ( ! done ) ;
5637
57- let idMap = { } ;
58- treeData . forEach ( function ( node ) {
59- idMap [ node . path ] = node . id ;
60- } ) ;
38+ const existingFilteredOut = possiblePaths . filter ( pathObj => {
39+ return ! accumulator [ pathObj . path ] ;
40+ } ) ;
6141
62- treeData . forEach ( function ( node ) {
63- if ( node . parent !== "#" ) {
64- node . parent = idMap [ node . parent ] ;
42+ existingFilteredOut . forEach ( pathObj => {
43+ accumulator [ pathObj . path ] = pathObj ;
44+ } ) ;
45+
46+ return accumulator ;
47+ } , { } ) ;
48+ } ) ,
49+
50+ /**
51+ * Calculate data for file tree
52+ */
53+ fileTreeData : computed ( 'fileTreeHash' , function ( ) {
54+ const fileTreeHash = this . get ( 'fileTreeHash' ) ;
55+ const fileTreeKeys = Object . keys ( fileTreeHash ) ;
56+ const fileTreeObjects = fileTreeKeys . map ( key => fileTreeHash [ key ] ) ;
57+
58+ return fileTreeObjects . map ( treeObject => {
59+ const treeDataObject = {
60+ id : treeObject . path ,
61+ text : treeObject . nodeName ,
62+ parent : treeObject . parent ,
63+ path : treeObject . path
64+ } ;
65+
66+ if ( treeObject . isFile ) {
67+ treeDataObject . leaf = true ;
68+ treeDataObject . icon = 'glyphicon glyphicon-file light-gray' ;
69+ } else {
70+ treeDataObject . icon = 'glyphicon glyphicon-folder-open yellow' ;
6571 }
66- } ) ;
6772
68- return treeData ;
73+ return treeDataObject ;
74+ } ) ;
6975 } ) ,
7076
7177 actions : {
@@ -77,7 +83,10 @@ export default Ember.Component.extend({
7783 this . get ( 'jsTreeActionReceiver' ) . send ( 'toggleNode' , node . id ) ;
7884 } ,
7985
80- handleReady ( ) {
86+ didBecomeReady ( ) {
87+ if ( this . attrs . didBecomeReady ) {
88+ this . attrs . didBecomeReady ( ) ;
89+ }
8190 } ,
8291
8392 hideFileTree ( ) {
0 commit comments