jquery.nestable.rtl.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*!
  2. * Nestable jQuery Plugin - Copyright (c) 2012 David Bushell - http://dbushell.com/
  3. * Dual-licensed under the BSD or MIT licenses
  4. */
  5. ;(function($, window, document, undefined) {
  6. var hasTouch = 'ontouchstart' in window;
  7. /**
  8. * Detect CSS pointer-events property
  9. * events are normally disabled on the dragging element to avoid conflicts
  10. * https://github.com/ausi/Feature-detection-technique-for-pointer-events/blob/master/modernizr-pointerevents.js
  11. */
  12. var hasPointerEvents = (function() {
  13. var el = document.createElement('div'), docEl = document.documentElement;
  14. if(!('pointerEvents' in el.style)) {
  15. return false;
  16. }
  17. el.style.pointerEvents = 'auto';
  18. el.style.pointerEvents = 'x';
  19. docEl.appendChild(el);
  20. var supports = window.getComputedStyle && window.getComputedStyle(el, '').pointerEvents === 'auto';
  21. docEl.removeChild(el);
  22. return !!supports;
  23. })();
  24. var eStart = hasTouch ? 'touchstart' : 'mousedown', eMove = hasTouch ? 'touchmove' : 'mousemove', eEnd = hasTouch ? 'touchend' : 'mouseup', eCancel = hasTouch ? 'touchcancel' : 'mouseup';
  25. var defaults = {
  26. listNodeName: 'ol',
  27. itemNodeName: 'li',
  28. rootClass: 'dd',
  29. listClass: 'dd-list',
  30. itemClass: 'dd-item',
  31. dragClass: 'dd-dragel',
  32. handleClass: 'dd-handle',
  33. collapsedClass: 'dd-collapsed',
  34. placeClass: 'dd-placeholder',
  35. noDragClass: 'dd-nodrag',
  36. emptyClass: 'dd-empty',
  37. expandBtnHTML: '<button data-action ="expand" type="button">Expand</button>',
  38. collapseBtnHTML: '<button data-action ="collapse" type="button">Collapse</button>',
  39. group: 0,
  40. maxDepth: 5,
  41. threshold: 20,
  42. scroll: false,
  43. scrollSensitivity: 1,
  44. scrollSpeed: 5,
  45. scrollTriggers: {
  46. top: 40,
  47. left: 40,
  48. right: -40,
  49. bottom: -40
  50. }
  51. };
  52. function Plugin(element, options) {
  53. this.w = $(window);
  54. this.el = $(element);
  55. this.rtl = this.el.css('direction') == "rtl";
  56. // console.log('this.rtl',this.rtl);
  57. this.options = $.extend({}, defaults, options);
  58. this.init();
  59. }
  60. Plugin.prototype = {
  61. init: function() {
  62. var list = this;
  63. list.reset();
  64. list.el.data('nestable-group', this.options.group);
  65. list.placeEl = $('<div class="' + list.options.placeClass + '"/>');
  66. $.each(this.el.find(list.options.itemNodeName), function(k, el) {
  67. list.setParent($(el));
  68. });
  69. list.el.on('click', 'button', function(e) {
  70. if(list.dragEl || (!hasTouch && e.button !== 0)) {
  71. return;
  72. }
  73. var target = $(e.currentTarget), action = target.data('action'), item = target.parent(list.options.itemNodeName);
  74. if(action === 'collapse') {
  75. list.collapseItem(item);
  76. }
  77. if(action === 'expand') {
  78. list.expandItem(item);
  79. }
  80. });
  81. var onStartEvent = function(e) {
  82. var handle = $(e.target);
  83. if(!handle.hasClass(list.options.handleClass)) {
  84. if(handle.closest('.' + list.options.noDragClass).length) {
  85. return;
  86. }
  87. handle = handle.closest('.' + list.options.handleClass);
  88. }
  89. if(!handle.length || list.dragEl || (!hasTouch && e.button !== 0) || (hasTouch && e.touches.length !== 1)) {
  90. return;
  91. }
  92. e.preventDefault();
  93. list.dragStart( hasTouch ? e.touches[0] : e);
  94. };
  95. var onMoveEvent = function(e) {
  96. if(list.dragEl) {
  97. e.preventDefault();
  98. list.dragMove( hasTouch ? e.touches[0] : e);
  99. }
  100. };
  101. var onEndEvent = function(e) {
  102. if(list.dragEl) {
  103. e.preventDefault();
  104. list.dragStop( hasTouch ? e.touches[0] : e);
  105. }
  106. };
  107. if(hasTouch) {
  108. list.el[0].addEventListener(eStart, onStartEvent, false);
  109. window.addEventListener(eMove, onMoveEvent, false);
  110. window.addEventListener(eEnd, onEndEvent, false);
  111. window.addEventListener(eCancel, onEndEvent, false);
  112. } else {
  113. list.el.on(eStart, onStartEvent);
  114. list.w.on(eMove, onMoveEvent);
  115. list.w.on(eEnd, onEndEvent);
  116. }
  117. },
  118. serialize: function() {
  119. var data, depth = 0, list = this;
  120. step = function(level, depth) {
  121. var array = [], items = level.children(list.options.itemNodeName);
  122. items.each(function() {
  123. var li = $(this), item = $.extend({}, li.data()), sub = li.children(list.options.listNodeName);
  124. if(sub.length) {
  125. item.children = step(sub, depth + 1);
  126. }
  127. array.push(item);
  128. });
  129. return array;
  130. };
  131. data = step(list.el.find(list.options.listNodeName).first(), depth);
  132. return data;
  133. },
  134. serialise: function() {
  135. return this.serialize();
  136. },
  137. reset: function() {
  138. this.mouse = {
  139. offsetX: 0,
  140. offsetY: 0,
  141. startX: 0,
  142. startY: 0,
  143. lastX: 0,
  144. lastY: 0,
  145. nowX: 0,
  146. nowY: 0,
  147. distX: 0,
  148. distY: 0,
  149. dirAx: 0,
  150. dirX: 0,
  151. dirY: 0,
  152. lastDirX: 0,
  153. lastDirY: 0,
  154. distAxX: 0,
  155. distAxY: 0
  156. };
  157. this.moving = false;
  158. this.dragEl = null;
  159. this.dragRootEl = null;
  160. this.dragDepth = 0;
  161. this.hasNewRoot = false;
  162. this.pointEl = null;
  163. },
  164. expandItem: function(li) {
  165. li.removeClass(this.options.collapsedClass);
  166. li.children('[data-action="expand"]').hide();
  167. li.children('[data-action="collapse"]').show();
  168. li.children(this.options.listNodeName).show();
  169. this.el.trigger('expand', [li]);
  170. li.trigger('expand');
  171. },
  172. collapseItem: function(li) {
  173. var lists = li.children(this.options.listNodeName);
  174. if(lists.length) {
  175. li.addClass(this.options.collapsedClass);
  176. li.children('[data-action="collapse"]').hide();
  177. li.children('[data-action="expand"]').show();
  178. li.children(this.options.listNodeName).hide();
  179. }
  180. this.el.trigger('collapse', [li]);
  181. li.trigger('collapse');
  182. },
  183. expandAll: function() {
  184. var list = this;
  185. list.el.find(list.options.itemNodeName).each(function() {
  186. list.expandItem($(this));
  187. });
  188. },
  189. collapseAll: function() {
  190. var list = this;
  191. list.el.find(list.options.itemNodeName).each(function() {
  192. list.collapseItem($(this));
  193. });
  194. },
  195. setParent: function(li) {
  196. if(li.children(this.options.listNodeName).length) {
  197. li.prepend($(this.options.expandBtnHTML));
  198. li.prepend($(this.options.collapseBtnHTML));
  199. }
  200. li.children('[data-action="expand"]').hide();
  201. },
  202. unsetParent: function(li) {
  203. li.removeClass(this.options.collapsedClass);
  204. li.children('[data-action]').remove();
  205. li.children(this.options.listNodeName).remove();
  206. },
  207. dragStart: function(e) {
  208. var mouse = this.mouse, target = $(e.target),
  209. // dragItem = target.closest(this.options.itemNodeName);
  210. dragItem = target.closest('.' + this.options.handleClass).closest(this.options.itemNodeName);
  211. this.target_width = target.width();
  212. // for rtl
  213. this.placeEl.css('height', dragItem.height());
  214. mouse.offsetX = e.offsetX !== undefined ? e.offsetX : e.pageX - target.offset().left;
  215. mouse.offsetY = e.offsetY !== undefined ? e.offsetY : e.pageY - target.offset().top;
  216. mouse.startX = mouse.lastX = e.pageX;
  217. mouse.startY = mouse.lastY = e.pageY;
  218. this.dragRootEl = this.el;
  219. this.dragEl = $(document.createElement(this.options.listNodeName)).addClass(this.options.listClass + ' ' + this.options.dragClass);
  220. this.dragEl.css('width', dragItem.width());
  221. // fix for zepto.js
  222. //dragItem.after(this.placeEl).detach().appendTo(this.dragEl);
  223. dragItem.after(this.placeEl);
  224. dragItem[0].parentNode.removeChild(dragItem[0]);
  225. dragItem.appendTo(this.dragEl);
  226. var rtlFix = 0;
  227. if(this.rtl)
  228. rtlFix = this.dragEl.width() - this.target_width;
  229. // console.log( 'rtl:', this.rtl );
  230. // console.log( 'e.pageX: ', e.pageX, 'e.pageY: ', e.pageY, 'mouse.offsetX: ', mouse.offsetX, 'mouse.offsetY: ', mouse.offsetY );
  231. // console.log( 'left:', e.pageX - mouse.offsetX + rtlFix, 'top:', e.pageY - mouse.offsetY , 'rtlFix:', + rtlFix, 'this.dragEl.W:', this.dragEl.width(), 'target.w', target_width );
  232. // console.log('target:', target);
  233. // console.log('target.w',target_width );
  234. $(document.body).append(this.dragEl);
  235. this.dragEl.css({
  236. 'left': e.pageX - mouse.offsetX - rtlFix,
  237. 'top': e.pageY - mouse.offsetY
  238. });
  239. // total depth of dragging item
  240. var i, depth, items = this.dragEl.find(this.options.itemNodeName);
  241. for( i = 0; i < items.length; i++) {
  242. depth = $(items[i]).parents(this.options.listNodeName).length;
  243. if(depth > this.dragDepth) {
  244. this.dragDepth = depth;
  245. }
  246. }
  247. },
  248. dragStop: function(e) {
  249. // fix for zepto.js
  250. //this.placeEl.replaceWith(this.dragEl.children(this.options.itemNodeName + ':first').detach());
  251. var el = this.dragEl.children(this.options.itemNodeName).first();
  252. el[0].parentNode.removeChild(el[0]);
  253. this.placeEl.replaceWith(el);
  254. this.dragEl.remove();
  255. this.el.trigger('change');
  256. if(this.hasNewRoot) {
  257. this.dragRootEl.trigger('change');
  258. }
  259. this.reset();
  260. },
  261. dragMove: function(e) {
  262. var list, parent, prev, next, depth, opt = this.options, mouse = this.mouse;
  263. var rtlFix = 0;
  264. if(this.rtl)
  265. rtlFix = this.dragEl.width() - this.target_width;
  266. this.dragEl.css({
  267. 'left': e.pageX - mouse.offsetX - rtlFix,
  268. 'top': e.pageY - mouse.offsetY
  269. });
  270. // mouse position last events
  271. mouse.lastX = mouse.nowX;
  272. mouse.lastY = mouse.nowY;
  273. // mouse position this events
  274. mouse.nowX = e.pageX;
  275. mouse.nowY = e.pageY;
  276. // distance mouse moved between events
  277. mouse.distX = mouse.nowX - mouse.lastX;
  278. mouse.distY = mouse.nowY - mouse.lastY;
  279. // direction mouse was moving
  280. mouse.lastDirX = mouse.dirX;
  281. mouse.lastDirY = mouse.dirY;
  282. // direction mouse is now moving (on both axis)
  283. mouse.dirX = mouse.distX === 0 ? 0 : mouse.distX > 0 ? 1 : -1;
  284. mouse.dirY = mouse.distY === 0 ? 0 : mouse.distY > 0 ? 1 : -1;
  285. // axis mouse is now moving on
  286. var newAx = Math.abs(mouse.distX) > Math.abs(mouse.distY) ? 1 : 0;
  287. // do nothing on first move
  288. if(!mouse.moving) {
  289. mouse.dirAx = newAx;
  290. mouse.moving = true;
  291. return;
  292. }
  293. //Do scrolling
  294. if(opt.scroll) {
  295. var scrolled = false;
  296. var scrollParent = this.el.scrollParent()[0];
  297. if(scrollParent != document && scrollParent.tagName != 'HTML') {
  298. if((opt.scrollTriggers.bottom + scrollParent.offsetHeight) - e.pageY < opt.scrollSensitivity)
  299. scrollParent.scrollTop = scrolled = scrollParent.scrollTop + opt.scrollSpeed;
  300. else if(e.pageY - opt.scrollTriggers.top < opt.scrollSensitivity)
  301. scrollParent.scrollTop = scrolled = scrollParent.scrollTop - opt.scrollSpeed;
  302. if((opt.scrollTriggers.right + scrollParent.offsetWidth) - e.pageX < opt.scrollSensitivity)
  303. scrollParent.scrollLeft = scrolled = scrollParent.scrollLeft + opt.scrollSpeed;
  304. else if(e.pageX - opt.scrollTriggers.left < opt.scrollSensitivity)
  305. scrollParent.scrollLeft = scrolled = scrollParent.scrollLeft - opt.scrollSpeed;
  306. } else {
  307. if(e.pageY - $(document).scrollTop() < opt.scrollSensitivity)
  308. scrolled = $(document).scrollTop($(document).scrollTop() - opt.scrollSpeed);
  309. else if($(window).height() - (e.pageY - $(document).scrollTop()) < opt.scrollSensitivity)
  310. scrolled = $(document).scrollTop($(document).scrollTop() + opt.scrollSpeed);
  311. if(e.pageX - $(document).scrollLeft() < opt.scrollSensitivity)
  312. scrolled = $(document).scrollLeft($(document).scrollLeft() - opt.scrollSpeed);
  313. else if($(window).width() - (e.pageX - $(document).scrollLeft()) < opt.scrollSensitivity)
  314. scrolled = $(document).scrollLeft($(document).scrollLeft() + opt.scrollSpeed);
  315. }
  316. }
  317. if(this.scrollTimer)
  318. clearTimeout(this.scrollTimer);
  319. if(opt.scroll && scrolled) {
  320. this.scrollTimer = setTimeout(function() {
  321. $(window).trigger(e);
  322. }, 10);
  323. }
  324. // calc distance moved on this axis (and direction)
  325. if(mouse.dirAx !== newAx) {
  326. mouse.distAxX = 0;
  327. mouse.distAxY = 0;
  328. } else {
  329. mouse.distAxX += Math.abs(mouse.distX);
  330. if(mouse.dirX !== 0 && mouse.dirX !== mouse.lastDirX) {
  331. mouse.distAxX = 0;
  332. }
  333. mouse.distAxY += Math.abs(mouse.distY);
  334. if(mouse.dirY !== 0 && mouse.dirY !== mouse.lastDirY) {
  335. mouse.distAxY = 0;
  336. }
  337. }
  338. mouse.dirAx = newAx;
  339. /**
  340. * move horizontal
  341. */
  342. if(mouse.dirAx && mouse.distAxX >= opt.threshold) {
  343. // reset move distance on x-axis for new phase
  344. mouse.distAxX = 0;
  345. prev = this.placeEl.prev(opt.itemNodeName);
  346. // increase horizontal level if previous sibling exists and is not collapsed
  347. //! rtl?
  348. var distX_direction = true;
  349. // console.log('this.rtl',this.rtl);
  350. if(this.rtl && mouse.distX > 0) {
  351. distX_direction = false;
  352. }
  353. if(!this.rtl && mouse.distX < 0) {
  354. distX_direction = false;
  355. }
  356. if(distX_direction && prev.length && !prev.hasClass(opt.collapsedClass)) {
  357. // console.log('moving right');
  358. // cannot increase level when item above is collapsed
  359. list = prev.find(opt.listNodeName).last();
  360. // check if depth limit has reached
  361. depth = this.placeEl.parents(opt.listNodeName).length;
  362. if(depth + this.dragDepth <= opt.maxDepth) {
  363. // create new sub-level if one doesn't exist
  364. if(!list.length) {
  365. list = $('<' + opt.listNodeName + '/>').addClass(opt.listClass);
  366. list.append(this.placeEl);
  367. prev.append(list);
  368. this.setParent(prev);
  369. } else {
  370. // else append to next level up
  371. list = prev.children(opt.listNodeName).last();
  372. list.append(this.placeEl);
  373. }
  374. }
  375. }
  376. // decrease horizontal level
  377. if(!distX_direction) {
  378. // console.log('moving left');
  379. // we can't decrease a level if an item preceeds the current one
  380. next = this.placeEl.next(opt.itemNodeName);
  381. if(!next.length) {
  382. parent = this.placeEl.parent();
  383. this.placeEl.closest(opt.itemNodeName).after(this.placeEl);
  384. if(!parent.children().length) {
  385. this.unsetParent(parent.parent());
  386. }
  387. }
  388. }
  389. }
  390. var isEmpty = false;
  391. // find list item under cursor
  392. if(!hasPointerEvents) {
  393. this.dragEl[0].style.visibility = 'hidden';
  394. }
  395. this.pointEl = $(document.elementFromPoint(e.pageX - document.documentElement.scrollLeft, e.pageY - (window.pageYOffset || document.documentElement.scrollTop)));
  396. if(!hasPointerEvents) {
  397. this.dragEl[0].style.visibility = 'visible';
  398. }
  399. if(this.pointEl.hasClass(opt.handleClass)) {
  400. // this.pointEl = this.pointEl.parent(opt.itemNodeName);
  401. this.pointEl = this.pointEl.closest(opt.itemNodeName);
  402. }
  403. if(this.pointEl.hasClass(opt.emptyClass)) {
  404. isEmpty = true;
  405. } else if(!this.pointEl.length || !this.pointEl.hasClass(opt.itemClass)) {
  406. return;
  407. }
  408. // find parent list of item under cursor
  409. var pointElRoot = this.pointEl.closest('.' + opt.rootClass), isNewRoot = this.dragRootEl.data('nestable-id') !== pointElRoot.data('nestable-id');
  410. /**
  411. * move vertical
  412. */
  413. if(!mouse.dirAx || isNewRoot || isEmpty) {
  414. // check if groups match if dragging over new root
  415. if(isNewRoot && opt.group !== pointElRoot.data('nestable-group')) {
  416. return;
  417. }
  418. // check depth limit
  419. depth = this.dragDepth - 1 + this.pointEl.parents(opt.listNodeName).length;
  420. if(depth > opt.maxDepth) {
  421. return;
  422. }
  423. var before = e.pageY < (this.pointEl.offset().top + this.pointEl.height() / 2);
  424. parent = this.placeEl.parent();
  425. // if empty create new list to replace empty placeholder
  426. if(isEmpty) {
  427. list = $(document.createElement(opt.listNodeName)).addClass(opt.listClass);
  428. list.append(this.placeEl);
  429. this.pointEl.replaceWith(list);
  430. } else if(before) {
  431. this.pointEl.before(this.placeEl);
  432. } else {
  433. this.pointEl.after(this.placeEl);
  434. }
  435. if(!parent.children().length) {
  436. this.unsetParent(parent.parent());
  437. }
  438. if(!this.dragRootEl.find(opt.itemNodeName).length) {
  439. //console.log('nest add empty placeholder');
  440. this.dragRootEl.append('<div class="' + opt.emptyClass + '"/>');
  441. }
  442. // parent root list has changed
  443. this.dragRootEl = pointElRoot;
  444. if(isNewRoot) {
  445. //this.dragRootEl = pointElRoot;
  446. this.hasNewRoot = this.el[0] !== this.dragRootEl[0];
  447. }
  448. }
  449. }
  450. };
  451. $.fn.nestable = function(params) {
  452. var lists = this, retval = this;
  453. lists.each(function(iel) {
  454. var plugin = $(this).data("nestable");
  455. if(!plugin) {
  456. $(this).data("nestable", new Plugin(this, params));
  457. // $(this).data("nestable-id", new Date().getTime());
  458. $(this).data("nestable-id", iel);
  459. } else {
  460. if( typeof params === 'string' && typeof plugin[params] === 'function') {
  461. retval = plugin[params]();
  462. }
  463. }
  464. });
  465. return retval || lists;
  466. };
  467. })(window.jQuery || window.Zepto, window, document);