99 GIT_REPO_NAME = require ( './node_modules/git-repo-name' ) ,
1010
1111 // Git class that perfoms git commands
12- GIT = require ( './app/core/git' ) ;
12+ GIT = require ( './app/core/git' ) ,
13+
14+ // Locale language
15+ LANG = window . navigator . userLanguage || window . navigator . language ,
16+
17+ // Messages and labels of the application
18+ MSGS ;
1319
1420WIN . focus ( ) ;
1521
@@ -20,12 +26,21 @@ process.on('uncaughtException', function(err) {
2026 alert ( err ) ;
2127} ) ;
2228
29+ /* Get the locale language */
30+ try {
31+ MSGS = require ( './language/' . concat ( LANG ) . concat ( '.json' ) ) ;
32+ } catch ( err ) {
33+ MSGS = require ( './language/en.json' ) ;
34+ }
35+
2336/* AngularJS app init */
2437( function ( ) {
2538 var app = angular . module ( 'gitpie' , [ 'components' , 'attributes' , 'header' , 'content' ] ) ;
2639
27- app . factory ( 'CommomService' , function ( ) {
28- var storagedRepos = JSON . parse ( localStorage . getItem ( 'repos' ) ) || [ ] ,
40+ app . factory ( 'CommomService' , function ( $rootScope ) {
41+ var repositoriesStr = localStorage . getItem ( 'repos' ) ,
42+
43+ repositories = JSON . parse ( repositoriesStr ) || { } ,
2944
3045 findWhere = function ( array , object ) {
3146
@@ -39,47 +54,134 @@ process.on('uncaughtException', function(err) {
3954 return null ;
4055 } ,
4156
42- repositories = [ ] ;
57+ saveRepository = function ( repository ) {
58+ var storagedRepositories = JSON . parse ( repositoriesStr ) || { } ;
59+
60+ storagedRepositories . github = storagedRepositories . github || [ ] ;
61+ storagedRepositories . bitbucket = storagedRepositories . bitbucket || [ ] ;
62+ storagedRepositories . outhers = storagedRepositories . outhers || [ ] ;
63+
64+ switch ( repository . type ) {
65+ case 'GITHUB' :
66+ storagedRepositories . github . push ( repository ) ;
67+ break ;
68+
69+ case 'BITBUCKET' :
70+ storagedRepositories . bitbucket . push ( repository ) ;
71+ break ;
72+
73+ default :
74+ storagedRepositories . outhers . push ( repository ) ;
75+ break ;
76+ }
4377
44- storagedRepos . forEach ( function ( item ) {
45- delete item . $$hashKey ;
46- delete item . selected ;
78+ localStorage . setItem ( 'repos' , JSON . stringify ( storagedRepositories ) ) ;
79+ repositoriesStr = JSON . stringify ( storagedRepositories ) ;
80+ } ;
4781
48- repositories . push ( item ) ;
49- } ) ;
82+ repositories . github = repositories . github || [ ] ;
83+ repositories . bitbucket = repositories . bitbucket || [ ] ;
84+ repositories . outhers = repositories . outhers || [ ] ;
85+
86+ if ( repositories . github . length > 0 || repositories . bitbucket . length > 0 || repositories . outhers . length > 0 ) {
87+ repositories . isEmpty = false ;
88+ } else {
89+ repositories . isEmpty = true ;
90+ }
91+
92+ // Set the application messages globally
93+ $rootScope . MSGS = MSGS ;
5094
5195 return {
5296
53- addRepository : function ( repositoryPath ) {
97+ addRepository : function ( repositoryPath , callback ) {
5498
5599 if ( repositoryPath ) {
56- var repositoryExists = findWhere ( repositories , { path : repositoryPath } ) ;
57100
58101 // Easter egg :D
59- if ( repositoryPath . toLowerCase ( ) === 'i have no idea' ) {
60- alert ( 'It happends with me all the time too. But let \'s try find your project again!' ) ;
102+ if ( repositoryPath . toLowerCase ( ) === $rootScope . MSGS [ 'i have no idea' ] ) {
103+ alert ( $rootScope . MSGS [ 'It happends with me all the time too. But lets \'s try find your project again!' ] ) ;
61104
62- } else if ( ! repositoryExists ) {
63- var name = GIT_REPO_NAME ( repositoryPath ) ;
105+ } else {
106+ var name = GIT_REPO_NAME ( repositoryPath ) ,
107+ type ,
108+ index ,
109+ repositoryExists ,
110+ repository ;
64111
65112 if ( name ) {
66- var index = repositories . push ( {
67- name : name ,
68- path : repositoryPath
69- } ) ;
70113
71- localStorage . setItem ( 'repos' , JSON . stringify ( repositories ) ) ;
114+ GIT . listRemotes ( repositoryPath , function ( err , stdout ) {
115+
116+ if ( stdout . toLowerCase ( ) . indexOf ( 'github.com' ) != - 1 ) {
117+ repositoryExists = findWhere ( repositories . github , { path : repositoryPath } ) ;
118+ type = 'github' ;
119+
120+ } else if ( stdout . toLowerCase ( ) . indexOf ( 'bitbucket.org' ) != - 1 ) {
121+ repositoryExists = findWhere ( repositories . bitbucket , { path : repositoryPath } ) ;
122+ type = 'bitbucket' ;
123+
124+ } else {
125+ repositoryExists = findWhere ( repositories . outhers , { path : repositoryPath } ) ;
126+ type = 'outhers' ;
127+ }
128+
129+ if ( ! repositoryExists ) {
130+
131+ index = repositories [ type ] . push ( {
132+ name : name ,
133+ path : repositoryPath ,
134+ type : type . toUpperCase ( )
135+ } ) ;
136+
137+ repository = repositories [ type ] [ index - 1 ] ;
138+
139+ saveRepository ( repository ) ;
140+ } else {
141+ repository = repositoryExists ;
142+ }
143+
144+ repositories . isEmpty = false ;
145+
146+ if ( callback && typeof callback == 'function' ) {
147+ callback . call ( this , repository ) ;
148+ }
149+ } ) ;
72150
73- return repositories [ index - 1 ] ;
74151 } else {
75- alert ( 'Nothing for me here.\n The folder ' + repositoryPath + ' is not a git project') ;
152+ alert ( $rootScope . MSGS [ 'Nothing for me here.\n The folder {folder} is not a git project' ] . replace ( '{folder}' , repositoryPath ) ) ;
76153 }
77- } else {
78- return repositoryExists ;
79154 }
80155 }
81156 } ,
82157
158+ // Return true if the repository was selected and false case not
159+ removeRepository : function ( repositoryType , index ) {
160+ var storagedRepositories = JSON . parse ( repositoriesStr ) || { } ,
161+ type = repositoryType . toLowerCase ( ) ,
162+ removedRepository ;
163+
164+ storagedRepositories . github = storagedRepositories . github || [ ] ;
165+ storagedRepositories . bitbucket = storagedRepositories . bitbucket || [ ] ;
166+ storagedRepositories . outhers = storagedRepositories . outhers || [ ] ;
167+
168+ storagedRepositories [ type ] . splice ( index , 1 ) ;
169+ removedRepository = repositories [ type ] . splice ( index , 1 ) ;
170+
171+ localStorage . setItem ( 'repos' , JSON . stringify ( storagedRepositories ) ) ;
172+ repositoriesStr = JSON . stringify ( storagedRepositories ) ;
173+
174+ return removedRepository [ 0 ] . selected ;
175+ } ,
176+
177+ closeAnyContextMenu : function ( ) {
178+ var contextMenus = document . querySelectorAll ( '.context-menu' ) ;
179+
180+ angular . forEach ( contextMenus , function ( item ) {
181+ document . body . removeChild ( item ) ;
182+ } ) ;
183+ } ,
184+
83185 repositories : repositories
84186 } ;
85187 } ) ;
0 commit comments