controller.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. Core(function(scope) {
  2. function Controller() {
  3. this.proxyAll();
  4. this.refreshElements();
  5. }
  6. var isIE = window.navigator.msPointerEnabled;
  7. $.extend(Controller.prototype, {
  8. proxy: function(func) {
  9. return func.bind(this);
  10. },
  11. proxyAll: function() {
  12. if (this.proxies) for (var method, i = this.proxies.length; i--; ) method = this.proxies[i],
  13. this[method] = this.proxy(this[method]);
  14. },
  15. withDelay: function(func, timeout) {
  16. return setTimeout(this.proxy(func), timeout || 0);
  17. },
  18. $: function(selector) {
  19. return this.$el.find(selector);
  20. },
  21. refreshElements: function() {
  22. if (this.elements) for (var element in this.elements) this[element] = this.$(this.elements[element]);
  23. },
  24. on: function(eventType, handler) {
  25. return this.$el.on(eventType, handler), this;
  26. },
  27. off: function(eventType, handler) {
  28. return this.$el.off(eventType, handler), this;
  29. },
  30. clientX: function(e) {
  31. return isIE ? e.pageX : document.createTouch ? e.changedTouches[0].clientX : e.clientX;
  32. },
  33. clientY: function(e) {
  34. return isIE ? e.pageY : document.createTouch ? e.changedTouches[0].clientY : e.clientY;
  35. },
  36. intersect: function(x, y) {
  37. var pos = this.$el.position();
  38. return pos.top < y && pos.bottom > y && pos.left < x && pos.right > x;
  39. }
  40. }),
  41. scope.Controller = Controller;
  42. });