@@ -218,6 +218,41 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
218
218
return state ;
219
219
}
220
220
221
+ // Checks text to see if it looks like a glob.
222
+ function isGlob ( text ) {
223
+ return text . indexOf ( '*' ) > - 1 ;
224
+ }
225
+
226
+ // Returns true if glob matches current $state name.
227
+ function doesStateMatchGlob ( glob ) {
228
+ var globSegments = glob . split ( '.' ) ,
229
+ segments = $state . $current . name . split ( '.' ) ;
230
+
231
+ //match greedy starts
232
+ if ( globSegments [ 0 ] === '**' ) {
233
+ segments = segments . slice ( segments . indexOf ( globSegments [ 1 ] ) ) ;
234
+ segments . unshift ( '**' ) ;
235
+ }
236
+ //match greedy ends
237
+ if ( globSegments [ globSegments . length - 1 ] === '**' ) {
238
+ segments . splice ( segments . indexOf ( globSegments [ globSegments . length - 2 ] ) + 1 , Number . MAX_VALUE ) ;
239
+ segments . push ( '**' ) ;
240
+ }
241
+
242
+ if ( globSegments . length != segments . length ) {
243
+ return false ;
244
+ }
245
+
246
+ //match single stars
247
+ for ( var i = 0 , l = globSegments . length ; i < l ; i ++ ) {
248
+ if ( globSegments [ i ] === '*' ) {
249
+ segments [ i ] = '*' ;
250
+ }
251
+ }
252
+
253
+ return segments . join ( '' ) === globSegments . join ( '' ) ;
254
+ }
255
+
221
256
222
257
// Implicit root state that is always active
223
258
root = registerState ( {
@@ -970,19 +1005,46 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
970
1005
*
971
1006
* @example
972
1007
* <pre>
1008
+ * $state.$current.name = 'contacts.details.item';
1009
+ *
973
1010
* $state.includes("contacts"); // returns true
974
1011
* $state.includes("contacts.details"); // returns true
975
1012
* $state.includes("contacts.details.item"); // returns true
976
1013
* $state.includes("contacts.list"); // returns false
977
1014
* $state.includes("about"); // returns false
978
1015
* </pre>
979
1016
*
980
- * @param {string|object } stateOrName A full or partial state name to be searched for within the current state name.
981
- * @param {object= } params A param object, e.g. `{sectionId: section.id}`,
1017
+ * @description
1018
+ * Basic globing patterns will also work.
1019
+ *
1020
+ * @example
1021
+ * <pre>
1022
+ * $state.$current.name = 'contacts.details.item.url';
1023
+ *
1024
+ * $state.includes("*.details.*.*"); // returns true
1025
+ * $state.includes("*.details.**"); // returns true
1026
+ * $state.includes("**.item.**"); // returns true
1027
+ * $state.includes("*.details.item.url"); // returns true
1028
+ * $state.includes("*.details.*.url"); // returns true
1029
+ * $state.includes("*.details.*"); // returns false
1030
+ * $state.includes("item.**"); // returns false
1031
+ * </pre>
1032
+ *
1033
+ * @param {string } stateOrName A partial name to be searched for within the current state name.
1034
+ * @param {object } params A param object, e.g. `{sectionId: section.id}`,
982
1035
* that you'd like to test against the current active state.
983
1036
* @returns {boolean } Returns true if it does include the state
984
1037
*/
1038
+
985
1039
$state . includes = function includes ( stateOrName , params ) {
1040
+ if ( isString ( stateOrName ) && isGlob ( stateOrName ) ) {
1041
+ if ( doesStateMatchGlob ( stateOrName ) ) {
1042
+ stateOrName = $state . $current . name ;
1043
+ } else {
1044
+ return false ;
1045
+ }
1046
+ }
1047
+
986
1048
var state = findState ( stateOrName ) ;
987
1049
if ( ! isDefined ( state ) ) {
988
1050
return undefined ;
@@ -1001,6 +1063,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
1001
1063
return validParams ;
1002
1064
} ;
1003
1065
1066
+
1004
1067
/**
1005
1068
* @ngdoc function
1006
1069
* @name ui.router.state.$state#href
0 commit comments