Advertisement
Guest User

Mootools Drag&Drop for mobiles

a guest
May 13th, 2011
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Drag = new Class({
  2.  
  3.     Implements: [Events, Options],
  4.  
  5.     options: {/*
  6.         onBeforeStart: function(thisElement){},
  7.         onStart: function(thisElement, event){},
  8.         onSnap: function(thisElement){},
  9.         onDrag: function(thisElement, event){},
  10.         onCancel: function(thisElement){},
  11.         onComplete: function(thisElement, event){},*/
  12.         snap: 6,
  13.         unit: 'px',
  14.         grid: false,
  15.         style: true,
  16.         limit: false,
  17.         handle: false,
  18.         invert: false,
  19.         preventDefault: false,
  20.         stopPropagation: false,
  21.         modifiers: {x: 'left', y: 'top'}
  22.     },
  23.  
  24.     initialize: function(){
  25.         var params = Array.link(arguments, {
  26.             'options': Type.isObject,
  27.             'element': function(obj){
  28.                 return obj != null;
  29.             }
  30.         });
  31.  
  32.         this.element = document.id(params.element);
  33.         this.document = this.element.getDocument();
  34.         this.setOptions(params.options || {});
  35.         var htype = typeOf(this.options.handle);
  36.         this.handles = ((htype == 'array' || htype == 'collection') ? $$(this.options.handle) : document.id(this.options.handle)) || this.element;
  37.         this.mouse = {'now': {}, 'pos': {}};
  38.         this.value = {'start': {}, 'now': {}};
  39.  
  40.         this.selection = (Browser.ie) ? 'selectstart' : 'mousedown';
  41.  
  42.  
  43.         if (Browser.ie && !Drag.ondragstartFixed){
  44.             document.ondragstart = Function.from(false);
  45.             Drag.ondragstartFixed = true;
  46.         }
  47.  
  48.         this.bound = {
  49.             start: this.start.bind(this),
  50.             check: this.check.bind(this),
  51.             drag: this.drag.bind(this),
  52.             stop: this.stop.bind(this),
  53.             cancel: this.cancel.bind(this),
  54.             eventStop: Function.from(false)
  55.         };
  56.         this.attach();
  57.     },
  58.  
  59.     attach: function(){
  60.         this.handles.addEvent('mousedown', this.bound.start);
  61.         this.handles.addEvent('touchstart', this.bound.start);
  62.         return this;
  63.     },
  64.  
  65.     detach: function(){
  66.         this.handles.removeEvent('mousedown', this.bound.start);
  67.         this.handles.removeEvent('touchstart', this.bound.start);
  68.         return this;
  69.     },
  70.  
  71.     start: function(event){
  72.         var options = this.options;
  73.  
  74.         if (event.rightClick) return;
  75.  
  76.         if (options.preventDefault) event.preventDefault();
  77.         if (options.stopPropagation) event.stopPropagation();
  78.         this.mouse.start = event.page;
  79.  
  80.         this.fireEvent('beforeStart', this.element);
  81.  
  82.         var limit = options.limit;
  83.         this.limit = {x: [], y: []};
  84.  
  85.         var z, coordinates;
  86.         for (z in options.modifiers){
  87.             if (!options.modifiers[z]) continue;
  88.  
  89.             var style = this.element.getStyle(options.modifiers[z]);
  90.  
  91.             // Some browsers (IE and Opera) don't always return pixels.
  92.             if (style && !style.match(/px$/)){
  93.                 if (!coordinates) coordinates = this.element.getCoordinates(this.element.getOffsetParent());
  94.                 style = coordinates[options.modifiers[z]];
  95.             }
  96.  
  97.             if (options.style) this.value.now[z] = (style || 0).toInt();
  98.             else this.value.now[z] = this.element[options.modifiers[z]];
  99.  
  100.             if (options.invert) this.value.now[z] *= -1;
  101.  
  102.             this.mouse.pos[z] = event.page[z] - this.value.now[z];
  103.  
  104.             if (limit && limit[z]){
  105.                 var i = 2;
  106.                 while (i--){
  107.                     var limitZI = limit[z][i];
  108.                     if (limitZI || limitZI === 0) this.limit[z][i] = (typeof limitZI == 'function') ? limitZI() : limitZI;
  109.                 }
  110.             }
  111.         }
  112.  
  113.         if (typeOf(this.options.grid) == 'number') this.options.grid = {
  114.             x: this.options.grid,
  115.             y: this.options.grid
  116.         };
  117.  
  118.         var events = {
  119.             mousemove: this.bound.check,
  120.             mouseup: this.bound.cancel
  121.         };
  122.         events[this.selection] = this.bound.eventStop;
  123.         this.document.addEvents(events);
  124.        
  125.         this.document.addEvent('touchmove', this.bound.check);
  126.         this.document.addEvent('touchend', this.bound.check);
  127.     },
  128.  
  129.     check: function(event){
  130.         if (this.options.preventDefault) event.preventDefault();
  131.         var distance = Math.round(Math.sqrt(Math.pow(event.page.x - this.mouse.start.x, 2) + Math.pow(event.page.y - this.mouse.start.y, 2)));
  132.         if (distance > this.options.snap){
  133.             this.cancel();
  134.             this.document.addEvents({
  135.                 mousemove: this.bound.drag,
  136.                 mouseup: this.bound.stop
  137.             });
  138.            
  139.             this.document.addEvent('touchmove', this.bound.drag);
  140.             this.document.addEvent('touchend', this.bound.stop);
  141.            
  142.             this.fireEvent('start', [this.element, event]).fireEvent('snap', this.element);
  143.            
  144.            
  145.         }
  146.     },
  147.  
  148.     drag: function(event){
  149.         var options = this.options;
  150.  
  151.         if (options.preventDefault) event.preventDefault();
  152.         this.mouse.now = event.page;
  153.  
  154.         for (var z in options.modifiers){
  155.             if (!options.modifiers[z]) continue;
  156.             this.value.now[z] = this.mouse.now[z] - this.mouse.pos[z];
  157.  
  158.             if (options.invert) this.value.now[z] *= -1;
  159.  
  160.             if (options.limit && this.limit[z]){
  161.                 if ((this.limit[z][1] || this.limit[z][1] === 0) && (this.value.now[z] > this.limit[z][1])){
  162.                     this.value.now[z] = this.limit[z][1];
  163.                 } else if ((this.limit[z][0] || this.limit[z][0] === 0) && (this.value.now[z] < this.limit[z][0])){
  164.                     this.value.now[z] = this.limit[z][0];
  165.                 }
  166.             }
  167.  
  168.             if (options.grid[z]) this.value.now[z] -= ((this.value.now[z] - (this.limit[z][0]||0)) % options.grid[z]);
  169.  
  170.             if (options.style) this.element.setStyle(options.modifiers[z], this.value.now[z] + options.unit);
  171.             else this.element[options.modifiers[z]] = this.value.now[z];
  172.         }
  173.  
  174.         this.fireEvent('drag', [this.element, event]);
  175.     },
  176.  
  177.     cancel: function(event){
  178.         this.document.removeEvents({
  179.             mousemove: this.bound.check,
  180.             mouseup: this.bound.cancel
  181.         });
  182.         if (event){
  183.             this.document.removeEvent(this.selection, this.bound.eventStop);
  184.             this.fireEvent('cancel', this.element);
  185.         }
  186.     },
  187.  
  188.     stop: function(event){
  189.         var events = {
  190.             mousemove: this.bound.drag,
  191.             mouseup: this.bound.stop
  192.         };
  193.         events[this.selection] = this.bound.eventStop;
  194.         this.document.removeEvents(events);
  195.         if (event) this.fireEvent('complete', [this.element, event]);
  196.     }
  197.  
  198. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement