controller.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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($el) {
  21. // var pos = this.$el.position();
  22. // return pos.top < y && pos.bottom > y && pos.left < x && pos.right > x;
  23. var d0 = this.$el.position()
  24. , d1 = $el.position()
  25. , x11 = d0.left
  26. , y11 = d0.top
  27. , x12 = d0.left + this.$el.width()
  28. , y12 = d0.top + this.$el.height()
  29. , x21 = d1.left
  30. , y21 = d1.top
  31. , x22 = d1.left + $el.width()
  32. , y22 = d1.top + $el.height()
  33. , x_overlap = Math.max(0, Math.min(x12,x22) - Math.max(x11,x21))
  34. , y_overlap = Math.max(0, Math.min(y12,y22) - Math.max(y11,y21))
  35. return {
  36. square: x_overlap * y_overlap
  37. , res : x_overlap * y_overlap > $el.width() * $el.height() * .25
  38. }
  39. }
  40. }),
  41. scope.Controller = Controller;
  42. }