Coder Social home page Coder Social logo

dwperrin / angular-match-media Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jacopotarantino/angular-match-media

0.0 1.0 0.0 65 KB

Angular module to use Bootstrap3 media queries in your angular controllers.

Home Page: https://jack.ofspades.com/angular-matchmedia-module/

License: Other

JavaScript 100.00%

angular-match-media's Introduction

Angular matchMedia Module

Provides an Angular service that returns true if the current screen width matches or false if not. Uses the screen widths predefined in Twitter Bootstrap 3 or a customized size you define. There is a staic method is which checks for a match on page load and a dynamic method on which checks for a match and updates the value on window resize.

Installation

Download the component via bower:

bower install --save angular-media-queries

Include AngularJS on the page and then include this script. If possible, include these scripts in the footer of your site before the closing </body> tag.

<script type='text/javascript' src='/static/path/to/angular.min.js'></script>
<script type='text/javascript' src='/static/path/to/angular-media-queries/match-media.js'></script>

Usage

Require the matchMedia module as a dependency in your app:

angular.module('myApp', ['matchMedia'])

In a Controller

Is

Use the service's is method to determine if the screensize matches the given string/array.

angular.module('myApp', ['matchMedia'])
.controller('mainController', ['screenSize', function (screenSize) {
  var data = complicatedChartData;

  //Determine to either perform cpu/network-intensive actions(desktop) or retrieve a small static image(mobile). 
  if (screenSize.is('xs, sm')) {
    // it's a mobile device so fetch a small image
    $http.post('/imageGenerator', data)
    .success(function (response) {
      document.querySelector('.chart').src = response.chartUrl;
    });
  }
  else {
    // it's a desktop size so do the complicated calculations and render that
    document.querySelector('.chart')
    .appendCanvas()
    .parseData()
    .renderCrazyChart();
  }
}]);

On

The callback fed to .on will execute on every window resize and takes the truthiness of the media query as its first argument. Be careful using this method as resize events fire often and can bog down your application if not handled properly.

  • Executes the callback function on window resize with the match truthiness as the first argument.
  • Returns the current match truthiness.
  • Passing $scope as a third parameter is optional, when not passed it will use $rootScope
angular.module('myApp', ['matchMedia'])
.controller('mainController', ['screenSize', function (screenSize) {
  $scope.isMobile = screenSize.on('xs, sm', function(isMatch){
    $scope.isMobile = isMatch;
  });
}]);

When

If you only want the callback to fire while in the correct screensize, use the when method. Be careful using this method as resize events fire often and can bog down your application if not handled properly.

angular.module('myApp', ['matchMedia'])
.controller('mainController', ['screenSize', function (screenSize) {

    // Will fire as long as the screen is size between 768px and 991px
    screenSize.when('sm', function() {
        console.log('Your screen size at the moment is between 768px and 991px.');
    });
}]);

OnChange

The callback fed to .onChange will execute on every window resize and takes the truthiness of the media query as its first argument. Be careful using this method as resize events fire often and can bog down your application if not handled properly.

  • Executes the callback function ONLY when the match differs from previous match.
  • Returns the current match truthiness.
  • The 'scope' parameter is required for cleanup reasons (destroy event).
angular.module('myApp', ['matchMedia'])
.controller('mainController', ['screenSize', function (screenSize) {
  $scope.isMobile = screenSize.onChange('xs, sm', function(isMatch){
    $scope.isMobile = isMatch;
  });
}]);

isRetina

This will return a boolean to indicate if the current screen is hi-def/retina.

angular.module('myApp', ['matchMedia'])
.controller('mainController', ['screenSize', function (screenSize) {
  $scope.isRetina = screenSize.isRetina;
}]);

Filter

Operate on string values with the filter: Have the placeholder sign % replaced by the actual media query rule name.

Example:

    <div> {{'Your screen size is: ' | media }} "</div>

Example with replace:

    <div ng-include="'/views/_partials/_team_%.html' | media:{ replace: '%' }"></div>

Extended example:

    <div ng-include="'/views/_partials/_team_%.html' | media:{ replace: '%', groups: { mobile:['ti','xs','sm'], desktop:['md','lg'] } }"></div>

ngIf Example

In your controller you can create variables that correspond to screen sizes. For example add the following to your controller:

// Using static method `is`
angular.module('myApp', ['matchMedia'])
.controller('mainController', ['screenSize', function (screenSize) {
  $scope.desktop = screenSize.is('md, lg');
  $scope.mobile = screenSize.is('xs, sm');
}]);

// Using dynamic method `on`, which will set the variables initially and then update the variable on window resize
$scope.desktop = screenSize.on('md, lg', function(match){
    $scope.desktop = match;
});
$scope.mobile = screenSize.on('xs, sm', function(match){
    $scope.mobile = match;
});

Then in your HTML you can show or hide content using ngIf or similar directives that take an Angular expression:

<img ng-if='desktop' ng-src='http://example.com/path/to/giant/image.jpg'>

This particular example is great for only loading large, unnecessary images on desktop computers.

Note: It's important if you plan on using screensize.is() in directives to assign its return value to a scoped variable. If you don't, it will only be evaluated once and will not update if the window is resized or if a mobile device is turned sideways.

Custom Screen Sizes or Media Queries

You can access and therefore customize the media queries or create your own:

angular.module('myApp', ['matchMedia'])
.controller('mainController', ['screenSize', function (screenSize) {
  screenSize.rules = {
    retina: 'only screen and (min-device-pixel-ratio: 2), only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx)',
    superJumbo: '(min-width: 2000px)',

  };

  if (screenSize.is('retina')) {
    // switch out regular images for hi-dpi ones
  }

  if (screenSize.is('superJumbo')) {
    // do something for enormous screens
  }
}]);

License

This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/4.0/deed.en_US.

Contributors

Todo

  • Write tests.
  • Add a simple directive wrapper for ng-if.
  • Add Grunt tasks.

angular-match-media's People

Contributors

bitflower avatar chrismcmacken avatar jacopotarantino avatar jhdrn avatar mollwe avatar sravenhorst avatar yizhao avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.