MediaQuery.js 666 B

123456789101112131415161718192021222324
  1. var Emitter = require('./Emitter');
  2. exports = Emitter.extend({
  3. className: 'MediaQuery',
  4. initialize: function(query) {
  5. var _this = this;
  6. this.callSuper(Emitter, 'initialize');
  7. this._listener = function() {
  8. _this.emit(_this.isMatch() ? 'match' : 'unmatch');
  9. };
  10. this.setQuery(query);
  11. },
  12. setQuery: function(query) {
  13. if (this._mql) {
  14. this._mql.removeListener(this._listener);
  15. }
  16. this._mql = window.matchMedia(query);
  17. this._mql.addListener(this._listener);
  18. },
  19. isMatch: function() {
  20. return this._mql.matches;
  21. }
  22. });
  23. module.exports = exports;