draggabilly.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*!
  2. * Draggabilly v2.3.0
  3. * Make that shiz draggable
  4. * https://draggabilly.desandro.com
  5. * MIT license
  6. */
  7. /* jshint browser: true, strict: true, undef: true, unused: true */
  8. ( function( window, factory ) {
  9. // universal module definition
  10. /* jshint strict: false */ /* globals define */
  11. if ( typeof define == 'function' && define.amd ) {
  12. // AMD
  13. define( [
  14. 'get-size/get-size',
  15. 'unidragger/unidragger',
  16. ],
  17. function( getSize, Unidragger ) {
  18. return factory( window, getSize, Unidragger );
  19. } );
  20. } else if ( typeof module == 'object' && module.exports ) {
  21. // CommonJS
  22. module.exports = factory(
  23. window,
  24. require('get-size'),
  25. require('unidragger')
  26. );
  27. } else {
  28. // browser global
  29. window.Draggabilly = factory(
  30. window,
  31. window.getSize,
  32. window.Unidragger
  33. );
  34. }
  35. }( window, function factory( window, getSize, Unidragger ) {
  36. // -------------------------- helpers & variables -------------------------- //
  37. // extend objects
  38. function extend( a, b ) {
  39. for ( var prop in b ) {
  40. a[ prop ] = b[ prop ];
  41. }
  42. return a;
  43. }
  44. function noop() {}
  45. var jQuery = window.jQuery;
  46. // -------------------------- -------------------------- //
  47. function Draggabilly( element, options ) {
  48. // querySelector if string
  49. this.element = typeof element == 'string' ?
  50. document.querySelector( element ) : element;
  51. if ( jQuery ) {
  52. this.$element = jQuery( this.element );
  53. }
  54. // options
  55. this.options = extend( {}, this.constructor.defaults );
  56. this.option( options );
  57. this._create();
  58. }
  59. // inherit Unidragger methods
  60. var proto = Draggabilly.prototype = Object.create( Unidragger.prototype );
  61. Draggabilly.defaults = {
  62. };
  63. /**
  64. * set options
  65. * @param {Object} opts
  66. */
  67. proto.option = function( opts ) {
  68. extend( this.options, opts );
  69. };
  70. // css position values that don't need to be set
  71. var positionValues = {
  72. relative: true,
  73. absolute: true,
  74. fixed: true,
  75. };
  76. proto._create = function() {
  77. // properties
  78. this.position = {};
  79. this._getPosition();
  80. this.startPoint = { x: 0, y: 0 };
  81. this.dragPoint = { x: 0, y: 0 };
  82. this.startPosition = extend( {}, this.position );
  83. // set relative positioning
  84. var style = getComputedStyle( this.element );
  85. if ( !positionValues[ style.position ] ) {
  86. this.element.style.position = 'relative';
  87. }
  88. // events, bridge jQuery events from vanilla
  89. this.on( 'pointerMove', this.onPointerMove );
  90. this.on( 'pointerUp', this.onPointerUp );
  91. this.enable();
  92. this.setHandles();
  93. };
  94. /**
  95. * set this.handles and bind start events to 'em
  96. */
  97. proto.setHandles = function() {
  98. this.handles = this.options.handle ?
  99. this.element.querySelectorAll( this.options.handle ) : [ this.element ];
  100. this.bindHandles();
  101. };
  102. /**
  103. * emits events via EvEmitter and jQuery events
  104. * @param {String} type - name of event
  105. * @param {Event} event - original event
  106. * @param {Array} args - extra arguments
  107. */
  108. proto.dispatchEvent = function( type, event, args ) {
  109. var emitArgs = [ event ].concat( args );
  110. this.emitEvent( type, emitArgs );
  111. this.dispatchJQueryEvent( type, event, args );
  112. };
  113. proto.dispatchJQueryEvent = function( type, event, args ) {
  114. var jquery = window.jQuery;
  115. // trigger jQuery event
  116. if ( !jquery || !this.$element ) {
  117. return;
  118. }
  119. // create jQuery event
  120. /* eslint-disable-next-line new-cap */
  121. var $event = jquery.Event( event );
  122. $event.type = type;
  123. this.$element.trigger( $event, args );
  124. };
  125. // -------------------------- position -------------------------- //
  126. // get x/y position from style
  127. proto._getPosition = function() {
  128. var style = getComputedStyle( this.element );
  129. var x = this._getPositionCoord( style.left, 'width' );
  130. var y = this._getPositionCoord( style.top, 'height' );
  131. // clean up 'auto' or other non-integer values
  132. this.position.x = isNaN( x ) ? 0 : x;
  133. this.position.y = isNaN( y ) ? 0 : y;
  134. this._addTransformPosition( style );
  135. };
  136. proto._getPositionCoord = function( styleSide, measure ) {
  137. if ( styleSide.indexOf('%') != -1 ) {
  138. // convert percent into pixel for Safari, #75
  139. var parentSize = getSize( this.element.parentNode );
  140. // prevent not-in-DOM element throwing bug, #131
  141. return !parentSize ? 0 :
  142. ( parseFloat( styleSide ) / 100 ) * parentSize[ measure ];
  143. }
  144. return parseInt( styleSide, 10 );
  145. };
  146. // add transform: translate( x, y ) to position
  147. proto._addTransformPosition = function( style ) {
  148. var transform = style.transform;
  149. // bail out if value is 'none'
  150. if ( transform.indexOf('matrix') !== 0 ) {
  151. return;
  152. }
  153. // split matrix(1, 0, 0, 1, x, y)
  154. var matrixValues = transform.split(',');
  155. // translate X value is in 12th or 4th position
  156. var xIndex = transform.indexOf('matrix3d') === 0 ? 12 : 4;
  157. var translateX = parseInt( matrixValues[ xIndex ], 10 );
  158. // translate Y value is in 13th or 5th position
  159. var translateY = parseInt( matrixValues[ xIndex + 1 ], 10 );
  160. this.position.x += translateX;
  161. this.position.y += translateY;
  162. };
  163. // -------------------------- events -------------------------- //
  164. proto.onPointerDown = function( event, pointer ) {
  165. this.element.classList.add('is-pointer-down');
  166. this.dispatchJQueryEvent( 'pointerDown', event, [ pointer ] );
  167. };
  168. proto.pointerDown = function( event, pointer ) {
  169. var isOkay = this.okayPointerDown( event );
  170. if ( !isOkay || !this.isEnabled ) {
  171. this._pointerReset();
  172. return;
  173. }
  174. // track start event position
  175. // Safari 9 overrides pageX and pageY. These values needs to be copied. flickity#842
  176. this.pointerDownPointer = {
  177. pageX: pointer.pageX,
  178. pageY: pointer.pageY,
  179. };
  180. event.preventDefault();
  181. this.pointerDownBlur();
  182. // bind move and end events
  183. this._bindPostStartEvents( event );
  184. this.element.classList.add('is-pointer-down');
  185. this.dispatchEvent( 'pointerDown', event, [ pointer ] );
  186. };
  187. /**
  188. * drag start
  189. * @param {Event} event
  190. * @param {[Event, Touch]} pointer
  191. */
  192. proto.dragStart = function( event, pointer ) {
  193. if ( !this.isEnabled ) {
  194. return;
  195. }
  196. this._getPosition();
  197. this.measureContainment();
  198. // position _when_ drag began
  199. this.startPosition.x = this.position.x;
  200. this.startPosition.y = this.position.y;
  201. // reset left/top style
  202. this.setLeftTop();
  203. this.dragPoint.x = 0;
  204. this.dragPoint.y = 0;
  205. this.element.classList.add('is-dragging');
  206. this.dispatchEvent( 'dragStart', event, [ pointer ] );
  207. // start animation
  208. this.animate();
  209. };
  210. proto.measureContainment = function() {
  211. var container = this.getContainer();
  212. if ( !container ) {
  213. return;
  214. }
  215. var elemSize = getSize( this.element );
  216. var containerSize = getSize( container );
  217. var elemRect = this.element.getBoundingClientRect();
  218. var containerRect = container.getBoundingClientRect();
  219. var borderSizeX = containerSize.borderLeftWidth + containerSize.borderRightWidth;
  220. var borderSizeY = containerSize.borderTopWidth + containerSize.borderBottomWidth;
  221. var position = this.relativeStartPosition = {
  222. x: elemRect.left - ( containerRect.left + containerSize.borderLeftWidth ),
  223. y: elemRect.top - ( containerRect.top + containerSize.borderTopWidth ),
  224. };
  225. this.containSize = {
  226. width: ( containerSize.width - borderSizeX ) - position.x - elemSize.width,
  227. height: ( containerSize.height - borderSizeY ) - position.y - elemSize.height,
  228. };
  229. };
  230. proto.getContainer = function() {
  231. var containment = this.options.containment;
  232. if ( !containment ) {
  233. return;
  234. }
  235. var isElement = containment instanceof HTMLElement;
  236. // use as element
  237. if ( isElement ) {
  238. return containment;
  239. }
  240. // querySelector if string
  241. if ( typeof containment == 'string' ) {
  242. return document.querySelector( containment );
  243. }
  244. // fallback to parent element
  245. return this.element.parentNode;
  246. };
  247. // ----- move event ----- //
  248. proto.onPointerMove = function( event, pointer, moveVector ) {
  249. this.dispatchJQueryEvent( 'pointerMove', event, [ pointer, moveVector ] );
  250. };
  251. /**
  252. * drag move
  253. * @param {Event} event
  254. * @param {[Event, Touch]} pointer
  255. * @param {Object} moveVector - x and y coordinates
  256. */
  257. proto.dragMove = function( event, pointer, moveVector ) {
  258. if ( !this.isEnabled ) {
  259. return;
  260. }
  261. var dragX = moveVector.x;
  262. var dragY = moveVector.y;
  263. var grid = this.options.grid;
  264. var gridX = grid && grid[0];
  265. var gridY = grid && grid[1];
  266. dragX = applyGrid( dragX, gridX );
  267. dragY = applyGrid( dragY, gridY );
  268. dragX = this.containDrag( 'x', dragX, gridX );
  269. dragY = this.containDrag( 'y', dragY, gridY );
  270. // constrain to axis
  271. dragX = this.options.axis == 'y' ? 0 : dragX;
  272. dragY = this.options.axis == 'x' ? 0 : dragY;
  273. this.position.x = this.startPosition.x + dragX;
  274. this.position.y = this.startPosition.y + dragY;
  275. // set dragPoint properties
  276. this.dragPoint.x = dragX;
  277. this.dragPoint.y = dragY;
  278. this.dispatchEvent( 'dragMove', event, [ pointer, moveVector ] );
  279. };
  280. function applyGrid( value, grid, method ) {
  281. method = method || 'round';
  282. return grid ? Math[ method ]( value/grid ) * grid : value;
  283. }
  284. proto.containDrag = function( axis, drag, grid ) {
  285. if ( !this.options.containment ) {
  286. return drag;
  287. }
  288. var measure = axis == 'x' ? 'width' : 'height';
  289. var rel = this.relativeStartPosition[ axis ];
  290. var min = applyGrid( -rel, grid, 'ceil' );
  291. var max = this.containSize[ measure ];
  292. max = applyGrid( max, grid, 'floor' );
  293. return Math.max( min, Math.min( max, drag ) );
  294. };
  295. // ----- end event ----- //
  296. /**
  297. * pointer up
  298. * @param {Event} event
  299. * @param {[Event, Touch]} pointer
  300. */
  301. proto.onPointerUp = function( event, pointer ) {
  302. this.element.classList.remove('is-pointer-down');
  303. this.dispatchJQueryEvent( 'pointerUp', event, [ pointer ] );
  304. };
  305. /**
  306. * drag end
  307. * @param {Event} event
  308. * @param {[Event, Touch]} pointer
  309. */
  310. proto.dragEnd = function( event, pointer ) {
  311. if ( !this.isEnabled ) {
  312. return;
  313. }
  314. // use top left position when complete
  315. this.element.style.transform = '';
  316. this.setLeftTop();
  317. this.element.classList.remove('is-dragging');
  318. this.dispatchEvent( 'dragEnd', event, [ pointer ] );
  319. };
  320. // -------------------------- animation -------------------------- //
  321. proto.animate = function() {
  322. // only render and animate if dragging
  323. if ( !this.isDragging ) {
  324. return;
  325. }
  326. this.positionDrag();
  327. var _this = this;
  328. requestAnimationFrame( function animateFrame() {
  329. _this.animate();
  330. } );
  331. };
  332. // left/top positioning
  333. proto.setLeftTop = function() {
  334. this.element.style.left = this.position.x + 'px';
  335. this.element.style.top = this.position.y + 'px';
  336. };
  337. proto.positionDrag = function() {
  338. this.element.style.transform = 'translate3d( ' + this.dragPoint.x +
  339. 'px, ' + this.dragPoint.y + 'px, 0)';
  340. };
  341. // ----- staticClick ----- //
  342. proto.staticClick = function( event, pointer ) {
  343. this.dispatchEvent( 'staticClick', event, [ pointer ] );
  344. };
  345. // ----- methods ----- //
  346. /**
  347. * @param {Number} x
  348. * @param {Number} y
  349. */
  350. proto.setPosition = function( x, y ) {
  351. this.position.x = x;
  352. this.position.y = y;
  353. this.setLeftTop();
  354. };
  355. proto.enable = function() {
  356. this.isEnabled = true;
  357. };
  358. proto.disable = function() {
  359. this.isEnabled = false;
  360. if ( this.isDragging ) {
  361. this.dragEnd();
  362. }
  363. };
  364. proto.destroy = function() {
  365. this.disable();
  366. // reset styles
  367. this.element.style.transform = '';
  368. this.element.style.left = '';
  369. this.element.style.top = '';
  370. this.element.style.position = '';
  371. // unbind handles
  372. this.unbindHandles();
  373. // remove jQuery data
  374. if ( this.$element ) {
  375. this.$element.removeData('draggabilly');
  376. }
  377. };
  378. // ----- jQuery bridget ----- //
  379. // required for jQuery bridget
  380. proto._init = noop;
  381. if ( jQuery && jQuery.bridget ) {
  382. jQuery.bridget( 'draggabilly', Draggabilly );
  383. }
  384. // ----- ----- //
  385. return Draggabilly;
  386. } ) );