controller.js 1.4 KB

123456789101112131415161718192021222324252627282930
  1. function ControllerScope(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) { return func.bind(this); },
  9. proxyAll: function() {
  10. if (this.proxies) for (var method, i = this.proxies.length; i--; ) method = this.proxies[i],
  11. this[method] = this.proxy(this[method]);
  12. },
  13. withDelay: function(func, timeout) { return setTimeout(this.proxy(func), timeout || 0); },
  14. $: function(selector) { return this.$el.find(selector); },
  15. refreshElements: function() { if (this.elements) for (var element in this.elements) this[element] = this.$(this.elements[element]); },
  16. on: function(eventType, handler) { return this.$el.on(eventType, handler), this; },
  17. off: function(eventType, handler) { return this.$el.off(eventType, handler), this; },
  18. clientX: function(e) { return isIE ? e.pageX : document.createTouch ? e.changedTouches[0].clientX : e.clientX; },
  19. clientY: function(e) { return isIE ? e.pageY : document.createTouch ? e.changedTouches[0].clientY : e.clientY; },
  20. intersect: function(x, y) {
  21. var pos = this.$el.position();
  22. return pos.top < y && pos.bottom > y && pos.left < x && pos.right > x;
  23. }
  24. }),
  25. scope.Controller = Controller;
  26. }