11
11
12
12
( function ( window ) {
13
13
14
- var ngDefine ;
15
-
16
- var MODULE_DEPENDENCY = / ^ m o d u l e : ( [ ^ : ] * ) ( : ( .* ) ) ? $ / ;
17
- var INTERNAL = / ^ n g / ;
18
-
19
- function isFunction ( value ) { return typeof value == 'function' ; }
20
-
21
- function isInternal ( module ) {
22
- return INTERNAL . test ( module ) ;
23
- }
24
-
25
- function asFileDependency ( module ) {
26
- return module . replace ( / \. / g, "/" ) ;
27
- }
28
-
29
- function toArray ( arrayLike ) {
30
- return Array . prototype . slice . call ( arrayLike , 0 ) ;
31
- }
32
-
33
- /**
34
- * For each implementation as used by AngularJS
35
- */
36
- function forEach ( obj , iterator , context ) {
37
- var key ;
38
- if ( obj ) {
39
- if ( isFunction ( obj ) ) {
40
- for ( key in obj ) {
41
- if ( key != 'prototype' && key != 'length' && key != 'name' && obj . hasOwnProperty ( key ) ) {
42
- iterator . call ( context , obj [ key ] , key ) ;
43
- }
44
- }
45
- } else if ( obj . forEach && obj . forEach !== forEach ) {
46
- obj . forEach ( iterator , context ) ;
47
- } else if ( isArrayLike ( obj ) ) {
48
- for ( key = 0 ; key < obj . length ; key ++ )
49
- iterator . call ( context , obj [ key ] , key ) ;
50
- } else {
51
- for ( key in obj ) {
52
- if ( obj . hasOwnProperty ( key ) ) {
53
- iterator . call ( context , obj [ key ] , key ) ;
54
- }
55
- }
56
- }
57
- }
58
- return obj ;
59
- }
60
-
61
- function parseNgModule ( name , dependencies ) {
14
+ define ( [ 'angular' , 'ngParse' ] , function ( angular , ngParse ) {
62
15
63
- var files = [ ] ,
64
- modules = [ ] ;
16
+ //////// utilities /////////
65
17
66
- forEach ( dependencies , function ( d ) {
67
- var moduleMatch = d . match ( MODULE_DEPENDENCY ) ;
68
- if ( moduleMatch ) {
69
- var module = moduleMatch [ 1 ] ,
70
- path = moduleMatch [ 3 ] ;
18
+ function toArray ( arrayLike ) {
19
+ return Array . prototype . slice . call ( arrayLike , 0 ) ;
20
+ }
71
21
72
- if ( ! path && ! isInternal ( module ) ) {
73
- // infer path from module name
74
- path = asFileDependency ( module ) ;
75
- }
76
22
77
- // add module dependency
78
- modules . push ( module ) ;
23
+ ///////// main /////////
24
+
25
+ function internalModule ( angular , name , dependencies , body ) {
79
26
80
- if ( path ) {
81
- // add path dependency if it exists
82
- files . push ( path ) ;
83
- }
84
- } else {
85
- files . push ( d ) ;
27
+ if ( ! body ) {
28
+ body = dependencies ;
29
+ dependencies = null ;
86
30
}
87
- } ) ;
88
31
89
- return { name : name , fileDependencies : files , moduleDependencies : modules } ;
90
- }
32
+ var definition = ngParse . parseNgModule ( name , dependencies || [ ] ) ;
91
33
92
- function internalModule ( angular , name , dependencies , body ) {
34
+ var module , exists ;
93
35
94
- if ( ! body ) {
95
- body = dependencies ;
96
- dependencies = null ;
97
- }
36
+ var moduleDependencies = definition . moduleDependencies ,
37
+ fileDependencies = definition . fileDependencies ;
98
38
99
- var definition = parseNgModule ( name , dependencies || [ ] ) ;
39
+ try {
40
+ angular . module ( name ) ;
41
+ exists = true ;
42
+ } catch ( e ) {
43
+ exists = false ;
44
+ }
100
45
101
- var module , exists ;
46
+ if ( moduleDependencies . length && exists ) {
47
+ throw new Error (
48
+ "Cannot re-define angular module " + name + " with new dependencies [" + moduleDependencies + "]. " +
49
+ "Make sure the module is not defined else where or define a sub-module with additional angular module dependencies instead." ) ;
50
+ }
102
51
103
- var moduleDependencies = definition . moduleDependencies ,
104
- fileDependencies = definition . fileDependencies ;
52
+ if ( moduleDependencies . length || ! exists ) {
53
+ module = angular . module ( name , moduleDependencies ) ;
54
+ debugLog ( name , "defined with dependencies" , moduleDependencies ) ;
55
+ } else {
56
+ module = angular . module ( name ) ;
57
+ debugLog ( name , "looked up" ) ;
58
+ }
105
59
106
- try {
107
- angular . module ( name ) ;
108
- exists = true ;
109
- } catch ( e ) {
110
- exists = false ;
111
- }
60
+ define ( fileDependencies , function ( ) {
61
+ var results = toArray ( arguments ) ;
62
+ results . unshift ( module ) ;
112
63
113
- if ( moduleDependencies . length && exists ) {
114
- throw new Error (
115
- "Cannot re-define angular module " + name + " with new dependencies [" + moduleDependencies + "]. " +
116
- "Make sure the module is not defined else where or define a sub-module with additional angular module dependencies instead." ) ;
117
- }
64
+ body . apply ( window , results ) ;
118
65
119
- if ( moduleDependencies . length || ! exists ) {
120
- module = angular . module ( name , moduleDependencies ) ;
121
- debugLog ( name , "defined with dependencies" , moduleDependencies ) ;
122
- } else {
123
- module = angular . module ( name ) ;
124
- debugLog ( name , "looked up" ) ;
66
+ debugLog ( name , "loaded" ) ;
67
+ return module ;
68
+ } ) ;
125
69
}
126
70
127
- define ( fileDependencies , function ( ) {
128
- var results = toArray ( arguments ) ;
129
- results . unshift ( module ) ;
130
-
131
- body . apply ( window , results ) ;
132
-
133
- debugLog ( name , "loaded" ) ;
134
- return module ;
135
- } ) ;
136
- }
137
-
138
- /**
139
- * Angular module definition / lookup
140
- *
141
- * @param {string } name of the module
142
- * @param {string } (optional) dependencies of the module
143
- * @param {Function } body function defining the module
144
- */
145
- function angularDefine ( angular ) {
146
- var fn = function ( name , dependencies , body ) {
71
+
72
+ //////// module exports /////////
73
+
74
+ var exports = function ( name , dependencies , body ) {
147
75
if ( ! dependencies ) {
148
76
throw new Error ( "wrong number of arguments, expected name[, dependencies], body" ) ;
149
77
}
150
78
internalModule ( angular , name , dependencies , body ) ;
151
79
} ;
152
80
153
- fn . parseNgModule = parseNgModule ;
81
+ if ( typeof window !== undefined && ! window . ngDefine ) {
82
+ window . ngDefine = exports ;
83
+ }
154
84
155
- return fn ;
156
- }
157
85
158
- var debugLog ;
86
+ ///////// logging /////////
159
87
160
- // for logging only
161
- ( function ( ) {
162
- var log ;
88
+ var debugLog = ( function ( ) {
89
+ var log ;
163
90
164
- // IE 9 logging #!?.
165
- if ( Function . prototype . bind && window . console && window . console . log ) {
166
- log = Function . prototype . bind . call ( window . console . log , window . console ) ;
167
- }
168
-
169
- debugLog = function ( ) {
170
- if ( ! ngDefine . debug || ! log ) {
171
- return ;
91
+ // IE 9 logging #!?.
92
+ if ( Function . prototype . bind && window . console && window . console . log ) {
93
+ log = Function . prototype . bind . call ( window . console . log , window . console ) ;
172
94
}
173
95
174
- var args = toArray ( arguments ) ;
175
- args . unshift ( "[ngDefine]" ) ;
176
-
177
- log . apply ( log , args ) ;
178
- } ;
179
- } ) ( ) ;
96
+ return function ( ) {
97
+ if ( ! exports . debug || ! log ) {
98
+ return ;
99
+ }
180
100
181
- define ( [ 'angular' ] , function ( angular ) {
101
+ var args = toArray ( arguments ) ;
102
+ args . unshift ( "[ngDefine]" ) ;
182
103
183
- ngDefine = ngDefine || angularDefine ( angular ) ;
104
+ log . apply ( log , args ) ;
105
+ } ;
106
+ } ) ( ) ;
184
107
185
- if ( ! window . ngDefine ) {
186
- window . ngDefine = ngDefine ;
187
- }
188
108
189
- // publish as requireJS module
190
- return ngDefine ;
109
+ ///////// export //////////
110
+ return exports ;
191
111
} ) ;
192
-
193
- // publish statically in case we use the module outside
194
- // a requirejs context (e.g. during testing)
195
- if ( window . angular ) {
196
- window . ngDefine = ngDefine = ( ngDefine || angularDefine ( window . angular ) ) ;
197
- }
198
112
} ) ( window ) ;
0 commit comments