* Add Media Query
(stylesheet)
| 209 | * Add Media Query |
| 210 | **/ |
| 211 | function addViaMediaQuery(stylesheet) { |
| 212 | if (!stylesheet) { |
| 213 | if(DEBUG) $log.error('No stylesheet provided'); |
| 214 | return; |
| 215 | } |
| 216 | // Media query object |
| 217 | mediaQuery[stylesheet.href] = $window.matchMedia(stylesheet.media); |
| 218 | // Media Query Listener function |
| 219 | mediaQueryListener[stylesheet.href] = function(mediaQuery) { |
| 220 | // Trigger digest |
| 221 | $timeout(function () { |
| 222 | if (mediaQuery.matches) { |
| 223 | // Add stylesheet |
| 224 | $rootScope.stylesheets.push(stylesheet); |
| 225 | } else { |
| 226 | var index = $rootScope.stylesheets.indexOf($filter('filter')($rootScope.stylesheets, { |
| 227 | href: stylesheet.href |
| 228 | })[0]); |
| 229 | // Remove stylesheet |
| 230 | if (index !== -1) { |
| 231 | $rootScope.stylesheets.splice(index, 1); |
| 232 | } |
| 233 | } |
| 234 | }); |
| 235 | }; |
| 236 | // Listen for media query changes |
| 237 | mediaQuery[stylesheet.href].addListener(mediaQueryListener[stylesheet.href]); |
| 238 | // Invoke first media query check |
| 239 | mediaQueryListener[stylesheet.href](mediaQuery[stylesheet.href]); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Remove Media Query |